Add subscriptions endpoints to REST API
[oweals/peertube.git] / server / models / video / video-channel.ts
index acc2486b398b18dba072f9a2dec5359d9545703d..0273fab13c2958856b6eb5f3718444df2881f83e 100644 (file)
@@ -1,9 +1,11 @@
 import {
-  AfterDestroy,
   AllowNull,
+  BeforeDestroy,
   BelongsTo,
   Column,
   CreatedAt,
+  DataType,
+  Default,
   DefaultScope,
   ForeignKey,
   HasMany,
@@ -14,12 +16,19 @@ import {
   UpdatedAt
 } from 'sequelize-typescript'
 import { ActivityPubActor } from '../../../shared/models/activitypub'
-import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
+import { VideoChannel } from '../../../shared/models/videos'
+import {
+  isVideoChannelDescriptionValid,
+  isVideoChannelNameValid,
+  isVideoChannelSupportValid
+} from '../../helpers/custom-validators/video-channels'
 import { sendDeleteActor } from '../../lib/activitypub/send'
 import { AccountModel } from '../account/account'
 import { ActorModel } from '../activitypub/actor'
 import { getSort, throwIfNotValid } from '../utils'
 import { VideoModel } from './video'
+import { CONSTRAINTS_FIELDS } from '../../initializers'
+import { AvatarModel } from '../avatar/avatar'
 
 enum ScopeNames {
   WITH_ACCOUNT = 'WITH_ACCOUNT',
@@ -39,12 +48,18 @@ enum ScopeNames {
   [ScopeNames.WITH_ACCOUNT]: {
     include: [
       {
-        model: () => AccountModel,
+        model: () => AccountModel.unscoped(),
         required: true,
         include: [
           {
-            model: () => ActorModel,
-            required: true
+            model: () => ActorModel.unscoped(),
+            required: true,
+            include: [
+              {
+                model: () => AvatarModel.unscoped(),
+                required: false
+              }
+            ]
           }
         ]
       }
@@ -66,6 +81,9 @@ enum ScopeNames {
   indexes: [
     {
       fields: [ 'accountId' ]
+    },
+    {
+      fields: [ 'actorId' ]
     }
   ]
 })
@@ -77,10 +95,17 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
   name: string
 
   @AllowNull(true)
+  @Default(null)
   @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
-  @Column
+  @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
   description: string
 
+  @AllowNull(true)
+  @Default(null)
+  @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
+  @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
+  support: string
+
   @CreatedAt
   createdAt: Date
 
@@ -107,7 +132,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
     foreignKey: {
       allowNull: false
     },
-    onDelete: 'CASCADE'
+    hooks: true
   })
   Account: AccountModel
 
@@ -116,14 +141,19 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
       name: 'channelId',
       allowNull: false
     },
-    onDelete: 'CASCADE'
+    onDelete: 'CASCADE',
+    hooks: true
   })
   Videos: VideoModel[]
 
-  @AfterDestroy
-  static sendDeleteIfOwned (instance: VideoChannelModel) {
+  @BeforeDestroy
+  static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
+    if (!instance.Actor) {
+      instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
+    }
+
     if (instance.Actor.isOwned()) {
-      return sendDeleteActor(instance.Actor, undefined)
+      return sendDeleteActor(instance.Actor, options.transaction)
     }
 
     return undefined
@@ -143,7 +173,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
     const query = {
       offset: start,
       limit: count,
-      order: [ getSort(sort) ]
+      order: getSort(sort)
     }
 
     return VideoChannelModel
@@ -156,7 +186,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
 
   static listByAccount (accountId: number) {
     const query = {
-      order: [ getSort('createdAt') ],
+      order: getSort('createdAt'),
       include: [
         {
           model: AccountModel,
@@ -224,18 +254,39 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
       .findById(id, options)
   }
 
-  toFormattedJSON () {
+  static loadLocalByName (name: string) {
+    const query = {
+      include: [
+        {
+          model: ActorModel,
+          required: true,
+          where: {
+            preferredUsername: name,
+            serverId: null
+          }
+        }
+      ]
+    }
+
+    return VideoChannelModel.findOne(query)
+  }
+
+  toFormattedJSON (): VideoChannel {
     const actor = this.Actor.toFormattedJSON()
-    const account = {
+    const videoChannel = {
       id: this.id,
-      name: this.name,
+      displayName: this.getDisplayName(),
       description: this.description,
+      support: this.support,
       isLocal: this.Actor.isOwned(),
       createdAt: this.createdAt,
-      updatedAt: this.updatedAt
+      updatedAt: this.updatedAt,
+      ownerAccount: undefined
     }
 
-    return Object.assign(actor, account)
+    if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
+
+    return Object.assign(actor, videoChannel)
   }
 
   toActivityPubObject (): ActivityPubActor {
@@ -243,6 +294,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
 
     return Object.assign(obj, {
       summary: this.description,
+      support: this.support,
       attributedTo: [
         {
           type: 'Person' as 'Person',
@@ -251,4 +303,8 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
       ]
     })
   }
+
+  getDisplayName () {
+    return this.name
+  }
 }