]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
de94ac86 | 2 | import { join } from 'path' |
37a44fc9 | 3 | import { scheduleRefreshIfNeeded } from '@server/lib/activitypub/playlists' |
d4a8e7a6 | 4 | import { Hooks } from '@server/lib/plugins/hooks' |
de94ac86 C |
5 | import { getServerActor } from '@server/models/application/application' |
6 | import { MVideoPlaylistFull, MVideoPlaylistThumbnail, MVideoThumbnail } from '@server/types/models' | |
99b75748 | 7 | import { forceNumber } from '@shared/core-utils' |
0628157f | 8 | import { uuidToShort } from '@shared/extra-utils' |
e6346d59 | 9 | import { VideoPlaylistCreateResult, VideoPlaylistElementCreateResult } from '@shared/models' |
c0e8b12e | 10 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
de94ac86 C |
11 | import { VideoPlaylistCreate } from '../../../shared/models/videos/playlist/video-playlist-create.model' |
12 | import { VideoPlaylistElementCreate } from '../../../shared/models/videos/playlist/video-playlist-element-create.model' | |
13 | import { VideoPlaylistElementUpdate } from '../../../shared/models/videos/playlist/video-playlist-element-update.model' | |
14 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' | |
15 | import { VideoPlaylistReorder } from '../../../shared/models/videos/playlist/video-playlist-reorder.model' | |
16 | import { VideoPlaylistUpdate } from '../../../shared/models/videos/playlist/video-playlist-update.model' | |
17 | import { resetSequelizeInstance } from '../../helpers/database-utils' | |
a68ccaea | 18 | import { createReqFiles } from '../../helpers/express-utils' |
de94ac86 | 19 | import { logger } from '../../helpers/logger' |
e1c55031 | 20 | import { getFormattedObjects } from '../../helpers/utils' |
de94ac86 C |
21 | import { CONFIG } from '../../initializers/config' |
22 | import { MIMETYPES, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers/constants' | |
23 | import { sequelizeTypescript } from '../../initializers/database' | |
24 | import { sendCreateVideoPlaylist, sendDeleteVideoPlaylist, sendUpdateVideoPlaylist } from '../../lib/activitypub/send' | |
25 | import { getLocalVideoPlaylistActivityPubUrl, getLocalVideoPlaylistElementActivityPubUrl } from '../../lib/activitypub/url' | |
91f8f8db | 26 | import { updatePlaylistMiniatureFromExisting } from '../../lib/thumbnail' |
418d092a | 27 | import { |
e915cde3 | 28 | apiRateLimiter, |
418d092a C |
29 | asyncMiddleware, |
30 | asyncRetryTransactionMiddleware, | |
31 | authenticate, | |
09979f89 | 32 | optionalAuthenticate, |
418d092a C |
33 | paginationValidator, |
34 | setDefaultPagination, | |
35 | setDefaultSort | |
36 | } from '../../middlewares' | |
418d092a | 37 | import { videoPlaylistsSortValidator } from '../../middlewares/validators' |
418d092a | 38 | import { |
df0b219d | 39 | commonVideoPlaylistFiltersValidator, |
418d092a C |
40 | videoPlaylistsAddValidator, |
41 | videoPlaylistsAddVideoValidator, | |
42 | videoPlaylistsDeleteValidator, | |
43 | videoPlaylistsGetValidator, | |
44 | videoPlaylistsReorderVideosValidator, | |
45 | videoPlaylistsUpdateOrRemoveVideoValidator, | |
46 | videoPlaylistsUpdateValidator | |
47 | } from '../../middlewares/validators/videos/video-playlists' | |
df0b219d | 48 | import { AccountModel } from '../../models/account/account' |
de94ac86 C |
49 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
50 | import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' | |
418d092a | 51 | |
d3d3deaa | 52 | const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) |
418d092a C |
53 | |
54 | const videoPlaylistRouter = express.Router() | |
55 | ||
e915cde3 C |
56 | videoPlaylistRouter.use(apiRateLimiter) |
57 | ||
d4c9f45b C |
58 | videoPlaylistRouter.get('/privacies', listVideoPlaylistPrivacies) |
59 | ||
418d092a C |
60 | videoPlaylistRouter.get('/', |
61 | paginationValidator, | |
62 | videoPlaylistsSortValidator, | |
63 | setDefaultSort, | |
64 | setDefaultPagination, | |
df0b219d | 65 | commonVideoPlaylistFiltersValidator, |
418d092a C |
66 | asyncMiddleware(listVideoPlaylists) |
67 | ) | |
68 | ||
69 | videoPlaylistRouter.get('/:playlistId', | |
453e83ea | 70 | asyncMiddleware(videoPlaylistsGetValidator('summary')), |
418d092a C |
71 | getVideoPlaylist |
72 | ) | |
73 | ||
74 | videoPlaylistRouter.post('/', | |
75 | authenticate, | |
76 | reqThumbnailFile, | |
77 | asyncMiddleware(videoPlaylistsAddValidator), | |
78 | asyncRetryTransactionMiddleware(addVideoPlaylist) | |
79 | ) | |
80 | ||
81 | videoPlaylistRouter.put('/:playlistId', | |
82 | authenticate, | |
83 | reqThumbnailFile, | |
84 | asyncMiddleware(videoPlaylistsUpdateValidator), | |
85 | asyncRetryTransactionMiddleware(updateVideoPlaylist) | |
86 | ) | |
87 | ||
88 | videoPlaylistRouter.delete('/:playlistId', | |
89 | authenticate, | |
90 | asyncMiddleware(videoPlaylistsDeleteValidator), | |
91 | asyncRetryTransactionMiddleware(removeVideoPlaylist) | |
92 | ) | |
93 | ||
94 | videoPlaylistRouter.get('/:playlistId/videos', | |
453e83ea | 95 | asyncMiddleware(videoPlaylistsGetValidator('summary')), |
418d092a C |
96 | paginationValidator, |
97 | setDefaultPagination, | |
07b1a18a | 98 | optionalAuthenticate, |
418d092a C |
99 | asyncMiddleware(getVideoPlaylistVideos) |
100 | ) | |
101 | ||
102 | videoPlaylistRouter.post('/:playlistId/videos', | |
103 | authenticate, | |
104 | asyncMiddleware(videoPlaylistsAddVideoValidator), | |
105 | asyncRetryTransactionMiddleware(addVideoInPlaylist) | |
106 | ) | |
107 | ||
07b1a18a | 108 | videoPlaylistRouter.post('/:playlistId/videos/reorder', |
418d092a C |
109 | authenticate, |
110 | asyncMiddleware(videoPlaylistsReorderVideosValidator), | |
111 | asyncRetryTransactionMiddleware(reorderVideosPlaylist) | |
112 | ) | |
113 | ||
bfbd9128 | 114 | videoPlaylistRouter.put('/:playlistId/videos/:playlistElementId', |
418d092a C |
115 | authenticate, |
116 | asyncMiddleware(videoPlaylistsUpdateOrRemoveVideoValidator), | |
117 | asyncRetryTransactionMiddleware(updateVideoPlaylistElement) | |
118 | ) | |
119 | ||
bfbd9128 | 120 | videoPlaylistRouter.delete('/:playlistId/videos/:playlistElementId', |
418d092a C |
121 | authenticate, |
122 | asyncMiddleware(videoPlaylistsUpdateOrRemoveVideoValidator), | |
123 | asyncRetryTransactionMiddleware(removeVideoFromPlaylist) | |
124 | ) | |
125 | ||
126 | // --------------------------------------------------------------------------- | |
127 | ||
128 | export { | |
129 | videoPlaylistRouter | |
130 | } | |
131 | ||
132 | // --------------------------------------------------------------------------- | |
133 | ||
d4c9f45b C |
134 | function listVideoPlaylistPrivacies (req: express.Request, res: express.Response) { |
135 | res.json(VIDEO_PLAYLIST_PRIVACIES) | |
136 | } | |
137 | ||
418d092a C |
138 | async function listVideoPlaylists (req: express.Request, res: express.Response) { |
139 | const serverActor = await getServerActor() | |
140 | const resultList = await VideoPlaylistModel.listForApi({ | |
141 | followerActorId: serverActor.id, | |
142 | start: req.query.start, | |
143 | count: req.query.count, | |
df0b219d | 144 | sort: req.query.sort, |
16ccb437 | 145 | type: req.query.playlistType |
418d092a C |
146 | }) |
147 | ||
148 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
149 | } | |
150 | ||
151 | function getVideoPlaylist (req: express.Request, res: express.Response) { | |
453e83ea | 152 | const videoPlaylist = res.locals.videoPlaylistSummary |
418d092a | 153 | |
37a44fc9 | 154 | scheduleRefreshIfNeeded(videoPlaylist) |
9f79ade6 | 155 | |
418d092a C |
156 | return res.json(videoPlaylist.toFormattedJSON()) |
157 | } | |
158 | ||
159 | async function addVideoPlaylist (req: express.Request, res: express.Response) { | |
160 | const videoPlaylistInfo: VideoPlaylistCreate = req.body | |
dae86118 | 161 | const user = res.locals.oauth.token.User |
418d092a C |
162 | |
163 | const videoPlaylist = new VideoPlaylistModel({ | |
164 | name: videoPlaylistInfo.displayName, | |
165 | description: videoPlaylistInfo.description, | |
166 | privacy: videoPlaylistInfo.privacy || VideoPlaylistPrivacy.PRIVATE, | |
167 | ownerAccountId: user.Account.id | |
453e83ea | 168 | }) as MVideoPlaylistFull |
418d092a | 169 | |
de94ac86 | 170 | videoPlaylist.url = getLocalVideoPlaylistActivityPubUrl(videoPlaylist) // We use the UUID, so set the URL after building the object |
418d092a | 171 | |
d4c9f45b | 172 | if (videoPlaylistInfo.videoChannelId) { |
dae86118 | 173 | const videoChannel = res.locals.videoChannel |
418d092a C |
174 | |
175 | videoPlaylist.videoChannelId = videoChannel.id | |
176 | videoPlaylist.VideoChannel = videoChannel | |
177 | } | |
178 | ||
179 | const thumbnailField = req.files['thumbnailfile'] | |
e8bafea3 | 180 | const thumbnailModel = thumbnailField |
91f8f8db | 181 | ? await updatePlaylistMiniatureFromExisting({ |
a35a2279 C |
182 | inputPath: thumbnailField[0].path, |
183 | playlist: videoPlaylist, | |
184 | automaticallyGenerated: false | |
185 | }) | |
e8bafea3 | 186 | : undefined |
418d092a | 187 | |
453e83ea C |
188 | const videoPlaylistCreated = await sequelizeTypescript.transaction(async t => { |
189 | const videoPlaylistCreated = await videoPlaylist.save({ transaction: t }) as MVideoPlaylistFull | |
418d092a | 190 | |
65af03a2 C |
191 | if (thumbnailModel) { |
192 | thumbnailModel.automaticallyGenerated = false | |
193 | await videoPlaylistCreated.setAndSaveThumbnail(thumbnailModel, t) | |
194 | } | |
e8bafea3 | 195 | |
df0b219d C |
196 | // We need more attributes for the federation |
197 | videoPlaylistCreated.OwnerAccount = await AccountModel.load(user.Account.id, t) | |
418d092a C |
198 | await sendCreateVideoPlaylist(videoPlaylistCreated, t) |
199 | ||
200 | return videoPlaylistCreated | |
201 | }) | |
202 | ||
203 | logger.info('Video playlist with uuid %s created.', videoPlaylist.uuid) | |
204 | ||
205 | return res.json({ | |
206 | videoPlaylist: { | |
207 | id: videoPlaylistCreated.id, | |
d4a8e7a6 | 208 | shortUUID: uuidToShort(videoPlaylistCreated.uuid), |
418d092a | 209 | uuid: videoPlaylistCreated.uuid |
e6346d59 | 210 | } as VideoPlaylistCreateResult |
c158a5fa | 211 | }) |
418d092a C |
212 | } |
213 | ||
214 | async function updateVideoPlaylist (req: express.Request, res: express.Response) { | |
453e83ea | 215 | const videoPlaylistInstance = res.locals.videoPlaylistFull |
418d092a | 216 | const videoPlaylistInfoToUpdate = req.body as VideoPlaylistUpdate |
1b319b7a | 217 | |
418d092a | 218 | const wasPrivatePlaylist = videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE |
1b319b7a | 219 | const wasNotPrivatePlaylist = videoPlaylistInstance.privacy !== VideoPlaylistPrivacy.PRIVATE |
418d092a C |
220 | |
221 | const thumbnailField = req.files['thumbnailfile'] | |
e8bafea3 | 222 | const thumbnailModel = thumbnailField |
91f8f8db | 223 | ? await updatePlaylistMiniatureFromExisting({ |
a35a2279 C |
224 | inputPath: thumbnailField[0].path, |
225 | playlist: videoPlaylistInstance, | |
226 | automaticallyGenerated: false | |
227 | }) | |
e8bafea3 | 228 | : undefined |
418d092a C |
229 | |
230 | try { | |
231 | await sequelizeTypescript.transaction(async t => { | |
232 | const sequelizeOptions = { | |
233 | transaction: t | |
234 | } | |
235 | ||
236 | if (videoPlaylistInfoToUpdate.videoChannelId !== undefined) { | |
237 | if (videoPlaylistInfoToUpdate.videoChannelId === null) { | |
238 | videoPlaylistInstance.videoChannelId = null | |
239 | } else { | |
dae86118 | 240 | const videoChannel = res.locals.videoChannel |
418d092a C |
241 | |
242 | videoPlaylistInstance.videoChannelId = videoChannel.id | |
df0b219d | 243 | videoPlaylistInstance.VideoChannel = videoChannel |
418d092a C |
244 | } |
245 | } | |
246 | ||
247 | if (videoPlaylistInfoToUpdate.displayName !== undefined) videoPlaylistInstance.name = videoPlaylistInfoToUpdate.displayName | |
248 | if (videoPlaylistInfoToUpdate.description !== undefined) videoPlaylistInstance.description = videoPlaylistInfoToUpdate.description | |
249 | ||
250 | if (videoPlaylistInfoToUpdate.privacy !== undefined) { | |
4638cd71 | 251 | videoPlaylistInstance.privacy = forceNumber(videoPlaylistInfoToUpdate.privacy) |
1b319b7a C |
252 | |
253 | if (wasNotPrivatePlaylist === true && videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE) { | |
254 | await sendDeleteVideoPlaylist(videoPlaylistInstance, t) | |
255 | } | |
418d092a C |
256 | } |
257 | ||
258 | const playlistUpdated = await videoPlaylistInstance.save(sequelizeOptions) | |
259 | ||
65af03a2 C |
260 | if (thumbnailModel) { |
261 | thumbnailModel.automaticallyGenerated = false | |
262 | await playlistUpdated.setAndSaveThumbnail(thumbnailModel, t) | |
263 | } | |
e8bafea3 | 264 | |
418d092a C |
265 | const isNewPlaylist = wasPrivatePlaylist && playlistUpdated.privacy !== VideoPlaylistPrivacy.PRIVATE |
266 | ||
267 | if (isNewPlaylist) { | |
268 | await sendCreateVideoPlaylist(playlistUpdated, t) | |
269 | } else { | |
270 | await sendUpdateVideoPlaylist(playlistUpdated, t) | |
271 | } | |
272 | ||
273 | logger.info('Video playlist %s updated.', videoPlaylistInstance.uuid) | |
274 | ||
275 | return playlistUpdated | |
276 | }) | |
277 | } catch (err) { | |
278 | logger.debug('Cannot update the video playlist.', { err }) | |
279 | ||
418d092a | 280 | // If the transaction is retried, sequelize will think the object has not changed |
45657746 | 281 | // So we need to restore the previous fields |
823c34c0 | 282 | await resetSequelizeInstance(videoPlaylistInstance) |
418d092a C |
283 | |
284 | throw err | |
285 | } | |
286 | ||
2d53be02 | 287 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() |
418d092a C |
288 | } |
289 | ||
290 | async function removeVideoPlaylist (req: express.Request, res: express.Response) { | |
453e83ea | 291 | const videoPlaylistInstance = res.locals.videoPlaylistSummary |
418d092a C |
292 | |
293 | await sequelizeTypescript.transaction(async t => { | |
294 | await videoPlaylistInstance.destroy({ transaction: t }) | |
295 | ||
296 | await sendDeleteVideoPlaylist(videoPlaylistInstance, t) | |
297 | ||
298 | logger.info('Video playlist %s deleted.', videoPlaylistInstance.uuid) | |
299 | }) | |
300 | ||
2d53be02 | 301 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() |
418d092a C |
302 | } |
303 | ||
304 | async function addVideoInPlaylist (req: express.Request, res: express.Response) { | |
305 | const body: VideoPlaylistElementCreate = req.body | |
453e83ea C |
306 | const videoPlaylist = res.locals.videoPlaylistFull |
307 | const video = res.locals.onlyVideo | |
418d092a | 308 | |
453e83ea | 309 | const playlistElement = await sequelizeTypescript.transaction(async t => { |
418d092a C |
310 | const position = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id, t) |
311 | ||
312 | const playlistElement = await VideoPlaylistElementModel.create({ | |
418d092a C |
313 | position, |
314 | startTimestamp: body.startTimestamp || null, | |
315 | stopTimestamp: body.stopTimestamp || null, | |
316 | videoPlaylistId: videoPlaylist.id, | |
317 | videoId: video.id | |
318 | }, { transaction: t }) | |
319 | ||
de94ac86 | 320 | playlistElement.url = getLocalVideoPlaylistElementActivityPubUrl(videoPlaylist, playlistElement) |
37190663 C |
321 | await playlistElement.save({ transaction: t }) |
322 | ||
2a10aab3 | 323 | videoPlaylist.changed('updatedAt', true) |
f0a39880 | 324 | await videoPlaylist.save({ transaction: t }) |
418d092a | 325 | |
418d092a C |
326 | return playlistElement |
327 | }) | |
328 | ||
f0a39880 | 329 | // If the user did not set a thumbnail, automatically take the video thumbnail |
65af03a2 C |
330 | if (videoPlaylist.hasThumbnail() === false || (videoPlaylist.hasGeneratedThumbnail() && playlistElement.position === 1)) { |
331 | await generateThumbnailForPlaylist(videoPlaylist, video) | |
f0a39880 C |
332 | } |
333 | ||
65af03a2 C |
334 | sendUpdateVideoPlaylist(videoPlaylist, undefined) |
335 | .catch(err => logger.error('Cannot send video playlist update.', { err })) | |
336 | ||
418d092a C |
337 | logger.info('Video added in playlist %s at position %d.', videoPlaylist.uuid, playlistElement.position) |
338 | ||
7226e90f | 339 | Hooks.runAction('action:api.video-playlist-element.created', { playlistElement, req, res }) |
e2e0b645 | 340 | |
418d092a C |
341 | return res.json({ |
342 | videoPlaylistElement: { | |
343 | id: playlistElement.id | |
e6346d59 C |
344 | } as VideoPlaylistElementCreateResult |
345 | }) | |
418d092a C |
346 | } |
347 | ||
348 | async function updateVideoPlaylistElement (req: express.Request, res: express.Response) { | |
349 | const body: VideoPlaylistElementUpdate = req.body | |
453e83ea | 350 | const videoPlaylist = res.locals.videoPlaylistFull |
dae86118 | 351 | const videoPlaylistElement = res.locals.videoPlaylistElement |
418d092a C |
352 | |
353 | const playlistElement: VideoPlaylistElementModel = await sequelizeTypescript.transaction(async t => { | |
354 | if (body.startTimestamp !== undefined) videoPlaylistElement.startTimestamp = body.startTimestamp | |
355 | if (body.stopTimestamp !== undefined) videoPlaylistElement.stopTimestamp = body.stopTimestamp | |
356 | ||
357 | const element = await videoPlaylistElement.save({ transaction: t }) | |
358 | ||
2a10aab3 | 359 | videoPlaylist.changed('updatedAt', true) |
f0a39880 C |
360 | await videoPlaylist.save({ transaction: t }) |
361 | ||
418d092a C |
362 | await sendUpdateVideoPlaylist(videoPlaylist, t) |
363 | ||
364 | return element | |
365 | }) | |
366 | ||
367 | logger.info('Element of position %d of playlist %s updated.', playlistElement.position, videoPlaylist.uuid) | |
368 | ||
2d53be02 | 369 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() |
418d092a C |
370 | } |
371 | ||
372 | async function removeVideoFromPlaylist (req: express.Request, res: express.Response) { | |
dae86118 | 373 | const videoPlaylistElement = res.locals.videoPlaylistElement |
453e83ea | 374 | const videoPlaylist = res.locals.videoPlaylistFull |
418d092a C |
375 | const positionToDelete = videoPlaylistElement.position |
376 | ||
377 | await sequelizeTypescript.transaction(async t => { | |
378 | await videoPlaylistElement.destroy({ transaction: t }) | |
379 | ||
380 | // Decrease position of the next elements | |
11a554cf | 381 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, positionToDelete, -1, t) |
418d092a | 382 | |
2a10aab3 | 383 | videoPlaylist.changed('updatedAt', true) |
f0a39880 C |
384 | await videoPlaylist.save({ transaction: t }) |
385 | ||
418d092a C |
386 | logger.info('Video playlist element %d of playlist %s deleted.', videoPlaylistElement.position, videoPlaylist.uuid) |
387 | }) | |
388 | ||
65af03a2 C |
389 | // Do we need to regenerate the default thumbnail? |
390 | if (positionToDelete === 1 && videoPlaylist.hasGeneratedThumbnail()) { | |
391 | await regeneratePlaylistThumbnail(videoPlaylist) | |
392 | } | |
393 | ||
394 | sendUpdateVideoPlaylist(videoPlaylist, undefined) | |
395 | .catch(err => logger.error('Cannot send video playlist update.', { err })) | |
396 | ||
2d53be02 | 397 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() |
418d092a C |
398 | } |
399 | ||
400 | async function reorderVideosPlaylist (req: express.Request, res: express.Response) { | |
453e83ea | 401 | const videoPlaylist = res.locals.videoPlaylistFull |
15e9d5ca | 402 | const body: VideoPlaylistReorder = req.body |
418d092a | 403 | |
15e9d5ca C |
404 | const start: number = body.startPosition |
405 | const insertAfter: number = body.insertAfterPosition | |
406 | const reorderLength: number = body.reorderLength || 1 | |
418d092a C |
407 | |
408 | if (start === insertAfter) { | |
2d53be02 | 409 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
418d092a C |
410 | } |
411 | ||
412 | // Example: if we reorder position 2 and insert after position 5 (so at position 6): # 1 2 3 4 5 6 7 8 9 | |
413 | // * increase position when position > 5 # 1 2 3 4 5 7 8 9 10 | |
414 | // * update position 2 -> position 6 # 1 3 4 5 6 7 8 9 10 | |
415 | // * decrease position when position position > 2 # 1 2 3 4 5 6 7 8 9 | |
416 | await sequelizeTypescript.transaction(async t => { | |
417 | const newPosition = insertAfter + 1 | |
418 | ||
419 | // Add space after the position when we want to insert our reordered elements (increase) | |
11a554cf | 420 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, newPosition, reorderLength, t) |
418d092a C |
421 | |
422 | let oldPosition = start | |
423 | ||
424 | // We incremented the position of the elements we want to reorder | |
425 | if (start >= newPosition) oldPosition += reorderLength | |
426 | ||
427 | const endOldPosition = oldPosition + reorderLength - 1 | |
428 | // Insert our reordered elements in their place (update) | |
99b75748 C |
429 | await VideoPlaylistElementModel.reassignPositionOf({ |
430 | videoPlaylistId: videoPlaylist.id, | |
431 | firstPosition: oldPosition, | |
432 | endPosition: endOldPosition, | |
433 | newPosition, | |
434 | transaction: t | |
435 | }) | |
418d092a C |
436 | |
437 | // Decrease positions of elements after the old position of our ordered elements (decrease) | |
11a554cf | 438 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, -reorderLength, t) |
418d092a | 439 | |
2a10aab3 | 440 | videoPlaylist.changed('updatedAt', true) |
f0a39880 C |
441 | await videoPlaylist.save({ transaction: t }) |
442 | ||
418d092a C |
443 | await sendUpdateVideoPlaylist(videoPlaylist, t) |
444 | }) | |
445 | ||
65af03a2 C |
446 | // The first element changed |
447 | if ((start === 1 || insertAfter === 0) && videoPlaylist.hasGeneratedThumbnail()) { | |
448 | await regeneratePlaylistThumbnail(videoPlaylist) | |
449 | } | |
450 | ||
418d092a | 451 | logger.info( |
65af03a2 | 452 | 'Reordered playlist %s (inserted after position %d elements %d - %d).', |
418d092a C |
453 | videoPlaylist.uuid, insertAfter, start, start + reorderLength - 1 |
454 | ) | |
455 | ||
2d53be02 | 456 | return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() |
418d092a C |
457 | } |
458 | ||
459 | async function getVideoPlaylistVideos (req: express.Request, res: express.Response) { | |
453e83ea | 460 | const videoPlaylistInstance = res.locals.videoPlaylistSummary |
bfbd9128 C |
461 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined |
462 | const server = await getServerActor() | |
418d092a | 463 | |
c5ca7e1e | 464 | const apiOptions = await Hooks.wrapObject({ |
418d092a C |
465 | start: req.query.start, |
466 | count: req.query.count, | |
418d092a | 467 | videoPlaylistId: videoPlaylistInstance.id, |
bfbd9128 C |
468 | serverAccount: server.Account, |
469 | user | |
c5ca7e1e | 470 | }, 'filter:api.video-playlist.videos.list.params') |
471 | ||
472 | const resultList = await Hooks.wrapPromiseFun( | |
473 | VideoPlaylistElementModel.listForApi, | |
474 | apiOptions, | |
475 | 'filter:api.video-playlist.videos.list.result' | |
476 | ) | |
418d092a | 477 | |
a68ccaea | 478 | const options = { accountId: user?.Account?.id } |
bfbd9128 | 479 | return res.json(getFormattedObjects(resultList.data, resultList.total, options)) |
418d092a | 480 | } |
65af03a2 | 481 | |
453e83ea | 482 | async function regeneratePlaylistThumbnail (videoPlaylist: MVideoPlaylistThumbnail) { |
65af03a2 C |
483 | await videoPlaylist.Thumbnail.destroy() |
484 | videoPlaylist.Thumbnail = null | |
485 | ||
486 | const firstElement = await VideoPlaylistElementModel.loadFirstElementWithVideoThumbnail(videoPlaylist.id) | |
487 | if (firstElement) await generateThumbnailForPlaylist(videoPlaylist, firstElement.Video) | |
488 | } | |
489 | ||
453e83ea | 490 | async function generateThumbnailForPlaylist (videoPlaylist: MVideoPlaylistThumbnail, video: MVideoThumbnail) { |
65af03a2 C |
491 | logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url) |
492 | ||
eb11373f C |
493 | const videoMiniature = video.getMiniature() |
494 | if (!videoMiniature) { | |
495 | logger.info('Cannot generate thumbnail for playlist %s because video %s does not have any.', videoPlaylist.url, video.url) | |
496 | return | |
497 | } | |
498 | ||
499 | const inputPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoMiniature.filename) | |
91f8f8db | 500 | const thumbnailModel = await updatePlaylistMiniatureFromExisting({ |
a35a2279 C |
501 | inputPath, |
502 | playlist: videoPlaylist, | |
503 | automaticallyGenerated: true, | |
504 | keepOriginal: true | |
505 | }) | |
65af03a2 C |
506 | |
507 | thumbnailModel.videoPlaylistId = videoPlaylist.id | |
508 | ||
509 | videoPlaylist.Thumbnail = await thumbnailModel.save() | |
510 | } |