]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/sql/video-model-get-query-builder.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / video-model-get-query-builder.ts
index 4aab9ff1d8a578b1da7a379dbe6facc3882d625c..f234e87783a104c6142fc93fcc31d63c4434a74a 100644 (file)
@@ -10,11 +10,26 @@ import { VideoTables } from './shared/video-tables'
  *
  */
 
+export type GetType =
+  'api' |
+  'full-light' |
+  'account-blacklist-files' |
+  'all-files' |
+  'thumbnails' |
+  'thumbnails-blacklist' |
+  'id' |
+  'blacklist-rights'
+
 export type BuildVideoGetQueryOptions = {
-  id: number | string
-  transaction?: Transaction
+  id?: number | string
+  url?: string
+
+  type: GetType
+
   userId?: number
-  forGetAPI?: boolean
+  transaction?: Transaction
+
+  logging?: boolean
 }
 
 export class VideosModelGetQueryBuilder {
@@ -24,6 +39,8 @@ export class VideosModelGetQueryBuilder {
 
   private readonly videoModelBuilder: VideoModelBuilder
 
+  private static readonly videoFilesInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files', 'all-files' ])
+
   constructor (protected readonly sequelize: Sequelize) {
     this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
     this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
@@ -32,11 +49,17 @@ export class VideosModelGetQueryBuilder {
     this.videoModelBuilder = new VideoModelBuilder('get', new VideoTables('get'))
   }
 
-  async queryVideos (options: BuildVideoGetQueryOptions) {
+  async queryVideo (options: BuildVideoGetQueryOptions) {
     const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
       this.videoQueryBuilder.queryVideos(options),
-      this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options),
-      this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
+
+      VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
+        ? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options)
+        : Promise.resolve(undefined),
+
+      VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
+        ? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
+        : Promise.resolve(undefined)
     ])
 
     const videos = this.videoModelBuilder.buildVideosFromRows(videoRows, webtorrentFilesRows, streamingPlaylistFilesRows)
@@ -56,6 +79,31 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild
   protected webtorrentFilesQuery: string
   protected streamingPlaylistFilesQuery: string
 
+  private static readonly trackersInclude = new Set<GetType>([ 'api' ])
+  private static readonly liveInclude = new Set<GetType>([ 'api', 'full-light' ])
+  private static readonly scheduleUpdateInclude = new Set<GetType>([ 'api', 'full-light' ])
+  private static readonly tagsInclude = new Set<GetType>([ 'api', 'full-light' ])
+  private static readonly userHistoryInclude = new Set<GetType>([ 'api', 'full-light' ])
+  private static readonly accountInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files' ])
+  private static readonly ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])
+
+  private static readonly blacklistedInclude = new Set<GetType>([
+    'api',
+    'full-light',
+    'account-blacklist-files',
+    'thumbnails-blacklist',
+    'blacklist-rights'
+  ])
+
+  private static readonly thumbnailsInclude = new Set<GetType>([
+    'api',
+    'full-light',
+    'account-blacklist-files',
+    'all-files',
+    'thumbnails',
+    'thumbnails-blacklist'
+  ])
+
   constructor (protected readonly sequelize: Sequelize) {
     super('get')
   }
@@ -63,7 +111,7 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild
   queryVideos (options: BuildVideoGetQueryOptions) {
     this.buildMainGetQuery(options)
 
-    return this.runQuery(options.transaction)
+    return this.runQuery(options)
   }
 
   private buildMainGetQuery (options: BuildVideoGetQueryOptions) {
@@ -71,34 +119,53 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild
       '"video".*': ''
     }
 
-    this.includeChannels()
-    this.includeAccounts()
+    if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) {
+      this.includeThumbnails()
+    }
 
-    this.includeTags()
+    if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) {
+      this.includeBlacklisted()
+    }
 
-    this.includeThumbnails()
+    if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) {
+      this.includeChannels()
+      this.includeAccounts()
+    }
 
-    this.includeBlacklisted()
+    if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) {
+      this.includeTags()
+    }
 
-    this.includeScheduleUpdate()
+    if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) {
+      this.includeScheduleUpdate()
+    }
 
-    this.includeLive()
+    if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) {
+      this.includeLive()
+    }
 
-    if (options.userId) {
+    if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) {
       this.includeUserHistory(options.userId)
     }
 
-    if (options.forGetAPI === true) {
+    if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) {
+      this.includeOwnerUser()
+    }
+
+    if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) {
       this.includeTrackers()
     }
 
-    this.whereId(options.id)
+    this.whereId(options)
 
-    this.query = this.buildQuery()
+    this.query = this.buildQuery(options)
   }
 
-  private buildQuery () {
-    const order = 'ORDER BY "Tags"."name" ASC'
+  private buildQuery (options: BuildVideoGetQueryOptions) {
+    const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)
+      ? 'ORDER BY "Tags"."name" ASC'
+      : ''
+
     const from = `SELECT * FROM "video" ${this.where} LIMIT 1`
 
     return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`