]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Fetch directly all video attributes for get API
authorChocobozzz <me@florianbigard.com>
Fri, 11 Jun 2021 07:57:19 +0000 (09:57 +0200)
committerChocobozzz <me@florianbigard.com>
Fri, 11 Jun 2021 07:57:19 +0000 (09:57 +0200)
server/controllers/api/videos/index.ts
server/helpers/video.ts
server/lib/model-loaders/video.ts
server/middlewares/validators/shared/videos.ts
server/middlewares/validators/videos/videos.ts
server/models/video/video.ts
server/typings/express/index.d.ts

index 35992e99364808f3349be93f8d66c45254738a5f..5fdb7d5bc5ba6a09cd5ec9abc3303cf09fcad971 100644 (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 } })
index d3445bed5e53d1c2fbd83ad1e0936e97fecb53f3..c2e15a705582001a076c5b10318dc3da478b7c44 100644 (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) {
index 597c94395be0f01701f4e70d5b474f0300968014..07b373ed39e0dccdf6632924fd9f282d499b6ca7 100644 (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)
index 3134f623d9e2cffcc286c7b162facf88a91982c2..1a22d651381c37f65358670510c6ad4254aa6b46 100644 (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
index 7f278c9f65f32065ca9f6f7c8e18189f4c017f16..a707fd08690ebebc26f78543ff2c84a45e24ece0 100644 (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 [
index 9d56eb13c9e11d540fe1c31411a3d4b0cea421b5..00fbb18f69249b5f82f193f3049c2b622c41ddd8 100644 (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 () {
index cbbf40a7868d47f3cc173503273f3f6773229b4e..00ff689438597b11d225a8ae3d1195ce537b0c34 100644 (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