]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/sql/video-model-get-query-builder.ts
Refactor include checks
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / video-model-get-query-builder.ts
1 import { Sequelize, Transaction } from 'sequelize'
2 import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
3 import { VideoFileQueryBuilder } from './shared/video-file-query-builder'
4 import { VideoModelBuilder } from './shared/video-model-builder'
5 import { VideoTables } from './shared/video-tables'
6
7 /**
8 *
9 * Build a GET SQL query, fetch rows and create the video model
10 *
11 */
12
13 export type GetType =
14 'api' |
15 'full-light' |
16 'account-blacklist-files' |
17 'all-files' |
18 'thumbnails' |
19 'thumbnails-blacklist' |
20 'id' |
21 'blacklist-rights'
22
23 export type BuildVideoGetQueryOptions = {
24 id?: number | string
25 url?: string
26
27 type: GetType
28
29 userId?: number
30 transaction?: Transaction
31
32 logging?: boolean
33 }
34
35 export class VideosModelGetQueryBuilder {
36 videoQueryBuilder: VideosModelGetQuerySubBuilder
37 webtorrentFilesQueryBuilder: VideoFileQueryBuilder
38 streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
39
40 private readonly videoModelBuilder: VideoModelBuilder
41
42 private static readonly videoFilesInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files', 'all-files' ])
43
44 constructor (protected readonly sequelize: Sequelize) {
45 this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
46 this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
47 this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
48
49 this.videoModelBuilder = new VideoModelBuilder('get', new VideoTables('get'))
50 }
51
52 async queryVideo (options: BuildVideoGetQueryOptions) {
53 const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
54 this.videoQueryBuilder.queryVideos(options),
55
56 VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
57 ? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options)
58 : Promise.resolve(undefined),
59
60 VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
61 ? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
62 : Promise.resolve(undefined)
63 ])
64
65 const videos = this.videoModelBuilder.buildVideosFromRows(videoRows, webtorrentFilesRows, streamingPlaylistFilesRows)
66
67 if (videos.length > 1) {
68 throw new Error('Video results is more than ')
69 }
70
71 if (videos.length === 0) return null
72 return videos[0]
73 }
74 }
75
76 export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder {
77 protected attributes: { [key: string]: string }
78
79 protected webtorrentFilesQuery: string
80 protected streamingPlaylistFilesQuery: string
81
82 private static readonly trackersInclude = new Set<GetType>([ 'api' ])
83 private static readonly liveInclude = new Set<GetType>([ 'api', 'full-light' ])
84 private static readonly scheduleUpdateInclude = new Set<GetType>([ 'api', 'full-light' ])
85 private static readonly tagsInclude = new Set<GetType>([ 'api', 'full-light' ])
86 private static readonly userHistoryInclude = new Set<GetType>([ 'api', 'full-light' ])
87 private static readonly accountInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files' ])
88 private static readonly ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])
89
90 private static readonly blacklistedInclude = new Set<GetType>([
91 'api',
92 'full-light',
93 'account-blacklist-files',
94 'thumbnails-blacklist',
95 'blacklist-rights'
96 ])
97
98 private static readonly thumbnailsInclude = new Set<GetType>([
99 'api',
100 'full-light',
101 'account-blacklist-files',
102 'thumbnails',
103 'thumbnails-blacklist'
104 ])
105
106 constructor (protected readonly sequelize: Sequelize) {
107 super('get')
108 }
109
110 queryVideos (options: BuildVideoGetQueryOptions) {
111 this.buildMainGetQuery(options)
112
113 return this.runQuery(options)
114 }
115
116 private buildMainGetQuery (options: BuildVideoGetQueryOptions) {
117 this.attributes = {
118 '"video".*': ''
119 }
120
121 if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) {
122 this.includeThumbnails()
123 }
124
125 if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) {
126 this.includeBlacklisted()
127 }
128
129 if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) {
130 this.includeChannels()
131 this.includeAccounts()
132 }
133
134 if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) {
135 this.includeTags()
136 }
137
138 if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) {
139 this.includeScheduleUpdate()
140 }
141
142 if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) {
143 this.includeLive()
144 }
145
146 if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) {
147 this.includeUserHistory(options.userId)
148 }
149
150 if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) {
151 this.includeOwnerUser()
152 }
153
154 if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) {
155 this.includeTrackers()
156 }
157
158 this.whereId(options)
159
160 this.query = this.buildQuery(options)
161 }
162
163 private buildQuery (options: BuildVideoGetQueryOptions) {
164 const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)
165 ? 'ORDER BY "Tags"."name" ASC'
166 : ''
167
168 const from = `SELECT * FROM "video" ${this.where} LIMIT 1`
169
170 return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`
171 }
172 }