1 import { Sequelize } from 'sequelize'
2 import validator from 'validator'
3 import { exists } from '@server/helpers/custom-validators/misc'
4 import { buildDirectionAndField, createSafeIn } from '@server/models/utils'
5 import { MUserAccountId, MUserId } from '@server/types/models'
6 import { VideoFilter, VideoPrivacy, VideoState } from '@shared/models'
7 import { AbstractVideosQueryBuilder } from './abstract-videos-query-builder'
9 export type BuildVideosListQueryOptions = {
12 serverAccountId: number
13 followerActorId: number
14 includeLocalVideos: boolean
24 categoryOneOf?: number[]
25 licenceOneOf?: number[]
26 languageOneOf?: string[]
33 videoChannelId?: number
35 videoPlaylistId?: number
37 trendingAlgorithm?: string // best, hot, or any other algorithm implemented
41 historyOfUser?: MUserId
43 startDate?: string // ISO 8601
44 endDate?: string // ISO 8601
45 originallyPublishedStartDate?: string
46 originallyPublishedEndDate?: string
48 durationMin?: number // seconds
49 durationMax?: number // seconds
59 export class VideosIdListQueryBuilder extends AbstractVideosQueryBuilder {
60 private attributes: string[]
62 protected replacements: any = {}
63 private readonly and: string[] = []
64 private joins: string[] = []
66 private readonly cte: string[] = []
75 constructor (protected readonly sequelize: Sequelize) {
79 queryVideoIds (options: BuildVideosListQueryOptions) {
80 this.buildIdsListQuery(options)
82 return this.runQuery()
85 countVideoIds (countOptions: BuildVideosListQueryOptions): Promise<number> {
86 this.buildIdsListQuery(countOptions)
88 return this.runQuery().then(rows => rows.length !== 0 ? rows[0].total : 0)
91 getIdsListQueryAndSort (options: BuildVideosListQueryOptions) {
92 this.buildIdsListQuery(options)
93 return { query: this.query, sort: this.sort, replacements: this.replacements }
96 private buildIdsListQuery (options: BuildVideosListQueryOptions) {
97 this.attributes = options.attributes || [ '"video"."id"' ]
99 if (options.group) this.group = options.group
100 if (options.having) this.having = options.having
102 this.joins = this.joins.concat([
103 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId"',
104 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId"',
105 'INNER JOIN "actor" "accountActor" ON "account"."actorId" = "accountActor"."id"'
108 this.whereNotBlacklisted()
110 if (options.serverAccountId) {
111 this.whereNotBlocked(options.serverAccountId, options.user)
114 // Only list public/published videos
115 if (!options.filter || (options.filter !== 'all-local' && options.filter !== 'all')) {
116 this.whereStateAndPrivacyAvailable(options.user)
119 if (options.videoPlaylistId) {
120 this.joinPlaylist(options.videoPlaylistId)
123 if (options.filter && (options.filter === 'local' || options.filter === 'all-local')) {
124 this.whereOnlyLocal()
127 if (options.accountId) {
128 this.whereAccountId(options.accountId)
131 if (options.videoChannelId) {
132 this.whereChannelId(options.videoChannelId)
135 if (options.followerActorId) {
136 this.whereFollowerActorId(options.followerActorId, options.includeLocalVideos)
139 if (options.withFiles === true) {
140 this.whereFileExists()
143 if (options.tagsOneOf) {
144 this.whereTagsOneOf(options.tagsOneOf)
147 if (options.tagsAllOf) {
148 this.whereTagsAllOf(options.tagsAllOf)
151 if (options.nsfw === true) {
153 } else if (options.nsfw === false) {
157 if (options.isLive === true) {
159 } else if (options.isLive === false) {
163 if (options.categoryOneOf) {
164 this.whereCategoryOneOf(options.categoryOneOf)
167 if (options.licenceOneOf) {
168 this.whereLicenceOneOf(options.licenceOneOf)
171 if (options.languageOneOf) {
172 this.whereLanguageOneOf(options.languageOneOf)
175 // We don't exclude results in this so if we do a count we don't need to add this complex clause
176 if (options.isCount !== true) {
177 if (options.trendingDays) {
178 this.groupForTrending(options.trendingDays)
179 } else if ([ 'best', 'hot' ].includes(options.trendingAlgorithm)) {
180 this.groupForHotOrBest(options.trendingAlgorithm, options.user)
184 if (options.historyOfUser) {
185 this.joinHistory(options.historyOfUser.id)
188 if (options.startDate) {
189 this.whereStartDate(options.startDate)
192 if (options.endDate) {
193 this.whereEndDate(options.endDate)
196 if (options.originallyPublishedStartDate) {
197 this.whereOriginallyPublishedStartDate(options.originallyPublishedStartDate)
200 if (options.originallyPublishedEndDate) {
201 this.whereOriginallyPublishedEndDate(options.originallyPublishedEndDate)
204 if (options.durationMin) {
205 this.whereDurationMin(options.durationMin)
208 if (options.durationMax) {
209 this.whereDurationMax(options.durationMax)
212 this.whereSearch(options.search)
214 if (options.isCount === true) {
215 this.setCountAttribute()
217 if (exists(options.sort)) {
218 this.setSort(options.sort)
221 if (exists(options.count)) {
222 this.setLimit(options.count)
225 if (exists(options.start)) {
226 this.setOffset(options.start)
230 const cteString = this.cte.length !== 0
231 ? `WITH ${this.cte.join(', ')} `
234 this.query = cteString +
235 'SELECT ' + this.attributes.join(', ') + ' ' +
236 'FROM "video" ' + this.joins.join(' ') + ' ' +
237 'WHERE ' + this.and.join(' AND ') + ' ' +
245 private setCountAttribute () {
246 this.attributes = [ 'COUNT(*) as "total"' ]
249 private joinHistory (userId: number) {
250 this.joins.push('INNER JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId"')
252 this.and.push('"userVideoHistory"."userId" = :historyOfUser')
254 this.replacements.historyOfUser = userId
257 private joinPlaylist (playlistId: number) {
259 'INNER JOIN "videoPlaylistElement" "video"."id" = "videoPlaylistElement"."videoId" ' +
260 'AND "videoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
263 this.replacements.videoPlaylistId = playlistId
266 private whereStateAndPrivacyAvailable (user?: MUserAccountId) {
268 `("video"."state" = ${VideoState.PUBLISHED} OR ` +
269 `("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false))`
274 `("video"."privacy" = ${VideoPrivacy.PUBLIC} OR "video"."privacy" = ${VideoPrivacy.INTERNAL})`
276 } else { // Or only public videos
278 `"video"."privacy" = ${VideoPrivacy.PUBLIC}`
283 private whereOnlyLocal () {
284 this.and.push('"video"."remote" IS FALSE')
287 private whereAccountId (accountId: number) {
288 this.and.push('"account"."id" = :accountId')
289 this.replacements.accountId = accountId
292 private whereChannelId (channelId: number) {
293 this.and.push('"videoChannel"."id" = :videoChannelId')
294 this.replacements.videoChannelId = channelId
297 private whereFollowerActorId (followerActorId: number, includeLocalVideos: boolean) {
301 ' SELECT 1 FROM "videoShare" ' +
302 ' INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' +
303 ' AND "actorFollowShare"."actorId" = :followerActorId AND "actorFollowShare"."state" = \'accepted\' ' +
304 ' WHERE "videoShare"."videoId" = "video"."id"' +
308 ' SELECT 1 from "actorFollow" ' +
309 ' WHERE "actorFollow"."targetActorId" = "videoChannel"."actorId" AND "actorFollow"."actorId" = :followerActorId ' +
310 ' AND "actorFollow"."state" = \'accepted\'' +
313 if (includeLocalVideos) {
314 query += ' OR "video"."remote" IS FALSE'
320 this.replacements.followerActorId = followerActorId
323 private whereFileExists () {
326 ' EXISTS (SELECT 1 FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id") ' +
328 ' SELECT 1 FROM "videoStreamingPlaylist" ' +
329 ' INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ' +
330 ' WHERE "videoStreamingPlaylist"."videoId" = "video"."id"' +
336 private whereTagsOneOf (tagsOneOf: string[]) {
337 const tagsOneOfLower = tagsOneOf.map(t => t.toLowerCase())
341 ' SELECT 1 FROM "videoTag" ' +
342 ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
343 ' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsOneOfLower) + ') ' +
344 ' AND "video"."id" = "videoTag"."videoId"' +
349 private whereTagsAllOf (tagsAllOf: string[]) {
350 const tagsAllOfLower = tagsAllOf.map(t => t.toLowerCase())
354 ' SELECT 1 FROM "videoTag" ' +
355 ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
356 ' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsAllOfLower) + ') ' +
357 ' AND "video"."id" = "videoTag"."videoId" ' +
358 ' GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length +
363 private whereCategoryOneOf (categoryOneOf: number[]) {
364 this.and.push('"video"."category" IN (:categoryOneOf)')
365 this.replacements.categoryOneOf = categoryOneOf
368 private whereLicenceOneOf (licenceOneOf: number[]) {
369 this.and.push('"video"."licence" IN (:licenceOneOf)')
370 this.replacements.licenceOneOf = licenceOneOf
373 private whereLanguageOneOf (languageOneOf: string[]) {
374 const languages = languageOneOf.filter(l => l && l !== '_unknown')
375 const languagesQueryParts: string[] = []
377 if (languages.length !== 0) {
378 languagesQueryParts.push('"video"."language" IN (:languageOneOf)')
379 this.replacements.languageOneOf = languages
381 languagesQueryParts.push(
383 ' SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' +
384 ' IN (' + createSafeIn(this.sequelize, languages) + ') AND ' +
385 ' "videoCaption"."videoId" = "video"."id"' +
390 if (languageOneOf.includes('_unknown')) {
391 languagesQueryParts.push('"video"."language" IS NULL')
394 if (languagesQueryParts.length !== 0) {
395 this.and.push('(' + languagesQueryParts.join(' OR ') + ')')
399 private whereNSFW () {
400 this.and.push('"video"."nsfw" IS TRUE')
403 private whereSFW () {
404 this.and.push('"video"."nsfw" IS FALSE')
407 private whereLive () {
408 this.and.push('"video"."isLive" IS TRUE')
411 private whereVOD () {
412 this.and.push('"video"."isLive" IS FALSE')
415 private whereNotBlocked (serverAccountId: number, user?: MUserAccountId) {
416 const blockerIds = [ serverAccountId ]
417 if (user) blockerIds.push(user.Account.id)
419 const inClause = createSafeIn(this.sequelize, blockerIds)
423 ' SELECT 1 FROM "accountBlocklist" ' +
424 ' WHERE "accountBlocklist"."accountId" IN (' + inClause + ') ' +
425 ' AND "accountBlocklist"."targetAccountId" = "account"."id" ' +
428 ' SELECT 1 FROM "serverBlocklist" WHERE "serverBlocklist"."accountId" IN (' + inClause + ') ' +
429 ' AND "serverBlocklist"."targetServerId" = "accountActor"."serverId"' +
434 private whereSearch (search?: string) {
436 this.attributes.push('0 as similarity')
440 const escapedSearch = this.sequelize.escape(search)
441 const escapedLikeSearch = this.sequelize.escape('%' + search + '%')
444 '"trigramSearch" AS (' +
445 ' SELECT "video"."id", ' +
446 ` similarity(lower(immutable_unaccent("video"."name")), lower(immutable_unaccent(${escapedSearch}))) as similarity ` +
448 ' WHERE lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
449 ' lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
453 this.joins.push('LEFT JOIN "trigramSearch" ON "video"."id" = "trigramSearch"."id"')
456 ' "trigramSearch"."id" IS NOT NULL OR ' +
458 ' SELECT 1 FROM "videoTag" ' +
459 ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
460 ` WHERE lower("tag"."name") = ${escapedSearch} ` +
461 ' AND "video"."id" = "videoTag"."videoId"' +
464 if (validator.isUUID(search)) {
465 base += ` OR "video"."uuid" = ${escapedSearch}`
471 this.attributes.push(`COALESCE("trigramSearch"."similarity", 0) as similarity`)
474 private whereNotBlacklisted () {
475 this.and.push('"video"."id" NOT IN (SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")')
478 private whereStartDate (startDate: string) {
479 this.and.push('"video"."publishedAt" >= :startDate')
480 this.replacements.startDate = startDate
483 private whereEndDate (endDate: string) {
484 this.and.push('"video"."publishedAt" <= :endDate')
485 this.replacements.endDate = endDate
488 private whereOriginallyPublishedStartDate (startDate: string) {
489 this.and.push('"video"."originallyPublishedAt" >= :originallyPublishedStartDate')
490 this.replacements.originallyPublishedStartDate = startDate
493 private whereOriginallyPublishedEndDate (endDate: string) {
494 this.and.push('"video"."originallyPublishedAt" <= :originallyPublishedEndDate')
495 this.replacements.originallyPublishedEndDate = endDate
498 private whereDurationMin (durationMin: number) {
499 this.and.push('"video"."duration" >= :durationMin')
500 this.replacements.durationMin = durationMin
503 private whereDurationMax (durationMax: number) {
504 this.and.push('"video"."duration" <= :durationMax')
505 this.replacements.durationMax = durationMax
508 private groupForTrending (trendingDays: number) {
509 const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
511 this.joins.push('LEFT JOIN "videoView" ON "video"."id" = "videoView"."videoId" AND "videoView"."startDate" >= :viewsGteDate')
512 this.replacements.viewsGteDate = viewsGteDate
514 this.attributes.push('COALESCE(SUM("videoView"."views"), 0) AS "score"')
516 this.group = 'GROUP BY "video"."id"'
519 private groupForHotOrBest (trendingAlgorithm: string, user?: MUserAccountId) {
521 * "Hotness" is a measure based on absolute view/comment/like/dislike numbers,
522 * with fixed weights only applied to their log values.
524 * This algorithm gives little chance for an old video to have a good score,
525 * for which recent spikes in interactions could be a sign of "hotness" and
526 * justify a better score. However there are multiple ways to achieve that
527 * goal, which is left for later. Yes, this is a TODO :)
530 * - weights and base score are in number of half-days.
531 * - all comments are counted, regardless of being written by the video author or not
532 * see https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/_sorts.pyx#L47-L58
533 * - we have less interactions than on reddit, so multiply weights by an arbitrary factor
538 view: Math.floor((1 / 3) * 50),
539 comment: 2 * 50, // a comment takes more time than a like to do, but can be done multiple times
543 this.joins.push('LEFT JOIN "videoComment" ON "video"."id" = "videoComment"."videoId"')
546 `LOG(GREATEST(1, "video"."likes" - 1)) * ${weights.like} ` + // likes (+)
547 `+ LOG(GREATEST(1, "video"."dislikes" - 1)) * ${weights.dislike} ` + // dislikes (-)
548 `+ LOG("video"."views" + 1) * ${weights.view} ` + // views (+)
549 `+ LOG(GREATEST(1, COUNT(DISTINCT "videoComment"."id"))) * ${weights.comment} ` + // comments (+)
550 '+ (SELECT (EXTRACT(epoch FROM "video"."publishedAt") - 1446156582) / 47000) ' // base score (in number of half-days)
552 if (trendingAlgorithm === 'best' && user) {
554 'LEFT JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :bestUser'
556 this.replacements.bestUser = user.id
558 attribute += `+ POWER(COUNT(DISTINCT "userVideoHistory"."id"), 2.0) * ${weights.history} `
561 attribute += 'AS "score"'
562 this.attributes.push(attribute)
564 this.group = 'GROUP BY "video"."id"'
567 private setSort (sort: string) {
568 if (sort === '-originallyPublishedAt' || sort === 'originallyPublishedAt') {
569 this.attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"')
572 this.sort = this.buildOrder(sort)
575 private buildOrder (value: string) {
576 const { direction, field } = buildDirectionAndField(value)
577 if (field.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + field)
579 if (field.toLowerCase() === 'random') return 'ORDER BY RANDOM()'
581 if ([ 'trending', 'hot', 'best' ].includes(field.toLowerCase())) { // Sort by aggregation
582 return `ORDER BY "score" ${direction}, "video"."views" ${direction}`
585 let firstSort: string
587 if (field.toLowerCase() === 'match') { // Search
588 firstSort = '"similarity"'
589 } else if (field === 'originallyPublishedAt') {
590 firstSort = '"publishedAtForOrder"'
591 } else if (field.includes('.')) {
594 firstSort = `"video"."${field}"`
597 return `ORDER BY ${firstSort} ${direction}, "video"."id" ASC`
600 private setLimit (countArg: number) {
601 const count = parseInt(countArg + '', 10)
602 this.limit = `LIMIT ${count}`
605 private setOffset (startArg: number) {
606 const start = parseInt(startArg + '', 10)
607 this.offset = `OFFSET ${start}`