]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video.ts
Use ISO 639 for languages
[github/Chocobozzz/PeerTube.git] / server / models / video / video.ts
index 240a2b5a2584d5271d9545d79290e979f7976b22..b0fff65268fd8542153fb16649ab6f915cfd42a7 100644 (file)
@@ -95,7 +95,7 @@ enum ScopeNames {
 }
 
 @Scopes({
-  [ScopeNames.AVAILABLE_FOR_LIST]: (actorId: number, filter?: VideoFilter, withFiles?: boolean) => {
+  [ScopeNames.AVAILABLE_FOR_LIST]: (actorId: number, hideNSFW: boolean, filter?: VideoFilter, withFiles?: boolean) => {
     const query: IFindOptions<VideoModel> = {
       where: {
         id: {
@@ -161,6 +161,11 @@ enum ScopeNames {
       })
     }
 
+    // Hide nsfw videos?
+    if (hideNSFW === true) {
+      query.where['nsfw'] = false
+    }
+
     return query
   },
   [ScopeNames.WITH_ACCOUNT_DETAILS]: {
@@ -317,8 +322,8 @@ export class VideoModel extends Model<VideoModel> {
   @AllowNull(true)
   @Default(null)
   @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
-  @Column
-  language: number
+  @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max))
+  language: string
 
   @AllowNull(false)
   @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
@@ -640,7 +645,7 @@ export class VideoModel extends Model<VideoModel> {
     })
   }
 
-  static listUserVideosForApi (userId: number, start: number, count: number, sort: string, withFiles = false) {
+  static listAccountVideosForApi (accountId: number, start: number, count: number, sort: string, hideNSFW: boolean, withFiles = false) {
     const query: IFindOptions<VideoModel> = {
       offset: start,
       limit: count,
@@ -653,7 +658,7 @@ export class VideoModel extends Model<VideoModel> {
             {
               model: AccountModel,
               where: {
-                userId
+                id: accountId
               },
               required: true
             }
@@ -669,6 +674,12 @@ export class VideoModel extends Model<VideoModel> {
       })
     }
 
+    if (hideNSFW === true) {
+      query.where = {
+        nsfw: false
+      }
+    }
+
     return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
       return {
         data: rows,
@@ -677,7 +688,7 @@ export class VideoModel extends Model<VideoModel> {
     })
   }
 
-  static async listForApi (start: number, count: number, sort: string, filter?: VideoFilter, withFiles = false) {
+  static async listForApi (start: number, count: number, sort: string, hideNSFW: boolean, filter?: VideoFilter, withFiles = false) {
     const query = {
       offset: start,
       limit: count,
@@ -685,8 +696,7 @@ export class VideoModel extends Model<VideoModel> {
     }
 
     const serverActor = await getServerActor()
-
-    return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, filter, withFiles ] })
+    return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, hideNSFW, filter, withFiles ] })
       .findAndCountAll(query)
       .then(({ rows, count }) => {
         return {
@@ -696,7 +706,7 @@ export class VideoModel extends Model<VideoModel> {
       })
   }
 
-  static async searchAndPopulateAccountAndServerAndTags (value: string, start: number, count: number, sort: string) {
+  static async searchAndPopulateAccountAndServer (value: string, start: number, count: number, sort: string, hideNSFW: boolean) {
     const query: IFindOptions<VideoModel> = {
       offset: start,
       limit: count,
@@ -724,7 +734,7 @@ export class VideoModel extends Model<VideoModel> {
 
     const serverActor = await getServerActor()
 
-    return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id ] })
+    return VideoModel.scope({ method: [ ScopeNames.AVAILABLE_FOR_LIST, serverActor.id, hideNSFW ] })
       .findAndCountAll(query)
       .then(({ rows, count }) => {
         return {
@@ -867,13 +877,22 @@ export class VideoModel extends Model<VideoModel> {
     return licenceLabel
   }
 
-  private static getLanguageLabel (id: number) {
+  private static getLanguageLabel (id: string) {
     let languageLabel = VIDEO_LANGUAGES[id]
+    console.log(VIDEO_LANGUAGES)
+    console.log(id)
     if (!languageLabel) languageLabel = 'Unknown'
 
     return languageLabel
   }
 
+  private static getPrivacyLabel (id: number) {
+    let privacyLabel = VIDEO_PRIVACIES[id]
+    if (!privacyLabel) privacyLabel = 'Unknown'
+
+    return privacyLabel
+  }
+
   getOriginalFile () {
     if (Array.isArray(this.VideoFiles) === false) return undefined
 
@@ -927,8 +946,11 @@ export class VideoModel extends Model<VideoModel> {
     return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
   }
 
-  createTorrentAndSetInfoHash = async function (videoFile: VideoFileModel) {
+  async createTorrentAndSetInfoHash (videoFile: VideoFileModel) {
     const options = {
+      // Keep the extname, it's used by the client to stream the file inside a web browser
+      name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`,
+      createdBy: 'PeerTube',
       announceList: [
         [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
         [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
@@ -980,6 +1002,10 @@ export class VideoModel extends Model<VideoModel> {
         id: this.language,
         label: VideoModel.getLanguageLabel(this.language)
       },
+      privacy: {
+        id: this.privacy,
+        label: VideoModel.getPrivacyLabel(this.privacy)
+      },
       nsfw: this.nsfw,
       description: this.getTruncatedDescription(),
       isLocal: this.isOwned(),
@@ -1006,15 +1032,7 @@ export class VideoModel extends Model<VideoModel> {
   toFormattedDetailsJSON (): VideoDetails {
     const formattedJson = this.toFormattedJSON()
 
-    // Maybe our server is not up to date and there are new privacy settings since our version
-    let privacyLabel = VIDEO_PRIVACIES[this.privacy]
-    if (!privacyLabel) privacyLabel = 'Unknown'
-
     const detailsJson = {
-      privacy: {
-        id: this.privacy,
-        label: privacyLabel
-      },
       support: this.support,
       descriptionPath: this.getDescriptionPath(),
       channel: this.VideoChannel.toFormattedJSON(),
@@ -1067,7 +1085,7 @@ export class VideoModel extends Model<VideoModel> {
     let language
     if (this.language) {
       language = {
-        identifier: this.language + '',
+        identifier: this.language,
         name: VideoModel.getLanguageLabel(this.language)
       }
     }
@@ -1227,7 +1245,7 @@ export class VideoModel extends Model<VideoModel> {
     return peertubeTruncate(this.description, maxLength)
   }
 
-  optimizeOriginalVideofile = async function () {
+  async optimizeOriginalVideofile () {
     const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
     const newExtname = '.mp4'
     const inputVideoFile = this.getOriginalFile()
@@ -1264,7 +1282,7 @@ export class VideoModel extends Model<VideoModel> {
     }
   }
 
-  transcodeOriginalVideofile = async function (resolution: VideoResolution, isPortraitMode: boolean) {
+  async transcodeOriginalVideofile (resolution: VideoResolution, isPortraitMode: boolean) {
     const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
     const extname = '.mp4'