1 import { Sequelize } from 'sequelize'
2 import validator from 'validator'
3 import { exists } from '@server/helpers/custom-validators/misc'
4 import { WEBSERVER } from '@server/initializers/constants'
5 import { buildDirectionAndField, createSafeIn } from '@server/models/utils'
6 import { MUserAccountId, MUserId } from '@server/types/models'
7 import { VideoInclude, VideoPrivacy, VideoState } from '@shared/models'
8 import { AbstractRunQuery } from '../../../shared/abstract-run-query'
12 * Build videos list SQL query to fetch rows
16 export type DisplayOnlyForFollowerOptions = {
18 orLocalVideos: boolean
21 export type BuildVideosListQueryOptions = {
24 serverAccountIdForBlock: number
26 displayOnlyForFollower: DisplayOnlyForFollowerOptions
36 include?: VideoInclude
38 categoryOneOf?: number[]
39 licenceOneOf?: number[]
40 languageOneOf?: string[]
43 privacyOneOf?: VideoPrivacy[]
49 hasWebtorrentFiles?: boolean
52 videoChannelId?: number
54 videoPlaylistId?: number
56 trendingAlgorithm?: string // best, hot, or any other algorithm implemented
60 historyOfUser?: MUserId
62 startDate?: string // ISO 8601
63 endDate?: string // ISO 8601
64 originallyPublishedStartDate?: string
65 originallyPublishedEndDate?: string
67 durationMin?: number // seconds
68 durationMax?: number // seconds
78 export class VideosIdListQueryBuilder extends AbstractRunQuery {
79 protected replacements: any = {}
81 private attributes: string[]
82 private joins: string[] = []
84 private readonly and: string[] = []
86 private readonly cte: string[] = []
95 constructor (protected readonly sequelize: Sequelize) {
99 queryVideoIds (options: BuildVideosListQueryOptions) {
100 this.buildIdsListQuery(options)
102 return this.runQuery()
105 countVideoIds (countOptions: BuildVideosListQueryOptions): Promise<number> {
106 this.buildIdsListQuery(countOptions)
108 return this.runQuery().then(rows => rows.length !== 0 ? rows[0].total : 0)
111 getQuery (options: BuildVideosListQueryOptions) {
112 this.buildIdsListQuery(options)
114 return { query: this.query, sort: this.sort, replacements: this.replacements }
117 private buildIdsListQuery (options: BuildVideosListQueryOptions) {
118 this.attributes = options.attributes || [ '"video"."id"' ]
120 if (options.group) this.group = options.group
121 if (options.having) this.having = options.having
123 this.joins = this.joins.concat([
124 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId"',
125 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId"',
126 'INNER JOIN "actor" "accountActor" ON "account"."actorId" = "accountActor"."id"'
129 if (!(options.include & VideoInclude.BLACKLISTED)) {
130 this.whereNotBlacklisted()
133 if (options.serverAccountIdForBlock && !(options.include & VideoInclude.BLOCKED_OWNER)) {
134 this.whereNotBlocked(options.serverAccountIdForBlock, options.user)
137 // Only list published videos
138 if (!(options.include & VideoInclude.NOT_PUBLISHED_STATE)) {
139 this.whereStateAvailable()
142 if (options.videoPlaylistId) {
143 this.joinPlaylist(options.videoPlaylistId)
146 if (exists(options.isLocal)) {
147 this.whereLocal(options.isLocal)
151 this.whereHost(options.host)
154 if (options.accountId) {
155 this.whereAccountId(options.accountId)
158 if (options.videoChannelId) {
159 this.whereChannelId(options.videoChannelId)
162 if (options.displayOnlyForFollower) {
163 this.whereFollowerActorId(options.displayOnlyForFollower)
166 if (options.hasFiles === true) {
167 this.whereFileExists()
170 if (exists(options.hasWebtorrentFiles)) {
171 this.whereWebTorrentFileExists(options.hasWebtorrentFiles)
174 if (exists(options.hasHLSFiles)) {
175 this.whereHLSFileExists(options.hasHLSFiles)
178 if (options.tagsOneOf) {
179 this.whereTagsOneOf(options.tagsOneOf)
182 if (options.tagsAllOf) {
183 this.whereTagsAllOf(options.tagsAllOf)
186 if (options.privacyOneOf) {
187 this.wherePrivacyOneOf(options.privacyOneOf)
189 // Only list videos with the appropriate priavcy
190 this.wherePrivacyAvailable(options.user)
194 this.whereUUIDs(options.uuids)
197 if (options.nsfw === true) {
199 } else if (options.nsfw === false) {
203 if (options.isLive === true) {
205 } else if (options.isLive === false) {
209 if (options.categoryOneOf) {
210 this.whereCategoryOneOf(options.categoryOneOf)
213 if (options.licenceOneOf) {
214 this.whereLicenceOneOf(options.licenceOneOf)
217 if (options.languageOneOf) {
218 this.whereLanguageOneOf(options.languageOneOf)
221 // We don't exclude results in this so if we do a count we don't need to add this complex clause
222 if (options.isCount !== true) {
223 if (options.trendingDays) {
224 this.groupForTrending(options.trendingDays)
225 } else if ([ 'best', 'hot' ].includes(options.trendingAlgorithm)) {
226 this.groupForHotOrBest(options.trendingAlgorithm, options.user)
230 if (options.historyOfUser) {
231 this.joinHistory(options.historyOfUser.id)
234 if (options.startDate) {
235 this.whereStartDate(options.startDate)
238 if (options.endDate) {
239 this.whereEndDate(options.endDate)
242 if (options.originallyPublishedStartDate) {
243 this.whereOriginallyPublishedStartDate(options.originallyPublishedStartDate)
246 if (options.originallyPublishedEndDate) {
247 this.whereOriginallyPublishedEndDate(options.originallyPublishedEndDate)
250 if (options.durationMin) {
251 this.whereDurationMin(options.durationMin)
254 if (options.durationMax) {
255 this.whereDurationMax(options.durationMax)
258 this.whereSearch(options.search)
260 if (options.isCount === true) {
261 this.setCountAttribute()
263 if (exists(options.sort)) {
264 this.setSort(options.sort)
267 if (exists(options.count)) {
268 this.setLimit(options.count)
271 if (exists(options.start)) {
272 this.setOffset(options.start)
276 const cteString = this.cte.length !== 0
277 ? `WITH ${this.cte.join(', ')} `
280 this.query = cteString +
281 'SELECT ' + this.attributes.join(', ') + ' ' +
282 'FROM "video" ' + this.joins.join(' ') + ' ' +
283 'WHERE ' + this.and.join(' AND ') + ' ' +
291 private setCountAttribute () {
292 this.attributes = [ 'COUNT(*) as "total"' ]
295 private joinHistory (userId: number) {
296 this.joins.push('INNER JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId"')
298 this.and.push('"userVideoHistory"."userId" = :historyOfUser')
300 this.replacements.historyOfUser = userId
303 private joinPlaylist (playlistId: number) {
305 'INNER JOIN "videoPlaylistElement" "video"."id" = "videoPlaylistElement"."videoId" ' +
306 'AND "videoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
309 this.replacements.videoPlaylistId = playlistId
312 private whereStateAvailable () {
314 `("video"."state" = ${VideoState.PUBLISHED} OR ` +
315 `("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false))`
319 private wherePrivacyAvailable (user?: MUserAccountId) {
322 `("video"."privacy" = ${VideoPrivacy.PUBLIC} OR "video"."privacy" = ${VideoPrivacy.INTERNAL})`
324 } else { // Or only public videos
326 `"video"."privacy" = ${VideoPrivacy.PUBLIC}`
331 private whereLocal (isLocal: boolean) {
332 const isRemote = isLocal ? 'FALSE' : 'TRUE'
334 this.and.push('"video"."remote" IS ' + isRemote)
337 private whereHost (host: string) {
339 if (host === WEBSERVER.HOST) {
340 this.and.push('"accountActor"."serverId" IS NULL')
344 this.joins.push('INNER JOIN "server" ON "server"."id" = "accountActor"."serverId"')
346 this.and.push('"server"."host" = :host')
347 this.replacements.host = host
350 private whereAccountId (accountId: number) {
351 this.and.push('"account"."id" = :accountId')
352 this.replacements.accountId = accountId
355 private whereChannelId (channelId: number) {
356 this.and.push('"videoChannel"."id" = :videoChannelId')
357 this.replacements.videoChannelId = channelId
360 private whereFollowerActorId (options: { actorId: number, orLocalVideos: boolean }) {
363 ' EXISTS (' + // Videos shared by actors we follow
364 ' SELECT 1 FROM "videoShare" ' +
365 ' INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' +
366 ' AND "actorFollowShare"."actorId" = :followerActorId AND "actorFollowShare"."state" = \'accepted\' ' +
367 ' WHERE "videoShare"."videoId" = "video"."id"' +
370 ' EXISTS (' + // Videos published by channels or accounts we follow
371 ' SELECT 1 from "actorFollow" ' +
372 ' WHERE ("actorFollow"."targetActorId" = "account"."actorId" OR "actorFollow"."targetActorId" = "videoChannel"."actorId") ' +
373 ' AND "actorFollow"."actorId" = :followerActorId ' +
374 ' AND "actorFollow"."state" = \'accepted\'' +
377 if (options.orLocalVideos) {
378 query += ' OR "video"."remote" IS FALSE'
384 this.replacements.followerActorId = options.actorId
387 private whereFileExists () {
388 this.and.push(`(${this.buildWebTorrentFileExistsQuery(true)} OR ${this.buildHLSFileExistsQuery(true)})`)
391 private whereWebTorrentFileExists (exists: boolean) {
392 this.and.push(this.buildWebTorrentFileExistsQuery(exists))
395 private whereHLSFileExists (exists: boolean) {
396 this.and.push(this.buildHLSFileExistsQuery(exists))
399 private buildWebTorrentFileExistsQuery (exists: boolean) {
400 const prefix = exists ? '' : 'NOT '
402 return prefix + 'EXISTS (SELECT 1 FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id")'
405 private buildHLSFileExistsQuery (exists: boolean) {
406 const prefix = exists ? '' : 'NOT '
408 return prefix + 'EXISTS (' +
409 ' SELECT 1 FROM "videoStreamingPlaylist" ' +
410 ' INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ' +
411 ' WHERE "videoStreamingPlaylist"."videoId" = "video"."id"' +
415 private whereTagsOneOf (tagsOneOf: string[]) {
416 const tagsOneOfLower = tagsOneOf.map(t => t.toLowerCase())
420 ' SELECT 1 FROM "videoTag" ' +
421 ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
422 ' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsOneOfLower) + ') ' +
423 ' AND "video"."id" = "videoTag"."videoId"' +
428 private whereTagsAllOf (tagsAllOf: string[]) {
429 const tagsAllOfLower = tagsAllOf.map(t => t.toLowerCase())
433 ' SELECT 1 FROM "videoTag" ' +
434 ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
435 ' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsAllOfLower) + ') ' +
436 ' AND "video"."id" = "videoTag"."videoId" ' +
437 ' GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length +
442 private wherePrivacyOneOf (privacyOneOf: VideoPrivacy[]) {
443 this.and.push('"video"."privacy" IN (:privacyOneOf)')
444 this.replacements.privacyOneOf = privacyOneOf
447 private whereUUIDs (uuids: string[]) {
448 this.and.push('"video"."uuid" IN (' + createSafeIn(this.sequelize, uuids) + ')')
451 private whereCategoryOneOf (categoryOneOf: number[]) {
452 this.and.push('"video"."category" IN (:categoryOneOf)')
453 this.replacements.categoryOneOf = categoryOneOf
456 private whereLicenceOneOf (licenceOneOf: number[]) {
457 this.and.push('"video"."licence" IN (:licenceOneOf)')
458 this.replacements.licenceOneOf = licenceOneOf
461 private whereLanguageOneOf (languageOneOf: string[]) {
462 const languages = languageOneOf.filter(l => l && l !== '_unknown')
463 const languagesQueryParts: string[] = []
465 if (languages.length !== 0) {
466 languagesQueryParts.push('"video"."language" IN (:languageOneOf)')
467 this.replacements.languageOneOf = languages
469 languagesQueryParts.push(
471 ' SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' +
472 ' IN (' + createSafeIn(this.sequelize, languages) + ') AND ' +
473 ' "videoCaption"."videoId" = "video"."id"' +
478 if (languageOneOf.includes('_unknown')) {
479 languagesQueryParts.push('"video"."language" IS NULL')
482 if (languagesQueryParts.length !== 0) {
483 this.and.push('(' + languagesQueryParts.join(' OR ') + ')')
487 private whereNSFW () {
488 this.and.push('"video"."nsfw" IS TRUE')
491 private whereSFW () {
492 this.and.push('"video"."nsfw" IS FALSE')
495 private whereLive () {
496 this.and.push('"video"."isLive" IS TRUE')
499 private whereVOD () {
500 this.and.push('"video"."isLive" IS FALSE')
503 private whereNotBlocked (serverAccountId: number, user?: MUserAccountId) {
504 const blockerIds = [ serverAccountId ]
505 if (user) blockerIds.push(user.Account.id)
507 const inClause = createSafeIn(this.sequelize, blockerIds)
511 ' SELECT 1 FROM "accountBlocklist" ' +
512 ' WHERE "accountBlocklist"."accountId" IN (' + inClause + ') ' +
513 ' AND "accountBlocklist"."targetAccountId" = "account"."id" ' +
516 ' SELECT 1 FROM "serverBlocklist" WHERE "serverBlocklist"."accountId" IN (' + inClause + ') ' +
517 ' AND "serverBlocklist"."targetServerId" = "accountActor"."serverId"' +
522 private whereSearch (search?: string) {
524 this.attributes.push('0 as similarity')
528 const escapedSearch = this.sequelize.escape(search)
529 const escapedLikeSearch = this.sequelize.escape('%' + search + '%')
532 '"trigramSearch" AS (' +
533 ' SELECT "video"."id", ' +
534 ` similarity(lower(immutable_unaccent("video"."name")), lower(immutable_unaccent(${escapedSearch}))) as similarity ` +
536 ' WHERE lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
537 ' lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
541 this.joins.push('LEFT JOIN "trigramSearch" ON "video"."id" = "trigramSearch"."id"')
544 ' "trigramSearch"."id" IS NOT NULL OR ' +
546 ' SELECT 1 FROM "videoTag" ' +
547 ' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
548 ` WHERE lower("tag"."name") = lower(${escapedSearch}) ` +
549 ' AND "video"."id" = "videoTag"."videoId"' +
552 if (validator.isUUID(search)) {
553 base += ` OR "video"."uuid" = ${escapedSearch}`
559 this.attributes.push(`COALESCE("trigramSearch"."similarity", 0) as similarity`)
562 private whereNotBlacklisted () {
563 this.and.push('"video"."id" NOT IN (SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")')
566 private whereStartDate (startDate: string) {
567 this.and.push('"video"."publishedAt" >= :startDate')
568 this.replacements.startDate = startDate
571 private whereEndDate (endDate: string) {
572 this.and.push('"video"."publishedAt" <= :endDate')
573 this.replacements.endDate = endDate
576 private whereOriginallyPublishedStartDate (startDate: string) {
577 this.and.push('"video"."originallyPublishedAt" >= :originallyPublishedStartDate')
578 this.replacements.originallyPublishedStartDate = startDate
581 private whereOriginallyPublishedEndDate (endDate: string) {
582 this.and.push('"video"."originallyPublishedAt" <= :originallyPublishedEndDate')
583 this.replacements.originallyPublishedEndDate = endDate
586 private whereDurationMin (durationMin: number) {
587 this.and.push('"video"."duration" >= :durationMin')
588 this.replacements.durationMin = durationMin
591 private whereDurationMax (durationMax: number) {
592 this.and.push('"video"."duration" <= :durationMax')
593 this.replacements.durationMax = durationMax
596 private groupForTrending (trendingDays: number) {
597 const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
599 this.joins.push('LEFT JOIN "videoView" ON "video"."id" = "videoView"."videoId" AND "videoView"."startDate" >= :viewsGteDate')
600 this.replacements.viewsGteDate = viewsGteDate
602 this.attributes.push('COALESCE(SUM("videoView"."views"), 0) AS "score"')
604 this.group = 'GROUP BY "video"."id"'
607 private groupForHotOrBest (trendingAlgorithm: string, user?: MUserAccountId) {
609 * "Hotness" is a measure based on absolute view/comment/like/dislike numbers,
610 * with fixed weights only applied to their log values.
612 * This algorithm gives little chance for an old video to have a good score,
613 * for which recent spikes in interactions could be a sign of "hotness" and
614 * justify a better score. However there are multiple ways to achieve that
615 * goal, which is left for later. Yes, this is a TODO :)
618 * - weights and base score are in number of half-days.
619 * - all comments are counted, regardless of being written by the video author or not
620 * see https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/_sorts.pyx#L47-L58
621 * - we have less interactions than on reddit, so multiply weights by an arbitrary factor
626 view: Math.floor((1 / 3) * 50),
627 comment: 2 * 50, // a comment takes more time than a like to do, but can be done multiple times
631 this.joins.push('LEFT JOIN "videoComment" ON "video"."id" = "videoComment"."videoId"')
634 `LOG(GREATEST(1, "video"."likes" - 1)) * ${weights.like} ` + // likes (+)
635 `+ LOG(GREATEST(1, "video"."dislikes" - 1)) * ${weights.dislike} ` + // dislikes (-)
636 `+ LOG("video"."views" + 1) * ${weights.view} ` + // views (+)
637 `+ LOG(GREATEST(1, COUNT(DISTINCT "videoComment"."id"))) * ${weights.comment} ` + // comments (+)
638 '+ (SELECT (EXTRACT(epoch FROM "video"."publishedAt") - 1446156582) / 47000) ' // base score (in number of half-days)
640 if (trendingAlgorithm === 'best' && user) {
642 'LEFT JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :bestUser'
644 this.replacements.bestUser = user.id
646 attribute += `+ POWER(COUNT(DISTINCT "userVideoHistory"."id"), 2.0) * ${weights.history} `
649 attribute += 'AS "score"'
650 this.attributes.push(attribute)
652 this.group = 'GROUP BY "video"."id"'
655 private setSort (sort: string) {
656 if (sort === '-originallyPublishedAt' || sort === 'originallyPublishedAt') {
657 this.attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"')
660 this.sort = this.buildOrder(sort)
663 private buildOrder (value: string) {
664 const { direction, field } = buildDirectionAndField(value)
665 if (field.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + field)
667 if (field.toLowerCase() === 'random') return 'ORDER BY RANDOM()'
669 if ([ 'trending', 'hot', 'best' ].includes(field.toLowerCase())) { // Sort by aggregation
670 return `ORDER BY "score" ${direction}, "video"."views" ${direction}`
673 let firstSort: string
675 if (field.toLowerCase() === 'match') { // Search
676 firstSort = '"similarity"'
677 } else if (field === 'originallyPublishedAt') {
678 firstSort = '"publishedAtForOrder"'
679 } else if (field.includes('.')) {
682 firstSort = `"video"."${field}"`
685 return `ORDER BY ${firstSort} ${direction}, "video"."id" ASC`
688 private setLimit (countArg: number) {
689 const count = parseInt(countArg + '', 10)
690 this.limit = `LIMIT ${count}`
693 private setOffset (startArg: number) {
694 const start = parseInt(startArg + '', 10)
695 this.offset = `OFFSET ${start}`