]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-channel.ts
Add links to comment mentions
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel.ts
index e2cbf0422cd62bb2d721e8948d97df9b4c17962b..40f3be7fe562ff8826ab5411fb7ec233c8938700 100644 (file)
@@ -1,20 +1,14 @@
 import {
-  AfterDestroy,
-  AllowNull,
-  BelongsTo,
-  Column,
-  CreatedAt,
-  DefaultScope,
-  ForeignKey,
-  HasMany,
-  Is,
-  Model,
-  Scopes,
-  Table,
-  UpdatedAt
+  AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, HasMany, Is, Model, Scopes, Table,
+  UpdatedAt, Default
 } 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 { logger } from '../../helpers/logger'
 import { sendDeleteActor } from '../../lib/activitypub/send'
 import { AccountModel } from '../account/account'
 import { ActorModel } from '../activitypub/actor'
@@ -77,10 +71,17 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
   name: string
 
   @AllowNull(true)
+  @Default(null)
   @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
   @Column
   description: string
 
+  @AllowNull(true)
+  @Default(null)
+  @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
+  @Column
+  support: string
+
   @CreatedAt
   createdAt: Date
 
@@ -116,14 +117,21 @@ 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)
+      logger.debug('Sending delete of actor of video channel %s.', instance.Actor.url)
+
+      return sendDeleteActor(instance.Actor, options.transaction)
     }
 
     return undefined
@@ -143,7 +151,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
     const query = {
       offset: start,
       limit: count,
-      order: [ getSort(sort) ]
+      order: getSort(sort)
     }
 
     return VideoChannelModel
@@ -156,7 +164,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
 
   static listByAccount (accountId: number) {
     const query = {
-      order: [ getSort('createdAt') ],
+      order: getSort('createdAt'),
       include: [
         {
           model: AccountModel,
@@ -224,12 +232,13 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
       .findById(id, options)
   }
 
-  toFormattedJSON () {
+  toFormattedJSON (): VideoChannel {
     const actor = this.Actor.toFormattedJSON()
     const account = {
       id: this.id,
       displayName: this.name,
       description: this.description,
+      support: this.support,
       isLocal: this.Actor.isOwned(),
       createdAt: this.createdAt,
       updatedAt: this.updatedAt
@@ -243,6 +252,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
 
     return Object.assign(obj, {
       summary: this.description,
+      support: this.support,
       attributedTo: [
         {
           type: 'Person' as 'Person',