]>
Commit | Line | Data |
---|---|---|
1d43c3a6 | 1 | import validator from 'validator' |
d9bf974f | 2 | import { AbstractVideosQueryBuilder } from './abstract-videos-query-builder' |
17bb4538 | 3 | import { VideoTables } from './video-tables' |
1d43c3a6 C |
4 | |
5 | /** | |
6 | * | |
7 | * Abstract builder to create SQL query and fetch video models | |
8 | * | |
9 | */ | |
d9bf974f C |
10 | |
11 | export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder { | |
12 | protected attributes: { [key: string]: string } = {} | |
3c79c2ce C |
13 | |
14 | protected joins = '' | |
1d43c3a6 | 15 | protected where: string |
d9bf974f | 16 | |
17bb4538 | 17 | protected tables: VideoTables |
d9bf974f | 18 | |
1d43c3a6 | 19 | constructor (protected readonly mode: 'list' | 'get') { |
d9bf974f C |
20 | super() |
21 | ||
17bb4538 | 22 | this.tables = new VideoTables(this.mode) |
d9bf974f C |
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 () { | |
3c79c2ce C |
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"') | |
d9bf974f | 37 | |
3c79c2ce C |
38 | this.addJoin( |
39 | 'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"' | |
40 | ) | |
d9bf974f | 41 | |
3c79c2ce | 42 | this.addJoin( |
d9bf974f C |
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 | ||
17bb4538 | 50 | ...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()), |
d9bf974f C |
51 | ...this.buildActorInclude('VideoChannel->Actor'), |
52 | ...this.buildAvatarInclude('VideoChannel->Actor->Avatar'), | |
53 | ...this.buildServerInclude('VideoChannel->Actor->Server') | |
54 | } | |
55 | } | |
56 | ||
57 | protected includeAccounts () { | |
3c79c2ce C |
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 | ) | |
d9bf974f | 62 | |
3c79c2ce | 63 | this.addJoin( |
d9bf974f | 64 | 'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' + |
3c79c2ce C |
65 | 'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"' |
66 | ) | |
d9bf974f | 67 | |
3c79c2ce | 68 | this.addJoin( |
d9bf974f C |
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 | ||
17bb4538 | 76 | ...this.buildAttributesObject('VideoChannel->Account', this.tables.getAccountAttributes()), |
d9bf974f C |
77 | ...this.buildActorInclude('VideoChannel->Account->Actor'), |
78 | ...this.buildAvatarInclude('VideoChannel->Account->Actor->Avatar'), | |
79 | ...this.buildServerInclude('VideoChannel->Account->Actor->Server') | |
80 | } | |
81 | } | |
82 | ||
71d4af1e C |
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 | ||
d9bf974f | 95 | protected includeThumbnails () { |
3c79c2ce | 96 | this.addJoin('LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"') |
d9bf974f C |
97 | |
98 | this.attributes = { | |
99 | ...this.attributes, | |
100 | ||
17bb4538 | 101 | ...this.buildAttributesObject('Thumbnails', this.tables.getThumbnailAttributes()) |
d9bf974f C |
102 | } |
103 | } | |
104 | ||
668f864f C |
105 | protected includeWebtorrentFiles () { |
106 | this.addJoin('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"') | |
1d43c3a6 C |
107 | |
108 | this.attributes = { | |
109 | ...this.attributes, | |
110 | ||
17bb4538 | 111 | ...this.buildAttributesObject('VideoFiles', this.tables.getFileAttributes()) |
1d43c3a6 C |
112 | } |
113 | } | |
114 | ||
668f864f | 115 | protected includeStreamingPlaylistFiles () { |
3c79c2ce | 116 | this.addJoin( |
668f864f | 117 | 'LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"' |
3c79c2ce | 118 | ) |
d9bf974f | 119 | |
3c79c2ce | 120 | this.addJoin( |
668f864f | 121 | 'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' + |
d9bf974f C |
122 | 'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"' |
123 | ) | |
124 | ||
125 | this.attributes = { | |
126 | ...this.attributes, | |
127 | ||
17bb4538 C |
128 | ...this.buildAttributesObject('VideoStreamingPlaylists', this.tables.getStreamingPlaylistAttributes()), |
129 | ...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.tables.getFileAttributes()) | |
d9bf974f C |
130 | } |
131 | } | |
132 | ||
133 | protected includeUserHistory (userId: number) { | |
3c79c2ce | 134 | this.addJoin( |
d9bf974f C |
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 | ||
17bb4538 | 144 | ...this.buildAttributesObject('userVideoHistory', this.tables.getUserHistoryAttributes()) |
d9bf974f C |
145 | } |
146 | } | |
147 | ||
148 | protected includePlaylist (playlistId: number) { | |
3c79c2ce | 149 | this.addJoin( |
d9bf974f C |
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 | ||
17bb4538 | 159 | ...this.buildAttributesObject('VideoPlaylistElement', this.tables.getPlaylistAttributes()) |
d9bf974f C |
160 | } |
161 | } | |
162 | ||
163 | protected includeTags () { | |
3c79c2ce | 164 | this.addJoin( |
d9bf974f C |
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 | ||
17bb4538 C |
174 | ...this.buildAttributesObject('Tags', this.tables.getTagAttributes()), |
175 | ...this.buildAttributesObject('Tags->VideoTagModel', this.tables.getVideoTagAttributes()) | |
d9bf974f C |
176 | } |
177 | } | |
178 | ||
179 | protected includeBlacklisted () { | |
3c79c2ce | 180 | this.addJoin( |
d9bf974f C |
181 | 'LEFT OUTER JOIN "videoBlacklist" AS "VideoBlacklist" ON "video"."id" = "VideoBlacklist"."videoId"' |
182 | ) | |
183 | ||
184 | this.attributes = { | |
185 | ...this.attributes, | |
186 | ||
17bb4538 | 187 | ...this.buildAttributesObject('VideoBlacklist', this.tables.getBlacklistedAttributes()) |
d9bf974f C |
188 | } |
189 | } | |
190 | ||
191 | protected includeScheduleUpdate () { | |
3c79c2ce | 192 | this.addJoin( |
d9bf974f C |
193 | 'LEFT OUTER JOIN "scheduleVideoUpdate" AS "ScheduleVideoUpdate" ON "video"."id" = "ScheduleVideoUpdate"."videoId"' |
194 | ) | |
195 | ||
196 | this.attributes = { | |
197 | ...this.attributes, | |
198 | ||
17bb4538 | 199 | ...this.buildAttributesObject('ScheduleVideoUpdate', this.tables.getScheduleUpdateAttributes()) |
d9bf974f C |
200 | } |
201 | } | |
202 | ||
203 | protected includeLive () { | |
3c79c2ce | 204 | this.addJoin( |
d9bf974f C |
205 | 'LEFT OUTER JOIN "videoLive" AS "VideoLive" ON "video"."id" = "VideoLive"."videoId"' |
206 | ) | |
207 | ||
208 | this.attributes = { | |
209 | ...this.attributes, | |
210 | ||
17bb4538 | 211 | ...this.buildAttributesObject('VideoLive', this.tables.getLiveAttributes()) |
d9bf974f C |
212 | } |
213 | } | |
214 | ||
215 | protected includeTrackers () { | |
3c79c2ce | 216 | this.addJoin( |
d9bf974f C |
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 | ||
17bb4538 C |
226 | ...this.buildAttributesObject('Trackers', this.tables.getTrackerAttributes()), |
227 | ...this.buildAttributesObject('Trackers->VideoTrackerModel', this.tables.getVideoTrackerAttributes()) | |
d9bf974f C |
228 | } |
229 | } | |
230 | ||
1d43c3a6 | 231 | protected includeWebTorrentRedundancies () { |
3c79c2ce | 232 | this.addJoin( |
d9bf974f C |
233 | 'LEFT OUTER JOIN "videoRedundancy" AS "VideoFiles->RedundancyVideos" ON ' + |
234 | '"VideoFiles"."id" = "VideoFiles->RedundancyVideos"."videoFileId"' | |
235 | ) | |
236 | ||
237 | this.attributes = { | |
238 | ...this.attributes, | |
239 | ||
17bb4538 | 240 | ...this.buildAttributesObject('VideoFiles->RedundancyVideos', this.tables.getRedundancyAttributes()) |
1d43c3a6 C |
241 | } |
242 | } | |
243 | ||
244 | protected includeStreamingPlaylistRedundancies () { | |
3c79c2ce | 245 | this.addJoin( |
1d43c3a6 C |
246 | 'LEFT OUTER JOIN "videoRedundancy" AS "VideoStreamingPlaylists->RedundancyVideos" ' + |
247 | 'ON "VideoStreamingPlaylists"."id" = "VideoStreamingPlaylists->RedundancyVideos"."videoStreamingPlaylistId"' | |
248 | ) | |
249 | ||
250 | this.attributes = { | |
251 | ...this.attributes, | |
252 | ||
17bb4538 | 253 | ...this.buildAttributesObject('VideoStreamingPlaylists->RedundancyVideos', this.tables.getRedundancyAttributes()) |
d9bf974f C |
254 | } |
255 | } | |
256 | ||
257 | protected buildActorInclude (prefixKey: string) { | |
17bb4538 | 258 | return this.buildAttributesObject(prefixKey, this.tables.getActorAttributes()) |
d9bf974f C |
259 | } |
260 | ||
261 | protected buildAvatarInclude (prefixKey: string) { | |
17bb4538 | 262 | return this.buildAttributesObject(prefixKey, this.tables.getAvatarAttributes()) |
d9bf974f C |
263 | } |
264 | ||
265 | protected buildServerInclude (prefixKey: string) { | |
17bb4538 | 266 | return this.buildAttributesObject(prefixKey, this.tables.getServerAttributes()) |
d9bf974f C |
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 | } | |
1d43c3a6 | 280 | |
71d4af1e C |
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)) { | |
1d43c3a6 C |
289 | this.where = 'WHERE "video".id = :videoId' |
290 | } else { | |
291 | this.where = 'WHERE uuid = :videoId' | |
292 | } | |
293 | ||
71d4af1e | 294 | this.replacements.videoId = options.id |
1d43c3a6 | 295 | } |
3c79c2ce C |
296 | |
297 | protected addJoin (join: string) { | |
298 | this.joins += join + ' ' | |
299 | } | |
d9bf974f | 300 | } |