]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-caption.ts
Refactor include checks
[github/Chocobozzz/PeerTube.git] / server / models / video / video-caption.ts
index d2c742b66b4123af53fae2545d74416cade4655b..bbda0f3eeb60626a7607776df62ae9d913f6f20c 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
+  }
 }