]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-caption.ts
Add expect message to ease debug
[github/Chocobozzz/PeerTube.git] / server / models / video / video-caption.ts
index d2c742b66b4123af53fae2545d74416cade4655b..2eaa77407e35740f99bc72c92402a90d7089f55e 100644 (file)
@@ -15,15 +15,15 @@ import {
   Table,
   UpdatedAt
 } from 'sequelize-typescript'
-import { v4 as uuidv4 } from 'uuid'
 import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models'
-import { AttributesOnly } from '@shared/core-utils'
+import { buildUUID } from '@shared/extra-utils'
+import { AttributesOnly } from '@shared/typescript-utils'
 import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
 import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
 import { logger } from '../../helpers/logger'
 import { CONFIG } from '../../initializers/config'
 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants'
-import { buildWhereIdOrUUID, throwIfNotValid } from '../utils'
+import { buildWhereIdOrUUID, throwIfNotValid } from '../shared'
 import { VideoModel } from './video'
 
 export enum ScopeNames {
@@ -91,9 +91,9 @@ export class VideoCaptionModel extends Model<Partial<AttributesOnly<VideoCaption
   Video: VideoModel
 
   @BeforeDestroy
-  static async removeFiles (instance: VideoCaptionModel) {
+  static async removeFiles (instance: VideoCaptionModel, options) {
     if (!instance.Video) {
-      instance.Video = await instance.$get('Video')
+      instance.Video = await instance.$get('Video', { transaction: options.transaction })
     }
 
     if (instance.isOwned()) {
@@ -109,7 +109,7 @@ 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' ],
@@ -122,7 +122,8 @@ export class VideoCaptionModel extends Model<Partial<AttributesOnly<VideoCaption
       },
       include: [
         videoInclude
-      ]
+      ],
+      transaction
     }
 
     return VideoCaptionModel.findOne(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)
@@ -179,7 +182,7 @@ export class VideoCaptionModel extends Model<Partial<AttributesOnly<VideoCaption
   }
 
   static generateCaptionName (language: string) {
-    return `${uuidv4()}-${language}.vtt`
+    return `${buildUUID()}-${language}.vtt`
   }
 
   isOwned () {
@@ -192,7 +195,8 @@ export class VideoCaptionModel extends Model<Partial<AttributesOnly<VideoCaption
         id: this.language,
         label: VideoCaptionModel.getLanguageLabel(this.language)
       },
-      captionPath: this.getCaptionStaticPath()
+      captionPath: this.getCaptionStaticPath(),
+      updatedAt: this.updatedAt.toISOString()
     }
   }
 
@@ -211,4 +215,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
+  }
 }