2020-03-05 15:04:57 +01:00
|
|
|
import { VideoFilter, VideoPrivacy, VideoState } from '@shared/models'
|
|
|
|
import { buildDirectionAndField, createSafeIn } from '@server/models/utils'
|
|
|
|
import { Model } from 'sequelize-typescript'
|
2020-06-18 10:45:25 +02:00
|
|
|
import { MUserAccountId, MUserId } from '@server/types/models'
|
2020-03-05 15:04:57 +01:00
|
|
|
import validator from 'validator'
|
2020-03-11 08:40:13 +01:00
|
|
|
import { exists } from '@server/helpers/custom-validators/misc'
|
2020-03-05 15:04:57 +01:00
|
|
|
|
|
|
|
export type BuildVideosQueryOptions = {
|
|
|
|
attributes?: string[]
|
|
|
|
|
|
|
|
serverAccountId: number
|
|
|
|
followerActorId: number
|
|
|
|
includeLocalVideos: boolean
|
|
|
|
|
|
|
|
count: number
|
|
|
|
start: number
|
|
|
|
sort: string
|
|
|
|
|
|
|
|
filter?: VideoFilter
|
|
|
|
categoryOneOf?: number[]
|
|
|
|
nsfw?: boolean
|
|
|
|
licenceOneOf?: number[]
|
|
|
|
languageOneOf?: string[]
|
|
|
|
tagsOneOf?: string[]
|
|
|
|
tagsAllOf?: string[]
|
|
|
|
|
|
|
|
withFiles?: boolean
|
|
|
|
|
|
|
|
accountId?: number
|
|
|
|
videoChannelId?: number
|
|
|
|
|
|
|
|
videoPlaylistId?: number
|
|
|
|
|
2021-02-02 12:59:41 +01:00
|
|
|
trendingAlgorithm?: string // best, hot, or any other algorithm implemented
|
2020-03-05 15:04:57 +01:00
|
|
|
trendingDays?: number
|
2021-01-22 00:12:44 +01:00
|
|
|
|
2020-03-05 15:04:57 +01:00
|
|
|
user?: MUserAccountId
|
|
|
|
historyOfUser?: MUserId
|
|
|
|
|
|
|
|
startDate?: string // ISO 8601
|
|
|
|
endDate?: string // ISO 8601
|
|
|
|
originallyPublishedStartDate?: string
|
|
|
|
originallyPublishedEndDate?: string
|
|
|
|
|
|
|
|
durationMin?: number // seconds
|
|
|
|
durationMax?: number // seconds
|
|
|
|
|
|
|
|
search?: string
|
|
|
|
|
|
|
|
isCount?: boolean
|
|
|
|
|
|
|
|
group?: string
|
|
|
|
having?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions) {
|
|
|
|
const and: string[] = []
|
|
|
|
const joins: string[] = []
|
|
|
|
const replacements: any = {}
|
2020-03-09 14:44:44 +01:00
|
|
|
const cte: string[] = []
|
2020-03-05 15:04:57 +01:00
|
|
|
|
|
|
|
let attributes: string[] = options.attributes || [ '"video"."id"' ]
|
|
|
|
let group = options.group || ''
|
|
|
|
const having = options.having || ''
|
|
|
|
|
|
|
|
joins.push(
|
|
|
|
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId"' +
|
|
|
|
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId"' +
|
2020-03-09 14:44:44 +01:00
|
|
|
'INNER JOIN "actor" "accountActor" ON "account"."actorId" = "accountActor"."id"'
|
2020-03-05 15:04:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
and.push('"video"."id" NOT IN (SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")')
|
|
|
|
|
|
|
|
if (options.serverAccountId) {
|
|
|
|
const blockerIds = [ options.serverAccountId ]
|
|
|
|
if (options.user) blockerIds.push(options.user.Account.id)
|
|
|
|
|
2020-03-09 14:44:44 +01:00
|
|
|
const inClause = createSafeIn(model, blockerIds)
|
2020-03-05 15:04:57 +01:00
|
|
|
|
|
|
|
and.push(
|
2020-03-09 14:44:44 +01:00
|
|
|
'NOT EXISTS (' +
|
|
|
|
' SELECT 1 FROM "accountBlocklist" ' +
|
|
|
|
' WHERE "accountBlocklist"."accountId" IN (' + inClause + ') ' +
|
|
|
|
' AND "accountBlocklist"."targetAccountId" = "account"."id" ' +
|
|
|
|
')' +
|
|
|
|
'AND NOT EXISTS (' +
|
|
|
|
' SELECT 1 FROM "serverBlocklist" WHERE "serverBlocklist"."accountId" IN (' + inClause + ') ' +
|
|
|
|
' AND "serverBlocklist"."targetServerId" = "accountActor"."serverId"' +
|
|
|
|
')'
|
2020-03-05 15:04:57 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only list public/published videos
|
2020-11-18 15:29:38 +01:00
|
|
|
if (!options.filter || (options.filter !== 'all-local' && options.filter !== 'all')) {
|
2020-03-05 15:04:57 +01:00
|
|
|
and.push(
|
|
|
|
`("video"."state" = ${VideoState.PUBLISHED} OR ` +
|
|
|
|
`("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false))`
|
|
|
|
)
|
|
|
|
|
|
|
|
if (options.user) {
|
|
|
|
and.push(
|
|
|
|
`("video"."privacy" = ${VideoPrivacy.PUBLIC} OR "video"."privacy" = ${VideoPrivacy.INTERNAL})`
|
|
|
|
)
|
|
|
|
} else { // Or only public videos
|
|
|
|
and.push(
|
|
|
|
`"video"."privacy" = ${VideoPrivacy.PUBLIC}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.videoPlaylistId) {
|
|
|
|
joins.push(
|
|
|
|
'INNER JOIN "videoPlaylistElement" "video"."id" = "videoPlaylistElement"."videoId" ' +
|
|
|
|
'AND "videoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
|
|
|
|
)
|
|
|
|
|
|
|
|
replacements.videoPlaylistId = options.videoPlaylistId
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.filter && (options.filter === 'local' || options.filter === 'all-local')) {
|
|
|
|
and.push('"video"."remote" IS FALSE')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.accountId) {
|
|
|
|
and.push('"account"."id" = :accountId')
|
|
|
|
replacements.accountId = options.accountId
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.videoChannelId) {
|
|
|
|
and.push('"videoChannel"."id" = :videoChannelId')
|
|
|
|
replacements.videoChannelId = options.videoChannelId
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.followerActorId) {
|
|
|
|
let query =
|
|
|
|
'(' +
|
|
|
|
' EXISTS (' +
|
|
|
|
' SELECT 1 FROM "videoShare" ' +
|
|
|
|
' INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' +
|
2020-06-09 15:59:35 +02:00
|
|
|
' AND "actorFollowShare"."actorId" = :followerActorId AND "actorFollowShare"."state" = \'accepted\' ' +
|
|
|
|
' WHERE "videoShare"."videoId" = "video"."id"' +
|
2020-03-05 15:04:57 +01:00
|
|
|
' )' +
|
|
|
|
' OR' +
|
|
|
|
' EXISTS (' +
|
|
|
|
' SELECT 1 from "actorFollow" ' +
|
2020-06-09 15:59:35 +02:00
|
|
|
' WHERE "actorFollow"."targetActorId" = "videoChannel"."actorId" AND "actorFollow"."actorId" = :followerActorId ' +
|
|
|
|
' AND "actorFollow"."state" = \'accepted\'' +
|
2020-03-05 15:04:57 +01:00
|
|
|
' )'
|
|
|
|
|
|
|
|
if (options.includeLocalVideos) {
|
|
|
|
query += ' OR "video"."remote" IS FALSE'
|
|
|
|
}
|
|
|
|
|
|
|
|
query += ')'
|
|
|
|
|
|
|
|
and.push(query)
|
|
|
|
replacements.followerActorId = options.followerActorId
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.withFiles === true) {
|
2020-08-24 16:11:37 +02:00
|
|
|
and.push(
|
|
|
|
'(' +
|
|
|
|
' EXISTS (SELECT 1 FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id") ' +
|
|
|
|
' OR EXISTS (' +
|
|
|
|
' SELECT 1 FROM "videoStreamingPlaylist" ' +
|
|
|
|
' INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ' +
|
|
|
|
' WHERE "videoStreamingPlaylist"."videoId" = "video"."id"' +
|
|
|
|
' )' +
|
|
|
|
')'
|
|
|
|
)
|
2020-03-05 15:04:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.tagsOneOf) {
|
|
|
|
const tagsOneOfLower = options.tagsOneOf.map(t => t.toLowerCase())
|
|
|
|
|
|
|
|
and.push(
|
|
|
|
'EXISTS (' +
|
|
|
|
' SELECT 1 FROM "videoTag" ' +
|
|
|
|
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
|
|
|
' WHERE lower("tag"."name") IN (' + createSafeIn(model, tagsOneOfLower) + ') ' +
|
|
|
|
' AND "video"."id" = "videoTag"."videoId"' +
|
|
|
|
')'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.tagsAllOf) {
|
|
|
|
const tagsAllOfLower = options.tagsAllOf.map(t => t.toLowerCase())
|
|
|
|
|
|
|
|
and.push(
|
|
|
|
'EXISTS (' +
|
|
|
|
' SELECT 1 FROM "videoTag" ' +
|
|
|
|
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
|
|
|
' WHERE lower("tag"."name") IN (' + createSafeIn(model, tagsAllOfLower) + ') ' +
|
|
|
|
' AND "video"."id" = "videoTag"."videoId" ' +
|
|
|
|
' GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length +
|
|
|
|
')'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.nsfw === true) {
|
|
|
|
and.push('"video"."nsfw" IS TRUE')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.nsfw === false) {
|
|
|
|
and.push('"video"."nsfw" IS FALSE')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.categoryOneOf) {
|
|
|
|
and.push('"video"."category" IN (:categoryOneOf)')
|
|
|
|
replacements.categoryOneOf = options.categoryOneOf
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.licenceOneOf) {
|
|
|
|
and.push('"video"."licence" IN (:licenceOneOf)')
|
|
|
|
replacements.licenceOneOf = options.licenceOneOf
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.languageOneOf) {
|
2020-04-16 17:00:40 +02:00
|
|
|
const languages = options.languageOneOf.filter(l => l && l !== '_unknown')
|
|
|
|
const languagesQueryParts: string[] = []
|
|
|
|
|
|
|
|
if (languages.length !== 0) {
|
2020-04-20 10:24:58 +02:00
|
|
|
languagesQueryParts.push('"video"."language" IN (:languageOneOf)')
|
2020-04-16 17:00:40 +02:00
|
|
|
replacements.languageOneOf = languages
|
|
|
|
|
|
|
|
languagesQueryParts.push(
|
2020-04-20 10:24:58 +02:00
|
|
|
'EXISTS (' +
|
|
|
|
' SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' +
|
|
|
|
' IN (' + createSafeIn(model, languages) + ') AND ' +
|
|
|
|
' "videoCaption"."videoId" = "video"."id"' +
|
2020-04-16 17:00:40 +02:00
|
|
|
')'
|
|
|
|
)
|
|
|
|
}
|
2020-03-05 15:04:57 +01:00
|
|
|
|
|
|
|
if (options.languageOneOf.includes('_unknown')) {
|
2020-04-16 17:00:40 +02:00
|
|
|
languagesQueryParts.push('"video"."language" IS NULL')
|
2020-03-05 15:04:57 +01:00
|
|
|
}
|
|
|
|
|
2020-04-20 10:24:58 +02:00
|
|
|
if (languagesQueryParts.length !== 0) {
|
|
|
|
and.push('(' + languagesQueryParts.join(' OR ') + ')')
|
|
|
|
}
|
2020-03-05 15:04:57 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 00:12:44 +01:00
|
|
|
// We don't exclude results in this so if we do a count we don't need to add this complex clause
|
2021-01-26 01:53:13 +01:00
|
|
|
if (options.isCount !== true) {
|
|
|
|
if (options.trendingDays) {
|
|
|
|
const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * options.trendingDays)
|
|
|
|
|
|
|
|
joins.push('LEFT JOIN "videoView" ON "video"."id" = "videoView"."videoId" AND "videoView"."startDate" >= :viewsGteDate')
|
|
|
|
replacements.viewsGteDate = viewsGteDate
|
|
|
|
|
|
|
|
attributes.push('COALESCE(SUM("videoView"."views"), 0) AS "score"')
|
|
|
|
|
|
|
|
group = 'GROUP BY "video"."id"'
|
2021-02-02 12:59:41 +01:00
|
|
|
} else if ([ 'best', 'hot' ].includes(options.trendingAlgorithm)) {
|
2021-01-26 01:53:13 +01:00
|
|
|
/**
|
|
|
|
* "Hotness" is a measure based on absolute view/comment/like/dislike numbers,
|
|
|
|
* with fixed weights only applied to their log values.
|
|
|
|
*
|
|
|
|
* This algorithm gives little chance for an old video to have a good score,
|
|
|
|
* for which recent spikes in interactions could be a sign of "hotness" and
|
|
|
|
* justify a better score. However there are multiple ways to achieve that
|
|
|
|
* goal, which is left for later. Yes, this is a TODO :)
|
|
|
|
*
|
2021-01-28 14:43:02 +01:00
|
|
|
* notes:
|
|
|
|
* - weights and base score are in number of half-days.
|
|
|
|
* - all comments are counted, regardless of being written by the video author or not
|
2021-01-26 01:53:13 +01:00
|
|
|
* see https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/_sorts.pyx#L47-L58
|
2021-03-03 13:52:58 +01:00
|
|
|
* - we have less interactions than on reddit, so multiply weights by an arbitrary factor
|
2021-01-26 01:53:13 +01:00
|
|
|
*/
|
|
|
|
const weights = {
|
2021-03-03 13:52:58 +01:00
|
|
|
like: 3 * 50,
|
|
|
|
dislike: -3 * 50,
|
|
|
|
view: Math.floor((1 / 3) * 50),
|
|
|
|
comment: 2 * 50, // a comment takes more time than a like to do, but can be done multiple times
|
|
|
|
history: -2 * 50
|
2021-01-26 01:53:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
joins.push('LEFT JOIN "videoComment" ON "video"."id" = "videoComment"."videoId"')
|
|
|
|
|
2021-02-02 12:59:41 +01:00
|
|
|
let attribute =
|
2021-01-26 01:53:13 +01:00
|
|
|
`LOG(GREATEST(1, "video"."likes" - 1)) * ${weights.like} ` + // likes (+)
|
2021-02-02 12:59:41 +01:00
|
|
|
`+ LOG(GREATEST(1, "video"."dislikes" - 1)) * ${weights.dislike} ` + // dislikes (-)
|
2021-01-26 01:53:13 +01:00
|
|
|
`+ LOG("video"."views" + 1) * ${weights.view} ` + // views (+)
|
|
|
|
`+ LOG(GREATEST(1, COUNT(DISTINCT "videoComment"."id"))) * ${weights.comment} ` + // comments (+)
|
2021-03-03 13:52:58 +01:00
|
|
|
'+ (SELECT (EXTRACT(epoch FROM "video"."publishedAt") - 1446156582) / 47000) ' // base score (in number of half-days)
|
2021-02-02 12:59:41 +01:00
|
|
|
|
|
|
|
if (options.trendingAlgorithm === 'best' && options.user) {
|
|
|
|
joins.push(
|
|
|
|
'LEFT JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :bestUser'
|
|
|
|
)
|
|
|
|
replacements.bestUser = options.user.id
|
|
|
|
|
|
|
|
attribute += `+ POWER(COUNT(DISTINCT "userVideoHistory"."id"), 2.0) * ${weights.history} `
|
|
|
|
}
|
|
|
|
|
|
|
|
attribute += 'AS "score"'
|
|
|
|
attributes.push(attribute)
|
2020-03-09 14:44:44 +01:00
|
|
|
|
2021-01-26 01:53:13 +01:00
|
|
|
group = 'GROUP BY "video"."id"'
|
|
|
|
}
|
2020-03-05 15:04:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.historyOfUser) {
|
2021-02-02 12:59:41 +01:00
|
|
|
joins.push('INNER JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId"')
|
2020-03-05 15:04:57 +01:00
|
|
|
|
|
|
|
and.push('"userVideoHistory"."userId" = :historyOfUser')
|
2020-03-09 14:44:44 +01:00
|
|
|
replacements.historyOfUser = options.historyOfUser.id
|
2020-03-05 15:04:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.startDate) {
|
|
|
|
and.push('"video"."publishedAt" >= :startDate')
|
|
|
|
replacements.startDate = options.startDate
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.endDate) {
|
|
|
|
and.push('"video"."publishedAt" <= :endDate')
|
|
|
|
replacements.endDate = options.endDate
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.originallyPublishedStartDate) {
|
|
|
|
and.push('"video"."originallyPublishedAt" >= :originallyPublishedStartDate')
|
|
|
|
replacements.originallyPublishedStartDate = options.originallyPublishedStartDate
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.originallyPublishedEndDate) {
|
|
|
|
and.push('"video"."originallyPublishedAt" <= :originallyPublishedEndDate')
|
|
|
|
replacements.originallyPublishedEndDate = options.originallyPublishedEndDate
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.durationMin) {
|
|
|
|
and.push('"video"."duration" >= :durationMin')
|
|
|
|
replacements.durationMin = options.durationMin
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.durationMax) {
|
|
|
|
and.push('"video"."duration" <= :durationMax')
|
|
|
|
replacements.durationMax = options.durationMax
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.search) {
|
|
|
|
const escapedSearch = model.sequelize.escape(options.search)
|
|
|
|
const escapedLikeSearch = model.sequelize.escape('%' + options.search + '%')
|
|
|
|
|
2020-03-09 14:44:44 +01:00
|
|
|
cte.push(
|
|
|
|
'"trigramSearch" AS (' +
|
|
|
|
' SELECT "video"."id", ' +
|
|
|
|
` similarity(lower(immutable_unaccent("video"."name")), lower(immutable_unaccent(${escapedSearch}))) as similarity ` +
|
|
|
|
' FROM "video" ' +
|
|
|
|
' WHERE lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
|
|
|
|
' lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
|
|
|
|
')'
|
|
|
|
)
|
|
|
|
|
|
|
|
joins.push('LEFT JOIN "trigramSearch" ON "video"."id" = "trigramSearch"."id"')
|
|
|
|
|
2020-03-05 15:04:57 +01:00
|
|
|
let base = '(' +
|
2020-03-09 14:44:44 +01:00
|
|
|
' "trigramSearch"."id" IS NOT NULL OR ' +
|
2020-03-05 15:04:57 +01:00
|
|
|
' EXISTS (' +
|
|
|
|
' SELECT 1 FROM "videoTag" ' +
|
|
|
|
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
|
|
|
` WHERE lower("tag"."name") = ${escapedSearch} ` +
|
|
|
|
' AND "video"."id" = "videoTag"."videoId"' +
|
|
|
|
' )'
|
|
|
|
|
|
|
|
if (validator.isUUID(options.search)) {
|
|
|
|
base += ` OR "video"."uuid" = ${escapedSearch}`
|
|
|
|
}
|
|
|
|
|
|
|
|
base += ')'
|
|
|
|
and.push(base)
|
|
|
|
|
2020-03-09 14:44:44 +01:00
|
|
|
attributes.push(`COALESCE("trigramSearch"."similarity", 0) as similarity`)
|
2020-03-05 15:04:57 +01:00
|
|
|
} else {
|
|
|
|
attributes.push('0 as similarity')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.isCount === true) attributes = [ 'COUNT(*) as "total"' ]
|
|
|
|
|
2020-03-09 14:44:44 +01:00
|
|
|
let suffix = ''
|
|
|
|
let order = ''
|
2020-03-05 15:04:57 +01:00
|
|
|
if (options.isCount !== true) {
|
|
|
|
|
2020-03-11 08:40:13 +01:00
|
|
|
if (exists(options.sort)) {
|
2020-04-20 14:05:52 +02:00
|
|
|
if (options.sort === '-originallyPublishedAt' || options.sort === 'originallyPublishedAt') {
|
|
|
|
attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"')
|
|
|
|
}
|
|
|
|
|
2020-07-07 17:18:26 +02:00
|
|
|
order = buildOrder(options.sort)
|
2020-03-11 08:40:13 +01:00
|
|
|
suffix += `${order} `
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists(options.count)) {
|
|
|
|
const count = parseInt(options.count + '', 10)
|
|
|
|
suffix += `LIMIT ${count} `
|
|
|
|
}
|
2020-03-09 14:44:44 +01:00
|
|
|
|
2020-03-11 08:40:13 +01:00
|
|
|
if (exists(options.start)) {
|
|
|
|
const start = parseInt(options.start + '', 10)
|
|
|
|
suffix += `OFFSET ${start} `
|
|
|
|
}
|
2020-03-05 15:04:57 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 14:44:44 +01:00
|
|
|
const cteString = cte.length !== 0
|
|
|
|
? `WITH ${cte.join(', ')} `
|
|
|
|
: ''
|
|
|
|
|
|
|
|
const query = cteString +
|
|
|
|
'SELECT ' + attributes.join(', ') + ' ' +
|
|
|
|
'FROM "video" ' + joins.join(' ') + ' ' +
|
|
|
|
'WHERE ' + and.join(' AND ') + ' ' +
|
|
|
|
group + ' ' +
|
|
|
|
having + ' ' +
|
|
|
|
suffix
|
|
|
|
|
|
|
|
return { query, replacements, order }
|
2020-03-05 15:04:57 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 17:18:26 +02:00
|
|
|
function buildOrder (value: string) {
|
2020-03-05 15:04:57 +01:00
|
|
|
const { direction, field } = buildDirectionAndField(value)
|
2020-03-09 14:44:44 +01:00
|
|
|
if (field.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + field)
|
2020-03-05 15:04:57 +01:00
|
|
|
|
|
|
|
if (field.toLowerCase() === 'random') return 'ORDER BY RANDOM()'
|
|
|
|
|
2021-02-02 12:59:41 +01:00
|
|
|
if ([ 'trending', 'hot', 'best' ].includes(field.toLowerCase())) { // Sort by aggregation
|
2021-01-22 00:12:44 +01:00
|
|
|
return `ORDER BY "score" ${direction}, "video"."views" ${direction}`
|
2020-03-05 15:04:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let firstSort: string
|
|
|
|
|
|
|
|
if (field.toLowerCase() === 'match') { // Search
|
|
|
|
firstSort = '"similarity"'
|
2020-04-20 14:05:52 +02:00
|
|
|
} else if (field === 'originallyPublishedAt') {
|
|
|
|
firstSort = '"publishedAtForOrder"'
|
2020-03-09 14:44:44 +01:00
|
|
|
} else if (field.includes('.')) {
|
|
|
|
firstSort = field
|
2020-03-05 15:04:57 +01:00
|
|
|
} else {
|
|
|
|
firstSort = `"video"."${field}"`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `ORDER BY ${firstSort} ${direction}, "video"."id" ASC`
|
|
|
|
}
|
|
|
|
|
2020-03-09 14:44:44 +01:00
|
|
|
function wrapForAPIResults (baseQuery: string, replacements: any, options: BuildVideosQueryOptions, order: string) {
|
|
|
|
const attributes = {
|
|
|
|
'"video".*': '',
|
|
|
|
'"VideoChannel"."id"': '"VideoChannel.id"',
|
|
|
|
'"VideoChannel"."name"': '"VideoChannel.name"',
|
|
|
|
'"VideoChannel"."description"': '"VideoChannel.description"',
|
|
|
|
'"VideoChannel"."actorId"': '"VideoChannel.actorId"',
|
|
|
|
'"VideoChannel->Actor"."id"': '"VideoChannel.Actor.id"',
|
|
|
|
'"VideoChannel->Actor"."preferredUsername"': '"VideoChannel.Actor.preferredUsername"',
|
|
|
|
'"VideoChannel->Actor"."url"': '"VideoChannel.Actor.url"',
|
|
|
|
'"VideoChannel->Actor"."serverId"': '"VideoChannel.Actor.serverId"',
|
|
|
|
'"VideoChannel->Actor"."avatarId"': '"VideoChannel.Actor.avatarId"',
|
|
|
|
'"VideoChannel->Account"."id"': '"VideoChannel.Account.id"',
|
|
|
|
'"VideoChannel->Account"."name"': '"VideoChannel.Account.name"',
|
|
|
|
'"VideoChannel->Account->Actor"."id"': '"VideoChannel.Account.Actor.id"',
|
|
|
|
'"VideoChannel->Account->Actor"."preferredUsername"': '"VideoChannel.Account.Actor.preferredUsername"',
|
|
|
|
'"VideoChannel->Account->Actor"."url"': '"VideoChannel.Account.Actor.url"',
|
|
|
|
'"VideoChannel->Account->Actor"."serverId"': '"VideoChannel.Account.Actor.serverId"',
|
|
|
|
'"VideoChannel->Account->Actor"."avatarId"': '"VideoChannel.Account.Actor.avatarId"',
|
|
|
|
'"VideoChannel->Actor->Server"."id"': '"VideoChannel.Actor.Server.id"',
|
|
|
|
'"VideoChannel->Actor->Server"."host"': '"VideoChannel.Actor.Server.host"',
|
|
|
|
'"VideoChannel->Actor->Avatar"."id"': '"VideoChannel.Actor.Avatar.id"',
|
|
|
|
'"VideoChannel->Actor->Avatar"."filename"': '"VideoChannel.Actor.Avatar.filename"',
|
|
|
|
'"VideoChannel->Actor->Avatar"."fileUrl"': '"VideoChannel.Actor.Avatar.fileUrl"',
|
|
|
|
'"VideoChannel->Actor->Avatar"."onDisk"': '"VideoChannel.Actor.Avatar.onDisk"',
|
|
|
|
'"VideoChannel->Actor->Avatar"."createdAt"': '"VideoChannel.Actor.Avatar.createdAt"',
|
|
|
|
'"VideoChannel->Actor->Avatar"."updatedAt"': '"VideoChannel.Actor.Avatar.updatedAt"',
|
|
|
|
'"VideoChannel->Account->Actor->Server"."id"': '"VideoChannel.Account.Actor.Server.id"',
|
|
|
|
'"VideoChannel->Account->Actor->Server"."host"': '"VideoChannel.Account.Actor.Server.host"',
|
|
|
|
'"VideoChannel->Account->Actor->Avatar"."id"': '"VideoChannel.Account.Actor.Avatar.id"',
|
|
|
|
'"VideoChannel->Account->Actor->Avatar"."filename"': '"VideoChannel.Account.Actor.Avatar.filename"',
|
|
|
|
'"VideoChannel->Account->Actor->Avatar"."fileUrl"': '"VideoChannel.Account.Actor.Avatar.fileUrl"',
|
|
|
|
'"VideoChannel->Account->Actor->Avatar"."onDisk"': '"VideoChannel.Account.Actor.Avatar.onDisk"',
|
|
|
|
'"VideoChannel->Account->Actor->Avatar"."createdAt"': '"VideoChannel.Account.Actor.Avatar.createdAt"',
|
|
|
|
'"VideoChannel->Account->Actor->Avatar"."updatedAt"': '"VideoChannel.Account.Actor.Avatar.updatedAt"',
|
|
|
|
'"Thumbnails"."id"': '"Thumbnails.id"',
|
|
|
|
'"Thumbnails"."type"': '"Thumbnails.type"',
|
|
|
|
'"Thumbnails"."filename"': '"Thumbnails.filename"'
|
|
|
|
}
|
|
|
|
|
|
|
|
const joins = [
|
|
|
|
'INNER JOIN "video" ON "tmp"."id" = "video"."id"',
|
|
|
|
|
|
|
|
'INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"',
|
|
|
|
'INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."actorId" = "VideoChannel->Actor"."id"',
|
|
|
|
'INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"',
|
|
|
|
'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."actorId" = "VideoChannel->Account->Actor"."id"',
|
|
|
|
|
|
|
|
'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"',
|
|
|
|
'LEFT OUTER JOIN "avatar" AS "VideoChannel->Actor->Avatar" ON "VideoChannel->Actor"."avatarId" = "VideoChannel->Actor->Avatar"."id"',
|
|
|
|
|
|
|
|
'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' +
|
|
|
|
'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"',
|
|
|
|
|
|
|
|
'LEFT OUTER JOIN "avatar" AS "VideoChannel->Account->Actor->Avatar" ' +
|
|
|
|
'ON "VideoChannel->Account->Actor"."avatarId" = "VideoChannel->Account->Actor->Avatar"."id"',
|
|
|
|
|
|
|
|
'LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"'
|
|
|
|
]
|
|
|
|
|
|
|
|
if (options.withFiles) {
|
2020-08-24 16:11:37 +02:00
|
|
|
joins.push('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')
|
|
|
|
|
|
|
|
joins.push('LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"')
|
|
|
|
joins.push(
|
|
|
|
'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' +
|
|
|
|
'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"'
|
|
|
|
)
|
2020-03-09 14:44:44 +01:00
|
|
|
|
|
|
|
Object.assign(attributes, {
|
|
|
|
'"VideoFiles"."id"': '"VideoFiles.id"',
|
|
|
|
'"VideoFiles"."createdAt"': '"VideoFiles.createdAt"',
|
|
|
|
'"VideoFiles"."updatedAt"': '"VideoFiles.updatedAt"',
|
|
|
|
'"VideoFiles"."resolution"': '"VideoFiles.resolution"',
|
|
|
|
'"VideoFiles"."size"': '"VideoFiles.size"',
|
|
|
|
'"VideoFiles"."extname"': '"VideoFiles.extname"',
|
2021-02-16 16:25:53 +01:00
|
|
|
'"VideoFiles"."filename"': '"VideoFiles.filename"',
|
|
|
|
'"VideoFiles"."fileUrl"': '"VideoFiles.fileUrl"',
|
|
|
|
'"VideoFiles"."torrentFilename"': '"VideoFiles.torrentFilename"',
|
|
|
|
'"VideoFiles"."torrentUrl"': '"VideoFiles.torrentUrl"',
|
2020-03-09 14:44:44 +01:00
|
|
|
'"VideoFiles"."infoHash"': '"VideoFiles.infoHash"',
|
|
|
|
'"VideoFiles"."fps"': '"VideoFiles.fps"',
|
2020-08-24 16:11:37 +02:00
|
|
|
'"VideoFiles"."videoId"': '"VideoFiles.videoId"',
|
|
|
|
|
|
|
|
'"VideoStreamingPlaylists"."id"': '"VideoStreamingPlaylists.id"',
|
2021-01-27 16:42:13 +01:00
|
|
|
'"VideoStreamingPlaylists"."playlistUrl"': '"VideoStreamingPlaylists.playlistUrl"',
|
|
|
|
'"VideoStreamingPlaylists"."type"': '"VideoStreamingPlaylists.type"',
|
2020-08-24 16:11:37 +02:00
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."id"': '"VideoStreamingPlaylists.VideoFiles.id"',
|
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."createdAt"': '"VideoStreamingPlaylists.VideoFiles.createdAt"',
|
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."updatedAt"': '"VideoStreamingPlaylists.VideoFiles.updatedAt"',
|
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."resolution"': '"VideoStreamingPlaylists.VideoFiles.resolution"',
|
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."size"': '"VideoStreamingPlaylists.VideoFiles.size"',
|
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."extname"': '"VideoStreamingPlaylists.VideoFiles.extname"',
|
2021-02-16 16:25:53 +01:00
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."filename"': '"VideoStreamingPlaylists.VideoFiles.filename"',
|
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."fileUrl"': '"VideoStreamingPlaylists.VideoFiles.fileUrl"',
|
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."torrentFilename"': '"VideoStreamingPlaylists.VideoFiles.torrentFilename"',
|
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."torrentUrl"': '"VideoStreamingPlaylists.VideoFiles.torrentUrl"',
|
2020-08-24 16:11:37 +02:00
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."infoHash"': '"VideoStreamingPlaylists.VideoFiles.infoHash"',
|
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."fps"': '"VideoStreamingPlaylists.VideoFiles.fps"',
|
2021-01-27 16:42:13 +01:00
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId"': '"VideoStreamingPlaylists.VideoFiles.videoStreamingPlaylistId"',
|
2020-08-24 16:11:37 +02:00
|
|
|
'"VideoStreamingPlaylists->VideoFiles"."videoId"': '"VideoStreamingPlaylists.VideoFiles.videoId"'
|
2020-03-09 14:44:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.user) {
|
|
|
|
joins.push(
|
|
|
|
'LEFT OUTER JOIN "userVideoHistory" ' +
|
|
|
|
'ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :userVideoHistoryId'
|
|
|
|
)
|
|
|
|
replacements.userVideoHistoryId = options.user.id
|
|
|
|
|
|
|
|
Object.assign(attributes, {
|
|
|
|
'"userVideoHistory"."id"': '"userVideoHistory.id"',
|
|
|
|
'"userVideoHistory"."currentTime"': '"userVideoHistory.currentTime"'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.videoPlaylistId) {
|
|
|
|
joins.push(
|
|
|
|
'INNER JOIN "videoPlaylistElement" as "VideoPlaylistElement" ON "videoPlaylistElement"."videoId" = "video"."id" ' +
|
|
|
|
'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
|
|
|
|
)
|
|
|
|
replacements.videoPlaylistId = options.videoPlaylistId
|
|
|
|
|
|
|
|
Object.assign(attributes, {
|
|
|
|
'"VideoPlaylistElement"."createdAt"': '"VideoPlaylistElement.createdAt"',
|
|
|
|
'"VideoPlaylistElement"."updatedAt"': '"VideoPlaylistElement.updatedAt"',
|
|
|
|
'"VideoPlaylistElement"."url"': '"VideoPlaylistElement.url"',
|
|
|
|
'"VideoPlaylistElement"."position"': '"VideoPlaylistElement.position"',
|
|
|
|
'"VideoPlaylistElement"."startTimestamp"': '"VideoPlaylistElement.startTimestamp"',
|
|
|
|
'"VideoPlaylistElement"."stopTimestamp"': '"VideoPlaylistElement.stopTimestamp"',
|
|
|
|
'"VideoPlaylistElement"."videoPlaylistId"': '"VideoPlaylistElement.videoPlaylistId"'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const select = 'SELECT ' + Object.keys(attributes).map(key => {
|
|
|
|
const value = attributes[key]
|
|
|
|
if (value) return `${key} AS ${value}`
|
|
|
|
|
|
|
|
return key
|
|
|
|
}).join(', ')
|
|
|
|
|
|
|
|
return `${select} FROM (${baseQuery}) AS "tmp" ${joins.join(' ')} ${order}`
|
|
|
|
}
|
|
|
|
|
2020-03-05 15:04:57 +01:00
|
|
|
export {
|
2020-03-09 14:44:44 +01:00
|
|
|
buildListQuery,
|
|
|
|
wrapForAPIResults
|
2020-03-05 15:04:57 +01:00
|
|
|
}
|