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