]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video.ts
Playlist reorder support
[github/Chocobozzz/PeerTube.git] / server / models / video / video.ts
index 7a102b058c6679381046391da70003e77d8aceed..7624b064904c63e83c382ab6e487aec776fe727f 100644 (file)
@@ -53,7 +53,7 @@ import {
   API_VERSION,
   CONFIG,
   CONSTRAINTS_FIELDS,
-  HLS_PLAYLIST_DIRECTORY,
+  HLS_STREAMING_PLAYLIST_DIRECTORY,
   HLS_REDUNDANCY_DIRECTORY,
   PREVIEWS_SIZE,
   REMOTE_SCHEME,
@@ -225,7 +225,7 @@ type AvailableForListIDsOptions = {
       },
       include: [
         {
-          model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY)
+          model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] })
         }
       ]
     }
@@ -240,7 +240,10 @@ type AvailableForListIDsOptions = {
     if (options.videoPlaylistId) {
       query.include.push({
         model: VideoPlaylistElementModel.unscoped(),
-        required: true
+        required: true,
+        where: {
+          videoPlaylistId: options.videoPlaylistId
+        }
       })
     }
 
@@ -304,6 +307,8 @@ type AvailableForListIDsOptions = {
           videoPlaylistId: options.videoPlaylistId
         }
       })
+
+      query.subQuery = false
     }
 
     if (options.filter || options.accountId || options.videoChannelId) {
@@ -1535,18 +1540,7 @@ export class VideoModel extends Model<VideoModel> {
 
     if (ids.length === 0) return { data: [], total: count }
 
-    // FIXME: typings
-    const apiScope: any[] = [
-      {
-        method: [ ScopeNames.FOR_API, { ids, withFiles: options.withFiles } as ForAPIOptions ]
-      }
-    ]
-
-    if (options.user) {
-      apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] })
-    }
-
-    const secondQuery = {
+    const secondQuery: IFindOptions<VideoModel> = {
       offset: 0,
       limit: query.limit,
       attributes: query.attributes,
@@ -1556,6 +1550,29 @@ export class VideoModel extends Model<VideoModel> {
         )
       ]
     }
+
+    // FIXME: typing
+    const apiScope: any[] = []
+
+    if (options.user) {
+      apiScope.push({ method: [ ScopeNames.WITH_USER_HISTORY, options.user.id ] })
+
+      // Even if the relation is n:m, we know that a user only have 0..1 video history
+      // So we won't have multiple rows for the same video
+      // A subquery adds some bugs in our query so disable it
+      secondQuery.subQuery = false
+    }
+
+    apiScope.push({
+      method: [
+        ScopeNames.FOR_API, {
+          ids, withFiles:
+          options.withFiles,
+          videoPlaylistId: options.videoPlaylistId
+        } as ForAPIOptions
+      ]
+    })
+
     const rows = await VideoModel.scope(apiScope).findAll(secondQuery)
 
     return {
@@ -1739,7 +1756,7 @@ export class VideoModel extends Model<VideoModel> {
   }
 
   removeStreamingPlaylist (isRedundancy = false) {
-    const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_PLAYLIST_DIRECTORY
+    const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_STREAMING_PLAYLIST_DIRECTORY
 
     const filePath = join(baseDir, this.uuid)
     return remove(filePath)