From 57a0a9cde4760314d9b1fdd920e0b4a180a9925e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 9 Jun 2021 16:22:01 +0200 Subject: [PATCH] Optimize AP video captions update --- .../videos/shared/abstract-builder.ts | 25 ++++++++++++++++--- server/models/video/video-caption.ts | 19 ++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/server/lib/activitypub/videos/shared/abstract-builder.ts b/server/lib/activitypub/videos/shared/abstract-builder.ts index 22280fce1..e89c94bcd 100644 --- a/server/lib/activitypub/videos/shared/abstract-builder.ts +++ b/server/lib/activitypub/videos/shared/abstract-builder.ts @@ -75,11 +75,28 @@ export abstract class APVideoAbstractBuilder { } protected async insertOrReplaceCaptions (video: MVideoFullLight, t: Transaction) { - const videoCaptionsPromises = getCaptionAttributesFromObject(video, this.videoObject) - .map(a => new VideoCaptionModel(a) as MVideoCaption) - .map(c => VideoCaptionModel.insertOrReplaceLanguage(c, t)) + const existingCaptions = await VideoCaptionModel.listVideoCaptions(video.id, t) - await Promise.all(videoCaptionsPromises) + let captionsToCreate = getCaptionAttributesFromObject(video, this.videoObject) + .map(a => new VideoCaptionModel(a) as MVideoCaption) + + for (const existingCaption of existingCaptions) { + // Only keep captions that do not already exist + const filtered = captionsToCreate.filter(c => !c.isEqual(existingCaption)) + + // This caption already exists, we don't need to destroy and create it + if (filtered.length !== captionsToCreate.length) { + captionsToCreate = filtered + continue + } + + // Destroy this caption that does not exist anymore + await existingCaption.destroy({ transaction: t }) + } + + for (const captionToCreate of captionsToCreate) { + await captionToCreate.save({ transaction: t }) + } } protected async insertOrReplaceLive (video: MVideoFullLight, transaction: Transaction) { diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index d2c742b66..b4918e519 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts @@ -109,11 +109,12 @@ export class VideoCaptionModel extends Model { + static loadByVideoIdAndLanguage (videoId: string | number, language: string, transaction?: Transaction): Promise { const videoInclude = { model: VideoModel.unscoped(), attributes: [ 'id', 'remote', 'uuid' ], - where: buildWhereIdOrUUID(videoId) + where: buildWhereIdOrUUID(videoId), + transaction } const query = { @@ -145,19 +146,21 @@ export class VideoCaptionModel extends Model { + static listVideoCaptions (videoId: number, transaction: Transaction): Promise { const query = { order: [ [ 'language', 'ASC' ] ] as OrderItem[], where: { videoId - } + }, + transaction } return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query) @@ -211,4 +214,10 @@ export class VideoCaptionModel extends Model