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', videosRouter.get('/:id',
openapiOperationDoc({ operationId: 'getVideo' }), openapiOperationDoc({ operationId: 'getVideo' }),
optionalAuthenticate, optionalAuthenticate,
asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), asyncMiddleware(videosCustomGetValidator('for-api')),
asyncMiddleware(checkVideoFollowConstraints), asyncMiddleware(checkVideoFollowConstraints),
asyncMiddleware(getVideo) asyncMiddleware(getVideo)
) )
@ -142,14 +142,7 @@ function listVideoPrivacies (_req: express.Request, res: express.Response) {
} }
async function getVideo (_req: express.Request, res: express.Response) { async function getVideo (_req: express.Request, res: express.Response) {
// We need more attributes const video = res.locals.videoAPI
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'
)
if (video.isOutdated()) { if (video.isOutdated()) {
JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } }) 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' import { VideoPrivacy, VideoState } from '@shared/models'
function getVideoWithAttributes (res: Response) { 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) { function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {

View File

@ -1,15 +1,18 @@
import { VideoModel } from '@server/models/video/video' import { VideoModel } from '@server/models/video/video'
import { import {
MVideoAccountLightBlacklistAllFiles, MVideoAccountLightBlacklistAllFiles,
MVideoFormattableDetails,
MVideoFullLight, MVideoFullLight,
MVideoIdThumbnail, MVideoIdThumbnail,
MVideoImmutable, MVideoImmutable,
MVideoThumbnail, MVideoThumbnail,
MVideoWithRights MVideoWithRights
} from '@server/types/models' } 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: 'all', userId?: number): Promise<MVideoFullLight>
function loadVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable> function loadVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable>
function loadVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise<MVideoThumbnail> function loadVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise<MVideoThumbnail>
@ -25,6 +28,15 @@ function loadVideo (
fetchType: VideoLoadType, fetchType: VideoLoadType,
userId?: number userId?: number
): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> { ): 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 === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id) if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)

View File

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

View File

@ -258,7 +258,7 @@ async function checkVideoFollowConstraints (req: express.Request, res: express.R
} }
const videosCustomGetValidator = ( 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 authenticateInQuery = false
) => { ) => {
return [ return [

View File

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

View File

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