Fetch directly all video attributes for get API

This commit is contained in:
Chocobozzz 2021-06-11 09:57:19 +02:00
parent 1d43c3a613
commit ca4b4b2e55
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
7 changed files with 27 additions and 15 deletions

View File

@ -100,7 +100,7 @@ videosRouter.get('/:id/metadata/:videoFileId',
videosRouter.get('/:id',
openapiOperationDoc({ operationId: 'getVideo' }),
optionalAuthenticate,
asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
asyncMiddleware(videosCustomGetValidator('for-api')),
asyncMiddleware(checkVideoFollowConstraints),
asyncMiddleware(getVideo)
)
@ -142,14 +142,7 @@ function listVideoPrivacies (_req: express.Request, res: express.Response) {
}
async function getVideo (_req: express.Request, res: express.Response) {
// We need more attributes
const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null
const video = await Hooks.wrapPromiseFun(
VideoModel.loadForGetAPI,
{ id: _req.params.id, userId },
'filter:api.video.get.result'
)
const video = res.locals.videoAPI
if (video.isOutdated()) {
JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } })

View File

@ -4,7 +4,7 @@ import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/ty
import { VideoPrivacy, VideoState } from '@shared/models'
function getVideoWithAttributes (res: Response) {
return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
return res.locals.videoAPI || res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights
}
function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {

View File

@ -1,15 +1,18 @@
import { VideoModel } from '@server/models/video/video'
import {
MVideoAccountLightBlacklistAllFiles,
MVideoFormattableDetails,
MVideoFullLight,
MVideoIdThumbnail,
MVideoImmutable,
MVideoThumbnail,
MVideoWithRights
} from '@server/types/models'
import { Hooks } from '../plugins/hooks'
type VideoLoadType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
type VideoLoadType = 'for-api' | 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
function loadVideo (id: number | string, fetchType: 'for-api', userId?: number): Promise<MVideoFormattableDetails>
function loadVideo (id: number | string, fetchType: 'all', userId?: number): Promise<MVideoFullLight>
function loadVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable>
function loadVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise<MVideoThumbnail>
@ -25,6 +28,15 @@ function loadVideo (
fetchType: VideoLoadType,
userId?: number
): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> {
if (fetchType === 'for-api') {
return Hooks.wrapPromiseFun(
VideoModel.loadForGetAPI,
{ id, userId },
'filter:api.video.get.result'
)
}
if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)

View File

@ -6,6 +6,7 @@ import {
MUser,
MUserAccountId,
MVideoAccountLight,
MVideoFormattableDetails,
MVideoFullLight,
MVideoIdThumbnail,
MVideoImmutable,
@ -29,6 +30,10 @@ async function doesVideoExist (id: number | string, res: Response, fetchType: Vi
}
switch (fetchType) {
case 'for-api':
res.locals.videoAPI = video as MVideoFormattableDetails
break
case 'all':
res.locals.videoAll = video as MVideoFullLight
break

View File

@ -258,7 +258,7 @@ async function checkVideoFollowConstraints (req: express.Request, res: express.R
}
const videosCustomGetValidator = (
fetchType: 'all' | 'only-video' | 'only-video-with-rights' | 'only-immutable-attributes',
fetchType: 'for-api' | 'all' | 'only-video' | 'only-video-with-rights' | 'only-immutable-attributes',
authenticateInQuery = false
) => {
return [

View File

@ -1472,13 +1472,13 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> {
static loadForGetAPI (parameters: {
id: number | string
t?: Transaction
transaction?: Transaction
userId?: number
}): Promise<MVideoDetails> {
const { id, t, userId } = parameters
const { id, transaction, userId } = parameters
const queryBuilder = new VideosModelGetQueryBuilder(VideoModel.sequelize)
return queryBuilder.queryVideos({ id, transaction: t, forGetAPI: true, userId })
return queryBuilder.queryVideos({ id, transaction, forGetAPI: true, userId })
}
static async getStats () {

View File

@ -10,6 +10,7 @@ import {
MStreamingPlaylist,
MVideoChangeOwnershipFull,
MVideoFile,
MVideoFormattableDetails,
MVideoImmutable,
MVideoLive,
MVideoPlaylistFull,
@ -101,6 +102,7 @@ declare module 'express' {
locals: {
docUrl?: string
videoAPI?: MVideoFormattableDetails
videoAll?: MVideoFullLight
onlyImmutableVideo?: MVideoImmutable
onlyVideo?: MVideoThumbnail