]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Optimize AP video captions update
authorChocobozzz <me@florianbigard.com>
Wed, 9 Jun 2021 14:22:01 +0000 (16:22 +0200)
committerChocobozzz <me@florianbigard.com>
Wed, 9 Jun 2021 14:22:01 +0000 (16:22 +0200)
server/lib/activitypub/videos/shared/abstract-builder.ts
server/models/video/video-caption.ts

index 22280fce123797119195e66cb9217311f32cbcb7..e89c94bcd22bdd3131348e76c3a2f31d2cd7cc11 100644 (file)
@@ -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) {
index d2c742b66b4123af53fae2545d74416cade4655b..b4918e519f59ba7cf89adb9fc0d9c58e8d2be6b9 100644 (file)
@@ -109,11 +109,12 @@ export class VideoCaptionModel extends Model<Partial<AttributesOnly<VideoCaption
     return undefined
   }
 
-  static loadByVideoIdAndLanguage (videoId: string | number, language: string): Promise<MVideoCaptionVideo> {
+  static loadByVideoIdAndLanguage (videoId: string | number, language: string, transaction?: Transaction): Promise<MVideoCaptionVideo> {
     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<Partial<AttributesOnly<VideoCaption
   }
 
   static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) {
-    const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language)
+    const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language, transaction)
+
     // Delete existing file
     if (existing) await existing.destroy({ transaction })
 
     return caption.save({ transaction })
   }
 
-  static listVideoCaptions (videoId: number): Promise<MVideoCaptionVideo[]> {
+  static listVideoCaptions (videoId: number, transaction: Transaction): Promise<MVideoCaptionVideo[]> {
     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<Partial<AttributesOnly<VideoCaption
 
     return this.fileUrl
   }
+
+  isEqual (this: MVideoCaption, other: MVideoCaption) {
+    if (this.fileUrl) return this.fileUrl === other.fileUrl
+
+    return this.filename === other.filename
+  }
 }