]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-channel.ts
Optimize SQL query that fetch actor outbox
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel.ts
index acc2486b398b18dba072f9a2dec5359d9545703d..7c161c8642b295ed77e5c9c6d14f4c66a28cfdd7 100644 (file)
@@ -1,20 +1,10 @@
 import {
-  AfterDestroy,
-  AllowNull,
-  BelongsTo,
-  Column,
-  CreatedAt,
-  DefaultScope,
-  ForeignKey,
-  HasMany,
-  Is,
-  Model,
-  Scopes,
-  Table,
+  AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, HasMany, Is, Model, Scopes, Table,
   UpdatedAt
 } from 'sequelize-typescript'
 import { ActivityPubActor } from '../../../shared/models/activitypub'
 import { isVideoChannelDescriptionValid, isVideoChannelNameValid } 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'
@@ -116,14 +106,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
@@ -228,7 +225,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
     const actor = this.Actor.toFormattedJSON()
     const account = {
       id: this.id,
-      name: this.name,
+      displayName: this.name,
       description: this.description,
       isLocal: this.Actor.isOwned(),
       createdAt: this.createdAt,