]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video.ts
Cleanup server fixme
[github/Chocobozzz/PeerTube.git] / server / models / video / video.ts
index 990c66907f1b3bf848cd04ff5ce6077a4c01cd30..20e1f1c4aa97c043243001a231b75d23df473f17 100644 (file)
@@ -1,18 +1,7 @@
 import * as Bluebird from 'bluebird'
 import { maxBy, minBy } from 'lodash'
 import { join } from 'path'
-import {
-  CountOptions,
-  FindOptions,
-  IncludeOptions,
-  ModelIndexesOptions,
-  Op,
-  QueryTypes,
-  ScopeOptions,
-  Sequelize,
-  Transaction,
-  WhereOptions
-} from 'sequelize'
+import { CountOptions, FindOptions, IncludeOptions, Op, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
 import {
   AllowNull,
   BeforeDestroy,
@@ -142,75 +131,7 @@ import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../typings/mode
 import { MThumbnail } from '../../typings/models/video/thumbnail'
 import { VideoFile } from '@shared/models/videos/video-file.model'
 import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
-import * as validator from 'validator'
-
-// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
-const indexes: (ModelIndexesOptions & { where?: WhereOptions })[] = [
-  buildTrigramSearchIndex('video_name_trigram', 'name'),
-
-  { fields: [ 'createdAt' ] },
-  {
-    fields: [
-      { name: 'publishedAt', order: 'DESC' },
-      { name: 'id', order: 'ASC' }
-    ]
-  },
-  { fields: [ 'duration' ] },
-  { fields: [ 'views' ] },
-  { fields: [ 'channelId' ] },
-  {
-    fields: [ 'originallyPublishedAt' ],
-    where: {
-      originallyPublishedAt: {
-        [Op.ne]: null
-      }
-    }
-  },
-  {
-    fields: [ 'category' ], // We don't care videos with an unknown category
-    where: {
-      category: {
-        [Op.ne]: null
-      }
-    }
-  },
-  {
-    fields: [ 'licence' ], // We don't care videos with an unknown licence
-    where: {
-      licence: {
-        [Op.ne]: null
-      }
-    }
-  },
-  {
-    fields: [ 'language' ], // We don't care videos with an unknown language
-    where: {
-      language: {
-        [Op.ne]: null
-      }
-    }
-  },
-  {
-    fields: [ 'nsfw' ], // Most of the videos are not NSFW
-    where: {
-      nsfw: true
-    }
-  },
-  {
-    fields: [ 'remote' ], // Only index local videos
-    where: {
-      remote: false
-    }
-  },
-  {
-    fields: [ 'uuid' ],
-    unique: true
-  },
-  {
-    fields: [ 'url' ],
-    unique: true
-  }
-]
+import validator from 'validator'
 
 export enum ScopeNames {
   AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
@@ -291,7 +212,7 @@ export type AvailableForListIDsOptions = {
     if (options.ids) {
       query.where = {
         id: {
-          [ Op.in ]: options.ids // FIXME: sequelize ANY seems broken
+          [ Op.in ]: options.ids
         }
       }
     }
@@ -452,7 +373,14 @@ export type AvailableForListIDsOptions = {
                 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
                 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
                 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
-                ' UNION ALL ' +
+                ')'
+              )
+            }
+          },
+          {
+            id: {
+              [ Op.in ]: Sequelize.literal(
+                '(' +
                 'SELECT "video"."id" AS "id" FROM "video" ' +
                 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
                 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
@@ -752,7 +680,72 @@ export type AvailableForListIDsOptions = {
 }))
 @Table({
   tableName: 'video',
-  indexes
+  indexes: [
+    buildTrigramSearchIndex('video_name_trigram', 'name'),
+
+    { fields: [ 'createdAt' ] },
+    {
+      fields: [
+        { name: 'publishedAt', order: 'DESC' },
+        { name: 'id', order: 'ASC' }
+      ]
+    },
+    { fields: [ 'duration' ] },
+    { fields: [ 'views' ] },
+    { fields: [ 'channelId' ] },
+    {
+      fields: [ 'originallyPublishedAt' ],
+      where: {
+        originallyPublishedAt: {
+          [Op.ne]: null
+        }
+      }
+    },
+    {
+      fields: [ 'category' ], // We don't care videos with an unknown category
+      where: {
+        category: {
+          [Op.ne]: null
+        }
+      }
+    },
+    {
+      fields: [ 'licence' ], // We don't care videos with an unknown licence
+      where: {
+        licence: {
+          [Op.ne]: null
+        }
+      }
+    },
+    {
+      fields: [ 'language' ], // We don't care videos with an unknown language
+      where: {
+        language: {
+          [Op.ne]: null
+        }
+      }
+    },
+    {
+      fields: [ 'nsfw' ], // Most of the videos are not NSFW
+      where: {
+        nsfw: true
+      }
+    },
+    {
+      fields: [ 'remote' ], // Only index local videos
+      where: {
+        remote: false
+      }
+    },
+    {
+      fields: [ 'uuid' ],
+      unique: true
+    },
+    {
+      fields: [ 'url' ],
+      unique: true
+    }
+  ]
 })
 export class VideoModel extends Model<VideoModel> {
 
@@ -1054,7 +1047,7 @@ export class VideoModel extends Model<VideoModel> {
 
     if (instance.isOwned()) {
       if (!Array.isArray(instance.VideoFiles)) {
-        instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[]
+        instance.VideoFiles = await instance.$get('VideoFiles')
       }
 
       // Remove physical files and torrents
@@ -1064,7 +1057,13 @@ export class VideoModel extends Model<VideoModel> {
       })
 
       // Remove playlists file
-      tasks.push(instance.removeStreamingPlaylist())
+      if (!Array.isArray(instance.VideoStreamingPlaylists)) {
+        instance.VideoStreamingPlaylists = await instance.$get('VideoStreamingPlaylists')
+      }
+
+      for (const p of instance.VideoStreamingPlaylists) {
+        tasks.push(instance.removeStreamingPlaylistFiles(p))
+      }
     }
 
     // Do not wait video deletion because we could be in a transaction
@@ -1196,9 +1195,15 @@ export class VideoModel extends Model<VideoModel> {
     })
   }
 
-  static listUserVideosForApi (accountId: number, start: number, count: number, sort: string) {
+  static listUserVideosForApi (
+    accountId: number,
+    start: number,
+    count: number,
+    sort: string,
+    search?: string
+  ) {
     function buildBaseQuery (): FindOptions {
-      return {
+      let baseQuery = {
         offset: start,
         limit: count,
         order: getVideoSort(sort),
@@ -1218,12 +1223,24 @@ export class VideoModel extends Model<VideoModel> {
           }
         ]
       }
+
+      if (search) {
+        baseQuery = Object.assign(baseQuery, {
+          where: {
+            name: {
+              [ Op.iLike ]: '%' + search + '%'
+            }
+          }
+        })
+      }
+
+      return baseQuery
     }
 
     const countQuery = buildBaseQuery()
     const findQuery = buildBaseQuery()
 
-    const findScopes = [
+    const findScopes: (string | ScopeOptions)[] = [
       ScopeNames.WITH_SCHEDULED_UPDATE,
       ScopeNames.WITH_BLACKLISTED,
       ScopeNames.WITH_THUMBNAILS
@@ -1259,8 +1276,9 @@ export class VideoModel extends Model<VideoModel> {
     videoPlaylistId?: number,
     trendingDays?: number,
     user?: MUserAccountId,
-    historyOfUser?: MUserId
-  }, countVideos = true) {
+    historyOfUser?: MUserId,
+    countVideos?: boolean
+  }) {
     if (options.filter && options.filter === 'all-local' && !options.user.hasRight(UserRight.SEE_ALL_VIDEOS)) {
       throw new Error('Try to filter all-local but no user has not the see all videos right')
     }
@@ -1303,7 +1321,7 @@ export class VideoModel extends Model<VideoModel> {
       trendingDays
     }
 
-    return VideoModel.getAvailableForApi(query, queryOptions, countVideos)
+    return VideoModel.getAvailableForApi(query, queryOptions, options.countVideos)
   }
 
   static async searchAndPopulateAccountAndServer (options: {
@@ -1371,7 +1389,7 @@ export class VideoModel extends Model<VideoModel> {
             'UNION ALL ' +
             'SELECT "video"."id" FROM "video" LEFT JOIN "videoTag" ON "videoTag"."videoId" = "video"."id" ' +
             'INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
-            'WHERE "tag"."name" = ' + escapedSearch +
+            'WHERE lower("tag"."name") = lower(' + escapedSearch + ')' +
             ')'
           )
         }
@@ -1725,17 +1743,25 @@ export class VideoModel extends Model<VideoModel> {
       ]
     }
 
-    const [ count, ids ] = await Promise.all([
+    const [ count, rows ] = await Promise.all([
       countVideos
         ? VideoModel.scope(countScope).count(countQuery)
         : Promise.resolve<number>(undefined),
 
       VideoModel.scope(idsScope)
-                .findAll(query)
+                .findAll(Object.assign({}, query, { raw: true }))
                 .then(rows => rows.map(r => r.id))
+                .then(ids => VideoModel.loadCompleteVideosForApi(ids, query, options))
     ])
 
-    if (ids.length === 0) return { data: [], total: count }
+    return {
+      data: rows,
+      total: count
+    }
+  }
+
+  private static loadCompleteVideosForApi (ids: number[], query: FindOptions, options: AvailableForListIDsOptions) {
+    if (ids.length === 0) return []
 
     const secondQuery: FindOptions = {
       offset: 0,
@@ -1764,16 +1790,13 @@ export class VideoModel extends Model<VideoModel> {
       ]
     })
 
-    const rows = await VideoModel.scope(apiScope).findAll(secondQuery)
-
-    return {
-      data: rows,
-      total: count
-    }
+    return VideoModel.scope(apiScope).findAll(secondQuery)
   }
 
   private static isPrivacyForFederation (privacy: VideoPrivacy) {
-    return privacy === VideoPrivacy.PUBLIC || privacy === VideoPrivacy.UNLISTED
+    const castedPrivacy = parseInt(privacy + '', 10)
+
+    return castedPrivacy === VideoPrivacy.PUBLIC || castedPrivacy === VideoPrivacy.UNLISTED
   }
 
   static getCategoryLabel (id: number) {
@@ -1970,11 +1993,24 @@ export class VideoModel extends Model<VideoModel> {
       .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))
   }
 
-  removeStreamingPlaylist (isRedundancy = false) {
+  async removeStreamingPlaylistFiles (streamingPlaylist: MStreamingPlaylist, isRedundancy = false) {
     const directoryPath = getHLSDirectory(this, isRedundancy)
 
-    return remove(directoryPath)
-      .catch(err => logger.warn('Cannot delete playlist directory %s.', directoryPath, { err }))
+    await remove(directoryPath)
+
+    if (isRedundancy !== true) {
+      let streamingPlaylistWithFiles = streamingPlaylist as MStreamingPlaylistFilesVideo
+      streamingPlaylistWithFiles.Video = this
+
+      if (!Array.isArray(streamingPlaylistWithFiles.VideoFiles)) {
+        streamingPlaylistWithFiles.VideoFiles = await streamingPlaylistWithFiles.$get('VideoFiles')
+      }
+
+      // Remove physical files and torrents
+      await Promise.all(
+        streamingPlaylistWithFiles.VideoFiles.map(file => streamingPlaylistWithFiles.removeTorrent(file))
+      )
+    }
   }
 
   isOutdated () {