2021-08-27 07:32:44 -05:00
|
|
|
import express from 'express'
|
2020-11-20 04:21:08 -06:00
|
|
|
import { join } from 'path'
|
2021-06-17 09:02:38 -05:00
|
|
|
import { scheduleRefreshIfNeeded } from '@server/lib/activitypub/playlists'
|
2021-06-28 10:30:59 -05:00
|
|
|
import { Hooks } from '@server/lib/plugins/hooks'
|
2020-11-20 04:21:08 -06:00
|
|
|
import { getServerActor } from '@server/models/application/application'
|
|
|
|
import { MVideoPlaylistFull, MVideoPlaylistThumbnail, MVideoThumbnail } from '@server/types/models'
|
2021-12-29 07:44:58 -06:00
|
|
|
import { uuidToShort } from '@shared/extra-utils'
|
2021-07-08 08:54:39 -05:00
|
|
|
import { VideoPlaylistCreateResult, VideoPlaylistElementCreateResult } from '@shared/models'
|
2021-07-16 03:42:24 -05:00
|
|
|
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
|
2020-11-20 04:21:08 -06:00
|
|
|
import { VideoPlaylistCreate } from '../../../shared/models/videos/playlist/video-playlist-create.model'
|
|
|
|
import { VideoPlaylistElementCreate } from '../../../shared/models/videos/playlist/video-playlist-element-create.model'
|
|
|
|
import { VideoPlaylistElementUpdate } from '../../../shared/models/videos/playlist/video-playlist-element-update.model'
|
|
|
|
import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
|
|
|
|
import { VideoPlaylistReorder } from '../../../shared/models/videos/playlist/video-playlist-reorder.model'
|
|
|
|
import { VideoPlaylistUpdate } from '../../../shared/models/videos/playlist/video-playlist-update.model'
|
|
|
|
import { resetSequelizeInstance } from '../../helpers/database-utils'
|
|
|
|
import { buildNSFWFilter, createReqFiles } from '../../helpers/express-utils'
|
|
|
|
import { logger } from '../../helpers/logger'
|
2020-04-23 04:36:50 -05:00
|
|
|
import { getFormattedObjects } from '../../helpers/utils'
|
2020-11-20 04:21:08 -06:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
|
|
|
import { MIMETYPES, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers/constants'
|
|
|
|
import { sequelizeTypescript } from '../../initializers/database'
|
|
|
|
import { sendCreateVideoPlaylist, sendDeleteVideoPlaylist, sendUpdateVideoPlaylist } from '../../lib/activitypub/send'
|
|
|
|
import { getLocalVideoPlaylistActivityPubUrl, getLocalVideoPlaylistElementActivityPubUrl } from '../../lib/activitypub/url'
|
2021-06-04 02:19:01 -05:00
|
|
|
import { updatePlaylistMiniatureFromExisting } from '../../lib/thumbnail'
|
2019-02-26 03:55:40 -06:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
|
|
|
authenticate,
|
2019-03-05 04:30:43 -06:00
|
|
|
optionalAuthenticate,
|
2019-02-26 03:55:40 -06:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort
|
|
|
|
} from '../../middlewares'
|
|
|
|
import { videoPlaylistsSortValidator } from '../../middlewares/validators'
|
|
|
|
import {
|
2019-03-05 03:58:44 -06:00
|
|
|
commonVideoPlaylistFiltersValidator,
|
2019-02-26 03:55:40 -06:00
|
|
|
videoPlaylistsAddValidator,
|
|
|
|
videoPlaylistsAddVideoValidator,
|
|
|
|
videoPlaylistsDeleteValidator,
|
|
|
|
videoPlaylistsGetValidator,
|
|
|
|
videoPlaylistsReorderVideosValidator,
|
|
|
|
videoPlaylistsUpdateOrRemoveVideoValidator,
|
|
|
|
videoPlaylistsUpdateValidator
|
|
|
|
} from '../../middlewares/validators/videos/video-playlists'
|
2019-03-05 03:58:44 -06:00
|
|
|
import { AccountModel } from '../../models/account/account'
|
2020-11-20 04:21:08 -06:00
|
|
|
import { VideoPlaylistModel } from '../../models/video/video-playlist'
|
|
|
|
import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2022-03-04 03:57:36 -06:00
|
|
|
const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
const videoPlaylistRouter = express.Router()
|
|
|
|
|
2019-03-06 04:32:53 -06:00
|
|
|
videoPlaylistRouter.get('/privacies', listVideoPlaylistPrivacies)
|
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
videoPlaylistRouter.get('/',
|
|
|
|
paginationValidator,
|
|
|
|
videoPlaylistsSortValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
2019-03-05 03:58:44 -06:00
|
|
|
commonVideoPlaylistFiltersValidator,
|
2019-02-26 03:55:40 -06:00
|
|
|
asyncMiddleware(listVideoPlaylists)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoPlaylistRouter.get('/:playlistId',
|
2019-08-15 04:53:26 -05:00
|
|
|
asyncMiddleware(videoPlaylistsGetValidator('summary')),
|
2019-02-26 03:55:40 -06:00
|
|
|
getVideoPlaylist
|
|
|
|
)
|
|
|
|
|
|
|
|
videoPlaylistRouter.post('/',
|
|
|
|
authenticate,
|
|
|
|
reqThumbnailFile,
|
|
|
|
asyncMiddleware(videoPlaylistsAddValidator),
|
|
|
|
asyncRetryTransactionMiddleware(addVideoPlaylist)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoPlaylistRouter.put('/:playlistId',
|
|
|
|
authenticate,
|
|
|
|
reqThumbnailFile,
|
|
|
|
asyncMiddleware(videoPlaylistsUpdateValidator),
|
|
|
|
asyncRetryTransactionMiddleware(updateVideoPlaylist)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoPlaylistRouter.delete('/:playlistId',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoPlaylistsDeleteValidator),
|
|
|
|
asyncRetryTransactionMiddleware(removeVideoPlaylist)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoPlaylistRouter.get('/:playlistId/videos',
|
2019-08-15 04:53:26 -05:00
|
|
|
asyncMiddleware(videoPlaylistsGetValidator('summary')),
|
2019-02-26 03:55:40 -06:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
2019-02-28 04:14:26 -06:00
|
|
|
optionalAuthenticate,
|
2019-02-26 03:55:40 -06:00
|
|
|
asyncMiddleware(getVideoPlaylistVideos)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoPlaylistRouter.post('/:playlistId/videos',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoPlaylistsAddVideoValidator),
|
|
|
|
asyncRetryTransactionMiddleware(addVideoInPlaylist)
|
|
|
|
)
|
|
|
|
|
2019-02-28 04:14:26 -06:00
|
|
|
videoPlaylistRouter.post('/:playlistId/videos/reorder',
|
2019-02-26 03:55:40 -06:00
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoPlaylistsReorderVideosValidator),
|
|
|
|
asyncRetryTransactionMiddleware(reorderVideosPlaylist)
|
|
|
|
)
|
|
|
|
|
2019-07-31 08:57:32 -05:00
|
|
|
videoPlaylistRouter.put('/:playlistId/videos/:playlistElementId',
|
2019-02-26 03:55:40 -06:00
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoPlaylistsUpdateOrRemoveVideoValidator),
|
|
|
|
asyncRetryTransactionMiddleware(updateVideoPlaylistElement)
|
|
|
|
)
|
|
|
|
|
2019-07-31 08:57:32 -05:00
|
|
|
videoPlaylistRouter.delete('/:playlistId/videos/:playlistElementId',
|
2019-02-26 03:55:40 -06:00
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoPlaylistsUpdateOrRemoveVideoValidator),
|
|
|
|
asyncRetryTransactionMiddleware(removeVideoFromPlaylist)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoPlaylistRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-03-06 04:32:53 -06:00
|
|
|
function listVideoPlaylistPrivacies (req: express.Request, res: express.Response) {
|
|
|
|
res.json(VIDEO_PLAYLIST_PRIVACIES)
|
|
|
|
}
|
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
async function listVideoPlaylists (req: express.Request, res: express.Response) {
|
|
|
|
const serverActor = await getServerActor()
|
|
|
|
const resultList = await VideoPlaylistModel.listForApi({
|
|
|
|
followerActorId: serverActor.id,
|
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
2019-03-05 03:58:44 -06:00
|
|
|
sort: req.query.sort,
|
|
|
|
type: req.query.type
|
2019-02-26 03:55:40 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVideoPlaylist (req: express.Request, res: express.Response) {
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoPlaylist = res.locals.videoPlaylistSummary
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2021-06-17 09:02:38 -05:00
|
|
|
scheduleRefreshIfNeeded(videoPlaylist)
|
2019-03-19 08:13:53 -05:00
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
return res.json(videoPlaylist.toFormattedJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addVideoPlaylist (req: express.Request, res: express.Response) {
|
|
|
|
const videoPlaylistInfo: VideoPlaylistCreate = req.body
|
2019-03-19 04:35:15 -05:00
|
|
|
const user = res.locals.oauth.token.User
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
const videoPlaylist = new VideoPlaylistModel({
|
|
|
|
name: videoPlaylistInfo.displayName,
|
|
|
|
description: videoPlaylistInfo.description,
|
|
|
|
privacy: videoPlaylistInfo.privacy || VideoPlaylistPrivacy.PRIVATE,
|
|
|
|
ownerAccountId: user.Account.id
|
2019-08-15 04:53:26 -05:00
|
|
|
}) as MVideoPlaylistFull
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2020-11-20 04:21:08 -06:00
|
|
|
videoPlaylist.url = getLocalVideoPlaylistActivityPubUrl(videoPlaylist) // We use the UUID, so set the URL after building the object
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2019-03-06 04:32:53 -06:00
|
|
|
if (videoPlaylistInfo.videoChannelId) {
|
2019-03-19 04:35:15 -05:00
|
|
|
const videoChannel = res.locals.videoChannel
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
videoPlaylist.videoChannelId = videoChannel.id
|
|
|
|
videoPlaylist.VideoChannel = videoChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
const thumbnailField = req.files['thumbnailfile']
|
2019-04-17 03:07:00 -05:00
|
|
|
const thumbnailModel = thumbnailField
|
2021-06-04 02:19:01 -05:00
|
|
|
? await updatePlaylistMiniatureFromExisting({
|
2021-02-16 01:50:40 -06:00
|
|
|
inputPath: thumbnailField[0].path,
|
|
|
|
playlist: videoPlaylist,
|
|
|
|
automaticallyGenerated: false
|
|
|
|
})
|
2019-04-17 03:07:00 -05:00
|
|
|
: undefined
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoPlaylistCreated = await sequelizeTypescript.transaction(async t => {
|
|
|
|
const videoPlaylistCreated = await videoPlaylist.save({ transaction: t }) as MVideoPlaylistFull
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2019-08-01 09:54:24 -05:00
|
|
|
if (thumbnailModel) {
|
|
|
|
thumbnailModel.automaticallyGenerated = false
|
|
|
|
await videoPlaylistCreated.setAndSaveThumbnail(thumbnailModel, t)
|
|
|
|
}
|
2019-04-17 03:07:00 -05:00
|
|
|
|
2019-03-05 03:58:44 -06:00
|
|
|
// We need more attributes for the federation
|
|
|
|
videoPlaylistCreated.OwnerAccount = await AccountModel.load(user.Account.id, t)
|
2019-02-26 03:55:40 -06:00
|
|
|
await sendCreateVideoPlaylist(videoPlaylistCreated, t)
|
|
|
|
|
|
|
|
return videoPlaylistCreated
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Video playlist with uuid %s created.', videoPlaylist.uuid)
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
videoPlaylist: {
|
|
|
|
id: videoPlaylistCreated.id,
|
2021-06-28 10:30:59 -05:00
|
|
|
shortUUID: uuidToShort(videoPlaylistCreated.uuid),
|
2019-02-26 03:55:40 -06:00
|
|
|
uuid: videoPlaylistCreated.uuid
|
2021-07-08 08:54:39 -05:00
|
|
|
} as VideoPlaylistCreateResult
|
2021-05-12 07:51:17 -05:00
|
|
|
})
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateVideoPlaylist (req: express.Request, res: express.Response) {
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoPlaylistInstance = res.locals.videoPlaylistFull
|
2019-02-26 03:55:40 -06:00
|
|
|
const videoPlaylistFieldsSave = videoPlaylistInstance.toJSON()
|
|
|
|
const videoPlaylistInfoToUpdate = req.body as VideoPlaylistUpdate
|
2019-06-06 10:29:15 -05:00
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
const wasPrivatePlaylist = videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE
|
2019-06-06 10:29:15 -05:00
|
|
|
const wasNotPrivatePlaylist = videoPlaylistInstance.privacy !== VideoPlaylistPrivacy.PRIVATE
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
const thumbnailField = req.files['thumbnailfile']
|
2019-04-17 03:07:00 -05:00
|
|
|
const thumbnailModel = thumbnailField
|
2021-06-04 02:19:01 -05:00
|
|
|
? await updatePlaylistMiniatureFromExisting({
|
2021-02-16 01:50:40 -06:00
|
|
|
inputPath: thumbnailField[0].path,
|
|
|
|
playlist: videoPlaylistInstance,
|
|
|
|
automaticallyGenerated: false
|
|
|
|
})
|
2019-04-17 03:07:00 -05:00
|
|
|
: undefined
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
try {
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
const sequelizeOptions = {
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoPlaylistInfoToUpdate.videoChannelId !== undefined) {
|
|
|
|
if (videoPlaylistInfoToUpdate.videoChannelId === null) {
|
|
|
|
videoPlaylistInstance.videoChannelId = null
|
|
|
|
} else {
|
2019-03-19 04:35:15 -05:00
|
|
|
const videoChannel = res.locals.videoChannel
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
videoPlaylistInstance.videoChannelId = videoChannel.id
|
2019-03-05 03:58:44 -06:00
|
|
|
videoPlaylistInstance.VideoChannel = videoChannel
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoPlaylistInfoToUpdate.displayName !== undefined) videoPlaylistInstance.name = videoPlaylistInfoToUpdate.displayName
|
|
|
|
if (videoPlaylistInfoToUpdate.description !== undefined) videoPlaylistInstance.description = videoPlaylistInfoToUpdate.description
|
|
|
|
|
|
|
|
if (videoPlaylistInfoToUpdate.privacy !== undefined) {
|
|
|
|
videoPlaylistInstance.privacy = parseInt(videoPlaylistInfoToUpdate.privacy.toString(), 10)
|
2019-06-06 10:29:15 -05:00
|
|
|
|
|
|
|
if (wasNotPrivatePlaylist === true && videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE) {
|
|
|
|
await sendDeleteVideoPlaylist(videoPlaylistInstance, t)
|
|
|
|
}
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const playlistUpdated = await videoPlaylistInstance.save(sequelizeOptions)
|
|
|
|
|
2019-08-01 09:54:24 -05:00
|
|
|
if (thumbnailModel) {
|
|
|
|
thumbnailModel.automaticallyGenerated = false
|
|
|
|
await playlistUpdated.setAndSaveThumbnail(thumbnailModel, t)
|
|
|
|
}
|
2019-04-17 03:07:00 -05:00
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
const isNewPlaylist = wasPrivatePlaylist && playlistUpdated.privacy !== VideoPlaylistPrivacy.PRIVATE
|
|
|
|
|
|
|
|
if (isNewPlaylist) {
|
|
|
|
await sendCreateVideoPlaylist(playlistUpdated, t)
|
|
|
|
} else {
|
|
|
|
await sendUpdateVideoPlaylist(playlistUpdated, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Video playlist %s updated.', videoPlaylistInstance.uuid)
|
|
|
|
|
|
|
|
return playlistUpdated
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
logger.debug('Cannot update the video playlist.', { err })
|
|
|
|
|
|
|
|
// Force fields we want to update
|
|
|
|
// If the transaction is retried, sequelize will think the object has not changed
|
|
|
|
// So it will skip the SQL request, even if the last one was ROLLBACKed!
|
|
|
|
resetSequelizeInstance(videoPlaylistInstance, videoPlaylistFieldsSave)
|
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
2020-12-07 07:32:36 -06:00
|
|
|
return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeVideoPlaylist (req: express.Request, res: express.Response) {
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoPlaylistInstance = res.locals.videoPlaylistSummary
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
await videoPlaylistInstance.destroy({ transaction: t })
|
|
|
|
|
|
|
|
await sendDeleteVideoPlaylist(videoPlaylistInstance, t)
|
|
|
|
|
|
|
|
logger.info('Video playlist %s deleted.', videoPlaylistInstance.uuid)
|
|
|
|
})
|
|
|
|
|
2020-12-07 07:32:36 -06:00
|
|
|
return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function addVideoInPlaylist (req: express.Request, res: express.Response) {
|
|
|
|
const body: VideoPlaylistElementCreate = req.body
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoPlaylist = res.locals.videoPlaylistFull
|
|
|
|
const video = res.locals.onlyVideo
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
const playlistElement = await sequelizeTypescript.transaction(async t => {
|
2019-02-26 03:55:40 -06:00
|
|
|
const position = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id, t)
|
|
|
|
|
|
|
|
const playlistElement = await VideoPlaylistElementModel.create({
|
|
|
|
position,
|
|
|
|
startTimestamp: body.startTimestamp || null,
|
|
|
|
stopTimestamp: body.stopTimestamp || null,
|
|
|
|
videoPlaylistId: videoPlaylist.id,
|
|
|
|
videoId: video.id
|
|
|
|
}, { transaction: t })
|
|
|
|
|
2020-11-20 04:21:08 -06:00
|
|
|
playlistElement.url = getLocalVideoPlaylistElementActivityPubUrl(videoPlaylist, playlistElement)
|
2020-08-17 09:39:32 -05:00
|
|
|
await playlistElement.save({ transaction: t })
|
|
|
|
|
2019-03-14 03:19:03 -05:00
|
|
|
videoPlaylist.changed('updatedAt', true)
|
2019-03-07 10:06:00 -06:00
|
|
|
await videoPlaylist.save({ transaction: t })
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
return playlistElement
|
|
|
|
})
|
|
|
|
|
2019-03-07 10:06:00 -06:00
|
|
|
// If the user did not set a thumbnail, automatically take the video thumbnail
|
2019-08-01 09:54:24 -05:00
|
|
|
if (videoPlaylist.hasThumbnail() === false || (videoPlaylist.hasGeneratedThumbnail() && playlistElement.position === 1)) {
|
|
|
|
await generateThumbnailForPlaylist(videoPlaylist, video)
|
2019-03-07 10:06:00 -06:00
|
|
|
}
|
|
|
|
|
2019-08-01 09:54:24 -05:00
|
|
|
sendUpdateVideoPlaylist(videoPlaylist, undefined)
|
|
|
|
.catch(err => logger.error('Cannot send video playlist update.', { err }))
|
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
logger.info('Video added in playlist %s at position %d.', videoPlaylist.uuid, playlistElement.position)
|
|
|
|
|
2021-11-24 07:33:14 -06:00
|
|
|
Hooks.runAction('action:api.video-playlist-element.created', { playlistElement, req, res })
|
2021-06-28 02:22:15 -05:00
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
return res.json({
|
|
|
|
videoPlaylistElement: {
|
|
|
|
id: playlistElement.id
|
2021-07-08 08:54:39 -05:00
|
|
|
} as VideoPlaylistElementCreateResult
|
|
|
|
})
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateVideoPlaylistElement (req: express.Request, res: express.Response) {
|
|
|
|
const body: VideoPlaylistElementUpdate = req.body
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoPlaylist = res.locals.videoPlaylistFull
|
2019-03-19 04:35:15 -05:00
|
|
|
const videoPlaylistElement = res.locals.videoPlaylistElement
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
const playlistElement: VideoPlaylistElementModel = await sequelizeTypescript.transaction(async t => {
|
|
|
|
if (body.startTimestamp !== undefined) videoPlaylistElement.startTimestamp = body.startTimestamp
|
|
|
|
if (body.stopTimestamp !== undefined) videoPlaylistElement.stopTimestamp = body.stopTimestamp
|
|
|
|
|
|
|
|
const element = await videoPlaylistElement.save({ transaction: t })
|
|
|
|
|
2019-03-14 03:19:03 -05:00
|
|
|
videoPlaylist.changed('updatedAt', true)
|
2019-03-07 10:06:00 -06:00
|
|
|
await videoPlaylist.save({ transaction: t })
|
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
await sendUpdateVideoPlaylist(videoPlaylist, t)
|
|
|
|
|
|
|
|
return element
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Element of position %d of playlist %s updated.', playlistElement.position, videoPlaylist.uuid)
|
|
|
|
|
2020-12-07 07:32:36 -06:00
|
|
|
return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeVideoFromPlaylist (req: express.Request, res: express.Response) {
|
2019-03-19 04:35:15 -05:00
|
|
|
const videoPlaylistElement = res.locals.videoPlaylistElement
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoPlaylist = res.locals.videoPlaylistFull
|
2019-02-26 03:55:40 -06:00
|
|
|
const positionToDelete = videoPlaylistElement.position
|
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
await videoPlaylistElement.destroy({ transaction: t })
|
|
|
|
|
|
|
|
// Decrease position of the next elements
|
2021-11-16 02:29:54 -06:00
|
|
|
await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, positionToDelete, -1, t)
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2019-03-14 03:19:03 -05:00
|
|
|
videoPlaylist.changed('updatedAt', true)
|
2019-03-07 10:06:00 -06:00
|
|
|
await videoPlaylist.save({ transaction: t })
|
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
logger.info('Video playlist element %d of playlist %s deleted.', videoPlaylistElement.position, videoPlaylist.uuid)
|
|
|
|
})
|
|
|
|
|
2019-08-01 09:54:24 -05:00
|
|
|
// Do we need to regenerate the default thumbnail?
|
|
|
|
if (positionToDelete === 1 && videoPlaylist.hasGeneratedThumbnail()) {
|
|
|
|
await regeneratePlaylistThumbnail(videoPlaylist)
|
|
|
|
}
|
|
|
|
|
|
|
|
sendUpdateVideoPlaylist(videoPlaylist, undefined)
|
|
|
|
.catch(err => logger.error('Cannot send video playlist update.', { err }))
|
|
|
|
|
2020-12-07 07:32:36 -06:00
|
|
|
return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function reorderVideosPlaylist (req: express.Request, res: express.Response) {
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoPlaylist = res.locals.videoPlaylistFull
|
2019-03-12 05:40:42 -05:00
|
|
|
const body: VideoPlaylistReorder = req.body
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2019-03-12 05:40:42 -05:00
|
|
|
const start: number = body.startPosition
|
|
|
|
const insertAfter: number = body.insertAfterPosition
|
|
|
|
const reorderLength: number = body.reorderLength || 1
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
if (start === insertAfter) {
|
2020-12-07 07:32:36 -06:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Example: if we reorder position 2 and insert after position 5 (so at position 6): # 1 2 3 4 5 6 7 8 9
|
|
|
|
// * increase position when position > 5 # 1 2 3 4 5 7 8 9 10
|
|
|
|
// * update position 2 -> position 6 # 1 3 4 5 6 7 8 9 10
|
|
|
|
// * decrease position when position position > 2 # 1 2 3 4 5 6 7 8 9
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
const newPosition = insertAfter + 1
|
|
|
|
|
|
|
|
// Add space after the position when we want to insert our reordered elements (increase)
|
2021-11-16 02:29:54 -06:00
|
|
|
await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, newPosition, reorderLength, t)
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
let oldPosition = start
|
|
|
|
|
|
|
|
// We incremented the position of the elements we want to reorder
|
|
|
|
if (start >= newPosition) oldPosition += reorderLength
|
|
|
|
|
|
|
|
const endOldPosition = oldPosition + reorderLength - 1
|
|
|
|
// Insert our reordered elements in their place (update)
|
|
|
|
await VideoPlaylistElementModel.reassignPositionOf(videoPlaylist.id, oldPosition, endOldPosition, newPosition, t)
|
|
|
|
|
|
|
|
// Decrease positions of elements after the old position of our ordered elements (decrease)
|
2021-11-16 02:29:54 -06:00
|
|
|
await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, -reorderLength, t)
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2019-03-14 03:19:03 -05:00
|
|
|
videoPlaylist.changed('updatedAt', true)
|
2019-03-07 10:06:00 -06:00
|
|
|
await videoPlaylist.save({ transaction: t })
|
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
await sendUpdateVideoPlaylist(videoPlaylist, t)
|
|
|
|
})
|
|
|
|
|
2019-08-01 09:54:24 -05:00
|
|
|
// The first element changed
|
|
|
|
if ((start === 1 || insertAfter === 0) && videoPlaylist.hasGeneratedThumbnail()) {
|
|
|
|
await regeneratePlaylistThumbnail(videoPlaylist)
|
|
|
|
}
|
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
logger.info(
|
2019-08-01 09:54:24 -05:00
|
|
|
'Reordered playlist %s (inserted after position %d elements %d - %d).',
|
2019-02-26 03:55:40 -06:00
|
|
|
videoPlaylist.uuid, insertAfter, start, start + reorderLength - 1
|
|
|
|
)
|
|
|
|
|
2020-12-07 07:32:36 -06:00
|
|
|
return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getVideoPlaylistVideos (req: express.Request, res: express.Response) {
|
2019-08-15 04:53:26 -05:00
|
|
|
const videoPlaylistInstance = res.locals.videoPlaylistSummary
|
2019-07-31 08:57:32 -05:00
|
|
|
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
|
|
|
|
const server = await getServerActor()
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2022-02-15 01:24:22 -06:00
|
|
|
const apiOptions = await Hooks.wrapObject({
|
2019-02-26 03:55:40 -06:00
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
|
|
|
videoPlaylistId: videoPlaylistInstance.id,
|
2019-07-31 08:57:32 -05:00
|
|
|
serverAccount: server.Account,
|
|
|
|
user
|
2022-02-15 01:24:22 -06:00
|
|
|
}, 'filter:api.video-playlist.videos.list.params')
|
|
|
|
|
|
|
|
const resultList = await Hooks.wrapPromiseFun(
|
|
|
|
VideoPlaylistElementModel.listForApi,
|
|
|
|
apiOptions,
|
|
|
|
'filter:api.video-playlist.videos.list.result'
|
|
|
|
)
|
2019-02-26 03:55:40 -06:00
|
|
|
|
2019-07-31 08:57:32 -05:00
|
|
|
const options = {
|
|
|
|
displayNSFW: buildNSFWFilter(res, req.query.nsfw),
|
|
|
|
accountId: user ? user.Account.id : undefined
|
|
|
|
}
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total, options))
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
2019-08-01 09:54:24 -05:00
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
async function regeneratePlaylistThumbnail (videoPlaylist: MVideoPlaylistThumbnail) {
|
2019-08-01 09:54:24 -05:00
|
|
|
await videoPlaylist.Thumbnail.destroy()
|
|
|
|
videoPlaylist.Thumbnail = null
|
|
|
|
|
|
|
|
const firstElement = await VideoPlaylistElementModel.loadFirstElementWithVideoThumbnail(videoPlaylist.id)
|
|
|
|
if (firstElement) await generateThumbnailForPlaylist(videoPlaylist, firstElement.Video)
|
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
async function generateThumbnailForPlaylist (videoPlaylist: MVideoPlaylistThumbnail, video: MVideoThumbnail) {
|
2019-08-01 09:54:24 -05:00
|
|
|
logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url)
|
|
|
|
|
2020-03-19 03:46:50 -05:00
|
|
|
const videoMiniature = video.getMiniature()
|
|
|
|
if (!videoMiniature) {
|
|
|
|
logger.info('Cannot generate thumbnail for playlist %s because video %s does not have any.', videoPlaylist.url, video.url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const inputPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoMiniature.filename)
|
2021-06-04 02:19:01 -05:00
|
|
|
const thumbnailModel = await updatePlaylistMiniatureFromExisting({
|
2021-02-16 01:50:40 -06:00
|
|
|
inputPath,
|
|
|
|
playlist: videoPlaylist,
|
|
|
|
automaticallyGenerated: true,
|
|
|
|
keepOriginal: true
|
|
|
|
})
|
2019-08-01 09:54:24 -05:00
|
|
|
|
|
|
|
thumbnailModel.videoPlaylistId = videoPlaylist.id
|
|
|
|
|
|
|
|
videoPlaylist.Thumbnail = await thumbnailModel.save()
|
|
|
|
}
|