]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/video-playlist.ts
Playlist reorder support
[github/Chocobozzz/PeerTube.git] / server / controllers / api / video-playlist.ts
index 145764d3508570b99da579ec5bf6c4d61756364c..0a7ff92df21218490556173b825a7726ead4b4bd 100644 (file)
@@ -41,6 +41,7 @@ import { VideoPlaylistElementCreate } from '../../../shared/models/videos/playli
 import { VideoPlaylistElementUpdate } from '../../../shared/models/videos/playlist/video-playlist-element-update.model'
 import { copy, pathExists } from 'fs-extra'
 import { AccountModel } from '../../models/account/account'
+import { VideoPlaylistReorder } from '../../../shared/models/videos/playlist/video-playlist-reorder.model'
 
 const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR })
 
@@ -291,23 +292,26 @@ async function addVideoInPlaylist (req: express.Request, res: express.Response)
       videoId: video.id
     }, { transaction: t })
 
-    // If the user did not set a thumbnail, automatically take the video thumbnail
-    if (playlistElement.position === 1) {
-      const playlistThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoPlaylist.getThumbnailName())
-
-      if (await pathExists(playlistThumbnailPath) === false) {
-        logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url)
-
-        const videoThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
-        await copy(videoThumbnailPath, playlistThumbnailPath)
-      }
-    }
+    videoPlaylist.updatedAt = new Date()
+    await videoPlaylist.save({ transaction: t })
 
     await sendUpdateVideoPlaylist(videoPlaylist, t)
 
     return playlistElement
   })
 
+  // If the user did not set a thumbnail, automatically take the video thumbnail
+  if (playlistElement.position === 1) {
+    const playlistThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoPlaylist.getThumbnailName())
+
+    if (await pathExists(playlistThumbnailPath) === false) {
+      logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url)
+
+      const videoThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
+      await copy(videoThumbnailPath, playlistThumbnailPath)
+    }
+  }
+
   logger.info('Video added in playlist %s at position %d.', videoPlaylist.uuid, playlistElement.position)
 
   return res.json({
@@ -328,6 +332,9 @@ async function updateVideoPlaylistElement (req: express.Request, res: express.Re
 
     const element = await videoPlaylistElement.save({ transaction: t })
 
+    videoPlaylist.updatedAt = new Date()
+    await videoPlaylist.save({ transaction: t })
+
     await sendUpdateVideoPlaylist(videoPlaylist, t)
 
     return element
@@ -349,6 +356,9 @@ async function removeVideoFromPlaylist (req: express.Request, res: express.Respo
     // Decrease position of the next elements
     await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, positionToDelete, null, -1, t)
 
+    videoPlaylist.updatedAt = new Date()
+    await videoPlaylist.save({ transaction: t })
+
     await sendUpdateVideoPlaylist(videoPlaylist, t)
 
     logger.info('Video playlist element %d of playlist %s deleted.', videoPlaylistElement.position, videoPlaylist.uuid)
@@ -359,10 +369,11 @@ async function removeVideoFromPlaylist (req: express.Request, res: express.Respo
 
 async function reorderVideosPlaylist (req: express.Request, res: express.Response) {
   const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
+  const body: VideoPlaylistReorder = req.body
 
-  const start: number = req.body.startPosition
-  const insertAfter: number = req.body.insertAfterPosition
-  const reorderLength: number = req.body.reorderLength || 1
+  const start: number = body.startPosition
+  const insertAfter: number = body.insertAfterPosition
+  const reorderLength: number = body.reorderLength || 1
 
   if (start === insertAfter) {
     return res.status(204).end()
@@ -390,6 +401,9 @@ async function reorderVideosPlaylist (req: express.Request, res: express.Respons
     // Decrease positions of elements after the old position of our ordered elements (decrease)
     await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, null, -reorderLength, t)
 
+    videoPlaylist.updatedAt = new Date()
+    await videoPlaylist.save({ transaction: t })
+
     await sendUpdateVideoPlaylist(videoPlaylist, t)
   })