]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/sql/shared/abstract-videos-model-query-builder.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / shared / abstract-videos-model-query-builder.ts
1 import validator from 'validator'
2 import { AbstractVideosQueryBuilder } from './abstract-videos-query-builder'
3 import { VideoTables } from './video-tables'
4
5 /**
6 *
7 * Abstract builder to create SQL query and fetch video models
8 *
9 */
10
11 export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder {
12 protected attributes: { [key: string]: string } = {}
13
14 protected joins = ''
15 protected where: string
16
17 protected tables: VideoTables
18
19 constructor (protected readonly mode: 'list' | 'get') {
20 super()
21
22 this.tables = new VideoTables(this.mode)
23 }
24
25 protected buildSelect () {
26 return 'SELECT ' + Object.keys(this.attributes).map(key => {
27 const value = this.attributes[key]
28 if (value) return `${key} AS ${value}`
29
30 return key
31 }).join(', ')
32 }
33
34 protected includeChannels () {
35 this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
36 this.addJoin('INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."actorId" = "VideoChannel->Actor"."id"')
37
38 this.addJoin(
39 'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"'
40 )
41
42 this.addJoin(
43 'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Actor->Avatar" ' +
44 'ON "VideoChannel->Actor"."avatarId" = "VideoChannel->Actor->Avatar"."id"'
45 )
46
47 this.attributes = {
48 ...this.attributes,
49
50 ...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
51 ...this.buildActorInclude('VideoChannel->Actor'),
52 ...this.buildAvatarInclude('VideoChannel->Actor->Avatar'),
53 ...this.buildServerInclude('VideoChannel->Actor->Server')
54 }
55 }
56
57 protected includeAccounts () {
58 this.addJoin('INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"')
59 this.addJoin(
60 'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."actorId" = "VideoChannel->Account->Actor"."id"'
61 )
62
63 this.addJoin(
64 'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' +
65 'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"'
66 )
67
68 this.addJoin(
69 'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Account->Actor->Avatar" ' +
70 'ON "VideoChannel->Account->Actor"."avatarId" = "VideoChannel->Account->Actor->Avatar"."id"'
71 )
72
73 this.attributes = {
74 ...this.attributes,
75
76 ...this.buildAttributesObject('VideoChannel->Account', this.tables.getAccountAttributes()),
77 ...this.buildActorInclude('VideoChannel->Account->Actor'),
78 ...this.buildAvatarInclude('VideoChannel->Account->Actor->Avatar'),
79 ...this.buildServerInclude('VideoChannel->Account->Actor->Server')
80 }
81 }
82
83 protected includeOwnerUser () {
84 this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
85 this.addJoin('INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"')
86
87 this.attributes = {
88 ...this.attributes,
89
90 ...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
91 ...this.buildAttributesObject('VideoChannel->Account', this.tables.getUserAccountAttributes())
92 }
93 }
94
95 protected includeThumbnails () {
96 this.addJoin('LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"')
97
98 this.attributes = {
99 ...this.attributes,
100
101 ...this.buildAttributesObject('Thumbnails', this.tables.getThumbnailAttributes())
102 }
103 }
104
105 protected includeWebtorrentFiles () {
106 this.addJoin('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')
107
108 this.attributes = {
109 ...this.attributes,
110
111 ...this.buildAttributesObject('VideoFiles', this.tables.getFileAttributes())
112 }
113 }
114
115 protected includeStreamingPlaylistFiles () {
116 this.addJoin(
117 'LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"'
118 )
119
120 this.addJoin(
121 'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' +
122 'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"'
123 )
124
125 this.attributes = {
126 ...this.attributes,
127
128 ...this.buildAttributesObject('VideoStreamingPlaylists', this.tables.getStreamingPlaylistAttributes()),
129 ...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.tables.getFileAttributes())
130 }
131 }
132
133 protected includeUserHistory (userId: number) {
134 this.addJoin(
135 'LEFT OUTER JOIN "userVideoHistory" ' +
136 'ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :userVideoHistoryId'
137 )
138
139 this.replacements.userVideoHistoryId = userId
140
141 this.attributes = {
142 ...this.attributes,
143
144 ...this.buildAttributesObject('userVideoHistory', this.tables.getUserHistoryAttributes())
145 }
146 }
147
148 protected includePlaylist (playlistId: number) {
149 this.addJoin(
150 'INNER JOIN "videoPlaylistElement" as "VideoPlaylistElement" ON "videoPlaylistElement"."videoId" = "video"."id" ' +
151 'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
152 )
153
154 this.replacements.videoPlaylistId = playlistId
155
156 this.attributes = {
157 ...this.attributes,
158
159 ...this.buildAttributesObject('VideoPlaylistElement', this.tables.getPlaylistAttributes())
160 }
161 }
162
163 protected includeTags () {
164 this.addJoin(
165 'LEFT OUTER JOIN (' +
166 '"videoTag" AS "Tags->VideoTagModel" INNER JOIN "tag" AS "Tags" ON "Tags"."id" = "Tags->VideoTagModel"."tagId"' +
167 ') ' +
168 'ON "video"."id" = "Tags->VideoTagModel"."videoId"'
169 )
170
171 this.attributes = {
172 ...this.attributes,
173
174 ...this.buildAttributesObject('Tags', this.tables.getTagAttributes()),
175 ...this.buildAttributesObject('Tags->VideoTagModel', this.tables.getVideoTagAttributes())
176 }
177 }
178
179 protected includeBlacklisted () {
180 this.addJoin(
181 'LEFT OUTER JOIN "videoBlacklist" AS "VideoBlacklist" ON "video"."id" = "VideoBlacklist"."videoId"'
182 )
183
184 this.attributes = {
185 ...this.attributes,
186
187 ...this.buildAttributesObject('VideoBlacklist', this.tables.getBlacklistedAttributes())
188 }
189 }
190
191 protected includeScheduleUpdate () {
192 this.addJoin(
193 'LEFT OUTER JOIN "scheduleVideoUpdate" AS "ScheduleVideoUpdate" ON "video"."id" = "ScheduleVideoUpdate"."videoId"'
194 )
195
196 this.attributes = {
197 ...this.attributes,
198
199 ...this.buildAttributesObject('ScheduleVideoUpdate', this.tables.getScheduleUpdateAttributes())
200 }
201 }
202
203 protected includeLive () {
204 this.addJoin(
205 'LEFT OUTER JOIN "videoLive" AS "VideoLive" ON "video"."id" = "VideoLive"."videoId"'
206 )
207
208 this.attributes = {
209 ...this.attributes,
210
211 ...this.buildAttributesObject('VideoLive', this.tables.getLiveAttributes())
212 }
213 }
214
215 protected includeTrackers () {
216 this.addJoin(
217 'LEFT OUTER JOIN (' +
218 '"videoTracker" AS "Trackers->VideoTrackerModel" ' +
219 'INNER JOIN "tracker" AS "Trackers" ON "Trackers"."id" = "Trackers->VideoTrackerModel"."trackerId"' +
220 ') ON "video"."id" = "Trackers->VideoTrackerModel"."videoId"'
221 )
222
223 this.attributes = {
224 ...this.attributes,
225
226 ...this.buildAttributesObject('Trackers', this.tables.getTrackerAttributes()),
227 ...this.buildAttributesObject('Trackers->VideoTrackerModel', this.tables.getVideoTrackerAttributes())
228 }
229 }
230
231 protected includeWebTorrentRedundancies () {
232 this.addJoin(
233 'LEFT OUTER JOIN "videoRedundancy" AS "VideoFiles->RedundancyVideos" ON ' +
234 '"VideoFiles"."id" = "VideoFiles->RedundancyVideos"."videoFileId"'
235 )
236
237 this.attributes = {
238 ...this.attributes,
239
240 ...this.buildAttributesObject('VideoFiles->RedundancyVideos', this.tables.getRedundancyAttributes())
241 }
242 }
243
244 protected includeStreamingPlaylistRedundancies () {
245 this.addJoin(
246 'LEFT OUTER JOIN "videoRedundancy" AS "VideoStreamingPlaylists->RedundancyVideos" ' +
247 'ON "VideoStreamingPlaylists"."id" = "VideoStreamingPlaylists->RedundancyVideos"."videoStreamingPlaylistId"'
248 )
249
250 this.attributes = {
251 ...this.attributes,
252
253 ...this.buildAttributesObject('VideoStreamingPlaylists->RedundancyVideos', this.tables.getRedundancyAttributes())
254 }
255 }
256
257 protected buildActorInclude (prefixKey: string) {
258 return this.buildAttributesObject(prefixKey, this.tables.getActorAttributes())
259 }
260
261 protected buildAvatarInclude (prefixKey: string) {
262 return this.buildAttributesObject(prefixKey, this.tables.getAvatarAttributes())
263 }
264
265 protected buildServerInclude (prefixKey: string) {
266 return this.buildAttributesObject(prefixKey, this.tables.getServerAttributes())
267 }
268
269 protected buildAttributesObject (prefixKey: string, attributeKeys: string[]) {
270 const result: { [id: string]: string} = {}
271
272 const prefixValue = prefixKey.replace(/->/g, '.')
273
274 for (const attribute of attributeKeys) {
275 result[`"${prefixKey}"."${attribute}"`] = `"${prefixValue}.${attribute}"`
276 }
277
278 return result
279 }
280
281 protected whereId (options: { id?: string | number, url?: string }) {
282 if (options.url) {
283 this.where = 'WHERE "video"."url" = :videoUrl'
284 this.replacements.videoUrl = options.url
285 return
286 }
287
288 if (validator.isInt('' + options.id)) {
289 this.where = 'WHERE "video".id = :videoId'
290 } else {
291 this.where = 'WHERE uuid = :videoId'
292 }
293
294 this.replacements.videoId = options.id
295 }
296
297 protected addJoin (join: string) {
298 this.joins += join + ' '
299 }
300 }