Refactor video playlist middlewares

This commit is contained in:
Chocobozzz 2019-03-05 11:30:43 +01:00 committed by Chocobozzz
parent df0b219d36
commit 09979f8959
7 changed files with 44 additions and 32 deletions

View File

@ -4,7 +4,8 @@ import {
asyncMiddleware, asyncMiddleware,
asyncRetryTransactionMiddleware, asyncRetryTransactionMiddleware,
authenticate, authenticate,
commonVideosFiltersValidator, optionalAuthenticate, commonVideosFiltersValidator,
optionalAuthenticate,
paginationValidator, paginationValidator,
setDefaultPagination, setDefaultPagination,
setDefaultSort setDefaultSort
@ -31,15 +32,8 @@ import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/vid
import { processImage } from '../../helpers/image-utils' import { processImage } from '../../helpers/image-utils'
import { join } from 'path' import { join } from 'path'
import { UserModel } from '../../models/account/user' import { UserModel } from '../../models/account/user'
import { import { sendCreateVideoPlaylist, sendDeleteVideoPlaylist, sendUpdateVideoPlaylist } from '../../lib/activitypub/send'
sendCreateVideoPlaylist, import { getVideoPlaylistActivityPubUrl, getVideoPlaylistElementActivityPubUrl } from '../../lib/activitypub/url'
sendDeleteVideoPlaylist,
sendUpdateVideoPlaylist
} from '../../lib/activitypub/send'
import {
getVideoPlaylistActivityPubUrl,
getVideoPlaylistElementActivityPubUrl
} from '../../lib/activitypub/url'
import { VideoPlaylistUpdate } from '../../../shared/models/videos/playlist/video-playlist-update.model' import { VideoPlaylistUpdate } from '../../../shared/models/videos/playlist/video-playlist-update.model'
import { VideoModel } from '../../models/video/video' import { VideoModel } from '../../models/video/video'
import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'
@ -233,8 +227,6 @@ async function updateVideoPlaylist (req: express.Request, res: express.Response)
} }
const playlistUpdated = await videoPlaylistInstance.save(sequelizeOptions) const playlistUpdated = await videoPlaylistInstance.save(sequelizeOptions)
// We need more attributes for the federation
playlistUpdated.OwnerAccount = await AccountModel.load(playlistUpdated.OwnerAccount.id, t)
const isNewPlaylist = wasPrivatePlaylist && playlistUpdated.privacy !== VideoPlaylistPrivacy.PRIVATE const isNewPlaylist = wasPrivatePlaylist && playlistUpdated.privacy !== VideoPlaylistPrivacy.PRIVATE
@ -305,8 +297,6 @@ async function addVideoInPlaylist (req: express.Request, res: express.Response)
} }
} }
// We need more attributes for the federation
videoPlaylist.OwnerAccount = await AccountModel.load(videoPlaylist.OwnerAccount.id, t)
await sendUpdateVideoPlaylist(videoPlaylist, t) await sendUpdateVideoPlaylist(videoPlaylist, t)
return playlistElement return playlistElement
@ -332,8 +322,6 @@ async function updateVideoPlaylistElement (req: express.Request, res: express.Re
const element = await videoPlaylistElement.save({ transaction: t }) const element = await videoPlaylistElement.save({ transaction: t })
// We need more attributes for the federation
videoPlaylist.OwnerAccount = await AccountModel.load(videoPlaylist.OwnerAccount.id, t)
await sendUpdateVideoPlaylist(videoPlaylist, t) await sendUpdateVideoPlaylist(videoPlaylist, t)
return element return element
@ -355,8 +343,6 @@ async function removeVideoFromPlaylist (req: express.Request, res: express.Respo
// Decrease position of the next elements // Decrease position of the next elements
await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, positionToDelete, null, -1, t) await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, positionToDelete, null, -1, t)
// We need more attributes for the federation
videoPlaylist.OwnerAccount = await AccountModel.load(videoPlaylist.OwnerAccount.id, t)
await sendUpdateVideoPlaylist(videoPlaylist, t) await sendUpdateVideoPlaylist(videoPlaylist, t)
logger.info('Video playlist element %d of playlist %s deleted.', videoPlaylistElement.position, videoPlaylist.uuid) logger.info('Video playlist element %d of playlist %s deleted.', videoPlaylistElement.position, videoPlaylist.uuid)
@ -398,8 +384,6 @@ async function reorderVideosPlaylist (req: express.Request, res: express.Respons
// Decrease positions of elements after the old position of our ordered elements (decrease) // Decrease positions of elements after the old position of our ordered elements (decrease)
await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, null, -reorderLength, t) await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, null, -reorderLength, t)
// We need more attributes for the federation
videoPlaylist.OwnerAccount = await AccountModel.load(videoPlaylist.OwnerAccount.id, t)
await sendUpdateVideoPlaylist(videoPlaylist, t) await sendUpdateVideoPlaylist(videoPlaylist, t)
}) })

View File

