From ca4b4b2e5590c1b37cff1fe1be7f797b93351229 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 11 Jun 2021 09:57:19 +0200 Subject: Fetch directly all video attributes for get API --- server/controllers/api/videos/index.ts | 11 ++--------- server/helpers/video.ts | 2 +- server/lib/model-loaders/video.ts | 14 +++++++++++++- server/middlewares/validators/shared/videos.ts | 5 +++++ server/middlewares/validators/videos/videos.ts | 2 +- server/models/video/video.ts | 6 +++--- server/typings/express/index.d.ts | 2 ++ 7 files changed, 27 insertions(+), 15 deletions(-) (limited to 'server') diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 35992e993..5fdb7d5bc 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -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 } }) diff --git a/server/helpers/video.ts b/server/helpers/video.ts index d3445bed5..c2e15a705 100644 --- a/server/helpers/video.ts +++ b/server/helpers/video.ts @@ -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) { diff --git a/server/lib/model-loaders/video.ts b/server/lib/model-loaders/video.ts index 597c94395..07b373ed3 100644 --- a/server/lib/model-loaders/video.ts +++ b/server/lib/model-loaders/video.ts @@ -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 function loadVideo (id: number | string, fetchType: 'all', userId?: number): Promise function loadVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise function loadVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise @@ -25,6 +28,15 @@ function loadVideo ( fetchType: VideoLoadType, userId?: number ): Promise { + + 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) diff --git a/server/middlewares/validators/shared/videos.ts b/server/middlewares/validators/shared/videos.ts index 3134f623d..1a22d6513 100644 --- a/server/middlewares/validators/shared/videos.ts +++ b/server/middlewares/validators/shared/videos.ts @@ -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 diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts index 7f278c9f6..a707fd086 100644 --- a/server/middlewares/validators/videos/videos.ts +++ b/server/middlewares/validators/videos/videos.ts @@ -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 [ diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 9d56eb13c..00fbb18f6 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1472,13 +1472,13 @@ export class VideoModel extends Model>> { static loadForGetAPI (parameters: { id: number | string - t?: Transaction + transaction?: Transaction userId?: number }): Promise { - 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 () { diff --git a/server/typings/express/index.d.ts b/server/typings/express/index.d.ts index cbbf40a78..00ff68943 100644 --- a/server/typings/express/index.d.ts +++ b/server/typings/express/index.d.ts @@ -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 -- cgit v1.2.3