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