2019-02-26 10:55:40 +01:00
|
|
|
import * as express from 'express'
|
|
|
|
import { getFormattedObjects, getServerActor } from '../../helpers/utils'
|
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
|
|
|
authenticate,
|
2019-03-05 11:30:43 +01:00
|
|
|
optionalAuthenticate,
|
2019-02-26 10:55:40 +01:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort
|
|
|
|
} from '../../middlewares'
|
|
|
|
import { videoPlaylistsSortValidator } from '../../middlewares/validators'
|
2019-07-31 15:57:32 +02:00
|
|
|
import { buildNSFWFilter, createReqFiles } from '../../helpers/express-utils'
|
2019-04-17 10:07:00 +02:00
|
|
|
import { MIMETYPES, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers/constants'
|
2019-02-26 10:55:40 +01:00
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { resetSequelizeInstance } from '../../helpers/database-utils'
|
|
|
|
import { VideoPlaylistModel } from '../../models/video/video-playlist'
|
|
|
|
import {
|
2019-03-05 10:58:44 +01:00
|
|
|
commonVideoPlaylistFiltersValidator,
|
2019-02-26 10:55:40 +01:00
|
|
|
videoPlaylistsAddValidator,
|
|
|
|
videoPlaylistsAddVideoValidator,
|
|
|
|
videoPlaylistsDeleteValidator,
|
|
|
|
videoPlaylistsGetValidator,
|
|
|
|
videoPlaylistsReorderVideosValidator,
|
|
|
|
videoPlaylistsUpdateOrRemoveVideoValidator,
|
|
|
|
videoPlaylistsUpdateValidator
|
|
|
|
} from '../../middlewares/validators/videos/video-playlists'
|
|
|
|
import { VideoPlaylistCreate } from '../../../shared/models/videos/playlist/video-playlist-create.model'
|
|
|
|
import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
|
|
|
|
import { join } from 'path'
|
2019-03-05 11:30:43 +01:00
|
|
|
import { sendCreateVideoPlaylist, sendDeleteVideoPlaylist, sendUpdateVideoPlaylist } from '../../lib/activitypub/send'
|
|
|
|
import { getVideoPlaylistActivityPubUrl, getVideoPlaylistElementActivityPubUrl } from '../../lib/activitypub/url'
|
2019-02-26 10:55:40 +01:00
|
|
|
import { VideoPlaylistUpdate } from '../../../shared/models/videos/playlist/video-playlist-update.model'
|
|
|
|
import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'
|
|
|
|
import { VideoPlaylistElementCreate } from '../../../shared/models/videos/playlist/video-playlist-element-create.model'
|
|
|
|
import { VideoPlaylistElementUpdate } from '../../../shared/models/videos/playlist/video-playlist-element-update.model'
|
2019-03-05 10:58:44 +01:00
|
|
|
import { AccountModel } from '../../models/account/account'
|
2019-03-12 11:40:42 +01:00
|
|
|
import { VideoPlaylistReorder } from '../../../shared/models/videos/playlist/video-playlist-reorder.model'
|
2019-03-19 14:13:53 +01:00
|
|
|
import { JobQueue } from '../../lib/job-queue'
|
2019-04-11 11:33:44 +02:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { sequelizeTypescript } from '../../initializers/database'
|
2019-04-23 09:50:57 +02:00
|
|
|
import { createPlaylistMiniatureFromExisting } from '../../lib/thumbnail'
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR })
|
|
|
|
|
|
|
|
const videoPlaylistRouter = express.Router()
|
|
|
|
|
2019-03-06 11:32:53 +01:00
|
|
|
videoPlaylistRouter.get('/privacies', listVideoPlaylistPrivacies)
|
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
videoPlaylistRouter.get('/',
|
|
|
|
paginationValidator,
|
|
|
|
videoPlaylistsSortValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
2019-03-05 10:58:44 +01:00
|
|
|
commonVideoPlaylistFiltersValidator,
|
2019-02-26 10:55:40 +01:00
|
|
|
asyncMiddleware(listVideoPlaylists)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoPlaylistRouter.get('/:playlistId',
|
|
|
|
asyncMiddleware(videoPlaylistsGetValidator),
|
|
|
|
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',
|
|
|
|
asyncMiddleware(videoPlaylistsGetValidator),
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
2019-02-28 11:14:26 +01:00
|
|
|
optionalAuthenticate,
|
2019-02-26 10:55:40 +01:00
|
|
|
asyncMiddleware(getVideoPlaylistVideos)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoPlaylistRouter.post('/:playlistId/videos',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoPlaylistsAddVideoValidator),
|
|
|
|
asyncRetryTransactionMiddleware(addVideoInPlaylist)
|
|
|
|
)
|
|
|
|
|
2019-02-28 11:14:26 +01:00
|
|
|
videoPlaylistRouter.post('/:playlistId/videos/reorder',
|
2019-02-26 10:55:40 +01:00
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoPlaylistsReorderVideosValidator),
|
|
|
|
asyncRetryTransactionMiddleware(reorderVideosPlaylist)
|
|
|
|
)
|
|
|
|
|
2019-07-31 15:57:32 +02:00
|
|
|
videoPlaylistRouter.put('/:playlistId/videos/:playlistElementId',
|
2019-02-26 10:55:40 +01:00
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoPlaylistsUpdateOrRemoveVideoValidator),
|
|
|
|
asyncRetryTransactionMiddleware(updateVideoPlaylistElement)
|
|
|
|
)
|
|
|
|
|
2019-07-31 15:57:32 +02:00
|
|
|
videoPlaylistRouter.delete('/:playlistId/videos/:playlistElementId',
|
2019-02-26 10:55:40 +01:00
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoPlaylistsUpdateOrRemoveVideoValidator),
|
|
|
|
asyncRetryTransactionMiddleware(removeVideoFromPlaylist)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoPlaylistRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-03-06 11:32:53 +01:00
|
|
|
function listVideoPlaylistPrivacies (req: express.Request, res: express.Response) {
|
|
|
|
res.json(VIDEO_PLAYLIST_PRIVACIES)
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:55:40 +01: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 10:58:44 +01:00
|
|
|
sort: req.query.sort,
|
|
|
|
type: req.query.type
|
2019-02-26 10:55:40 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVideoPlaylist (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const videoPlaylist = res.locals.videoPlaylist
|
2019-02-26 10:55:40 +01:00
|
|
|
|
2019-03-19 14:13:53 +01:00
|
|
|
if (videoPlaylist.isOutdated()) {
|
|
|
|
JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video-playlist', url: videoPlaylist.url } })
|
|
|
|
.catch(err => logger.error('Cannot create AP refresher job for playlist %s.', videoPlaylist.url, { err }))
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
return res.json(videoPlaylist.toFormattedJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addVideoPlaylist (req: express.Request, res: express.Response) {
|
|
|
|
const videoPlaylistInfo: VideoPlaylistCreate = req.body
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
const videoPlaylist = new VideoPlaylistModel({
|
|
|
|
name: videoPlaylistInfo.displayName,
|
|
|
|
description: videoPlaylistInfo.description,
|
|
|
|
privacy: videoPlaylistInfo.privacy || VideoPlaylistPrivacy.PRIVATE,
|
|
|
|
ownerAccountId: user.Account.id
|
|
|
|
})
|
|
|
|
|
|
|
|
videoPlaylist.url = getVideoPlaylistActivityPubUrl(videoPlaylist) // We use the UUID, so set the URL after building the object
|
|
|
|
|
2019-03-06 11:32:53 +01:00
|
|
|
if (videoPlaylistInfo.videoChannelId) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const videoChannel = res.locals.videoChannel
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
videoPlaylist.videoChannelId = videoChannel.id
|
|
|
|
videoPlaylist.VideoChannel = videoChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
const thumbnailField = req.files['thumbnailfile']
|
2019-04-17 10:07:00 +02:00
|
|
|
const thumbnailModel = thumbnailField
|
2019-04-23 09:50:57 +02:00
|
|
|
? await createPlaylistMiniatureFromExisting(thumbnailField[0].path, videoPlaylist)
|
2019-04-17 10:07:00 +02:00
|
|
|
: undefined
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
const videoPlaylistCreated: VideoPlaylistModel = await sequelizeTypescript.transaction(async t => {
|
|
|
|
const videoPlaylistCreated = await videoPlaylist.save({ transaction: t })
|
|
|
|
|
2019-04-23 09:50:57 +02:00
|
|
|
if (thumbnailModel) await videoPlaylistCreated.setAndSaveThumbnail(thumbnailModel, t)
|
2019-04-17 10:07:00 +02:00
|
|
|
|
2019-03-05 10:58:44 +01:00
|
|
|
// We need more attributes for the federation
|
|
|
|
videoPlaylistCreated.OwnerAccount = await AccountModel.load(user.Account.id, t)
|
2019-02-26 10:55:40 +01:00
|
|
|
await sendCreateVideoPlaylist(videoPlaylistCreated, t)
|
|
|
|
|
|
|
|
return videoPlaylistCreated
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Video playlist with uuid %s created.', videoPlaylist.uuid)
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
videoPlaylist: {
|
|
|
|
id: videoPlaylistCreated.id,
|
|
|
|
uuid: videoPlaylistCreated.uuid
|
|
|
|
}
|
|
|
|
}).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateVideoPlaylist (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const videoPlaylistInstance = res.locals.videoPlaylist
|
2019-02-26 10:55:40 +01:00
|
|
|
const videoPlaylistFieldsSave = videoPlaylistInstance.toJSON()
|
|
|
|
const videoPlaylistInfoToUpdate = req.body as VideoPlaylistUpdate
|
2019-06-06 17:29:15 +02:00
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
const wasPrivatePlaylist = videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE
|
2019-06-06 17:29:15 +02:00
|
|
|
const wasNotPrivatePlaylist = videoPlaylistInstance.privacy !== VideoPlaylistPrivacy.PRIVATE
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
const thumbnailField = req.files['thumbnailfile']
|
2019-04-17 10:07:00 +02:00
|
|
|
const thumbnailModel = thumbnailField
|
2019-04-23 09:50:57 +02:00
|
|
|
? await createPlaylistMiniatureFromExisting(thumbnailField[0].path, videoPlaylistInstance)
|
2019-04-17 10:07:00 +02:00
|
|
|
: undefined
|
2019-02-26 10:55:40 +01: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 10:35:15 +01:00
|
|
|
const videoChannel = res.locals.videoChannel
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
videoPlaylistInstance.videoChannelId = videoChannel.id
|
2019-03-05 10:58:44 +01:00
|
|
|
videoPlaylistInstance.VideoChannel = videoChannel
|
2019-02-26 10:55:40 +01: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 17:29:15 +02:00
|
|
|
|
|
|
|
if (wasNotPrivatePlaylist === true && videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE) {
|
|
|
|
await sendDeleteVideoPlaylist(videoPlaylistInstance, t)
|
|
|
|
}
|
2019-02-26 10:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const playlistUpdated = await videoPlaylistInstance.save(sequelizeOptions)
|
|
|
|
|
2019-04-23 09:50:57 +02:00
|
|
|
if (thumbnailModel) await playlistUpdated.setAndSaveThumbnail(thumbnailModel, t)
|
2019-04-17 10:07:00 +02:00
|
|
|
|
2019-02-26 10:55:40 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeVideoPlaylist (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const videoPlaylistInstance = res.locals.videoPlaylist
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
await videoPlaylistInstance.destroy({ transaction: t })
|
|
|
|
|
|
|
|
await sendDeleteVideoPlaylist(videoPlaylistInstance, t)
|
|
|
|
|
|
|
|
logger.info('Video playlist %s deleted.', videoPlaylistInstance.uuid)
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addVideoInPlaylist (req: express.Request, res: express.Response) {
|
|
|
|
const body: VideoPlaylistElementCreate = req.body
|
2019-03-19 10:35:15 +01:00
|
|
|
const videoPlaylist = res.locals.videoPlaylist
|
|
|
|
const video = res.locals.video
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
const playlistElement: VideoPlaylistElementModel = await sequelizeTypescript.transaction(async t => {
|
|
|
|
const position = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id, t)
|
|
|
|
|
|
|
|
const playlistElement = await VideoPlaylistElementModel.create({
|
|
|
|
url: getVideoPlaylistElementActivityPubUrl(videoPlaylist, video),
|
|
|
|
position,
|
|
|
|
startTimestamp: body.startTimestamp || null,
|
|
|
|
stopTimestamp: body.stopTimestamp || null,
|
|
|
|
videoPlaylistId: videoPlaylist.id,
|
|
|
|
videoId: video.id
|
|
|
|
}, { transaction: t })
|
|
|
|
|
2019-03-14 09:19:03 +01:00
|
|
|
videoPlaylist.changed('updatedAt', true)
|
2019-03-07 17:06:00 +01:00
|
|
|
await videoPlaylist.save({ transaction: t })
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
await sendUpdateVideoPlaylist(videoPlaylist, t)
|
|
|
|
|
|
|
|
return playlistElement
|
|
|
|
})
|
|
|
|
|
2019-03-07 17:06:00 +01:00
|
|
|
// If the user did not set a thumbnail, automatically take the video thumbnail
|
2019-04-24 11:10:42 +02:00
|
|
|
if (videoPlaylist.hasThumbnail() === false) {
|
2019-04-17 10:07:00 +02:00
|
|
|
logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url)
|
2019-03-07 17:06:00 +01:00
|
|
|
|
2019-04-23 09:50:57 +02:00
|
|
|
const inputPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getMiniature().filename)
|
|
|
|
const thumbnailModel = await createPlaylistMiniatureFromExisting(inputPath, videoPlaylist, true)
|
2019-03-07 17:06:00 +01:00
|
|
|
|
2019-04-17 10:07:00 +02:00
|
|
|
thumbnailModel.videoPlaylistId = videoPlaylist.id
|
|
|
|
|
|
|
|
await thumbnailModel.save()
|
2019-03-07 17:06:00 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
logger.info('Video added in playlist %s at position %d.', videoPlaylist.uuid, playlistElement.position)
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
videoPlaylistElement: {
|
|
|
|
id: playlistElement.id
|
|
|
|
}
|
|
|
|
}).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateVideoPlaylistElement (req: express.Request, res: express.Response) {
|
|
|
|
const body: VideoPlaylistElementUpdate = req.body
|
2019-03-19 10:35:15 +01:00
|
|
|
const videoPlaylist = res.locals.videoPlaylist
|
|
|
|
const videoPlaylistElement = res.locals.videoPlaylistElement
|
2019-02-26 10:55:40 +01: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 09:19:03 +01:00
|
|
|
videoPlaylist.changed('updatedAt', true)
|
2019-03-07 17:06:00 +01:00
|
|
|
await videoPlaylist.save({ transaction: t })
|
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
await sendUpdateVideoPlaylist(videoPlaylist, t)
|
|
|
|
|
|
|
|
return element
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Element of position %d of playlist %s updated.', playlistElement.position, videoPlaylist.uuid)
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeVideoFromPlaylist (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const videoPlaylistElement = res.locals.videoPlaylistElement
|
|
|
|
const videoPlaylist = res.locals.videoPlaylist
|
2019-02-26 10:55:40 +01:00
|
|
|
const positionToDelete = videoPlaylistElement.position
|
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
await videoPlaylistElement.destroy({ transaction: t })
|
|
|
|
|
|
|
|
// Decrease position of the next elements
|
|
|
|
await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, positionToDelete, null, -1, t)
|
|
|
|
|
2019-03-14 09:19:03 +01:00
|
|
|
videoPlaylist.changed('updatedAt', true)
|
2019-03-07 17:06:00 +01:00
|
|
|
await videoPlaylist.save({ transaction: t })
|
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
await sendUpdateVideoPlaylist(videoPlaylist, t)
|
|
|
|
|
|
|
|
logger.info('Video playlist element %d of playlist %s deleted.', videoPlaylistElement.position, videoPlaylist.uuid)
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function reorderVideosPlaylist (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const videoPlaylist = res.locals.videoPlaylist
|
2019-03-12 11:40:42 +01:00
|
|
|
const body: VideoPlaylistReorder = req.body
|
2019-02-26 10:55:40 +01:00
|
|
|
|
2019-03-12 11:40:42 +01:00
|
|
|
const start: number = body.startPosition
|
|
|
|
const insertAfter: number = body.insertAfterPosition
|
|
|
|
const reorderLength: number = body.reorderLength || 1
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
if (start === insertAfter) {
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, newPosition, null, reorderLength, t)
|
|
|
|
|
|
|
|
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)
|
|
|
|
await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, null, -reorderLength, t)
|
|
|
|
|
2019-03-14 09:19:03 +01:00
|
|
|
videoPlaylist.changed('updatedAt', true)
|
2019-03-07 17:06:00 +01:00
|
|
|
await videoPlaylist.save({ transaction: t })
|
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
await sendUpdateVideoPlaylist(videoPlaylist, t)
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
'Reordered playlist %s (inserted after %d elements %d - %d).',
|
|
|
|
videoPlaylist.uuid, insertAfter, start, start + reorderLength - 1
|
|
|
|
)
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getVideoPlaylistVideos (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const videoPlaylistInstance = res.locals.videoPlaylist
|
2019-07-31 15:57:32 +02:00
|
|
|
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
|
|
|
|
const server = await getServerActor()
|
2019-02-26 10:55:40 +01:00
|
|
|
|
2019-07-31 15:57:32 +02:00
|
|
|
const resultList = await VideoPlaylistElementModel.listForApi({
|
2019-02-26 10:55:40 +01:00
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
|
|
|
videoPlaylistId: videoPlaylistInstance.id,
|
2019-07-31 15:57:32 +02:00
|
|
|
serverAccount: server.Account,
|
|
|
|
user
|
2019-02-26 10:55:40 +01:00
|
|
|
})
|
|
|
|
|
2019-07-31 15:57:32 +02: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 10:55:40 +01:00
|
|
|
}
|