@ -26,8 +26,10 @@ function isVideoPlaylistTypeValid (value: any) {
return exists(value) && VIDEO_PLAYLIST_TYPES[ value ] !== undefined return exists(value) && VIDEO_PLAYLIST_TYPES[ value ] !== undefined
} }
async function isVideoPlaylistExist (id: number | string, res: express.Response) { async function isVideoPlaylistExist (id: number | string, res: express.Response, fetchType: 'summary' | 'all' = 'summary') {
const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined) const videoPlaylist = fetchType === 'summary'
? await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined)
: await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined)
if (!videoPlaylist) { if (!videoPlaylist) {
res.status(404) res.status(404)

View File

@ -45,7 +45,7 @@ const videoPlaylistsUpdateValidator = getCommonPlaylistEditAttributes().concat([
if (areValidationErrors(req, res)) return cleanUpReqFiles(req) if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
if (!await isVideoPlaylistExist(req.params.playlistId, res)) return cleanUpReqFiles(req) if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return cleanUpReqFiles(req)
const videoPlaylist = res.locals.videoPlaylist const videoPlaylist = res.locals.videoPlaylist
@ -153,7 +153,7 @@ const videoPlaylistsAddVideoValidator = [
if (areValidationErrors(req, res)) return if (areValidationErrors(req, res)) return
if (!await isVideoPlaylistExist(req.params.playlistId, res)) return if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return
if (!await isVideoExist(req.body.videoId, res, 'only-video')) return if (!await isVideoExist(req.body.videoId, res, 'only-video')) return
const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
@ -193,7 +193,7 @@ const videoPlaylistsUpdateOrRemoveVideoValidator = [
if (areValidationErrors(req, res)) return if (areValidationErrors(req, res)) return
if (!await isVideoPlaylistExist(req.params.playlistId, res)) return if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return
if (!await isVideoExist(req.params.videoId, res, 'id')) return if (!await isVideoExist(req.params.videoId, res, 'id')) return
const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
@ -261,7 +261,7 @@ const videoPlaylistsReorderVideosValidator = [
if (areValidationErrors(req, res)) return if (areValidationErrors(req, res)) return
if (!await isVideoPlaylistExist(req.params.playlistId, res)) return if (!await isVideoPlaylistExist(req.params.playlistId, res, 'all')) return
const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist const videoPlaylist: VideoPlaylistModel = res.locals.videoPlaylist
if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return if (!checkUserCanManageVideoPlaylist(res.locals.oauth.token.User, videoPlaylist, UserRight.UPDATE_ANY_VIDEO_PLAYLIST, res)) return

View File

@ -10,7 +10,8 @@ import {
ForeignKey, ForeignKey,
HasMany, HasMany,
Is, Is,
Model, Scopes, Model,
Scopes,
Table, Table,
UpdatedAt UpdatedAt
} from 'sequelize-typescript' } from 'sequelize-typescript'
@ -26,7 +27,6 @@ import { VideoCommentModel } from '../video/video-comment'
import { UserModel } from './user' import { UserModel } from './user'
import { CONFIG } from '../../initializers' import { CONFIG } from '../../initializers'
import { AvatarModel } from '../avatar/avatar' import { AvatarModel } from '../avatar/avatar'
import { WhereOptions } from 'sequelize'
import { VideoPlaylistModel } from '../video/video-playlist' import { VideoPlaylistModel } from '../video/video-playlist'
export enum ScopeNames { export enum ScopeNames {
@ -42,7 +42,7 @@ export enum ScopeNames {
] ]
}) })
@Scopes({ @Scopes({
[ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions<ActorModel>) => { [ ScopeNames.SUMMARY ]: (whereActor?: Sequelize.WhereOptions<ActorModel>) => {
return { return {
attributes: [ 'id', 'name' ], attributes: [ 'id', 'name' ],
include: [ include: [

View File

@ -47,7 +47,8 @@ enum ScopeNames {
AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH', WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH',
WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY', WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY',
WITH_ACCOUNT = 'WITH_ACCOUNT' WITH_ACCOUNT = 'WITH_ACCOUNT',
WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL'
} }
type AvailableForListOptions = { type AvailableForListOptions = {
@ -89,6 +90,18 @@ type AvailableForListOptions = {
} }
] ]
}, },
[ ScopeNames.WITH_ACCOUNT_AND_CHANNEL ]: {
include: [
{
model: () => AccountModel,
required: true
},
{
model: () => VideoChannelModel,
required: false
}
]
},
[ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => { [ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => {
// Only list local playlists OR playlists that are on an instance followed by actorId // Only list local playlists OR playlists that are on an instance followed by actorId
const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId) const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
@ -317,7 +330,7 @@ export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
.then(e => !!e) .then(e => !!e)
} }
static loadWithAccountAndChannel (id: number | string, transaction: Sequelize.Transaction) { static loadWithAccountAndChannelSummary (id: number | string, transaction: Sequelize.Transaction) {
const where = buildWhereIdOrUUID(id) const where = buildWhereIdOrUUID(id)
const query = { const query = {
@ -330,6 +343,19 @@ export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
.findOne(query) .findOne(query)
} }
static loadWithAccountAndChannel (id: number | string, transaction: Sequelize.Transaction) {
const where = buildWhereIdOrUUID(id)
const query = {
where,
transaction
}
return VideoPlaylistModel
.scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH ])
.findOne(query)
}
static loadByUrlAndPopulateAccount (url: string) { static loadByUrlAndPopulateAccount (url: string) {
const query = { const query = {
where: { where: {

View File

@ -11,6 +11,7 @@ import './video-description'
import './video-hls' import './video-hls'
import './video-imports' import './video-imports'
import './video-nsfw' import './video-nsfw'
import './video-playlists'
import './video-privacy' import './video-privacy'
import './video-schedule-update' import './video-schedule-update'
import './video-transcoder' import './video-transcoder'

View File

@ -608,7 +608,6 @@ async function uploadVideoAndGetId (options: { server: ServerInfo, videoName: st
const videoAttrs: any = { name: options.videoName } const videoAttrs: any = { name: options.videoName }
if (options.nsfw) videoAttrs.nsfw = options.nsfw if (options.nsfw) videoAttrs.nsfw = options.nsfw
const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs) const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
return { id: res.body.video.id, uuid: res.body.video.uuid } return { id: res.body.video.id, uuid: res.body.video.uuid }