]>
Commit | Line | Data |
---|---|---|
5f3e2425 C |
1 | import { VideoFilter, VideoPrivacy, VideoState } from '@shared/models' |
2 | import { buildDirectionAndField, createSafeIn } from '@server/models/utils' | |
3 | import { Model } from 'sequelize-typescript' | |
26d6bf65 | 4 | import { MUserAccountId, MUserId } from '@server/types/models' |
5f3e2425 | 5 | import validator from 'validator' |
fab67463 | 6 | import { exists } from '@server/helpers/custom-validators/misc' |
5f3e2425 C |
7 | |
8 | export type BuildVideosQueryOptions = { | |
9 | attributes?: string[] | |
10 | ||
11 | serverAccountId: number | |
12 | followerActorId: number | |
13 | includeLocalVideos: boolean | |
14 | ||
15 | count: number | |
16 | start: number | |
17 | sort: string | |
18 | ||
19 | filter?: VideoFilter | |
20 | categoryOneOf?: number[] | |
21 | nsfw?: boolean | |
22 | licenceOneOf?: number[] | |
23 | languageOneOf?: string[] | |
24 | tagsOneOf?: string[] | |
25 | tagsAllOf?: string[] | |
26 | ||
27 | withFiles?: boolean | |
28 | ||
29 | accountId?: number | |
30 | videoChannelId?: number | |
31 | ||
32 | videoPlaylistId?: number | |
33 | ||
3d4e112d | 34 | trendingAlgorithm?: string // best, hot, or any other algorithm implemented |
5f3e2425 | 35 | trendingDays?: number |
5bcbcbe3 | 36 | |
5f3e2425 C |
37 | user?: MUserAccountId |
38 | historyOfUser?: MUserId | |
39 | ||
40 | startDate?: string // ISO 8601 | |
41 | endDate?: string // ISO 8601 | |
42 | originallyPublishedStartDate?: string | |
43 | originallyPublishedEndDate?: string | |
44 | ||
45 | durationMin?: number // seconds | |
46 | durationMax?: number // seconds | |
47 | ||
48 | search?: string | |
49 | ||
50 | isCount?: boolean | |
51 | ||
52 | group?: string | |
53 | having?: string | |
54 | } | |
55 | ||
56 | function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) { | |
57 | const and: string[] = [] | |
5f3e2425 C |
58 | const joins: string[] = [] |
59 | const replacements: any = {} | |
6b842050 | 60 | const cte: string[] = [] |
5f3e2425 C |
61 | |
62 | let attributes: string[] = options.attributes || [ '"video"."id"' ] | |
63 | let group = options.group || '' | |
64 | const having = options.having || '' | |
65 | ||
66 | joins.push( | |
67 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId"' + | |
68 | 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId"' + | |
6b842050 | 69 | 'INNER JOIN "actor" "accountActor" ON "account"."actorId" = "accountActor"."id"' |
5f3e2425 C |
70 | ) |
71 | ||
72 | and.push('"video"."id" NOT IN (SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")') | |
73 | ||
74 | if (options.serverAccountId) { | |
75 | const blockerIds = [ options.serverAccountId ] | |
76 | if (options.user) blockerIds.push(options.user.Account.id) | |
77 | ||
6b842050 | 78 | const inClause = createSafeIn(model, blockerIds) |
5f3e2425 C |
79 | |
80 | and.push( | |
6b842050 C |
81 | 'NOT EXISTS (' + |
82 | ' SELECT 1 FROM "accountBlocklist" ' + | |
83 | ' WHERE "accountBlocklist"."accountId" IN (' + inClause + ') ' + | |
84 | ' AND "accountBlocklist"."targetAccountId" = "account"."id" ' + | |
85 | ')' + | |
86 | 'AND NOT EXISTS (' + | |
87 | ' SELECT 1 FROM "serverBlocklist" WHERE "serverBlocklist"."accountId" IN (' + inClause + ') ' + | |
88 | ' AND "serverBlocklist"."targetServerId" = "accountActor"."serverId"' + | |
89 | ')' | |
5f3e2425 | 90 | ) |
5f3e2425 C |
91 | } |
92 | ||
93 | // Only list public/published videos | |
0aa52e17 | 94 | if (!options.filter || (options.filter !== 'all-local' && options.filter !== 'all')) { |
5f3e2425 C |
95 | and.push( |
96 | `("video"."state" = ${VideoState.PUBLISHED} OR ` + | |
97 | `("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false))` | |
98 | ) | |
99 | ||
100 | if (options.user) { | |
101 | and.push( | |
102 | `("video"."privacy" = ${VideoPrivacy.PUBLIC} OR "video"."privacy" = ${VideoPrivacy.INTERNAL})` | |
103 | ) | |
104 | } else { // Or only public videos | |
105 | and.push( | |
106 | `"video"."privacy" = ${VideoPrivacy.PUBLIC}` | |
107 | ) | |
108 | } | |
109 | } | |
110 | ||
111 | if (options.videoPlaylistId) { | |
112 | joins.push( | |
113 | 'INNER JOIN "videoPlaylistElement" "video"."id" = "videoPlaylistElement"."videoId" ' + | |
114 | 'AND "videoPlaylistElement"."videoPlaylistId" = :videoPlaylistId' | |
115 | ) | |
116 | ||
117 | replacements.videoPlaylistId = options.videoPlaylistId | |
118 | } | |
119 | ||
120 | if (options.filter && (options.filter === 'local' || options.filter === 'all-local')) { | |
121 | and.push('"video"."remote" IS FALSE') | |
122 | } | |
123 | ||
124 | if (options.accountId) { | |
125 | and.push('"account"."id" = :accountId') | |
126 | replacements.accountId = options.accountId | |
127 | } | |
128 | ||
129 | if (options.videoChannelId) { | |
130 | and.push('"videoChannel"."id" = :videoChannelId') | |
131 | replacements.videoChannelId = options.videoChannelId | |
132 | } | |
133 | ||
134 | if (options.followerActorId) { | |
135 | let query = | |
136 | '(' + | |
137 | ' EXISTS (' + | |
138 | ' SELECT 1 FROM "videoShare" ' + | |
139 | ' INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' + | |
f046e2fa C |
140 | ' AND "actorFollowShare"."actorId" = :followerActorId AND "actorFollowShare"."state" = \'accepted\' ' + |
141 | ' WHERE "videoShare"."videoId" = "video"."id"' + | |
5f3e2425 C |
142 | ' )' + |
143 | ' OR' + | |
144 | ' EXISTS (' + | |
145 | ' SELECT 1 from "actorFollow" ' + | |
f046e2fa C |
146 | ' WHERE "actorFollow"."targetActorId" = "videoChannel"."actorId" AND "actorFollow"."actorId" = :followerActorId ' + |
147 | ' AND "actorFollow"."state" = \'accepted\'' + | |
5f3e2425 C |
148 | ' )' |
149 | ||
150 | if (options.includeLocalVideos) { | |
151 | query += ' OR "video"."remote" IS FALSE' | |
152 | } | |
153 | ||
154 | query += ')' | |
155 | ||
156 | and.push(query) | |
157 | replacements.followerActorId = options.followerActorId | |
158 | } | |
159 | ||
160 | if (options.withFiles === true) { | |
97816649 C |
161 | and.push( |
162 | '(' + | |
163 | ' EXISTS (SELECT 1 FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id") ' + | |
164 | ' OR EXISTS (' + | |
165 | ' SELECT 1 FROM "videoStreamingPlaylist" ' + | |
166 | ' INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ' + | |
167 | ' WHERE "videoStreamingPlaylist"."videoId" = "video"."id"' + | |
168 | ' )' + | |
169 | ')' | |
170 | ) | |
5f3e2425 C |
171 | } |
172 | ||
173 | if (options.tagsOneOf) { | |
174 | const tagsOneOfLower = options.tagsOneOf.map(t => t.toLowerCase()) | |
175 | ||
176 | and.push( | |
177 | 'EXISTS (' + | |
178 | ' SELECT 1 FROM "videoTag" ' + | |
179 | ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + | |
180 | ' WHERE lower("tag"."name") IN (' + createSafeIn(model, tagsOneOfLower) + ') ' + | |
181 | ' AND "video"."id" = "videoTag"."videoId"' + | |
182 | ')' | |
183 | ) | |
184 | } | |
185 | ||
186 | if (options.tagsAllOf) { | |
187 | const tagsAllOfLower = options.tagsAllOf.map(t => t.toLowerCase()) | |
188 | ||
189 | and.push( | |
190 | 'EXISTS (' + | |
191 | ' SELECT 1 FROM "videoTag" ' + | |
192 | ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + | |
193 | ' WHERE lower("tag"."name") IN (' + createSafeIn(model, tagsAllOfLower) + ') ' + | |
194 | ' AND "video"."id" = "videoTag"."videoId" ' + | |
195 | ' GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length + | |
196 | ')' | |
197 | ) | |
198 | } | |
199 | ||
200 | if (options.nsfw === true) { | |
201 | and.push('"video"."nsfw" IS TRUE') | |
202 | } | |
203 | ||
204 | if (options.nsfw === false) { | |
205 | and.push('"video"."nsfw" IS FALSE') | |
206 | } | |
207 | ||
208 | if (options.categoryOneOf) { | |
209 | and.push('"video"."category" IN (:categoryOneOf)') | |
210 | replacements.categoryOneOf = options.categoryOneOf | |
211 | } | |
212 | ||
213 | if (options.licenceOneOf) { | |
214 | and.push('"video"."licence" IN (:licenceOneOf)') | |
215 | replacements.licenceOneOf = options.licenceOneOf | |
216 | } | |
217 | ||
218 | if (options.languageOneOf) { | |
14cbb9a6 C |
219 | const languages = options.languageOneOf.filter(l => l && l !== '_unknown') |
220 | const languagesQueryParts: string[] = [] | |
221 | ||
222 | if (languages.length !== 0) { | |
8f31261f | 223 | languagesQueryParts.push('"video"."language" IN (:languageOneOf)') |
14cbb9a6 C |
224 | replacements.languageOneOf = languages |
225 | ||
226 | languagesQueryParts.push( | |
8f31261f C |
227 | 'EXISTS (' + |
228 | ' SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' + | |
229 | ' IN (' + createSafeIn(model, languages) + ') AND ' + | |
230 | ' "videoCaption"."videoId" = "video"."id"' + | |
14cbb9a6 C |
231 | ')' |
232 | ) | |
233 | } | |
5f3e2425 C |
234 | |
235 | if (options.languageOneOf.includes('_unknown')) { | |
14cbb9a6 | 236 | languagesQueryParts.push('"video"."language" IS NULL') |
5f3e2425 C |
237 | } |
238 | ||
8f31261f C |
239 | if (languagesQueryParts.length !== 0) { |
240 | and.push('(' + languagesQueryParts.join(' OR ') + ')') | |
241 | } | |
5f3e2425 C |
242 | } |
243 | ||
5bcbcbe3 | 244 | // We don't exclude results in this so if we do a count we don't need to add this complex clause |
3da68f0a RK |
245 | if (options.isCount !== true) { |
246 | if (options.trendingDays) { | |
247 | const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * options.trendingDays) | |
248 | ||
249 | joins.push('LEFT JOIN "videoView" ON "video"."id" = "videoView"."videoId" AND "videoView"."startDate" >= :viewsGteDate') | |
250 | replacements.viewsGteDate = viewsGteDate | |
251 | ||
252 | attributes.push('COALESCE(SUM("videoView"."views"), 0) AS "score"') | |
253 | ||
254 | group = 'GROUP BY "video"."id"' | |
3d4e112d | 255 | } else if ([ 'best', 'hot' ].includes(options.trendingAlgorithm)) { |
3da68f0a RK |
256 | /** |
257 | * "Hotness" is a measure based on absolute view/comment/like/dislike numbers, | |
258 | * with fixed weights only applied to their log values. | |
259 | * | |
260 | * This algorithm gives little chance for an old video to have a good score, | |
261 | * for which recent spikes in interactions could be a sign of "hotness" and | |
262 | * justify a better score. However there are multiple ways to achieve that | |
263 | * goal, which is left for later. Yes, this is a TODO :) | |
264 | * | |
923d3d5a RK |
265 | * notes: |
266 | * - weights and base score are in number of half-days. | |
267 | * - all comments are counted, regardless of being written by the video author or not | |
3da68f0a | 268 | * see https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/_sorts.pyx#L47-L58 |
5a22296b | 269 | * - we have less interactions than on reddit, so multiply weights by an arbitrary factor |
3da68f0a RK |
270 | */ |
271 | const weights = { | |
5a22296b C |
272 | like: 3 * 50, |
273 | dislike: -3 * 50, | |
274 | view: Math.floor((1 / 3) * 50), | |
275 | comment: 2 * 50, // a comment takes more time than a like to do, but can be done multiple times | |
276 | history: -2 * 50 | |
3da68f0a RK |
277 | } |
278 | ||
279 | joins.push('LEFT JOIN "videoComment" ON "video"."id" = "videoComment"."videoId"') | |
280 | ||
3d4e112d | 281 | let attribute = |
3da68f0a | 282 | `LOG(GREATEST(1, "video"."likes" - 1)) * ${weights.like} ` + // likes (+) |
3d4e112d | 283 | `+ LOG(GREATEST(1, "video"."dislikes" - 1)) * ${weights.dislike} ` + // dislikes (-) |
3da68f0a RK |
284 | `+ LOG("video"."views" + 1) * ${weights.view} ` + // views (+) |
285 | `+ LOG(GREATEST(1, COUNT(DISTINCT "videoComment"."id"))) * ${weights.comment} ` + // comments (+) | |
5a22296b | 286 | '+ (SELECT (EXTRACT(epoch FROM "video"."publishedAt") - 1446156582) / 47000) ' // base score (in number of half-days) |
3d4e112d RK |
287 | |
288 | if (options.trendingAlgorithm === 'best' && options.user) { | |
289 | joins.push( | |
290 | 'LEFT JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :bestUser' | |
291 | ) | |
292 | replacements.bestUser = options.user.id | |
293 | ||
294 | attribute += `+ POWER(COUNT(DISTINCT "userVideoHistory"."id"), 2.0) * ${weights.history} ` | |
295 | } | |
296 | ||
297 | attribute += 'AS "score"' | |
298 | attributes.push(attribute) | |
6b842050 | 299 | |
3da68f0a RK |
300 | group = 'GROUP BY "video"."id"' |
301 | } | |
5f3e2425 C |
302 | } |
303 | ||
304 | if (options.historyOfUser) { | |
3d4e112d | 305 | joins.push('INNER JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId"') |
5f3e2425 C |
306 | |
307 | and.push('"userVideoHistory"."userId" = :historyOfUser') | |
6b842050 | 308 | replacements.historyOfUser = options.historyOfUser.id |
5f3e2425 C |
309 | } |
310 | ||
311 | if (options.startDate) { | |
312 | and.push('"video"."publishedAt" >= :startDate') | |
313 | replacements.startDate = options.startDate | |
314 | } | |
315 | ||
316 | if (options.endDate) { | |
317 | and.push('"video"."publishedAt" <= :endDate') | |
318 | replacements.endDate = options.endDate | |
319 | } | |
320 | ||
321 | if (options.originallyPublishedStartDate) { | |
322 | and.push('"video"."originallyPublishedAt" >= :originallyPublishedStartDate') | |
323 | replacements.originallyPublishedStartDate = options.originallyPublishedStartDate | |
324 | } | |
325 | ||
326 | if (options.originallyPublishedEndDate) { | |
327 | and.push('"video"."originallyPublishedAt" <= :originallyPublishedEndDate') | |
328 | replacements.originallyPublishedEndDate = options.originallyPublishedEndDate | |
329 | } | |
330 | ||
331 | if (options.durationMin) { | |
332 | and.push('"video"."duration" >= :durationMin') | |
333 | replacements.durationMin = options.durationMin | |
334 | } | |
335 | ||
336 | if (options.durationMax) { | |
337 | and.push('"video"."duration" <= :durationMax') | |
338 | replacements.durationMax = options.durationMax | |
339 | } | |
340 | ||
341 | if (options.search) { | |
342 | const escapedSearch = model.sequelize.escape(options.search) | |
343 | const escapedLikeSearch = model.sequelize.escape('%' + options.search + '%') | |
344 | ||
6b842050 C |
345 | cte.push( |
346 | '"trigramSearch" AS (' + | |
347 | ' SELECT "video"."id", ' + | |
348 | ` similarity(lower(immutable_unaccent("video"."name")), lower(immutable_unaccent(${escapedSearch}))) as similarity ` + | |
349 | ' FROM "video" ' + | |
350 | ' WHERE lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' + | |
351 | ' lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' + | |
352 | ')' | |
353 | ) | |
354 | ||
355 | joins.push('LEFT JOIN "trigramSearch" ON "video"."id" = "trigramSearch"."id"') | |
356 | ||
5f3e2425 | 357 | let base = '(' + |
6b842050 | 358 | ' "trigramSearch"."id" IS NOT NULL OR ' + |
5f3e2425 C |
359 | ' EXISTS (' + |
360 | ' SELECT 1 FROM "videoTag" ' + | |
361 | ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' + | |
362 | ` WHERE lower("tag"."name") = ${escapedSearch} ` + | |
363 | ' AND "video"."id" = "videoTag"."videoId"' + | |
364 | ' )' | |
365 | ||
366 | if (validator.isUUID(options.search)) { | |
367 | base += ` OR "video"."uuid" = ${escapedSearch}` | |
368 | } | |
369 | ||
370 | base += ')' | |
371 | and.push(base) | |
372 | ||
6b842050 | 373 | attributes.push(`COALESCE("trigramSearch"."similarity", 0) as similarity`) |
5f3e2425 C |
374 | } else { |
375 | attributes.push('0 as similarity') | |
376 | } | |
377 | ||
378 | if (options.isCount === true) attributes = [ 'COUNT(*) as "total"' ] | |
379 | ||
6b842050 C |
380 | let suffix = '' |
381 | let order = '' | |
5f3e2425 | 382 | if (options.isCount !== true) { |
5f3e2425 | 383 | |
fab67463 | 384 | if (exists(options.sort)) { |
2fd59d7d C |
385 | if (options.sort === '-originallyPublishedAt' || options.sort === 'originallyPublishedAt') { |
386 | attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"') | |
387 | } | |
388 | ||
811cef14 | 389 | order = buildOrder(options.sort) |
fab67463 C |
390 | suffix += `${order} ` |
391 | } | |
392 | ||
393 | if (exists(options.count)) { | |
394 | const count = parseInt(options.count + '', 10) | |
395 | suffix += `LIMIT ${count} ` | |
396 | } | |
6b842050 | 397 | |
fab67463 C |
398 | if (exists(options.start)) { |
399 | const start = parseInt(options.start + '', 10) | |
400 | suffix += `OFFSET ${start} ` | |
401 | } | |
5f3e2425 C |
402 | } |
403 | ||
6b842050 C |
404 | const cteString = cte.length !== 0 |
405 | ? `WITH ${cte.join(', ')} ` | |
406 | : '' | |
407 | ||
408 | const query = cteString + | |
409 | 'SELECT ' + attributes.join(', ') + ' ' + | |
410 | 'FROM "video" ' + joins.join(' ') + ' ' + | |
411 | 'WHERE ' + and.join(' AND ') + ' ' + | |
412 | group + ' ' + | |
413 | having + ' ' + | |
414 | suffix | |
415 | ||
416 | return { query, replacements, order } | |
5f3e2425 C |
417 | } |
418 | ||
811cef14 | 419 | function buildOrder (value: string) { |
5f3e2425 | 420 | const { direction, field } = buildDirectionAndField(value) |
6b842050 | 421 | if (field.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + field) |
5f3e2425 C |
422 | |
423 | if (field.toLowerCase() === 'random') return 'ORDER BY RANDOM()' | |
424 | ||
3d4e112d | 425 | if ([ 'trending', 'hot', 'best' ].includes(field.toLowerCase())) { // Sort by aggregation |
5bcbcbe3 | 426 | return `ORDER BY "score" ${direction}, "video"."views" ${direction}` |
5f3e2425 C |
427 | } |
428 | ||
429 | let firstSort: string | |
430 | ||
431 | if (field.toLowerCase() === 'match') { // Search | |
432 | firstSort = '"similarity"' | |
2fd59d7d C |
433 | } else if (field === 'originallyPublishedAt') { |
434 | firstSort = '"publishedAtForOrder"' | |
6b842050 C |
435 | } else if (field.includes('.')) { |
436 | firstSort = field | |
5f3e2425 C |
437 | } else { |
438 | firstSort = `"video"."${field}"` | |
439 | } | |
440 | ||
441 | return `ORDER BY ${firstSort} ${direction}, "video"."id" ASC` | |
442 | } | |
443 | ||
6b842050 C |
444 | function wrapForAPIResults (baseQuery: string, replacements: any, options: BuildVideosQueryOptions, order: string) { |
445 | const attributes = { | |
446 | '"video".*': '', | |
447 | '"VideoChannel"."id"': '"VideoChannel.id"', | |
448 | '"VideoChannel"."name"': '"VideoChannel.name"', | |
449 | '"VideoChannel"."description"': '"VideoChannel.description"', | |
450 | '"VideoChannel"."actorId"': '"VideoChannel.actorId"', | |
451 | '"VideoChannel->Actor"."id"': '"VideoChannel.Actor.id"', | |
452 | '"VideoChannel->Actor"."preferredUsername"': '"VideoChannel.Actor.preferredUsername"', | |
453 | '"VideoChannel->Actor"."url"': '"VideoChannel.Actor.url"', | |
454 | '"VideoChannel->Actor"."serverId"': '"VideoChannel.Actor.serverId"', | |
455 | '"VideoChannel->Actor"."avatarId"': '"VideoChannel.Actor.avatarId"', | |
456 | '"VideoChannel->Account"."id"': '"VideoChannel.Account.id"', | |
457 | '"VideoChannel->Account"."name"': '"VideoChannel.Account.name"', | |
458 | '"VideoChannel->Account->Actor"."id"': '"VideoChannel.Account.Actor.id"', | |
459 | '"VideoChannel->Account->Actor"."preferredUsername"': '"VideoChannel.Account.Actor.preferredUsername"', | |
460 | '"VideoChannel->Account->Actor"."url"': '"VideoChannel.Account.Actor.url"', | |
461 | '"VideoChannel->Account->Actor"."serverId"': '"VideoChannel.Account.Actor.serverId"', | |
462 | '"VideoChannel->Account->Actor"."avatarId"': '"VideoChannel.Account.Actor.avatarId"', | |
463 | '"VideoChannel->Actor->Server"."id"': '"VideoChannel.Actor.Server.id"', | |
464 | '"VideoChannel->Actor->Server"."host"': '"VideoChannel.Actor.Server.host"', | |
465 | '"VideoChannel->Actor->Avatar"."id"': '"VideoChannel.Actor.Avatar.id"', | |
466 | '"VideoChannel->Actor->Avatar"."filename"': '"VideoChannel.Actor.Avatar.filename"', | |
467 | '"VideoChannel->Actor->Avatar"."fileUrl"': '"VideoChannel.Actor.Avatar.fileUrl"', | |
468 | '"VideoChannel->Actor->Avatar"."onDisk"': '"VideoChannel.Actor.Avatar.onDisk"', | |
469 | '"VideoChannel->Actor->Avatar"."createdAt"': '"VideoChannel.Actor.Avatar.createdAt"', | |
470 | '"VideoChannel->Actor->Avatar"."updatedAt"': '"VideoChannel.Actor.Avatar.updatedAt"', | |
471 | '"VideoChannel->Account->Actor->Server"."id"': '"VideoChannel.Account.Actor.Server.id"', | |
472 | '"VideoChannel->Account->Actor->Server"."host"': '"VideoChannel.Account.Actor.Server.host"', | |
473 | '"VideoChannel->Account->Actor->Avatar"."id"': '"VideoChannel.Account.Actor.Avatar.id"', | |
474 | '"VideoChannel->Account->Actor->Avatar"."filename"': '"VideoChannel.Account.Actor.Avatar.filename"', | |
475 | '"VideoChannel->Account->Actor->Avatar"."fileUrl"': '"VideoChannel.Account.Actor.Avatar.fileUrl"', | |
476 | '"VideoChannel->Account->Actor->Avatar"."onDisk"': '"VideoChannel.Account.Actor.Avatar.onDisk"', | |
477 | '"VideoChannel->Account->Actor->Avatar"."createdAt"': '"VideoChannel.Account.Actor.Avatar.createdAt"', | |
478 | '"VideoChannel->Account->Actor->Avatar"."updatedAt"': '"VideoChannel.Account.Actor.Avatar.updatedAt"', | |
479 | '"Thumbnails"."id"': '"Thumbnails.id"', | |
480 | '"Thumbnails"."type"': '"Thumbnails.type"', | |
481 | '"Thumbnails"."filename"': '"Thumbnails.filename"' | |
482 | } | |
483 | ||
484 | const joins = [ | |
485 | 'INNER JOIN "video" ON "tmp"."id" = "video"."id"', | |
486 | ||
487 | 'INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"', | |
488 | 'INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."actorId" = "VideoChannel->Actor"."id"', | |
489 | 'INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"', | |
490 | 'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."actorId" = "VideoChannel->Account->Actor"."id"', | |
491 | ||
492 | 'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"', | |
493 | 'LEFT OUTER JOIN "avatar" AS "VideoChannel->Actor->Avatar" ON "VideoChannel->Actor"."avatarId" = "VideoChannel->Actor->Avatar"."id"', | |
494 | ||
495 | 'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' + | |
496 | 'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"', | |
497 | ||
498 | 'LEFT OUTER JOIN "avatar" AS "VideoChannel->Account->Actor->Avatar" ' + | |
499 | 'ON "VideoChannel->Account->Actor"."avatarId" = "VideoChannel->Account->Actor->Avatar"."id"', | |
500 | ||
501 | 'LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"' | |
502 | ] | |
503 | ||
504 | if (options.withFiles) { | |
97816649 C |
505 | joins.push('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"') |
506 | ||
507 | joins.push('LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"') | |
508 | joins.push( | |
509 | 'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' + | |
510 | 'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"' | |
511 | ) | |
6b842050 C |
512 | |
513 | Object.assign(attributes, { | |
514 | '"VideoFiles"."id"': '"VideoFiles.id"', | |
515 | '"VideoFiles"."createdAt"': '"VideoFiles.createdAt"', | |
516 | '"VideoFiles"."updatedAt"': '"VideoFiles.updatedAt"', | |
517 | '"VideoFiles"."resolution"': '"VideoFiles.resolution"', | |
518 | '"VideoFiles"."size"': '"VideoFiles.size"', | |
519 | '"VideoFiles"."extname"': '"VideoFiles.extname"', | |
90a8bd30 C |
520 | '"VideoFiles"."filename"': '"VideoFiles.filename"', |
521 | '"VideoFiles"."fileUrl"': '"VideoFiles.fileUrl"', | |
522 | '"VideoFiles"."torrentFilename"': '"VideoFiles.torrentFilename"', | |
523 | '"VideoFiles"."torrentUrl"': '"VideoFiles.torrentUrl"', | |
6b842050 C |
524 | '"VideoFiles"."infoHash"': '"VideoFiles.infoHash"', |
525 | '"VideoFiles"."fps"': '"VideoFiles.fps"', | |
97816649 C |
526 | '"VideoFiles"."videoId"': '"VideoFiles.videoId"', |
527 | ||
528 | '"VideoStreamingPlaylists"."id"': '"VideoStreamingPlaylists.id"', | |
7a499487 C |
529 | '"VideoStreamingPlaylists"."playlistUrl"': '"VideoStreamingPlaylists.playlistUrl"', |
530 | '"VideoStreamingPlaylists"."type"': '"VideoStreamingPlaylists.type"', | |
97816649 C |
531 | '"VideoStreamingPlaylists->VideoFiles"."id"': '"VideoStreamingPlaylists.VideoFiles.id"', |
532 | '"VideoStreamingPlaylists->VideoFiles"."createdAt"': '"VideoStreamingPlaylists.VideoFiles.createdAt"', | |
533 | '"VideoStreamingPlaylists->VideoFiles"."updatedAt"': '"VideoStreamingPlaylists.VideoFiles.updatedAt"', | |
534 | '"VideoStreamingPlaylists->VideoFiles"."resolution"': '"VideoStreamingPlaylists.VideoFiles.resolution"', | |
535 | '"VideoStreamingPlaylists->VideoFiles"."size"': '"VideoStreamingPlaylists.VideoFiles.size"', | |
536 | '"VideoStreamingPlaylists->VideoFiles"."extname"': '"VideoStreamingPlaylists.VideoFiles.extname"', | |
90a8bd30 C |
537 | '"VideoStreamingPlaylists->VideoFiles"."filename"': '"VideoStreamingPlaylists.VideoFiles.filename"', |
538 | '"VideoStreamingPlaylists->VideoFiles"."fileUrl"': '"VideoStreamingPlaylists.VideoFiles.fileUrl"', | |
539 | '"VideoStreamingPlaylists->VideoFiles"."torrentFilename"': '"VideoStreamingPlaylists.VideoFiles.torrentFilename"', | |
540 | '"VideoStreamingPlaylists->VideoFiles"."torrentUrl"': '"VideoStreamingPlaylists.VideoFiles.torrentUrl"', | |
97816649 C |
541 | '"VideoStreamingPlaylists->VideoFiles"."infoHash"': '"VideoStreamingPlaylists.VideoFiles.infoHash"', |
542 | '"VideoStreamingPlaylists->VideoFiles"."fps"': '"VideoStreamingPlaylists.VideoFiles.fps"', | |
7a499487 | 543 | '"VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId"': '"VideoStreamingPlaylists.VideoFiles.videoStreamingPlaylistId"', |
97816649 | 544 | '"VideoStreamingPlaylists->VideoFiles"."videoId"': '"VideoStreamingPlaylists.VideoFiles.videoId"' |
6b842050 C |
545 | }) |
546 | } | |
547 | ||
548 | if (options.user) { | |
549 | joins.push( | |
550 | 'LEFT OUTER JOIN "userVideoHistory" ' + | |
551 | 'ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :userVideoHistoryId' | |
552 | ) | |
553 | replacements.userVideoHistoryId = options.user.id | |
554 | ||
555 | Object.assign(attributes, { | |
556 | '"userVideoHistory"."id"': '"userVideoHistory.id"', | |
557 | '"userVideoHistory"."currentTime"': '"userVideoHistory.currentTime"' | |
558 | }) | |
559 | } | |
560 | ||
561 | if (options.videoPlaylistId) { | |
562 | joins.push( | |
563 | 'INNER JOIN "videoPlaylistElement" as "VideoPlaylistElement" ON "videoPlaylistElement"."videoId" = "video"."id" ' + | |
564 | 'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId' | |
565 | ) | |
566 | replacements.videoPlaylistId = options.videoPlaylistId | |
567 | ||
568 | Object.assign(attributes, { | |
569 | '"VideoPlaylistElement"."createdAt"': '"VideoPlaylistElement.createdAt"', | |
570 | '"VideoPlaylistElement"."updatedAt"': '"VideoPlaylistElement.updatedAt"', | |
571 | '"VideoPlaylistElement"."url"': '"VideoPlaylistElement.url"', | |
572 | '"VideoPlaylistElement"."position"': '"VideoPlaylistElement.position"', | |
573 | '"VideoPlaylistElement"."startTimestamp"': '"VideoPlaylistElement.startTimestamp"', | |
574 | '"VideoPlaylistElement"."stopTimestamp"': '"VideoPlaylistElement.stopTimestamp"', | |
575 | '"VideoPlaylistElement"."videoPlaylistId"': '"VideoPlaylistElement.videoPlaylistId"' | |
576 | }) | |
577 | } | |
578 | ||
579 | const select = 'SELECT ' + Object.keys(attributes).map(key => { | |
580 | const value = attributes[key] | |
581 | if (value) return `${key} AS ${value}` | |
582 | ||
583 | return key | |
584 | }).join(', ') | |
585 | ||
586 | return `${select} FROM (${baseQuery}) AS "tmp" ${joins.join(' ')} ${order}` | |
587 | } | |
588 | ||
5f3e2425 | 589 | export { |
6b842050 C |
590 | buildListQuery, |
591 | wrapForAPIResults | |
5f3e2425 | 592 | } |