Init commit
This commit is contained in:
@@ -0,0 +1,278 @@
|
||||
import { ActorImageType, VideoChannelCollaboratorState } from '@peertube/peertube-models'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { buildServerIdsFollowedBy } from '@server/models/shared/index.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { VideoChannelTableAttributes } from './video-channel-table-attributes.js'
|
||||
|
||||
export interface ListVideoChannelsOptions extends AbstractListQueryOptions {
|
||||
actorId?: number
|
||||
search?: string
|
||||
host?: string
|
||||
handles?: string[]
|
||||
forCount?: boolean
|
||||
|
||||
accountId?: number
|
||||
|
||||
// If accountId is provided, include channels where the account is a collaborator
|
||||
// default: false
|
||||
includeCollaborations?: boolean
|
||||
|
||||
statsDaysPrior?: number
|
||||
}
|
||||
|
||||
export class VideoChannelListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoChannelTableAttributes()
|
||||
|
||||
private builtActorJoin = false
|
||||
private builtAccountJoin = false
|
||||
private builtChannelCollaboratorsJoin = false
|
||||
private builtAccountAvatarJoin = false
|
||||
private builtChannelAvatarJoin = false
|
||||
private builtChannelBannerJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoChannelsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoChannelModel', tableName: 'videoChannel' }, options)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
const where: string[] = []
|
||||
|
||||
if (this.options.host) {
|
||||
this.buildActorJoin()
|
||||
|
||||
if (this.options.host === WEBSERVER.HOST) {
|
||||
where.push('"Actor"."serverId" IS NULL')
|
||||
} else {
|
||||
where.push('"Actor->Server"."host" = :host')
|
||||
|
||||
this.replacements.host = this.options.host
|
||||
}
|
||||
}
|
||||
|
||||
// Only list local channels OR channels that are on an instance followed by actorId
|
||||
if (this.options.actorId) {
|
||||
this.buildActorJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`"Actor"."serverId" IS NULL OR ` +
|
||||
`"Actor"."serverId" IN ${buildServerIdsFollowedBy(this.options.actorId)}` +
|
||||
`)`
|
||||
)
|
||||
}
|
||||
|
||||
if (this.options.accountId) {
|
||||
this.buildAccountJoin()
|
||||
|
||||
if (this.options.includeCollaborations !== true) {
|
||||
where.push('"VideoChannelModel"."accountId" = :accountId')
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
} else {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`("VideoChannelModel"."accountId" = :accountId OR "VideoChannelCollaborators"."accountId" = :accountId)`
|
||||
)
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(this.options.handles) && this.options.handles.length !== 0) {
|
||||
this.buildActorJoin()
|
||||
|
||||
const or: string[] = []
|
||||
|
||||
for (const handle of this.options.handles || []) {
|
||||
const [ preferredUsername, host ] = handle.split('@')
|
||||
|
||||
const sanitizedPreferredUsername = this.sequelize.escape(preferredUsername.toLowerCase())
|
||||
const sanitizedHost = this.sequelize.escape(host)
|
||||
|
||||
if (!host || host === WEBSERVER.HOST) {
|
||||
or.push(`(LOWER("Actor"."preferredUsername") = ${sanitizedPreferredUsername} AND "Actor"."serverId" IS NULL)`)
|
||||
} else {
|
||||
or.push(`(LOWER("Actor"."preferredUsername") = ${sanitizedPreferredUsername} AND "Actor->Server"."host" = ${sanitizedHost})`)
|
||||
}
|
||||
}
|
||||
|
||||
where.push(`(${or.join(' OR ')})`)
|
||||
}
|
||||
|
||||
if (this.options.search) {
|
||||
this.buildAccountJoin()
|
||||
|
||||
const escapedSearch = this.sequelize.escape(this.options.search)
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
|
||||
|
||||
this.subQueryAttributes.push(
|
||||
`word_similarity(lower(immutable_unaccent(${escapedSearch})), lower(immutable_unaccent("VideoChannelModel"."name"))) as similarity`
|
||||
)
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`lower(immutable_unaccent(${escapedSearch})) <% lower(immutable_unaccent("VideoChannelModel"."name")) OR ` +
|
||||
`lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(${escapedLikeSearch})) OR ` +
|
||||
`lower(immutable_unaccent("Account"."name")) LIKE lower(immutable_unaccent(${escapedLikeSearch}))` +
|
||||
`)`
|
||||
)
|
||||
} else {
|
||||
this.subQueryAttributes.push('0 as similarity')
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildActorJoin () {
|
||||
if (this.builtActorJoin) return
|
||||
|
||||
this.subQueryJoin += ' INNER JOIN "actor" "Actor" ON "Actor"."videoChannelId" = "VideoChannelModel"."id" ' +
|
||||
'LEFT JOIN "server" "Actor->Server" ON "Actor"."serverId" = "Actor->Server"."id" '
|
||||
|
||||
this.builtActorJoin = true
|
||||
}
|
||||
|
||||
private buildAccountJoin () {
|
||||
if (this.builtAccountJoin) return
|
||||
|
||||
this.subQueryJoin += ' INNER JOIN "account" "Account" ON "Account"."id" = "VideoChannelModel"."accountId" ' +
|
||||
'INNER JOIN "actor" "Account->Actor" ON "Account->Actor"."accountId" = "Account"."id" ' +
|
||||
'LEFT JOIN "server" "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id" '
|
||||
|
||||
this.builtAccountJoin = true
|
||||
}
|
||||
|
||||
private buildChannelCollaboratorsJoin () {
|
||||
if (this.builtChannelCollaboratorsJoin) return
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "VideoChannelCollaborators" ' +
|
||||
'ON "VideoChannelCollaborators"."channelId" = "VideoChannelModel"."id" ' +
|
||||
'AND "VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "VideoChannelCollaborators"."accountId" = :accountId '
|
||||
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
this.replacements.accountId = this.options.accountId
|
||||
|
||||
this.builtChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildAccountAvatarsJoin () {
|
||||
if (this.builtAccountAvatarJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "Account->Actor->Avatars" ` +
|
||||
`ON "Account->Actor->Avatars"."actorId" = "VideoChannelModel"."Account.Actor.id" ` +
|
||||
`AND "Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
|
||||
this.builtAccountAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "Actor->Avatars" ` +
|
||||
`ON "Actor->Avatars"."actorId" = "VideoChannelModel"."Actor.id" ` +
|
||||
`AND "Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
|
||||
this.builtChannelAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelBannersJoin () {
|
||||
if (this.builtChannelBannerJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "Actor->Banners" ` +
|
||||
`ON "Actor->Banners"."actorId" = "VideoChannelModel"."Actor.id" ` +
|
||||
`AND "Actor->Banners"."type" = ${ActorImageType.BANNER} `
|
||||
|
||||
this.builtChannelBannerJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.buildChannelAvatarsJoin()
|
||||
this.buildAccountAvatarsJoin()
|
||||
this.buildChannelBannersJoin()
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getAccountAvatarAttributes(),
|
||||
this.tableAttributes.getChannelAvatarAttributes(),
|
||||
this.tableAttributes.getChannelBannerAttributes()
|
||||
]
|
||||
|
||||
if (this.options.statsDaysPrior) {
|
||||
this.attributes.push(
|
||||
`(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id") AS "videosCount"`
|
||||
)
|
||||
|
||||
this.attributes.push(
|
||||
// dprint-ignore
|
||||
'(' +
|
||||
`SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
|
||||
'FROM ( ' +
|
||||
'WITH days AS ( ' +
|
||||
`SELECT generate_series(date_trunc('day', now()) - '${this.options.statsDaysPrior} day'::interval, ` +
|
||||
`date_trunc('day', now()), '1 day'::interval) AS day ` +
|
||||
') ' +
|
||||
'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
|
||||
'FROM days ' +
|
||||
'LEFT JOIN (' +
|
||||
'"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
|
||||
'AND "video"."channelId" = "VideoChannelModel"."id"' +
|
||||
`) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
|
||||
'GROUP BY day ORDER BY day ' +
|
||||
') t' +
|
||||
') AS "viewsPerDay"'
|
||||
)
|
||||
|
||||
this.attributes.push(
|
||||
'(' +
|
||||
'SELECT COALESCE(SUM("video".views), 0) AS totalViews ' +
|
||||
'FROM "video" ' +
|
||||
'WHERE "video"."channelId" = "VideoChannelModel"."id"' +
|
||||
') AS "totalViews"'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
this.buildActorJoin()
|
||||
this.buildAccountJoin()
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoChannelAttributes(),
|
||||
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes(),
|
||||
|
||||
this.tableAttributes.getAccountAttributes(),
|
||||
this.tableAttributes.getAccountActorAttributes(),
|
||||
this.tableAttributes.getAccountServerAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected getCalculatedAttributes () {
|
||||
return [ 'similarity' ]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { Memoize } from '@server/helpers/memoize.js'
|
||||
import { AccountModel } from '@server/models/account/account.js'
|
||||
import { ActorImageModel } from '@server/models/actor/actor-image.js'
|
||||
import { ActorModel } from '@server/models/actor/actor.js'
|
||||
import { ServerModel } from '@server/models/server/server.js'
|
||||
import { VideoChannelModel } from '../../video-channel.js'
|
||||
|
||||
export class VideoChannelTableAttributes {
|
||||
@Memoize()
|
||||
getVideoChannelAttributes () {
|
||||
return VideoChannelModel.getSQLAttributes('VideoChannelModel').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLAPIAttributes('Actor', `Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('Actor->Server', `Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Actor->Avatars', 'Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelBannerAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Actor->Banners', 'Actor.Banners.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getAccountAttributes () {
|
||||
return AccountModel.getSQLSummaryAttributes('Account', 'Account.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('Account->Actor', `Account.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('Account->Actor->Server', `Account.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Account->Actor->Avatars', 'Account.Actor.Avatars.').join(', ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
import { VideoChannelCollaboratorState, VideoPrivacy } from '@peertube/peertube-models'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { getAccountJoin, getActorJoin, getAvatarsJoin, getChannelJoin } from '@server/models/shared/sql/actor-helpers.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { createSafeIn } from '../../../shared/index.js'
|
||||
import { VideoCommentTableAttributes } from './video-comment-table-attributes.js'
|
||||
|
||||
export interface ListVideoCommentsOptions extends AbstractListQueryOptions {
|
||||
selectType: 'api-list' | 'api-video' | 'feed' | 'comment-only'
|
||||
|
||||
autoTagOfAccountId?: number
|
||||
|
||||
videoId?: number
|
||||
threadId?: number
|
||||
accountId?: number
|
||||
|
||||
blockerAccountIds?: number[]
|
||||
|
||||
isThread?: boolean
|
||||
notDeleted?: boolean
|
||||
|
||||
isLocal?: boolean
|
||||
onLocalVideo?: boolean
|
||||
|
||||
onPublicVideo?: boolean
|
||||
videoChannelOwnerId?: number
|
||||
videoAccountOwnerId?: number
|
||||
videoAccountOwnerIncludeCollaborations?: boolean
|
||||
|
||||
heldForReview: boolean
|
||||
heldForReviewAccountIdException?: number
|
||||
|
||||
autoTagOneOf?: string[]
|
||||
|
||||
search?: string
|
||||
searchAccount?: string
|
||||
searchVideo?: string
|
||||
|
||||
includeReplyCounters?: boolean
|
||||
}
|
||||
|
||||
export class VideoCommentListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoCommentTableAttributes()
|
||||
|
||||
private builtAccountJoin = false
|
||||
private builtAccountActorJoin = false
|
||||
private builtVideoJoin = false
|
||||
private builtVideoChannelJoin = false
|
||||
private builtVideoChannelActorJoin = false
|
||||
private builtVideoChannelCollaboratorsJoin = false
|
||||
private builtAccountAvatarJoin = false
|
||||
private builtChannelAvatarJoin = false
|
||||
private builtAutomaticTagsJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoCommentsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoCommentModel', tableName: 'videoComment' }, options)
|
||||
|
||||
if (this.options.includeReplyCounters && !this.options.videoId) {
|
||||
throw new Error('Cannot include reply counters without videoId')
|
||||
}
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
let where: string[] = []
|
||||
|
||||
if (this.options.videoId) {
|
||||
this.replacements.videoId = this.options.videoId
|
||||
|
||||
where.push('"VideoCommentModel"."videoId" = :videoId')
|
||||
}
|
||||
|
||||
if (this.options.threadId) {
|
||||
this.replacements.threadId = this.options.threadId
|
||||
|
||||
where.push('("VideoCommentModel"."id" = :threadId OR "VideoCommentModel"."originCommentId" = :threadId)')
|
||||
}
|
||||
|
||||
if (this.options.accountId) {
|
||||
this.replacements.accountId = this.options.accountId
|
||||
|
||||
where.push('"VideoCommentModel"."accountId" = :accountId')
|
||||
}
|
||||
|
||||
if (this.options.blockerAccountIds) {
|
||||
this.buildVideoChannelJoin()
|
||||
|
||||
where = where.concat(this.getBlockWhere('VideoCommentModel', 'Video->VideoChannel'))
|
||||
}
|
||||
|
||||
if (this.options.isThread === true) {
|
||||
where.push('"VideoCommentModel"."inReplyToCommentId" IS NULL')
|
||||
}
|
||||
|
||||
if (this.options.notDeleted === true) {
|
||||
where.push('"VideoCommentModel"."deletedAt" IS NULL')
|
||||
}
|
||||
|
||||
if (this.options.heldForReview === true) {
|
||||
where.push('"VideoCommentModel"."heldForReview" IS TRUE')
|
||||
} else if (this.options.heldForReview === false) {
|
||||
const base = '"VideoCommentModel"."heldForReview" IS FALSE'
|
||||
|
||||
if (this.options.heldForReviewAccountIdException) {
|
||||
this.replacements.heldForReviewAccountIdException = this.options.heldForReviewAccountIdException
|
||||
|
||||
where.push(`(${base} OR "VideoCommentModel"."accountId" = :heldForReviewAccountIdException)`)
|
||||
} else {
|
||||
where.push(base)
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.autoTagOneOf) {
|
||||
const tags = this.options.autoTagOneOf.map(t => t.toLowerCase())
|
||||
this.buildAutomaticTagsJoin()
|
||||
|
||||
where.push('lower("CommentAutomaticTags->AutomaticTag"."name") IN (' + createSafeIn(this.sequelize, tags) + ')')
|
||||
}
|
||||
|
||||
if (this.options.isLocal === true) {
|
||||
this.buildAccountJoin()
|
||||
this.buildAccountActorJoin()
|
||||
|
||||
where.push('"Account->Actor"."serverId" IS NULL')
|
||||
} else if (this.options.isLocal === false) {
|
||||
this.buildAccountJoin()
|
||||
this.buildAccountActorJoin()
|
||||
|
||||
where.push('"Account->Actor"."serverId" IS NOT NULL')
|
||||
}
|
||||
|
||||
if (this.options.onLocalVideo === true) {
|
||||
this.buildVideoJoin()
|
||||
|
||||
where.push('"Video"."remote" IS FALSE')
|
||||
} else if (this.options.onLocalVideo === false) {
|
||||
this.buildVideoJoin()
|
||||
|
||||
where.push('"Video"."remote" IS TRUE')
|
||||
}
|
||||
|
||||
if (this.options.onPublicVideo === true) {
|
||||
this.buildVideoJoin()
|
||||
|
||||
where.push(`"Video"."privacy" = ${VideoPrivacy.PUBLIC}`)
|
||||
}
|
||||
|
||||
if (this.options.videoAccountOwnerId) {
|
||||
this.buildVideoChannelJoin()
|
||||
|
||||
this.replacements.videoAccountOwnerId = this.options.videoAccountOwnerId
|
||||
|
||||
if (this.options.videoAccountOwnerIncludeCollaborations !== true) {
|
||||
where.push(`"Video->VideoChannel"."accountId" = :videoAccountOwnerId`)
|
||||
} else {
|
||||
this.buildVideoChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`"Video->VideoChannel"."accountId" = :videoAccountOwnerId OR ` +
|
||||
`"Video->VideoChannel->VideoChannelCollaborators"."accountId" = :videoAccountOwnerId` +
|
||||
`)`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.videoChannelOwnerId) {
|
||||
this.buildVideoChannelJoin()
|
||||
|
||||
this.replacements.videoChannelOwnerId = this.options.videoChannelOwnerId
|
||||
|
||||
where.push(`"Video->VideoChannel"."id" = :videoChannelOwnerId`)
|
||||
}
|
||||
|
||||
if (this.options.search) {
|
||||
this.buildVideoJoin()
|
||||
this.buildAccountJoin()
|
||||
this.buildAccountActorJoin()
|
||||
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`"VideoCommentModel"."text" ILIKE ${escapedLikeSearch} OR ` +
|
||||
`"Account->Actor"."preferredUsername" ILIKE ${escapedLikeSearch} OR ` +
|
||||
`"Account"."name" ILIKE ${escapedLikeSearch} OR ` +
|
||||
`"Video"."name" ILIKE ${escapedLikeSearch} ` +
|
||||
`)`
|
||||
)
|
||||
}
|
||||
|
||||
if (this.options.searchAccount) {
|
||||
this.buildAccountJoin()
|
||||
this.buildAccountActorJoin()
|
||||
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.searchAccount + '%')
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`"Account->Actor"."preferredUsername" ILIKE ${escapedLikeSearch} OR ` +
|
||||
`"Account"."name" ILIKE ${escapedLikeSearch} ` +
|
||||
`)`
|
||||
)
|
||||
}
|
||||
|
||||
if (this.options.searchVideo) {
|
||||
this.buildVideoJoin()
|
||||
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.searchVideo + '%')
|
||||
|
||||
where.push(`"Video"."name" ILIKE ${escapedLikeSearch}`)
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildAccountJoin () {
|
||||
if (this.builtAccountJoin) return
|
||||
|
||||
this.subQueryJoin += getAccountJoin({
|
||||
on: `"VideoCommentModel"."accountId"`,
|
||||
includeAvatars: false,
|
||||
includeActor: false,
|
||||
required: false
|
||||
})
|
||||
|
||||
this.builtAccountJoin = true
|
||||
}
|
||||
|
||||
private buildAccountActorJoin () {
|
||||
if (this.builtAccountActorJoin) return
|
||||
|
||||
this.subQueryJoin += getActorJoin({
|
||||
base: 'Account->',
|
||||
on: `"Account"."id"`,
|
||||
type: 'account',
|
||||
includeAvatars: false,
|
||||
required: false
|
||||
})
|
||||
|
||||
this.builtAccountActorJoin = true
|
||||
}
|
||||
|
||||
private buildVideoJoin () {
|
||||
if (this.builtVideoJoin) return
|
||||
|
||||
this.subQueryJoin += ' INNER JOIN "video" "Video" ON "Video"."id" = "VideoCommentModel"."videoId" '
|
||||
|
||||
this.builtVideoJoin = true
|
||||
}
|
||||
|
||||
private buildVideoChannelJoin () {
|
||||
if (this.builtVideoChannelJoin) return
|
||||
|
||||
this.buildVideoJoin()
|
||||
|
||||
this.subQueryJoin += getChannelJoin({
|
||||
base: 'Video->',
|
||||
on: '"Video"."channelId"',
|
||||
includeAccount: false,
|
||||
includeAvatars: false,
|
||||
includeActors: false,
|
||||
required: true
|
||||
})
|
||||
|
||||
this.builtVideoChannelJoin = true
|
||||
}
|
||||
|
||||
private buildVideoChannelActorJoin () {
|
||||
if (this.builtVideoChannelActorJoin) return
|
||||
|
||||
this.subQueryJoin += getActorJoin({
|
||||
base: 'Video->VideoChannel->',
|
||||
on: '"Video->VideoChannel"."id"',
|
||||
type: 'channel',
|
||||
includeAvatars: false,
|
||||
required: true
|
||||
})
|
||||
|
||||
this.builtVideoChannelActorJoin = true
|
||||
}
|
||||
|
||||
private buildVideoChannelCollaboratorsJoin () {
|
||||
if (this.builtVideoChannelCollaboratorsJoin) return
|
||||
|
||||
this.buildVideoChannelJoin()
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "Video->VideoChannel->VideoChannelCollaborators" ' +
|
||||
'ON "Video->VideoChannel->VideoChannelCollaborators"."channelId" = "Video->VideoChannel"."id" ' +
|
||||
'AND "Video->VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "Video->VideoChannel->VideoChannelCollaborators"."accountId" = :videoAccountOwnerId '
|
||||
|
||||
this.replacements.videoAccountOwnerId = this.options.videoAccountOwnerId
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
|
||||
this.builtVideoChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
private buildAutomaticTagsJoin () {
|
||||
if (this.builtAutomaticTagsJoin) return
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN (' +
|
||||
'"commentAutomaticTag" AS "CommentAutomaticTags" INNER JOIN "automaticTag" AS "CommentAutomaticTags->AutomaticTag" ' +
|
||||
'ON "CommentAutomaticTags->AutomaticTag"."id" = "CommentAutomaticTags"."automaticTagId" ' +
|
||||
') ON "VideoCommentModel"."id" = "CommentAutomaticTags"."commentId" AND "CommentAutomaticTags"."accountId" = :autoTagOfAccountId '
|
||||
|
||||
this.replacements.autoTagOfAccountId = this.options.autoTagOfAccountId
|
||||
this.builtAutomaticTagsJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildAccountAvatarsJoin () {
|
||||
if (this.builtAccountAvatarJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({
|
||||
base: 'Account->Actor->',
|
||||
on: '"VideoCommentModel"."Account.Actor.id"'
|
||||
})
|
||||
|
||||
this.builtAccountAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({
|
||||
base: 'Video->VideoChannel->Actor->',
|
||||
on: '"VideoCommentModel"."Video.VideoChannel.Actor.id"'
|
||||
})
|
||||
|
||||
this.builtChannelAvatarJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
const selectType = this.options.selectType
|
||||
|
||||
if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
|
||||
this.buildAccountAvatarsJoin()
|
||||
}
|
||||
|
||||
if (selectType === 'api-list') {
|
||||
this.buildChannelAvatarsJoin()
|
||||
}
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
const selectType = this.options.selectType
|
||||
|
||||
if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
|
||||
this.attributes.push(this.tableAttributes.getAccountAvatarAttributes())
|
||||
}
|
||||
|
||||
if (selectType === 'api-list') {
|
||||
this.attributes.push(this.tableAttributes.getChannelAvatarAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
const selectType = this.options.selectType
|
||||
|
||||
if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
|
||||
this.buildAccountJoin()
|
||||
this.buildAccountActorJoin()
|
||||
}
|
||||
|
||||
if (selectType === 'api-list') {
|
||||
this.buildVideoJoin()
|
||||
this.buildVideoChannelJoin()
|
||||
this.buildVideoChannelActorJoin()
|
||||
}
|
||||
|
||||
if (this.options.autoTagOfAccountId && selectType === 'api-list') {
|
||||
this.buildAutomaticTagsJoin()
|
||||
}
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
const selectType = this.options.selectType
|
||||
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoCommentAttributes()
|
||||
]
|
||||
|
||||
if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoAttributes(),
|
||||
|
||||
this.tableAttributes.getAccountAttributes(),
|
||||
this.tableAttributes.getAccountActorAttributes(),
|
||||
this.tableAttributes.getAccountServerAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
if (selectType === 'api-list') {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getChannelAttributes(),
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
if (this.options.autoTagOfAccountId && this.options.selectType === 'api-list') {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getCommentAutomaticTagAttributes(),
|
||||
this.tableAttributes.getAutomaticTagAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
if (this.options.includeReplyCounters === true) {
|
||||
this.subQueryAttributes.push('"totalRepliesFromVideoAuthor"."count" AS "totalRepliesFromVideoAuthor"')
|
||||
this.subQueryAttributes.push('"totalReplies"."count" AS "totalReplies"')
|
||||
}
|
||||
}
|
||||
|
||||
protected getCalculatedAttributes () {
|
||||
return [
|
||||
'totalRepliesFromVideoAuthor',
|
||||
'totalReplies'
|
||||
]
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryLateralJoin () {
|
||||
if (this.options.includeReplyCounters === true) {
|
||||
this.buildTotalRepliesLateralJoin()
|
||||
this.buildAuthorTotalRepliesLateralJoin()
|
||||
}
|
||||
}
|
||||
|
||||
private buildTotalRepliesLateralJoin () {
|
||||
const blockWhereString = this.getBlockWhere('replies', 'videoChannel').join(' AND ')
|
||||
|
||||
// Help the planner by providing videoId that should filter out many comments
|
||||
this.replacements.videoId = this.options.videoId
|
||||
|
||||
this.subQueryLateralJoin += `LEFT JOIN LATERAL (` +
|
||||
`SELECT COUNT("replies"."id") AS "count" FROM "videoComment" AS "replies" ` +
|
||||
`INNER JOIN "video" ON "video"."id" = "replies"."videoId" AND "replies"."videoId" = :videoId ` +
|
||||
`LEFT JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id" ` +
|
||||
`WHERE ("replies"."inReplyToCommentId" = "VideoCommentModel"."id" OR "replies"."originCommentId" = "VideoCommentModel"."id") ` +
|
||||
`AND "deletedAt" IS NULL ` +
|
||||
`AND ${blockWhereString} ` +
|
||||
`) "totalReplies" ON TRUE `
|
||||
}
|
||||
|
||||
private buildAuthorTotalRepliesLateralJoin () {
|
||||
// Help the planner by providing videoId that should filter out many comments
|
||||
this.replacements.videoId = this.options.videoId
|
||||
|
||||
this.subQueryLateralJoin += `LEFT JOIN LATERAL (` +
|
||||
`SELECT COUNT("replies"."id") AS "count" FROM "videoComment" AS "replies" ` +
|
||||
`INNER JOIN "video" ON "video"."id" = "replies"."videoId" AND "replies"."videoId" = :videoId ` +
|
||||
`INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
|
||||
`WHERE ("replies"."inReplyToCommentId" = "VideoCommentModel"."id" OR "replies"."originCommentId" = "VideoCommentModel"."id") ` +
|
||||
`AND "replies"."accountId" = "videoChannel"."accountId"` +
|
||||
`) "totalRepliesFromVideoAuthor" ON TRUE `
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private getBlockWhere (commentTableName: string, channelTableName: string) {
|
||||
const where: string[] = []
|
||||
|
||||
const blockerIdsString = createSafeIn(
|
||||
this.sequelize,
|
||||
this.options.blockerAccountIds,
|
||||
[ `"${channelTableName}"."accountId"` ]
|
||||
)
|
||||
|
||||
where.push(
|
||||
`NOT EXISTS (` +
|
||||
`SELECT 1 FROM "accountBlocklist" ` +
|
||||
`WHERE "targetAccountId" = "${commentTableName}"."accountId" ` +
|
||||
`AND "accountId" IN (${blockerIdsString})` +
|
||||
`)`
|
||||
)
|
||||
|
||||
where.push(
|
||||
`NOT EXISTS (` +
|
||||
`SELECT 1 FROM "account" ` +
|
||||
`INNER JOIN "actor" ON account."id" = actor."accountId" ` +
|
||||
`INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ` +
|
||||
`WHERE "account"."id" = "${commentTableName}"."accountId" ` +
|
||||
`AND "serverBlocklist"."accountId" IN (${blockerIdsString})` +
|
||||
`)`
|
||||
)
|
||||
|
||||
return where
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { Memoize } from '@server/helpers/memoize.js'
|
||||
import { AccountModel } from '@server/models/account/account.js'
|
||||
import { ActorImageModel } from '@server/models/actor/actor-image.js'
|
||||
import { ActorModel } from '@server/models/actor/actor.js'
|
||||
import { CommentAutomaticTagModel } from '@server/models/automatic-tag/comment-automatic-tag.js'
|
||||
import { ServerModel } from '@server/models/server/server.js'
|
||||
import { buildSQLAttributes } from '@server/models/shared/table.js'
|
||||
import { AutomaticTagModel } from '../../../automatic-tag/automatic-tag.js'
|
||||
import { VideoCommentModel } from '../../video-comment.js'
|
||||
import { VideoChannelModel } from '../../video-channel.js'
|
||||
|
||||
export class VideoCommentTableAttributes {
|
||||
@Memoize()
|
||||
getVideoCommentAttributes () {
|
||||
return VideoCommentModel.getSQLAttributes('VideoCommentModel').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getVideoAttributes () {
|
||||
return [
|
||||
`"Video"."id" AS "Video.id"`,
|
||||
`"Video"."uuid" AS "Video.uuid"`,
|
||||
`"Video"."name" AS "Video.name"`
|
||||
].join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getAccountAttributes () {
|
||||
return AccountModel.getSQLAttributes('Account', 'Account.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountActorAttributes () {
|
||||
return ActorModel.getSQLAPIAttributes('Account->Actor', `Account.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountServerAttributes () {
|
||||
return ServerModel.getSQLAttributes('Account->Actor->Server', `Account.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Account->Actor->Avatars', 'Account.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelAttributes () {
|
||||
return VideoChannelModel.getSQLSummaryAttributes('Video->VideoChannel', 'Video.VideoChannel.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('Video->VideoChannel->Actor', `Video.VideoChannel.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLAttributes('Video->VideoChannel->Actor->Server', `Video.VideoChannel.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Video->VideoChannel->Actor->Avatars', 'Video.VideoChannel.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getCommentAutomaticTagAttributes () {
|
||||
return buildSQLAttributes({
|
||||
model: CommentAutomaticTagModel,
|
||||
tableName: 'CommentAutomaticTags',
|
||||
aliasPrefix: 'CommentAutomaticTags.',
|
||||
idBuilder: [ 'commentId', 'automaticTagId', 'accountId' ]
|
||||
}).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAutomaticTagAttributes () {
|
||||
return buildSQLAttributes({
|
||||
model: AutomaticTagModel,
|
||||
tableName: 'CommentAutomaticTags->AutomaticTag',
|
||||
aliasPrefix: 'CommentAutomaticTags.AutomaticTag.'
|
||||
}).join(', ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
import { VideoChannelCollaboratorState } from '@peertube/peertube-models'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { getAvatarsJoin, getChannelJoin } from '@server/models/shared/sql/actor-helpers.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { VideoImportTableAttributes } from './video-import-table-attributes.js'
|
||||
|
||||
export interface ListVideoImportsOptions extends AbstractListQueryOptions {
|
||||
userId: number
|
||||
|
||||
id?: number
|
||||
videoId?: number
|
||||
search?: string
|
||||
targetUrl?: string
|
||||
videoChannelSyncId?: number
|
||||
|
||||
collaborationAccountId?: number
|
||||
}
|
||||
|
||||
export class VideoImportListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoImportTableAttributes()
|
||||
|
||||
private builtVideoJoin = false
|
||||
private builtTagJoin = false
|
||||
private builtChannelCollaboratorsJoin = false
|
||||
private builtThumbnailJoin = false
|
||||
private builtAccountAvatarJoin = false
|
||||
private builtChannelAvatarJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoImportsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoImportModel', tableName: 'videoImport' }, options)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
const where: string[] = []
|
||||
|
||||
this.buildVideoJoin()
|
||||
|
||||
if (this.options.collaborationAccountId) {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
'(' +
|
||||
'"VideoImportModel"."userId" = :userId OR ' +
|
||||
'"Video->VideoChannel->VideoChannelCollaborators"."accountId" = :collaborationAccountId OR ' +
|
||||
'"Video->VideoChannel->Account"."userId" = :userId' +
|
||||
')'
|
||||
)
|
||||
|
||||
this.replacements.collaborationAccountId = this.options.collaborationAccountId
|
||||
this.replacements.userId = this.options.userId
|
||||
} else {
|
||||
where.push(
|
||||
'(' +
|
||||
'"VideoImportModel"."userId" = :userId OR ' +
|
||||
'"Video->VideoChannel->Account"."userId" = :userId' +
|
||||
')'
|
||||
)
|
||||
|
||||
this.replacements.userId = this.options.userId
|
||||
}
|
||||
|
||||
if (this.options.id) {
|
||||
where.push('"VideoImportModel"."id" = :id')
|
||||
|
||||
this.replacements.id = this.options.id
|
||||
}
|
||||
|
||||
if (this.options.videoId) {
|
||||
where.push('"VideoImportModel"."videoId" = :videoId')
|
||||
|
||||
this.replacements.videoId = this.options.videoId
|
||||
}
|
||||
|
||||
if (this.options.targetUrl) {
|
||||
where.push('"VideoImportModel"."targetUrl" = :targetUrl')
|
||||
|
||||
this.replacements.targetUrl = this.options.targetUrl
|
||||
}
|
||||
|
||||
if (this.options.videoChannelSyncId) {
|
||||
where.push('"VideoImportModel"."videoChannelSyncId" = :videoChannelSyncId')
|
||||
|
||||
this.replacements.videoChannelSyncId = this.options.videoChannelSyncId
|
||||
}
|
||||
|
||||
if (this.options.search) {
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`lower(immutable_unaccent("Video"."name")) LIKE lower(immutable_unaccent(${escapedLikeSearch})) OR ` +
|
||||
`lower(immutable_unaccent("VideoImportModel"."targetUrl")) LIKE lower(immutable_unaccent(${escapedLikeSearch})) OR ` +
|
||||
`lower(immutable_unaccent("VideoImportModel"."torrentName")) LIKE lower(immutable_unaccent(${escapedLikeSearch})) OR ` +
|
||||
`lower(immutable_unaccent("VideoImportModel"."magnetUri")) LIKE lower(immutable_unaccent(${escapedLikeSearch}))` +
|
||||
`)`
|
||||
)
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
private buildVideoJoin () {
|
||||
if (this.builtVideoJoin) return
|
||||
|
||||
this.subQueryJoin += ` LEFT JOIN "video" "Video" ON "Video"."id" = "VideoImportModel"."videoId" ` +
|
||||
getChannelJoin({
|
||||
base: 'Video->',
|
||||
on: '"Video"."channelId"',
|
||||
includeAccount: true,
|
||||
includeActors: true,
|
||||
includeAvatars: false,
|
||||
required: false
|
||||
})
|
||||
|
||||
this.builtVideoJoin = true
|
||||
}
|
||||
|
||||
private buildChannelCollaboratorsJoin () {
|
||||
if (this.builtChannelCollaboratorsJoin) return
|
||||
|
||||
this.buildVideoJoin()
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "Video->VideoChannel->VideoChannelCollaborators" ' +
|
||||
'ON "Video->VideoChannel->VideoChannelCollaborators"."channelId" = "Video->VideoChannel"."id" ' +
|
||||
'AND "Video->VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "Video->VideoChannel->VideoChannelCollaborators"."accountId" = :collaborationAccountId '
|
||||
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
this.replacements.collaborationAccountId = this.options.collaborationAccountId
|
||||
|
||||
this.builtChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildThumbnailJoin () {
|
||||
if (this.builtThumbnailJoin) return
|
||||
|
||||
this.join += ' LEFT JOIN "thumbnail" "Video->Thumbnails" ON "Video->Thumbnails"."videoId" = "Video.id" '
|
||||
|
||||
this.builtThumbnailJoin = true
|
||||
}
|
||||
|
||||
private buildTagJoin () {
|
||||
if (this.builtTagJoin) return
|
||||
|
||||
this.join += ' LEFT JOIN "videoTag" ON "videoTag"."videoId" = "Video.id" ' +
|
||||
'LEFT JOIN "tag" "Video->Tags" ON "Video->Tags"."id" = "videoTag"."tagId" '
|
||||
|
||||
this.builtTagJoin = true
|
||||
}
|
||||
|
||||
private buildAccountAvatarsJoin () {
|
||||
if (this.builtAccountAvatarJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({ base: 'Video->VideoChannel->Account->Actor->', on: '"Video.VideoChannel.Account.Actor.id"' })
|
||||
|
||||
this.builtAccountAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({ base: 'Video->VideoChannel->Actor->', on: '"Video.VideoChannel.Actor.id"' })
|
||||
|
||||
this.builtChannelAvatarJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.buildChannelAvatarsJoin()
|
||||
this.buildAccountAvatarsJoin()
|
||||
this.buildTagJoin()
|
||||
this.buildThumbnailJoin()
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getVideoTagAttributes(),
|
||||
this.tableAttributes.getAccountAvatarAttributes(),
|
||||
this.tableAttributes.getChannelAvatarAttributes(),
|
||||
this.tableAttributes.getThumbnailAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
this.buildVideoJoin()
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoImportAttributes(),
|
||||
this.tableAttributes.getVideoAttributes(),
|
||||
|
||||
this.tableAttributes.getChannelAttributes(),
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes(),
|
||||
|
||||
this.tableAttributes.getAccountAttributes(),
|
||||
this.tableAttributes.getAccountActorAttributes(),
|
||||
this.tableAttributes.getAccountServerAttributes()
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { Memoize } from '@server/helpers/memoize.js'
|
||||
import { AccountModel } from '@server/models/account/account.js'
|
||||
import { ActorImageModel } from '@server/models/actor/actor-image.js'
|
||||
import { ActorModel } from '@server/models/actor/actor.js'
|
||||
import { ServerModel } from '@server/models/server/server.js'
|
||||
import { TagModel } from '../../tag.js'
|
||||
import { ThumbnailModel } from '../../thumbnail.js'
|
||||
import { VideoChannelModel } from '../../video-channel.js'
|
||||
import { VideoImportModel } from '../../video-import.js'
|
||||
import { VideoModel } from '../../video.js'
|
||||
|
||||
export class VideoImportTableAttributes {
|
||||
@Memoize()
|
||||
getVideoImportAttributes () {
|
||||
return VideoImportModel.getSQLAttributes('VideoImportModel').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getVideoAttributes () {
|
||||
return VideoModel.getSQLAttributes('Video', 'Video.').join(', ')
|
||||
}
|
||||
|
||||
getVideoTagAttributes () {
|
||||
return TagModel.getSQLAttributes('Video->Tags', 'Video.Tags.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelAttributes () {
|
||||
return VideoChannelModel.getSQLAttributes('Video->VideoChannel', 'Video.VideoChannel.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('Video->VideoChannel->Actor', `Video.VideoChannel.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('Video->VideoChannel->Actor->Server', `Video.VideoChannel.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Video->VideoChannel->Actor->Avatars', 'Video.VideoChannel.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getAccountAttributes () {
|
||||
return AccountModel.getSQLSummaryAttributes('Video->VideoChannel->Account', 'Video.VideoChannel.Account.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('Video->VideoChannel->Account->Actor', `Video.VideoChannel.Account.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('Video->VideoChannel->Account->Actor->Server', `Video.VideoChannel.Account.Actor.Server.`)
|
||||
.join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Video->VideoChannel->Account->Actor->Avatars', 'Video.VideoChannel.Account.Actor.Avatars.')
|
||||
.join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getThumbnailAttributes () {
|
||||
return ThumbnailModel.getSQLAttributes('Video->Thumbnails', 'Video.Thumbnails.').join(', ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
import { ActorImageType, VideoChannelCollaboratorState, VideoPlaylistPrivacy, VideoPlaylistType_Type } from '@peertube/peertube-models'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { buildServerIdsFollowedBy, getPlaylistSort } from '@server/models/shared/index.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { VideoPlaylistTableAttributes } from './video-playlist-table-attributes.js'
|
||||
|
||||
export interface ListVideoPlaylistsOptions extends AbstractListQueryOptions {
|
||||
followerActorId?: number
|
||||
type?: VideoPlaylistType_Type
|
||||
|
||||
accountId?: number
|
||||
includeCollaborationsForAccount?: number
|
||||
|
||||
videoChannelId?: number
|
||||
listMyPlaylists?: boolean
|
||||
search?: string
|
||||
host?: string
|
||||
uuids?: string[]
|
||||
channelNameOneOf?: string[]
|
||||
withVideos?: boolean
|
||||
}
|
||||
|
||||
export class VideoPlaylistListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoPlaylistTableAttributes()
|
||||
|
||||
private builtAccountJoin = false
|
||||
private builtVideoChannelJoin = false
|
||||
private builtChannelCollaboratorsJoin = false
|
||||
private builtAccountAvatarJoin = false
|
||||
private builtChannelAvatarJoin = false
|
||||
private builtThumbnailJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoPlaylistsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoPlaylistModel', tableName: 'videoPlaylist' }, options)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
const where: string[] = []
|
||||
|
||||
if (this.options.host) {
|
||||
this.buildAccountJoin()
|
||||
|
||||
if (this.options.host === WEBSERVER.HOST) {
|
||||
where.push('"OwnerAccount->Actor"."serverId" IS NULL')
|
||||
} else {
|
||||
where.push('"OwnerAccount->Actor->Server"."host" = :host')
|
||||
|
||||
this.replacements.host = this.options.host
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.listMyPlaylists !== true) {
|
||||
where.push('"VideoPlaylistModel"."privacy" = :privacy')
|
||||
|
||||
this.replacements.privacy = VideoPlaylistPrivacy.PUBLIC
|
||||
}
|
||||
|
||||
if (this.options.followerActorId) {
|
||||
this.buildAccountJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`"OwnerAccount->Actor"."serverId" IS NULL OR ` +
|
||||
`"OwnerAccount->Actor"."serverId" IN ${buildServerIdsFollowedBy(this.options.followerActorId)}` +
|
||||
`)`
|
||||
)
|
||||
}
|
||||
|
||||
if (this.options.accountId) {
|
||||
if (this.options.includeCollaborationsForAccount) {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
` "VideoPlaylistModel"."ownerAccountId" = :accountId OR ` +
|
||||
` "VideoChannel->VideoChannelCollaborators"."accountId" = :collaborationAccountId` +
|
||||
`)`
|
||||
)
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
this.replacements.collaborationAccountId = this.options.includeCollaborationsForAccount
|
||||
} else {
|
||||
where.push('"VideoPlaylistModel"."ownerAccountId" = :accountId')
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.videoChannelId) {
|
||||
if (this.options.includeCollaborationsForAccount) {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
` "VideoPlaylistModel"."videoChannelId" = :videoChannelId OR ` +
|
||||
` "VideoChannel->VideoChannelCollaborators"."accountId" = :collaborationAccountId` +
|
||||
`)`
|
||||
)
|
||||
|
||||
this.replacements.videoChannelId = this.options.videoChannelId
|
||||
this.replacements.collaborationAccountId = this.options.includeCollaborationsForAccount
|
||||
} else {
|
||||
where.push('"VideoPlaylistModel"."videoChannelId" = :videoChannelId')
|
||||
|
||||
this.replacements.videoChannelId = this.options.videoChannelId
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.channelNameOneOf && this.options.channelNameOneOf.length !== 0) {
|
||||
this.buildChannelJoin()
|
||||
|
||||
where.push('"VideoChannel->Actor"."preferredUsername" IN (:channelNameOneOf)')
|
||||
this.replacements.channelNameOneOf = this.options.channelNameOneOf
|
||||
}
|
||||
|
||||
if (this.options.type) {
|
||||
where.push('"VideoPlaylistModel"."type" = :type')
|
||||
|
||||
this.replacements.type = this.options.type
|
||||
}
|
||||
|
||||
if (this.options.uuids) {
|
||||
where.push('"VideoPlaylistModel"."uuid" IN (:uuids)')
|
||||
|
||||
this.replacements.uuids = this.options.uuids
|
||||
}
|
||||
|
||||
if (this.options.withVideos === true) {
|
||||
where.push(`(${this.getTotalVideosQuery()}) != 0`)
|
||||
}
|
||||
|
||||
if (this.options.search) {
|
||||
const escapedSearch = this.sequelize.escape(this.options.search)
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
|
||||
|
||||
this.subQueryAttributes.push(
|
||||
`word_similarity(lower(immutable_unaccent(${escapedSearch})), lower(immutable_unaccent("VideoPlaylistModel"."name"))) as similarity`
|
||||
)
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`lower(immutable_unaccent(${escapedSearch})) <% lower(immutable_unaccent("VideoPlaylistModel"."name")) OR ` +
|
||||
`lower(immutable_unaccent("VideoPlaylistModel"."name")) LIKE lower(immutable_unaccent(${escapedLikeSearch}))` +
|
||||
`)`
|
||||
)
|
||||
} else {
|
||||
this.subQueryAttributes.push('0 as similarity')
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
private buildAccountJoin () {
|
||||
if (this.builtAccountJoin) return
|
||||
|
||||
this.subQueryJoin += ' INNER JOIN "account" "OwnerAccount" ON "OwnerAccount"."id" = "VideoPlaylistModel"."ownerAccountId" ' +
|
||||
'INNER JOIN "actor" "OwnerAccount->Actor" ON "OwnerAccount->Actor"."accountId" = "OwnerAccount"."id" ' +
|
||||
'LEFT JOIN "server" "OwnerAccount->Actor->Server" ON "OwnerAccount->Actor"."serverId" = "OwnerAccount->Actor->Server"."id" '
|
||||
|
||||
this.builtAccountJoin = true
|
||||
}
|
||||
|
||||
private buildChannelJoin () {
|
||||
if (this.builtVideoChannelJoin) return
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannel" "VideoChannel" ON "VideoPlaylistModel"."videoChannelId" = "VideoChannel"."id" ' +
|
||||
'LEFT JOIN "actor" "VideoChannel->Actor" ON "VideoChannel->Actor"."videoChannelId" = "VideoChannel"."id" ' +
|
||||
'LEFT JOIN "server" "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id" '
|
||||
|
||||
this.builtVideoChannelJoin = true
|
||||
}
|
||||
|
||||
private buildChannelCollaboratorsJoin () {
|
||||
if (this.builtChannelCollaboratorsJoin) return
|
||||
|
||||
this.buildChannelJoin()
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "VideoChannel->VideoChannelCollaborators" ' +
|
||||
'ON "VideoChannel->VideoChannelCollaborators"."channelId" = "VideoChannel"."id" ' +
|
||||
'AND "VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "VideoChannel->VideoChannelCollaborators"."accountId" = :accountId '
|
||||
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
this.replacements.accountId = this.options.accountId
|
||||
|
||||
this.builtChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
private buildAccountAvatarsJoin () {
|
||||
if (this.builtAccountAvatarJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "OwnerAccount->Actor->Avatars" ` +
|
||||
`ON "OwnerAccount->Actor->Avatars"."actorId" = "VideoPlaylistModel"."OwnerAccount.Actor.id" ` +
|
||||
`AND "OwnerAccount->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
|
||||
this.builtAccountAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "VideoChannel->Actor->Avatars" ` +
|
||||
`ON "VideoChannel->Actor->Avatars"."actorId" = "VideoPlaylistModel"."VideoChannel.Actor.id" ` +
|
||||
`AND "VideoChannel->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
|
||||
this.builtChannelAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildThumbnailJoin () {
|
||||
if (this.builtThumbnailJoin) return
|
||||
|
||||
this.join += ' LEFT JOIN "thumbnail" "Thumbnail" ON "Thumbnail"."videoPlaylistId" = "VideoPlaylistModel"."id" '
|
||||
|
||||
this.builtThumbnailJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.buildChannelAvatarsJoin()
|
||||
this.buildAccountAvatarsJoin()
|
||||
this.buildThumbnailJoin()
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getAccountAvatarAttributes(),
|
||||
this.tableAttributes.getChannelAvatarAttributes(),
|
||||
this.tableAttributes.getThumbnailAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
this.buildAccountJoin()
|
||||
this.buildChannelJoin()
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoPlaylistAttributes(),
|
||||
|
||||
this.tableAttributes.getChannelAttributes(),
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes(),
|
||||
|
||||
this.tableAttributes.getAccountAttributes(),
|
||||
this.tableAttributes.getAccountActorAttributes(),
|
||||
this.tableAttributes.getAccountServerAttributes(),
|
||||
|
||||
this.getTotalVideosAttribute()
|
||||
]
|
||||
}
|
||||
|
||||
private getTotalVideosQuery () {
|
||||
return `SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id"`
|
||||
}
|
||||
|
||||
private getTotalVideosAttribute () {
|
||||
return `(${this.getTotalVideosQuery()}) AS "videosLength"`
|
||||
}
|
||||
|
||||
protected getSort (sort: string) {
|
||||
return getPlaylistSort(sort)
|
||||
}
|
||||
|
||||
protected getCalculatedAttributes () {
|
||||
return [ 'similarity', 'videosLength' ]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { Memoize } from '@server/helpers/memoize.js'
|
||||
import { AccountModel } from '@server/models/account/account.js'
|
||||
import { ActorImageModel } from '@server/models/actor/actor-image.js'
|
||||
import { ActorModel } from '@server/models/actor/actor.js'
|
||||
import { ServerModel } from '@server/models/server/server.js'
|
||||
import { VideoChannelModel } from '../../video-channel.js'
|
||||
import { VideoPlaylistModel } from '../../video-playlist.js'
|
||||
import { ThumbnailModel } from '../../thumbnail.js'
|
||||
|
||||
export class VideoPlaylistTableAttributes {
|
||||
@Memoize()
|
||||
getVideoPlaylistAttributes () {
|
||||
return VideoPlaylistModel.getSQLAttributes('VideoPlaylistModel').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelAttributes () {
|
||||
return VideoChannelModel.getSQLSummaryAttributes('VideoChannel', 'VideoChannel.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('VideoChannel->Actor', `VideoChannel.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('VideoChannel->Actor->Server', `VideoChannel.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('VideoChannel->Actor->Avatars', 'VideoChannel.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getAccountAttributes () {
|
||||
return AccountModel.getSQLSummaryAttributes('OwnerAccount', 'OwnerAccount.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('OwnerAccount->Actor', `OwnerAccount.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('OwnerAccount->Actor->Server', `OwnerAccount.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('OwnerAccount->Actor->Avatars', 'OwnerAccount.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getThumbnailAttributes () {
|
||||
return ThumbnailModel.getSQLAttributes('Thumbnail', 'Thumbnail.').join(', ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import { VideoChannelCollaboratorState } from '@peertube/peertube-models'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { getChannelSyncSort } from '@server/models/shared/index.js'
|
||||
import { OrderItem, Sequelize } from 'sequelize'
|
||||
import { VideoChannelSyncTableAttributes } from './video-channel-sync-table-attributes.js'
|
||||
import { getAvatarsJoin, getChannelJoin } from '@server/models/shared/sql/actor-helpers.js'
|
||||
|
||||
export interface ListVideoChannelSyncsOptions extends AbstractListQueryOptions {
|
||||
accountId: number
|
||||
|
||||
includeCollaborations?: boolean
|
||||
}
|
||||
|
||||
export class VideoChannelSyncListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoChannelSyncTableAttributes()
|
||||
|
||||
private builtChannelJoin = false
|
||||
private builtChannelAvatarsJoin = false
|
||||
private builtChannelCollaboratorsJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoChannelSyncsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoChannelSyncModel', tableName: 'videoChannelSync' }, options)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
const where: string[] = []
|
||||
|
||||
this.buildChannelJoin()
|
||||
|
||||
if (this.options.includeCollaborations) {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`("VideoChannel"."accountId" = :accountId OR "VideoChannel->VideoChannelCollaborators"."accountId" = :accountId)`
|
||||
)
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
} else {
|
||||
where.push(`"VideoChannel"."accountId" = :accountId`)
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildChannelJoin () {
|
||||
if (this.builtChannelJoin) return
|
||||
|
||||
this.subQueryJoin += getChannelJoin({
|
||||
on: `"VideoChannelSyncModel"."videoChannelId"`,
|
||||
includeAccount: false,
|
||||
includeAvatars: false,
|
||||
includeActors: true,
|
||||
required: true
|
||||
})
|
||||
this.builtChannelJoin = true
|
||||
}
|
||||
|
||||
private buildChannelCollaboratorsJoin () {
|
||||
if (this.builtChannelCollaboratorsJoin) return
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "VideoChannel->VideoChannelCollaborators" ' +
|
||||
'ON "VideoChannel->VideoChannelCollaborators"."channelId" = "VideoChannel"."id" ' +
|
||||
'AND "VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "VideoChannel->VideoChannelCollaborators"."accountId" = :accountId '
|
||||
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
this.replacements.accountId = this.options.accountId
|
||||
|
||||
this.builtChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarsJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({ base: 'VideoChannel->Actor->', on: `"VideoChannelSyncModel"."VideoChannel.Actor.id"` })
|
||||
|
||||
this.builtChannelAvatarsJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.buildChannelAvatarsJoin()
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getChannelAvatarAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
this.buildChannelJoin()
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoChannelSyncAttributes(),
|
||||
|
||||
this.tableAttributes.getChannelAttributes(),
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected getSort (sort: string): OrderItem[] {
|
||||
return getChannelSyncSort(sort)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Memoize } from '@server/helpers/memoize.js'
|
||||
import { ActorImageModel } from '@server/models/actor/actor-image.js'
|
||||
import { ActorModel } from '@server/models/actor/actor.js'
|
||||
import { ServerModel } from '@server/models/server/server.js'
|
||||
import { VideoChannelSyncModel } from '../../video-channel-sync.js'
|
||||
import { VideoChannelModel } from '../../video-channel.js'
|
||||
|
||||
export class VideoChannelSyncTableAttributes {
|
||||
@Memoize()
|
||||
getVideoChannelSyncAttributes () {
|
||||
return VideoChannelSyncModel.getSQLAttributes('VideoChannelSyncModel').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelAttributes () {
|
||||
return VideoChannelModel.getSQLSummaryAttributes('VideoChannel', `VideoChannel.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLAPIAttributes('VideoChannel->Actor', `VideoChannel.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('VideoChannel->Actor->Server', `VideoChannel.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('VideoChannel->Actor->Avatars', 'VideoChannel.Actor.Avatars.').join(', ')
|
||||
}
|
||||
}
|
||||
3
server/core/models/video/sql/video/index.ts
Normal file
3
server/core/models/video/sql/video/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './video-model-get-query-builder.js'
|
||||
export * from './videos-id-list-query-builder.js'
|
||||
export * from './videos-model-list-query-builder.js'
|
||||
@@ -0,0 +1,380 @@
|
||||
import { ActorImageType } from '@peertube/peertube-models'
|
||||
import { MUserAccountId } from '@server/types/models/index.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import validator from 'validator'
|
||||
import { AbstractRunQuery } from '../../../../shared/abstract-run-query.js'
|
||||
import { createSafeIn } from '../../../../shared/index.js'
|
||||
import { VideoTableAttributes } from './video-table-attributes.js'
|
||||
|
||||
/**
|
||||
* Abstract builder to create SQL query and fetch video models
|
||||
*/
|
||||
|
||||
export class AbstractVideoQueryBuilder extends AbstractRunQuery {
|
||||
protected attributes: { [key: string]: string } = {}
|
||||
|
||||
protected joins = ''
|
||||
protected where: string
|
||||
|
||||
protected tables: VideoTableAttributes
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly mode: 'list' | 'get'
|
||||
) {
|
||||
super(sequelize)
|
||||
|
||||
this.tables = new VideoTableAttributes(this.mode)
|
||||
}
|
||||
|
||||
protected buildSelect () {
|
||||
return 'SELECT ' + Object.keys(this.attributes).map(key => {
|
||||
const value = this.attributes[key]
|
||||
if (value) return `${key} AS ${value}`
|
||||
|
||||
return key
|
||||
}).join(', ')
|
||||
}
|
||||
|
||||
protected includeChannels () {
|
||||
this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
|
||||
this.addJoin('INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."id" = "VideoChannel->Actor"."videoChannelId"')
|
||||
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"'
|
||||
)
|
||||
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Actor->Avatars" ' +
|
||||
'ON "VideoChannel->Actor"."id" = "VideoChannel->Actor->Avatars"."actorId" ' +
|
||||
`AND "VideoChannel->Actor->Avatars"."type" = ${ActorImageType.AVATAR}`
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
|
||||
...this.buildActorInclude('VideoChannel->Actor'),
|
||||
...this.buildAvatarInclude('VideoChannel->Actor->Avatars'),
|
||||
...this.buildServerInclude('VideoChannel->Actor->Server')
|
||||
}
|
||||
}
|
||||
|
||||
protected includeAccounts () {
|
||||
this.addJoin('INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"')
|
||||
this.addJoin(
|
||||
'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."id" = "VideoChannel->Account->Actor"."accountId"'
|
||||
)
|
||||
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' +
|
||||
'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"'
|
||||
)
|
||||
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Account->Actor->Avatars" ' +
|
||||
'ON "VideoChannel->Account->Actor"."id" = "VideoChannel->Account->Actor->Avatars"."actorId" ' +
|
||||
`AND "VideoChannel->Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}`
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoChannel->Account', this.tables.getAccountAttributes()),
|
||||
...this.buildActorInclude('VideoChannel->Account->Actor'),
|
||||
...this.buildAvatarInclude('VideoChannel->Account->Actor->Avatars'),
|
||||
...this.buildServerInclude('VideoChannel->Account->Actor->Server')
|
||||
}
|
||||
}
|
||||
|
||||
protected includeOwnerUser () {
|
||||
this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
|
||||
this.addJoin('INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"')
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
|
||||
...this.buildAttributesObject('VideoChannel->Account', this.tables.getUserAccountAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeThumbnails () {
|
||||
this.addJoin('LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"')
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('Thumbnails', this.tables.getThumbnailAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeWebVideoFiles () {
|
||||
this.addJoin('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoFiles', this.tables.getFileAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeStreamingPlaylistFiles () {
|
||||
this.addJoin(
|
||||
'LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"'
|
||||
)
|
||||
|
||||
this.addJoin(
|
||||
'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' +
|
||||
'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoStreamingPlaylists', this.tables.getStreamingPlaylistAttributes()),
|
||||
...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.tables.getFileAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeUserHistory (userId: number) {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "userVideoHistory" ' +
|
||||
'ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :userVideoHistoryId'
|
||||
)
|
||||
|
||||
this.replacements.userVideoHistoryId = userId
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('userVideoHistory', this.tables.getUserHistoryAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includePlaylist (playlistId: number) {
|
||||
this.addJoin(
|
||||
'INNER JOIN "videoPlaylistElement" as "VideoPlaylistElement" ON "videoPlaylistElement"."videoId" = "video"."id" ' +
|
||||
'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
|
||||
)
|
||||
|
||||
this.replacements.videoPlaylistId = playlistId
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoPlaylistElement', this.tables.getPlaylistAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeTags () {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN (' +
|
||||
'"videoTag" AS "Tags->VideoTagModel" INNER JOIN "tag" AS "Tags" ON "Tags"."id" = "Tags->VideoTagModel"."tagId"' +
|
||||
') ' +
|
||||
'ON "video"."id" = "Tags->VideoTagModel"."videoId"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('Tags', this.tables.getTagAttributes()),
|
||||
...this.buildAttributesObject('Tags->VideoTagModel', this.tables.getVideoTagAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeBlacklisted () {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "videoBlacklist" AS "VideoBlacklist" ON "video"."id" = "VideoBlacklist"."videoId"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoBlacklist', this.tables.getBlacklistedAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeBlockedOwnerAndServer (serverAccountId: number, user?: MUserAccountId) {
|
||||
const blockerIds = [ serverAccountId ]
|
||||
if (user) blockerIds.push(user.Account.id)
|
||||
|
||||
const inClause = createSafeIn(this.sequelize, blockerIds)
|
||||
|
||||
this.addJoin(
|
||||
'LEFT JOIN "accountBlocklist" AS "VideoChannel->Account->AccountBlocklist" ' +
|
||||
'ON "VideoChannel->Account"."id" = "VideoChannel->Account->AccountBlocklist"."targetAccountId" ' +
|
||||
'AND "VideoChannel->Account->AccountBlocklist"."accountId" IN (' + inClause + ')'
|
||||
)
|
||||
|
||||
this.addJoin(
|
||||
'LEFT JOIN "serverBlocklist" AS "VideoChannel->Account->Actor->Server->ServerBlocklist" ' +
|
||||
'ON "VideoChannel->Account->Actor->Server->ServerBlocklist"."targetServerId" = "VideoChannel->Account->Actor"."serverId" ' +
|
||||
'AND "VideoChannel->Account->Actor->Server->ServerBlocklist"."accountId" IN (' + inClause + ') '
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoChannel->Account->AccountBlocklist', this.tables.getBlocklistAttributes()),
|
||||
...this.buildAttributesObject('VideoChannel->Account->Actor->Server->ServerBlocklist', this.tables.getBlocklistAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeScheduleUpdate () {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "scheduleVideoUpdate" AS "ScheduleVideoUpdate" ON "video"."id" = "ScheduleVideoUpdate"."videoId"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('ScheduleVideoUpdate', this.tables.getScheduleUpdateAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeLive () {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "videoLive" AS "VideoLive" ON "video"."id" = "VideoLive"."videoId"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoLive', this.tables.getLiveAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeLiveSchedules () {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "videoLiveSchedule" AS "VideoLive->VideoLiveSchedules" ' +
|
||||
'ON "VideoLive->VideoLiveSchedules"."liveVideoId" = "VideoLive"."id"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoLive->VideoLiveSchedules', this.tables.getLiveScheduleAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeVideoSource () {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "videoSource" AS "VideoSource" ON "video"."id" = "VideoSource"."videoId"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoSource', this.tables.getVideoSourceAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeAutomaticTags (autoTagOfAccountId: number) {
|
||||
this.addJoin(
|
||||
'LEFT JOIN (' +
|
||||
'"videoAutomaticTag" AS "VideoAutomaticTags" INNER JOIN "automaticTag" AS "VideoAutomaticTags->AutomaticTag" ' +
|
||||
'ON "VideoAutomaticTags->AutomaticTag"."id" = "VideoAutomaticTags"."automaticTagId" ' +
|
||||
') ON "video"."id" = "VideoAutomaticTags"."videoId" AND "VideoAutomaticTags"."accountId" = :autoTagOfAccountId'
|
||||
)
|
||||
|
||||
this.replacements.autoTagOfAccountId = autoTagOfAccountId
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoAutomaticTags', this.tables.getVideoAutoTagAttributes()),
|
||||
...this.buildAttributesObject('VideoAutomaticTags->AutomaticTag', this.tables.getAutoTagAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeTrackers () {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN (' +
|
||||
'"videoTracker" AS "Trackers->VideoTrackerModel" ' +
|
||||
'INNER JOIN "tracker" AS "Trackers" ON "Trackers"."id" = "Trackers->VideoTrackerModel"."trackerId"' +
|
||||
') ON "video"."id" = "Trackers->VideoTrackerModel"."videoId"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('Trackers', this.tables.getTrackerAttributes()),
|
||||
...this.buildAttributesObject('Trackers->VideoTrackerModel', this.tables.getVideoTrackerAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeStreamingPlaylistRedundancies () {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "videoRedundancy" AS "VideoStreamingPlaylists->RedundancyVideos" ' +
|
||||
'ON "VideoStreamingPlaylists"."id" = "VideoStreamingPlaylists->RedundancyVideos"."videoStreamingPlaylistId"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoStreamingPlaylists->RedundancyVideos', this.tables.getRedundancyAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected includeCaptions () {
|
||||
this.addJoin(
|
||||
'LEFT JOIN "videoCaption" AS "VideoCaptions" ON "video"."id" = "VideoCaptions"."videoId"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoCaptions', this.tables.getCaptionAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected buildActorInclude (prefixKey: string) {
|
||||
return this.buildAttributesObject(prefixKey, this.tables.getActorAttributes())
|
||||
}
|
||||
|
||||
protected buildAvatarInclude (prefixKey: string) {
|
||||
return this.buildAttributesObject(prefixKey, this.tables.getAvatarAttributes())
|
||||
}
|
||||
|
||||
protected buildServerInclude (prefixKey: string) {
|
||||
return this.buildAttributesObject(prefixKey, this.tables.getServerAttributes())
|
||||
}
|
||||
|
||||
protected buildAttributesObject (prefixKey: string, attributeKeys: string[]) {
|
||||
const result: { [id: string]: string } = {}
|
||||
|
||||
const prefixValue = prefixKey.replace(/->/g, '.')
|
||||
|
||||
for (const attribute of attributeKeys) {
|
||||
result[`"${prefixKey}"."${attribute}"`] = `"${prefixValue}.${attribute}"`
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
protected whereId (options: { ids?: number[], id?: string | number, url?: string }) {
|
||||
if (options.ids) {
|
||||
this.where = `WHERE "video"."id" IN (${createSafeIn(this.sequelize, options.ids)})`
|
||||
return
|
||||
}
|
||||
|
||||
if (options.url) {
|
||||
this.where = 'WHERE "video"."url" = :videoUrl'
|
||||
this.replacements.videoUrl = options.url
|
||||
return
|
||||
}
|
||||
|
||||
if (validator.default.isInt('' + options.id)) {
|
||||
this.where = 'WHERE "video".id = :videoId'
|
||||
} else {
|
||||
this.where = 'WHERE uuid = :videoId'
|
||||
}
|
||||
|
||||
this.replacements.videoId = options.id
|
||||
}
|
||||
|
||||
protected addJoin (join: string) {
|
||||
this.joins += join + ' '
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { Sequelize, Transaction } from 'sequelize'
|
||||
import { AbstractVideoQueryBuilder } from './abstract-video-query-builder.js'
|
||||
|
||||
export type FileQueryOptions = {
|
||||
id?: string | number
|
||||
url?: string
|
||||
|
||||
includeRedundancy: boolean
|
||||
|
||||
transaction?: Transaction
|
||||
|
||||
logging?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch files (web videos and streaming playlist) according to a video
|
||||
*/
|
||||
|
||||
export class VideoFileQueryBuilder extends AbstractVideoQueryBuilder {
|
||||
constructor (protected readonly sequelize: Sequelize) {
|
||||
super(sequelize, 'get')
|
||||
}
|
||||
|
||||
queryWebVideos (options: FileQueryOptions) {
|
||||
this.buildWebVideoFilesQuery(options)
|
||||
|
||||
return this.runQuery(options)
|
||||
}
|
||||
|
||||
queryStreamingPlaylistVideos (options: FileQueryOptions) {
|
||||
this.buildVideoStreamingPlaylistFilesQuery(options)
|
||||
|
||||
return this.runQuery(options)
|
||||
}
|
||||
|
||||
private buildWebVideoFilesQuery (options: FileQueryOptions) {
|
||||
this.attributes = {
|
||||
'"video"."id"': ''
|
||||
}
|
||||
|
||||
this.includeWebVideoFiles()
|
||||
|
||||
this.whereId(options)
|
||||
|
||||
this.query = this.buildQuery()
|
||||
}
|
||||
|
||||
private buildVideoStreamingPlaylistFilesQuery (options: FileQueryOptions) {
|
||||
this.attributes = {
|
||||
'"video"."id"': ''
|
||||
}
|
||||
|
||||
this.includeStreamingPlaylistFiles()
|
||||
|
||||
if (options.includeRedundancy) {
|
||||
this.includeStreamingPlaylistRedundancies()
|
||||
}
|
||||
|
||||
this.whereId(options)
|
||||
|
||||
this.query = this.buildQuery()
|
||||
}
|
||||
|
||||
private buildQuery () {
|
||||
return `${this.buildSelect()} FROM "video" ${this.joins} ${this.where}`
|
||||
}
|
||||
}
|
||||
500
server/core/models/video/sql/video/shared/video-model-builder.ts
Normal file
500
server/core/models/video/sql/video/shared/video-model-builder.ts
Normal file
@@ -0,0 +1,500 @@
|
||||
import { VideoInclude, VideoIncludeType } from '@peertube/peertube-models'
|
||||
import { AccountBlocklistModel } from '@server/models/account/account-blocklist.js'
|
||||
import { AccountModel } from '@server/models/account/account.js'
|
||||
import { ActorImageModel } from '@server/models/actor/actor-image.js'
|
||||
import { ActorModel } from '@server/models/actor/actor.js'
|
||||
import { AutomaticTagModel } from '@server/models/automatic-tag/automatic-tag.js'
|
||||
import { VideoAutomaticTagModel } from '@server/models/automatic-tag/video-automatic-tag.js'
|
||||
import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy.js'
|
||||
import { ServerBlocklistModel } from '@server/models/server/server-blocklist.js'
|
||||
import { ServerModel } from '@server/models/server/server.js'
|
||||
import { TrackerModel } from '@server/models/server/tracker.js'
|
||||
import { UserVideoHistoryModel } from '@server/models/user/user-video-history.js'
|
||||
import { VideoCaptionModel } from '@server/models/video/video-caption.js'
|
||||
import { VideoLiveScheduleModel } from '@server/models/video/video-live-schedule.js'
|
||||
import { VideoSourceModel } from '@server/models/video/video-source.js'
|
||||
import { ScheduleVideoUpdateModel } from '../../../schedule-video-update.js'
|
||||
import { TagModel } from '../../../tag.js'
|
||||
import { ThumbnailModel } from '../../../thumbnail.js'
|
||||
import { VideoBlacklistModel } from '../../../video-blacklist.js'
|
||||
import { VideoChannelModel } from '../../../video-channel.js'
|
||||
import { VideoFileModel } from '../../../video-file.js'
|
||||
import { VideoLiveModel } from '../../../video-live.js'
|
||||
import { VideoStreamingPlaylistModel } from '../../../video-streaming-playlist.js'
|
||||
import { VideoModel } from '../../../video.js'
|
||||
import { VideoTableAttributes } from './video-table-attributes.js'
|
||||
|
||||
type SQLRow = { [id: string]: string | number }
|
||||
|
||||
/**
|
||||
* Build video models from SQL rows
|
||||
*/
|
||||
|
||||
export class VideoModelBuilder {
|
||||
private videosMemo: { [id: number]: VideoModel }
|
||||
private videoStreamingPlaylistMemo: { [id: number]: VideoStreamingPlaylistModel }
|
||||
private videoFileMemo: { [id: number]: VideoFileModel }
|
||||
|
||||
private thumbnailsDone: Set<any>
|
||||
private actorImagesDone: Set<any>
|
||||
private historyDone: Set<any>
|
||||
private blacklistDone: Set<any>
|
||||
private accountBlocklistDone: Set<any>
|
||||
private serverBlocklistDone: Set<any>
|
||||
private liveDone: Set<any>
|
||||
private sourceDone: Set<any>
|
||||
private liveScheduleDone: Set<any>
|
||||
private redundancyDone: Set<any>
|
||||
private scheduleVideoUpdateDone: Set<any>
|
||||
|
||||
private trackersDone: Set<string>
|
||||
private tagsDone: Set<string>
|
||||
private autoTagsDone: Set<string>
|
||||
private captionsDone: Set<any>
|
||||
|
||||
private videos: VideoModel[]
|
||||
|
||||
private readonly buildOpts = { raw: true, isNewRecord: false }
|
||||
|
||||
constructor (
|
||||
private readonly mode: 'get' | 'list',
|
||||
private readonly tables: VideoTableAttributes
|
||||
) {
|
||||
}
|
||||
|
||||
buildVideosFromRows (options: {
|
||||
rows: SQLRow[]
|
||||
addCaptions: boolean
|
||||
include?: VideoIncludeType
|
||||
rowsWebVideoFiles?: SQLRow[]
|
||||
rowsStreamingPlaylist?: SQLRow[]
|
||||
}) {
|
||||
const { rows, rowsWebVideoFiles, rowsStreamingPlaylist, include, addCaptions } = options
|
||||
|
||||
this.reinit()
|
||||
|
||||
for (const row of rows) {
|
||||
this.buildVideoAndAccount(row, { addCaptions })
|
||||
|
||||
const videoModel = this.videosMemo[row.id as number]
|
||||
|
||||
this.setUserHistory(row, videoModel)
|
||||
this.addThumbnail(row, videoModel)
|
||||
this.setLive(row, videoModel)
|
||||
this.addLiveSchedule(row, videoModel)
|
||||
|
||||
const channelActor = videoModel.VideoChannel?.Actor
|
||||
if (channelActor) {
|
||||
this.addActorAvatar(row, 'VideoChannel.Actor', channelActor)
|
||||
}
|
||||
|
||||
const accountActor = videoModel.VideoChannel?.Account?.Actor
|
||||
if (accountActor) {
|
||||
this.addActorAvatar(row, 'VideoChannel.Account.Actor', accountActor)
|
||||
}
|
||||
|
||||
if (!rowsWebVideoFiles) {
|
||||
this.addWebVideoFile(row, videoModel)
|
||||
}
|
||||
|
||||
if (!rowsStreamingPlaylist) {
|
||||
this.addStreamingPlaylist(row, videoModel)
|
||||
this.addStreamingPlaylistFile(row)
|
||||
}
|
||||
|
||||
if (this.mode === 'get') {
|
||||
this.addTag(row, videoModel)
|
||||
this.addTracker(row, videoModel)
|
||||
this.addCaption(row, videoModel)
|
||||
this.setBlacklisted(row, videoModel)
|
||||
this.setScheduleVideoUpdate(row, videoModel)
|
||||
} else {
|
||||
if (include & VideoInclude.BLACKLISTED) {
|
||||
this.setBlacklisted(row, videoModel)
|
||||
}
|
||||
|
||||
if (include & VideoInclude.BLOCKED_OWNER) {
|
||||
this.setBlockedOwner(row, videoModel)
|
||||
this.setBlockedServer(row, videoModel)
|
||||
}
|
||||
|
||||
if (include & VideoInclude.NOT_PUBLISHED_STATE) {
|
||||
this.setScheduleVideoUpdate(row, videoModel)
|
||||
}
|
||||
|
||||
if (include & VideoInclude.SOURCE) {
|
||||
this.setSource(row, videoModel)
|
||||
}
|
||||
|
||||
if (include & VideoInclude.AUTOMATIC_TAGS) {
|
||||
this.addAutoTag(row, videoModel)
|
||||
}
|
||||
|
||||
if (include & VideoInclude.TAGS) {
|
||||
this.addTag(row, videoModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.grabSeparateWebVideoFiles(rowsWebVideoFiles)
|
||||
this.grabSeparateStreamingPlaylistFiles(rowsStreamingPlaylist)
|
||||
|
||||
return this.videos
|
||||
}
|
||||
|
||||
private reinit () {
|
||||
this.videosMemo = {}
|
||||
this.videoStreamingPlaylistMemo = {}
|
||||
this.videoFileMemo = {}
|
||||
|
||||
this.thumbnailsDone = new Set()
|
||||
this.actorImagesDone = new Set()
|
||||
this.historyDone = new Set()
|
||||
this.blacklistDone = new Set()
|
||||
this.liveDone = new Set()
|
||||
this.sourceDone = new Set()
|
||||
this.redundancyDone = new Set()
|
||||
this.scheduleVideoUpdateDone = new Set()
|
||||
this.liveScheduleDone = new Set()
|
||||
|
||||
this.accountBlocklistDone = new Set()
|
||||
this.serverBlocklistDone = new Set()
|
||||
|
||||
this.trackersDone = new Set()
|
||||
this.tagsDone = new Set()
|
||||
this.autoTagsDone = new Set()
|
||||
this.captionsDone = new Set()
|
||||
|
||||
this.videos = []
|
||||
}
|
||||
|
||||
private grabSeparateWebVideoFiles (rowsWebVideoFiles?: SQLRow[]) {
|
||||
if (!rowsWebVideoFiles) return
|
||||
|
||||
for (const row of rowsWebVideoFiles) {
|
||||
const id = row['VideoFiles.id']
|
||||
if (!id) continue
|
||||
|
||||
const videoModel = this.videosMemo[row.id]
|
||||
this.addWebVideoFile(row, videoModel)
|
||||
}
|
||||
}
|
||||
|
||||
private grabSeparateStreamingPlaylistFiles (rowsStreamingPlaylist?: SQLRow[]) {
|
||||
if (!rowsStreamingPlaylist) return
|
||||
|
||||
for (const row of rowsStreamingPlaylist) {
|
||||
const id = row['VideoStreamingPlaylists.id']
|
||||
if (!id) continue
|
||||
|
||||
const videoModel = this.videosMemo[row.id]
|
||||
|
||||
this.addStreamingPlaylist(row, videoModel)
|
||||
this.addStreamingPlaylistFile(row)
|
||||
this.addRedundancy(row, 'VideoStreamingPlaylists', this.videoStreamingPlaylistMemo[id])
|
||||
}
|
||||
}
|
||||
|
||||
private buildVideoAndAccount (row: SQLRow, options: {
|
||||
addCaptions: boolean
|
||||
}) {
|
||||
if (this.videosMemo[row.id]) return
|
||||
|
||||
const videoModel = new VideoModel(this.grab(row, this.tables.getVideoAttributes(), ''), this.buildOpts)
|
||||
|
||||
videoModel.UserVideoHistories = []
|
||||
videoModel.Thumbnails = []
|
||||
videoModel.VideoFiles = []
|
||||
videoModel.VideoStreamingPlaylists = []
|
||||
videoModel.Tags = []
|
||||
videoModel.VideoAutomaticTags = []
|
||||
videoModel.Trackers = []
|
||||
|
||||
if (options.addCaptions === true) {
|
||||
videoModel.VideoCaptions = []
|
||||
}
|
||||
|
||||
this.buildAccount(row, videoModel)
|
||||
|
||||
this.videosMemo[row.id] = videoModel
|
||||
|
||||
// Keep rows order
|
||||
this.videos.push(videoModel)
|
||||
}
|
||||
|
||||
private buildAccount (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoChannel.Account.id']
|
||||
if (!id) return
|
||||
|
||||
const channelModel = new VideoChannelModel(this.grab(row, this.tables.getChannelAttributes(), 'VideoChannel'), this.buildOpts)
|
||||
channelModel.Actor = this.buildActor(row, 'VideoChannel')
|
||||
|
||||
const accountModel = new AccountModel(this.grab(row, this.tables.getAccountAttributes(), 'VideoChannel.Account'), this.buildOpts)
|
||||
accountModel.Actor = this.buildActor(row, 'VideoChannel.Account')
|
||||
|
||||
accountModel.BlockedBy = []
|
||||
|
||||
channelModel.Account = accountModel
|
||||
|
||||
videoModel.VideoChannel = channelModel
|
||||
}
|
||||
|
||||
private buildActor (row: SQLRow, prefix: string) {
|
||||
const actorPrefix = `${prefix}.Actor`
|
||||
const serverPrefix = `${actorPrefix}.Server`
|
||||
|
||||
const serverModel = row[`${serverPrefix}.id`] !== null
|
||||
? new ServerModel(this.grab(row, this.tables.getServerAttributes(), serverPrefix), this.buildOpts)
|
||||
: null
|
||||
|
||||
if (serverModel) serverModel.BlockedBy = []
|
||||
|
||||
const actorModel = new ActorModel(this.grab(row, this.tables.getActorAttributes(), actorPrefix), this.buildOpts)
|
||||
actorModel.Server = serverModel
|
||||
actorModel.Avatars = []
|
||||
|
||||
return actorModel
|
||||
}
|
||||
|
||||
private setUserHistory (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['userVideoHistory.id']
|
||||
if (!id || this.historyDone.has(id)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getUserHistoryAttributes(), 'userVideoHistory')
|
||||
const historyModel = new UserVideoHistoryModel(attributes, this.buildOpts)
|
||||
videoModel.UserVideoHistories.push(historyModel)
|
||||
|
||||
this.historyDone.add(id)
|
||||
}
|
||||
|
||||
private addActorAvatar (row: SQLRow, actorPrefix: string, actor: ActorModel) {
|
||||
const avatarPrefix = `${actorPrefix}.Avatars`
|
||||
const id = row[`${avatarPrefix}.id`]
|
||||
const key = `${row.id}${id}`
|
||||
|
||||
if (!id || this.actorImagesDone.has(key)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getAvatarAttributes(), avatarPrefix)
|
||||
const avatarModel = new ActorImageModel(attributes, this.buildOpts)
|
||||
actor.Avatars.push(avatarModel)
|
||||
|
||||
this.actorImagesDone.add(key)
|
||||
}
|
||||
|
||||
private addThumbnail (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['Thumbnails.id']
|
||||
if (!id || this.thumbnailsDone.has(id)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getThumbnailAttributes(), 'Thumbnails')
|
||||
const thumbnailModel = new ThumbnailModel(attributes, this.buildOpts)
|
||||
videoModel.Thumbnails.push(thumbnailModel)
|
||||
|
||||
this.thumbnailsDone.add(id)
|
||||
}
|
||||
|
||||
private addWebVideoFile (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoFiles.id']
|
||||
if (!id || this.videoFileMemo[id]) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoFiles')
|
||||
const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
|
||||
videoModel.VideoFiles.push(videoFileModel)
|
||||
|
||||
this.videoFileMemo[id] = videoFileModel
|
||||
}
|
||||
|
||||
private addStreamingPlaylist (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoStreamingPlaylists.id']
|
||||
if (!id || this.videoStreamingPlaylistMemo[id]) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getStreamingPlaylistAttributes(), 'VideoStreamingPlaylists')
|
||||
const streamingPlaylist = new VideoStreamingPlaylistModel(attributes, this.buildOpts)
|
||||
streamingPlaylist.VideoFiles = []
|
||||
|
||||
videoModel.VideoStreamingPlaylists.push(streamingPlaylist)
|
||||
|
||||
this.videoStreamingPlaylistMemo[id] = streamingPlaylist
|
||||
}
|
||||
|
||||
private addStreamingPlaylistFile (row: SQLRow) {
|
||||
const id = row['VideoStreamingPlaylists.VideoFiles.id']
|
||||
if (!id || this.videoFileMemo[id]) return
|
||||
|
||||
const streamingPlaylist = this.videoStreamingPlaylistMemo[row['VideoStreamingPlaylists.id']]
|
||||
|
||||
const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoStreamingPlaylists.VideoFiles')
|
||||
const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
|
||||
streamingPlaylist.VideoFiles.push(videoFileModel)
|
||||
|
||||
this.videoFileMemo[id] = videoFileModel
|
||||
}
|
||||
|
||||
private addRedundancy (row: SQLRow, prefix: string, to: VideoStreamingPlaylistModel) {
|
||||
if (!to.RedundancyVideos) to.RedundancyVideos = []
|
||||
|
||||
const redundancyPrefix = `${prefix}.RedundancyVideos`
|
||||
const id = row[`${redundancyPrefix}.id`]
|
||||
|
||||
if (!id || this.redundancyDone.has(id)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getRedundancyAttributes(), redundancyPrefix)
|
||||
const redundancyModel = new VideoRedundancyModel(attributes, this.buildOpts)
|
||||
to.RedundancyVideos.push(redundancyModel)
|
||||
|
||||
this.redundancyDone.add(id)
|
||||
}
|
||||
|
||||
private addTag (row: SQLRow, videoModel: VideoModel) {
|
||||
if (!row['Tags.name']) return
|
||||
|
||||
const key = `${row['Tags.VideoTagModel.videoId']}-${row['Tags.VideoTagModel.tagId']}`
|
||||
if (this.tagsDone.has(key)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getTagAttributes(), 'Tags')
|
||||
const tagModel = new TagModel(attributes, this.buildOpts)
|
||||
videoModel.Tags.push(tagModel)
|
||||
|
||||
this.tagsDone.add(key)
|
||||
}
|
||||
|
||||
private addAutoTag (row: SQLRow, videoModel: VideoModel) {
|
||||
if (!row['VideoAutomaticTags.AutomaticTag.id']) return
|
||||
|
||||
const key = `${row['VideoAutomaticTags.videoId']}-${row['VideoAutomaticTags.accountId']}-${row['VideoAutomaticTags.automaticTagId']}`
|
||||
if (this.autoTagsDone.has(key)) return
|
||||
|
||||
const videoAutomaticTagAttributes = this.grab(row, this.tables.getVideoAutoTagAttributes(), 'VideoAutomaticTags')
|
||||
const automaticTagModel = new VideoAutomaticTagModel(videoAutomaticTagAttributes, this.buildOpts)
|
||||
|
||||
const automaticTagAttributes = this.grab(row, this.tables.getAutoTagAttributes(), 'VideoAutomaticTags.AutomaticTag')
|
||||
automaticTagModel.AutomaticTag = new AutomaticTagModel(automaticTagAttributes, this.buildOpts)
|
||||
|
||||
videoModel.VideoAutomaticTags.push(automaticTagModel)
|
||||
|
||||
this.autoTagsDone.add(key)
|
||||
}
|
||||
|
||||
private addTracker (row: SQLRow, videoModel: VideoModel) {
|
||||
if (!row['Trackers.id']) return
|
||||
|
||||
const key = `${row['Trackers.VideoTrackerModel.videoId']}-${row['Trackers.VideoTrackerModel.trackerId']}`
|
||||
if (this.trackersDone.has(key)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getTrackerAttributes(), 'Trackers')
|
||||
const trackerModel = new TrackerModel(attributes, this.buildOpts)
|
||||
videoModel.Trackers.push(trackerModel)
|
||||
|
||||
this.trackersDone.add(key)
|
||||
}
|
||||
|
||||
private addCaption (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoCaptions.id']
|
||||
|
||||
if (!id || this.captionsDone.has(id)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getCaptionAttributes(), 'VideoCaptions')
|
||||
const captionModel = new VideoCaptionModel(attributes, this.buildOpts)
|
||||
|
||||
videoModel.VideoCaptions.push(captionModel)
|
||||
|
||||
this.captionsDone.add(id)
|
||||
}
|
||||
|
||||
private setBlacklisted (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoBlacklist.id']
|
||||
if (!id || this.blacklistDone.has(id)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getBlacklistedAttributes(), 'VideoBlacklist')
|
||||
videoModel.VideoBlacklist = new VideoBlacklistModel(attributes, this.buildOpts)
|
||||
|
||||
this.blacklistDone.add(id)
|
||||
}
|
||||
|
||||
private setBlockedOwner (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoChannel.Account.AccountBlocklist.id']
|
||||
if (!id) return
|
||||
|
||||
const key = `${videoModel.id}-${id}`
|
||||
if (this.accountBlocklistDone.has(key)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getBlocklistAttributes(), 'VideoChannel.Account.AccountBlocklist')
|
||||
videoModel.VideoChannel.Account.BlockedBy.push(new AccountBlocklistModel(attributes, this.buildOpts))
|
||||
|
||||
this.accountBlocklistDone.add(key)
|
||||
}
|
||||
|
||||
private setBlockedServer (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoChannel.Account.Actor.Server.ServerBlocklist.id']
|
||||
if (!id || this.serverBlocklistDone.has(id)) return
|
||||
|
||||
const key = `${videoModel.id}-${id}`
|
||||
if (this.serverBlocklistDone.has(key)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getBlocklistAttributes(), 'VideoChannel.Account.Actor.Server.ServerBlocklist')
|
||||
videoModel.VideoChannel.Account.Actor.Server.BlockedBy.push(new ServerBlocklistModel(attributes, this.buildOpts))
|
||||
|
||||
this.serverBlocklistDone.add(key)
|
||||
}
|
||||
|
||||
private setScheduleVideoUpdate (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['ScheduleVideoUpdate.id']
|
||||
if (!id || this.scheduleVideoUpdateDone.has(id)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getScheduleUpdateAttributes(), 'ScheduleVideoUpdate')
|
||||
videoModel.ScheduleVideoUpdate = new ScheduleVideoUpdateModel(attributes, this.buildOpts)
|
||||
|
||||
this.scheduleVideoUpdateDone.add(id)
|
||||
}
|
||||
|
||||
private setLive (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoLive.id']
|
||||
if (!id || this.liveDone.has(id)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getLiveAttributes(), 'VideoLive')
|
||||
videoModel.VideoLive = new VideoLiveModel(attributes, this.buildOpts)
|
||||
|
||||
this.liveDone.add(id)
|
||||
}
|
||||
|
||||
private addLiveSchedule (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoLive.VideoLiveSchedules.id']
|
||||
if (!id) return
|
||||
if (this.liveScheduleDone.has(id)) return
|
||||
|
||||
const videoLiveScheduleAttributes = this.grab(row, this.tables.getLiveScheduleAttributes(), 'VideoLive.VideoLiveSchedules')
|
||||
|
||||
const liveScheduleModel = new VideoLiveScheduleModel(videoLiveScheduleAttributes, this.buildOpts)
|
||||
|
||||
if (!videoModel.VideoLive.LiveSchedules) {
|
||||
videoModel.VideoLive.LiveSchedules = []
|
||||
}
|
||||
|
||||
videoModel.VideoLive.LiveSchedules.push(liveScheduleModel)
|
||||
|
||||
this.liveScheduleDone.add(id)
|
||||
}
|
||||
|
||||
private setSource (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoSource.id']
|
||||
if (!id || this.sourceDone.has(id)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getVideoSourceAttributes(), 'VideoSource')
|
||||
videoModel.VideoSource = new VideoSourceModel(attributes, this.buildOpts)
|
||||
|
||||
this.sourceDone.add(id)
|
||||
}
|
||||
|
||||
private grab (row: SQLRow, attributes: string[], prefix: string) {
|
||||
const result: { [id: string]: string | number } = {}
|
||||
|
||||
for (const a of attributes) {
|
||||
const key = prefix
|
||||
? prefix + '.' + a
|
||||
: a
|
||||
|
||||
result[a] = row[key]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
/**
|
||||
* Class to build video attributes/join names we want to fetch from the database
|
||||
*/
|
||||
export class VideoTableAttributes {
|
||||
constructor (private readonly mode: 'get' | 'list') {
|
||||
}
|
||||
|
||||
getChannelAttributesForUser () {
|
||||
return [ 'id', 'accountId' ]
|
||||
}
|
||||
|
||||
getChannelAttributes () {
|
||||
let attributeKeys = [
|
||||
'id',
|
||||
'name',
|
||||
'description',
|
||||
'accountId'
|
||||
]
|
||||
|
||||
if (this.mode === 'get') {
|
||||
attributeKeys = attributeKeys.concat([
|
||||
'support',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
])
|
||||
}
|
||||
|
||||
return attributeKeys
|
||||
}
|
||||
|
||||
getUserAccountAttributes () {
|
||||
return [ 'id', 'userId' ]
|
||||
}
|
||||
|
||||
getAccountAttributes () {
|
||||
let attributeKeys = [ 'id', 'name' ]
|
||||
|
||||
if (this.mode === 'get') {
|
||||
attributeKeys = attributeKeys.concat([
|
||||
'description',
|
||||
'userId',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
])
|
||||
}
|
||||
|
||||
return attributeKeys
|
||||
}
|
||||
|
||||
getThumbnailAttributes () {
|
||||
let attributeKeys = [ 'id', 'type', 'filename' ]
|
||||
|
||||
if (this.mode === 'get') {
|
||||
attributeKeys = attributeKeys.concat([
|
||||
'height',
|
||||
'width',
|
||||
'fileUrl',
|
||||
'onDisk',
|
||||
'automaticallyGenerated',
|
||||
'videoId',
|
||||
'videoPlaylistId',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
])
|
||||
}
|
||||
|
||||
return attributeKeys
|
||||
}
|
||||
|
||||
getFileAttributes () {
|
||||
return [
|
||||
'id',
|
||||
'createdAt',
|
||||
'updatedAt',
|
||||
'resolution',
|
||||
'size',
|
||||
'extname',
|
||||
'filename',
|
||||
'fileUrl',
|
||||
'torrentFilename',
|
||||
'torrentUrl',
|
||||
'infoHash',
|
||||
'fps',
|
||||
'metadataUrl',
|
||||
'videoStreamingPlaylistId',
|
||||
'videoId',
|
||||
'width',
|
||||
'height',
|
||||
'formatFlags',
|
||||
'streams',
|
||||
'storage'
|
||||
]
|
||||
}
|
||||
|
||||
getStreamingPlaylistAttributes () {
|
||||
return [
|
||||
'id',
|
||||
'playlistUrl',
|
||||
'playlistFilename',
|
||||
'type',
|
||||
'p2pMediaLoaderInfohashes',
|
||||
'p2pMediaLoaderPeerVersion',
|
||||
'segmentsSha256Filename',
|
||||
'segmentsSha256Url',
|
||||
'videoId',
|
||||
'createdAt',
|
||||
'updatedAt',
|
||||
'storage'
|
||||
]
|
||||
}
|
||||
|
||||
getUserHistoryAttributes () {
|
||||
return [ 'id', 'currentTime' ]
|
||||
}
|
||||
|
||||
getPlaylistAttributes () {
|
||||
return [
|
||||
'createdAt',
|
||||
'updatedAt',
|
||||
'url',
|
||||
'position',
|
||||
'startTimestamp',
|
||||
'stopTimestamp',
|
||||
'videoPlaylistId'
|
||||
]
|
||||
}
|
||||
|
||||
getTagAttributes () {
|
||||
return [ 'id', 'name' ]
|
||||
}
|
||||
|
||||
getVideoTagAttributes () {
|
||||
return [ 'videoId', 'tagId', 'createdAt', 'updatedAt' ]
|
||||
}
|
||||
|
||||
getBlacklistedAttributes () {
|
||||
return [ 'id', 'reason', 'unfederated' ]
|
||||
}
|
||||
|
||||
getBlocklistAttributes () {
|
||||
return [ 'id' ]
|
||||
}
|
||||
|
||||
getScheduleUpdateAttributes () {
|
||||
return [
|
||||
'id',
|
||||
'updateAt',
|
||||
'privacy',
|
||||
'videoId',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
]
|
||||
}
|
||||
|
||||
getLiveAttributes () {
|
||||
return [
|
||||
'id',
|
||||
'streamKey',
|
||||
'saveReplay',
|
||||
'permanentLive',
|
||||
'latencyMode',
|
||||
'videoId',
|
||||
'replaySettingId',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
]
|
||||
}
|
||||
|
||||
getLiveScheduleAttributes () {
|
||||
return [
|
||||
'id',
|
||||
'startAt',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
]
|
||||
}
|
||||
|
||||
getVideoSourceAttributes () {
|
||||
return [
|
||||
'id',
|
||||
'inputFilename',
|
||||
'keptOriginalFilename',
|
||||
'resolution',
|
||||
'size',
|
||||
'width',
|
||||
'height',
|
||||
'fps',
|
||||
'metadata',
|
||||
'createdAt'
|
||||
]
|
||||
}
|
||||
|
||||
getTrackerAttributes () {
|
||||
return [ 'id', 'url' ]
|
||||
}
|
||||
|
||||
getVideoTrackerAttributes () {
|
||||
return [
|
||||
'videoId',
|
||||
'trackerId',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
]
|
||||
}
|
||||
|
||||
getVideoAutoTagAttributes () {
|
||||
return [ 'videoId', 'accountId', 'automaticTagId' ]
|
||||
}
|
||||
|
||||
getAutoTagAttributes () {
|
||||
return [ 'id', 'name' ]
|
||||
}
|
||||
|
||||
getRedundancyAttributes () {
|
||||
return [ 'id', 'fileUrl' ]
|
||||
}
|
||||
|
||||
getCaptionAttributes () {
|
||||
return [ 'id', 'language', 'fileUrl', 'storage', 'filename', 'automaticallyGenerated', 'm3u8Filename', 'm3u8Url' ]
|
||||
}
|
||||
|
||||
getActorAttributes () {
|
||||
let attributeKeys = [
|
||||
'id',
|
||||
'preferredUsername',
|
||||
'url',
|
||||
'serverId',
|
||||
'accountId',
|
||||
'videoChannelId'
|
||||
]
|
||||
|
||||
if (this.mode === 'get') {
|
||||
attributeKeys = attributeKeys.concat([
|
||||
'type',
|
||||
'followersCount',
|
||||
'followingCount',
|
||||
'inboxUrl',
|
||||
'outboxUrl',
|
||||
'sharedInboxUrl',
|
||||
'followersUrl',
|
||||
'followingUrl',
|
||||
'remoteCreatedAt',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
])
|
||||
}
|
||||
|
||||
return attributeKeys
|
||||
}
|
||||
|
||||
getAvatarAttributes () {
|
||||
let attributeKeys = [
|
||||
'id',
|
||||
'width',
|
||||
'filename',
|
||||
'type',
|
||||
'fileUrl',
|
||||
'onDisk',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
]
|
||||
|
||||
if (this.mode === 'get') {
|
||||
attributeKeys = attributeKeys.concat([
|
||||
'height',
|
||||
'width',
|
||||
'type'
|
||||
])
|
||||
}
|
||||
|
||||
return attributeKeys
|
||||
}
|
||||
|
||||
getServerAttributes () {
|
||||
return [ 'id', 'host' ]
|
||||
}
|
||||
|
||||
getVideoAttributes () {
|
||||
return [
|
||||
'id',
|
||||
'uuid',
|
||||
'name',
|
||||
'category',
|
||||
'licence',
|
||||
'language',
|
||||
'privacy',
|
||||
'nsfw',
|
||||
'nsfwSummary',
|
||||
'nsfwFlags',
|
||||
'description',
|
||||
'support',
|
||||
'duration',
|
||||
'views',
|
||||
'likes',
|
||||
'dislikes',
|
||||
'remote',
|
||||
'isLive',
|
||||
'aspectRatio',
|
||||
'url',
|
||||
'commentsPolicy',
|
||||
'downloadEnabled',
|
||||
'waitTranscoding',
|
||||
'state',
|
||||
'publishedAt',
|
||||
'originallyPublishedAt',
|
||||
'inputFileUpdatedAt',
|
||||
'channelId',
|
||||
'createdAt',
|
||||
'updatedAt',
|
||||
'moveJobsRunning',
|
||||
'comments'
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
import { pick } from '@peertube/peertube-core-utils'
|
||||
import { Sequelize, Transaction } from 'sequelize'
|
||||
import { AbstractVideoQueryBuilder } from './shared/abstract-video-query-builder.js'
|
||||
import { VideoFileQueryBuilder } from './shared/video-file-query-builder.js'
|
||||
import { VideoModelBuilder } from './shared/video-model-builder.js'
|
||||
import { VideoTableAttributes } from './shared/video-table-attributes.js'
|
||||
|
||||
/**
|
||||
* Build a GET SQL query, fetch rows and create the video model
|
||||
*/
|
||||
|
||||
export type GetType =
|
||||
| 'api'
|
||||
| 'full'
|
||||
| 'account-blacklist-files'
|
||||
| 'account'
|
||||
| 'all-files'
|
||||
| 'thumbnails'
|
||||
| 'thumbnails-blacklist'
|
||||
| 'id'
|
||||
| 'blacklist-rights'
|
||||
| 'seo'
|
||||
|
||||
const videoFilesInclude = new Set<GetType>([ 'api', 'full', 'account-blacklist-files', 'all-files' ])
|
||||
const captionsInclude = new Set<GetType>([ 'seo' ])
|
||||
|
||||
const trackersInclude = new Set<GetType>([ 'api' ])
|
||||
const liveInclude = new Set<GetType>([ 'api', 'full' ])
|
||||
const scheduleUpdateInclude = new Set<GetType>([ 'api', 'full' ])
|
||||
const tagsInclude = new Set<GetType>([ 'api', 'full', 'seo' ])
|
||||
const userHistoryInclude = new Set<GetType>([ 'api', 'full' ])
|
||||
const accountInclude = new Set<GetType>([ 'api', 'full', 'account', 'account-blacklist-files', 'seo' ])
|
||||
const ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])
|
||||
|
||||
const blacklistedInclude = new Set<GetType>([
|
||||
'api',
|
||||
'full',
|
||||
'account-blacklist-files',
|
||||
'thumbnails-blacklist',
|
||||
'blacklist-rights',
|
||||
'seo'
|
||||
])
|
||||
|
||||
const thumbnailsInclude = new Set<GetType>([
|
||||
'api',
|
||||
'full',
|
||||
'account-blacklist-files',
|
||||
'all-files',
|
||||
'thumbnails',
|
||||
'thumbnails-blacklist',
|
||||
'seo'
|
||||
])
|
||||
|
||||
export type BuildVideoGetQueryOptions = {
|
||||
id?: number | string
|
||||
url?: string
|
||||
|
||||
type: GetType
|
||||
|
||||
userId?: number
|
||||
transaction?: Transaction
|
||||
|
||||
logging?: boolean
|
||||
}
|
||||
|
||||
export class VideoModelGetQueryBuilder {
|
||||
videoQueryBuilder: VideosModelGetQuerySubBuilder
|
||||
webVideoFilesQueryBuilder: VideoFileQueryBuilder
|
||||
streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
|
||||
|
||||
private readonly videoModelBuilder: VideoModelBuilder
|
||||
|
||||
constructor (protected readonly sequelize: Sequelize) {
|
||||
this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
|
||||
this.webVideoFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
|
||||
this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
|
||||
|
||||
this.videoModelBuilder = new VideoModelBuilder('get', new VideoTableAttributes('get'))
|
||||
}
|
||||
|
||||
async queryVideo (options: BuildVideoGetQueryOptions) {
|
||||
const fileQueryOptions = {
|
||||
...pick(options, [ 'id', 'url', 'transaction', 'logging' ]),
|
||||
|
||||
includeRedundancy: this.shouldIncludeRedundancies(options)
|
||||
}
|
||||
|
||||
const [ videoRows, webVideoFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
|
||||
this.videoQueryBuilder.queryVideos(options),
|
||||
|
||||
videoFilesInclude.has(options.type)
|
||||
? this.webVideoFilesQueryBuilder.queryWebVideos(fileQueryOptions)
|
||||
: Promise.resolve(undefined),
|
||||
|
||||
videoFilesInclude.has(options.type)
|
||||
? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(fileQueryOptions)
|
||||
: Promise.resolve(undefined)
|
||||
])
|
||||
|
||||
const videos = this.videoModelBuilder.buildVideosFromRows({
|
||||
rows: videoRows,
|
||||
addCaptions: captionsInclude.has(options.type),
|
||||
rowsWebVideoFiles: webVideoFilesRows,
|
||||
rowsStreamingPlaylist: streamingPlaylistFilesRows
|
||||
})
|
||||
|
||||
if (videos.length > 1) {
|
||||
throw new Error('Video results is more than 1')
|
||||
}
|
||||
|
||||
if (videos.length === 0) return null
|
||||
|
||||
return videos[0]
|
||||
}
|
||||
|
||||
private shouldIncludeRedundancies (options: BuildVideoGetQueryOptions) {
|
||||
return options.type === 'api'
|
||||
}
|
||||
}
|
||||
|
||||
export class VideosModelGetQuerySubBuilder extends AbstractVideoQueryBuilder {
|
||||
protected webVideoFilesQuery: string
|
||||
protected streamingPlaylistFilesQuery: string
|
||||
|
||||
constructor (protected readonly sequelize: Sequelize) {
|
||||
super(sequelize, 'get')
|
||||
}
|
||||
|
||||
queryVideos (options: BuildVideoGetQueryOptions) {
|
||||
this.buildMainGetQuery(options)
|
||||
|
||||
return this.runQuery(options)
|
||||
}
|
||||
|
||||
private buildMainGetQuery (options: BuildVideoGetQueryOptions) {
|
||||
this.attributes = {
|
||||
'"video".*': ''
|
||||
}
|
||||
|
||||
if (thumbnailsInclude.has(options.type)) {
|
||||
this.includeThumbnails()
|
||||
}
|
||||
|
||||
if (blacklistedInclude.has(options.type)) {
|
||||
this.includeBlacklisted()
|
||||
}
|
||||
|
||||
if (accountInclude.has(options.type)) {
|
||||
this.includeChannels()
|
||||
this.includeAccounts()
|
||||
}
|
||||
|
||||
if (tagsInclude.has(options.type)) {
|
||||
this.includeTags()
|
||||
}
|
||||
|
||||
if (scheduleUpdateInclude.has(options.type)) {
|
||||
this.includeScheduleUpdate()
|
||||
}
|
||||
|
||||
if (liveInclude.has(options.type)) {
|
||||
this.includeLive()
|
||||
this.includeLiveSchedules()
|
||||
}
|
||||
|
||||
if (options.userId && userHistoryInclude.has(options.type)) {
|
||||
this.includeUserHistory(options.userId)
|
||||
}
|
||||
|
||||
if (ownerUserInclude.has(options.type)) {
|
||||
this.includeOwnerUser()
|
||||
}
|
||||
|
||||
if (trackersInclude.has(options.type)) {
|
||||
this.includeTrackers()
|
||||
}
|
||||
|
||||
if (captionsInclude.has(options.type)) {
|
||||
this.includeCaptions()
|
||||
}
|
||||
|
||||
this.whereId(options)
|
||||
|
||||
this.query = this.buildQuery(options)
|
||||
}
|
||||
|
||||
private buildQuery (options: BuildVideoGetQueryOptions) {
|
||||
const order = tagsInclude.has(options.type)
|
||||
? 'ORDER BY "Tags"."name" ASC'
|
||||
: ''
|
||||
|
||||
const from = `SELECT * FROM "video" ${this.where} LIMIT 1`
|
||||
|
||||
return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,907 @@
|
||||
import { forceNumber } from '@peertube/peertube-core-utils'
|
||||
import {
|
||||
VideoChannelCollaboratorState,
|
||||
VideoInclude,
|
||||
VideoIncludeType,
|
||||
VideoPrivacy,
|
||||
VideoPrivacyType,
|
||||
VideoState
|
||||
} from '@peertube/peertube-models'
|
||||
import { exists } from '@server/helpers/custom-validators/misc.js'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { buildSortDirectionAndField } from '@server/models/shared/index.js'
|
||||
import { MUserAccountId, MUserId } from '@server/types/models/index.js'
|
||||
import { Sequelize, Transaction } from 'sequelize'
|
||||
import validator from 'validator'
|
||||
import { AbstractRunQuery } from '../../../shared/abstract-run-query.js'
|
||||
import { createSafeIn, parseRowCountResult } from '../../../shared/index.js'
|
||||
|
||||
/**
|
||||
* Build videos list SQL query to fetch rows
|
||||
*/
|
||||
|
||||
export type DisplayOnlyForFollowerOptions = {
|
||||
actorId: number
|
||||
orLocalVideos: boolean
|
||||
}
|
||||
|
||||
export type BuildVideosListQueryOptions = {
|
||||
attributes?: string[]
|
||||
|
||||
serverAccountIdForBlock: number
|
||||
|
||||
displayOnlyForFollower: DisplayOnlyForFollowerOptions
|
||||
|
||||
count: number
|
||||
start: number
|
||||
sort: string
|
||||
|
||||
nsfw?: boolean
|
||||
nsfwFlagsIncluded?: number
|
||||
nsfwFlagsExcluded?: number
|
||||
|
||||
host?: string
|
||||
isLive?: boolean
|
||||
isLocal?: boolean
|
||||
include?: VideoIncludeType
|
||||
includeScheduledLive?: boolean
|
||||
|
||||
categoryOneOf?: number[]
|
||||
licenceOneOf?: number[]
|
||||
languageOneOf?: string[]
|
||||
|
||||
tagsOneOf?: string[]
|
||||
tagsAllOf?: string[]
|
||||
|
||||
privacyOneOf?: VideoPrivacyType[]
|
||||
|
||||
autoTagOneOf?: string[]
|
||||
|
||||
uuids?: string[]
|
||||
|
||||
hasFiles?: boolean
|
||||
hasHLSFiles?: boolean
|
||||
|
||||
hasWebVideoFiles?: boolean
|
||||
|
||||
accountId?: number
|
||||
includeCollaborations?: boolean
|
||||
|
||||
videoChannelId?: number
|
||||
channelNameOneOf?: string[]
|
||||
|
||||
videoPlaylistId?: number
|
||||
|
||||
trendingDays: number
|
||||
|
||||
// Used to include user history information, exclude blocked videos, include internal videos, adapt hot algorithm...
|
||||
user?: MUserAccountId
|
||||
|
||||
// Only list videos watched by this user
|
||||
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
|
||||
|
||||
transaction?: Transaction
|
||||
logging?: boolean
|
||||
|
||||
excludeAlreadyWatched?: boolean
|
||||
}
|
||||
|
||||
export class VideosIdListQueryBuilder extends AbstractRunQuery {
|
||||
protected replacements: any = {}
|
||||
|
||||
private attributes: string[]
|
||||
private joins: string[] = []
|
||||
|
||||
private readonly and: string[] = []
|
||||
|
||||
private readonly cte: string[] = []
|
||||
|
||||
private group = ''
|
||||
private having = ''
|
||||
|
||||
private sort = ''
|
||||
private limit = ''
|
||||
private offset = ''
|
||||
|
||||
private builtChannelJoin = false
|
||||
|
||||
constructor (protected readonly sequelize: Sequelize) {
|
||||
super(sequelize)
|
||||
}
|
||||
|
||||
queryVideoIds (options: BuildVideosListQueryOptions) {
|
||||
this.buildIdsListQuery(options)
|
||||
|
||||
return this.runQuery()
|
||||
}
|
||||
|
||||
countVideoIds (countOptions: BuildVideosListQueryOptions): Promise<number> {
|
||||
this.buildIdsListQuery(countOptions)
|
||||
|
||||
return this.runQuery().then(rows => parseRowCountResult(rows))
|
||||
}
|
||||
|
||||
getQuery (options: BuildVideosListQueryOptions) {
|
||||
this.buildIdsListQuery(options)
|
||||
|
||||
return {
|
||||
query: this.query,
|
||||
sort: this.sort,
|
||||
replacements: this.replacements,
|
||||
queryConfig: this.queryConfig
|
||||
}
|
||||
}
|
||||
|
||||
private buildIdsListQuery (options: BuildVideosListQueryOptions) {
|
||||
this.attributes = options.attributes || [ '"video"."id"' ]
|
||||
|
||||
if (options.group) this.group = options.group
|
||||
if (options.having) this.having = options.having
|
||||
|
||||
this.joins = this.joins.concat([
|
||||
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId"',
|
||||
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId"',
|
||||
'INNER JOIN "actor" "accountActor" ON "account"."id" = "accountActor"."accountId"'
|
||||
])
|
||||
|
||||
if (!(options.include & VideoInclude.BLACKLISTED)) {
|
||||
this.whereNotBlacklisted()
|
||||
}
|
||||
|
||||
if (options.serverAccountIdForBlock && !(options.include & VideoInclude.BLOCKED_OWNER)) {
|
||||
this.whereNotBlocked(options.serverAccountIdForBlock, options.user)
|
||||
}
|
||||
|
||||
// Only list published videos
|
||||
if (!(options.include & VideoInclude.NOT_PUBLISHED_STATE)) {
|
||||
if (options.includeScheduledLive) this.joinLiveSchedules()
|
||||
|
||||
this.whereStateAvailable({ includeScheduledLive: options.includeScheduledLive ?? false })
|
||||
}
|
||||
|
||||
if (options.videoPlaylistId) {
|
||||
this.joinPlaylist(options.videoPlaylistId)
|
||||
}
|
||||
|
||||
if (exists(options.isLocal)) {
|
||||
this.whereLocal(options.isLocal)
|
||||
}
|
||||
|
||||
if (options.host) {
|
||||
this.whereHost(options.host)
|
||||
}
|
||||
|
||||
if (options.accountId) {
|
||||
this.whereAccountId({ accountId: options.accountId, includeCollaborations: options.includeCollaborations })
|
||||
}
|
||||
|
||||
if (options.videoChannelId) {
|
||||
this.whereChannelId(options.videoChannelId)
|
||||
}
|
||||
|
||||
if (options.channelNameOneOf && options.channelNameOneOf.length !== 0) {
|
||||
this.whereChannelOneOf(options.channelNameOneOf)
|
||||
}
|
||||
|
||||
if (options.displayOnlyForFollower) {
|
||||
this.whereFollowerActorId({ ...options.displayOnlyForFollower, isCount: options.isCount === true })
|
||||
}
|
||||
|
||||
if (options.hasFiles === true) {
|
||||
this.whereFileExists()
|
||||
}
|
||||
|
||||
if (exists(options.hasWebVideoFiles)) {
|
||||
this.whereWebVideoFileExists(options.hasWebVideoFiles)
|
||||
}
|
||||
|
||||
if (exists(options.hasHLSFiles)) {
|
||||
this.whereHLSFileExists(options.hasHLSFiles)
|
||||
}
|
||||
|
||||
if (options.tagsOneOf) {
|
||||
this.whereTagsOneOf(options.tagsOneOf)
|
||||
}
|
||||
|
||||
if (options.tagsAllOf) {
|
||||
this.whereTagsAllOf(options.tagsAllOf)
|
||||
}
|
||||
|
||||
if (options.autoTagOneOf) {
|
||||
this.whereAutoTagOneOf(options.autoTagOneOf)
|
||||
}
|
||||
|
||||
if (options.privacyOneOf) {
|
||||
this.wherePrivacyOneOf(options.privacyOneOf)
|
||||
} else {
|
||||
// Only list videos with the appropriate privacy
|
||||
this.wherePrivacyAvailable(options.user)
|
||||
}
|
||||
|
||||
if (options.uuids) {
|
||||
this.whereUUIDs(options.uuids)
|
||||
}
|
||||
|
||||
if (options.nsfw === true) {
|
||||
this.whereNSFW(options.nsfwFlagsExcluded)
|
||||
} else if (options.nsfw === false) {
|
||||
this.whereSFW(options.nsfwFlagsIncluded)
|
||||
} else if (options.nsfwFlagsExcluded) {
|
||||
this.whereNSFWFlagsExcluded(options.nsfwFlagsExcluded)
|
||||
}
|
||||
|
||||
if (options.isLive === true) {
|
||||
this.whereLive()
|
||||
} else if (options.isLive === false) {
|
||||
this.whereVOD()
|
||||
}
|
||||
|
||||
if (options.categoryOneOf) {
|
||||
this.whereCategoryOneOf(options.categoryOneOf)
|
||||
}
|
||||
|
||||
if (options.licenceOneOf) {
|
||||
this.whereLicenceOneOf(options.licenceOneOf)
|
||||
}
|
||||
|
||||
if (options.languageOneOf) {
|
||||
this.whereLanguageOneOf(options.languageOneOf)
|
||||
}
|
||||
|
||||
// We don't exclude results in this so if we do a count we don't need to add this complex clause
|
||||
if (options.isCount !== true) {
|
||||
if (options.sort.endsWith('trending')) {
|
||||
this.groupForTrending(options.trendingDays)
|
||||
} else if (options.sort.endsWith('hot') || options.sort.endsWith('best')) {
|
||||
this.addAttributeForHotOrBest(options.sort, options.user)
|
||||
}
|
||||
}
|
||||
|
||||
if (options.historyOfUser) {
|
||||
this.joinHistory(options.historyOfUser.id)
|
||||
}
|
||||
|
||||
if (options.startDate) {
|
||||
this.whereStartDate(options.startDate)
|
||||
}
|
||||
|
||||
if (options.endDate) {
|
||||
this.whereEndDate(options.endDate)
|
||||
}
|
||||
|
||||
if (options.originallyPublishedStartDate) {
|
||||
this.whereOriginallyPublishedStartDate(options.originallyPublishedStartDate)
|
||||
}
|
||||
|
||||
if (options.originallyPublishedEndDate) {
|
||||
this.whereOriginallyPublishedEndDate(options.originallyPublishedEndDate)
|
||||
}
|
||||
|
||||
if (options.durationMin) {
|
||||
this.whereDurationMin(options.durationMin)
|
||||
}
|
||||
|
||||
if (options.durationMax) {
|
||||
this.whereDurationMax(options.durationMax)
|
||||
}
|
||||
|
||||
if (options.excludeAlreadyWatched) {
|
||||
if (exists(options.user.id)) {
|
||||
this.whereExcludeAlreadyWatched(options.user.id)
|
||||
} else {
|
||||
throw new Error('Cannot use excludeAlreadyWatched parameter when auth token is not provided')
|
||||
}
|
||||
}
|
||||
|
||||
this.whereSearch(options.search)
|
||||
|
||||
if (options.isCount === true) {
|
||||
this.setCountAttribute()
|
||||
} else {
|
||||
if (exists(options.sort)) {
|
||||
this.setSort(options.sort)
|
||||
}
|
||||
|
||||
if (exists(options.count)) {
|
||||
this.setLimit(options.count)
|
||||
}
|
||||
|
||||
if (exists(options.start)) {
|
||||
this.setOffset(options.start)
|
||||
}
|
||||
}
|
||||
|
||||
const cteString = this.cte.length !== 0
|
||||
? `WITH ${this.cte.join(', ')} `
|
||||
: ''
|
||||
|
||||
this.query = cteString +
|
||||
'SELECT ' + this.attributes.join(', ') + ' ' +
|
||||
'FROM "video" ' + this.joins.join(' ') + ' ' +
|
||||
'WHERE ' + this.and.join(' AND ') + ' ' +
|
||||
this.group + ' ' +
|
||||
this.having + ' ' +
|
||||
this.sort + ' ' +
|
||||
this.limit + ' ' +
|
||||
this.offset
|
||||
}
|
||||
|
||||
private setCountAttribute () {
|
||||
this.attributes = [ 'COUNT(*) as "total"' ]
|
||||
}
|
||||
|
||||
private joinHistory (userId: number) {
|
||||
this.joins.push('INNER JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId"')
|
||||
|
||||
this.and.push('"userVideoHistory"."userId" = :historyOfUser')
|
||||
|
||||
this.replacements.historyOfUser = userId
|
||||
}
|
||||
|
||||
private joinPlaylist (playlistId: number) {
|
||||
this.joins.push(
|
||||
'INNER JOIN "videoPlaylistElement" "video"."id" = "videoPlaylistElement"."videoId" ' +
|
||||
'AND "videoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
|
||||
)
|
||||
|
||||
this.replacements.videoPlaylistId = playlistId
|
||||
}
|
||||
|
||||
private joinLiveSchedules () {
|
||||
this.joins.push(
|
||||
'LEFT JOIN "videoLive" ON "video"."id" = "videoLive"."videoId"',
|
||||
'LEFT JOIN "videoLiveSchedule" ON "videoLiveSchedule"."liveVideoId" = "videoLive"."id"'
|
||||
)
|
||||
}
|
||||
|
||||
private joinChannel () {
|
||||
if (this.builtChannelJoin) return
|
||||
this.builtChannelJoin = true
|
||||
|
||||
this.joins.push('INNER JOIN "actor" "channelActor" ON "videoChannel"."id" = "channelActor"."videoChannelId"')
|
||||
}
|
||||
|
||||
private whereStateAvailable (options: {
|
||||
includeScheduledLive: boolean
|
||||
}) {
|
||||
const or: string[] = []
|
||||
|
||||
or.push(`"video"."state" = ${VideoState.PUBLISHED}`)
|
||||
or.push(`("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false)`)
|
||||
|
||||
if (options.includeScheduledLive) {
|
||||
or.push(`("video"."state" = ${VideoState.WAITING_FOR_LIVE} AND "videoLiveSchedule"."startAt" > NOW())`)
|
||||
}
|
||||
|
||||
this.and.push(`(${or.join(' OR ')})`)
|
||||
}
|
||||
|
||||
private wherePrivacyAvailable (user?: MUserAccountId) {
|
||||
if (user) {
|
||||
this.and.push(
|
||||
`("video"."privacy" = ${VideoPrivacy.PUBLIC} OR "video"."privacy" = ${VideoPrivacy.INTERNAL})`
|
||||
)
|
||||
} else { // Or only public videos
|
||||
this.and.push(
|
||||
`"video"."privacy" = ${VideoPrivacy.PUBLIC}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private whereLocal (isLocal: boolean) {
|
||||
const isRemote = isLocal ? 'FALSE' : 'TRUE'
|
||||
|
||||
this.and.push('"video"."remote" IS ' + isRemote)
|
||||
}
|
||||
|
||||
private whereHost (host: string) {
|
||||
// Local instance
|
||||
if (host === WEBSERVER.HOST) {
|
||||
this.and.push('"accountActor"."serverId" IS NULL')
|
||||
return
|
||||
}
|
||||
|
||||
this.joins.push('INNER JOIN "server" ON "server"."id" = "accountActor"."serverId"')
|
||||
|
||||
this.and.push('"server"."host" = :host')
|
||||
this.replacements.host = host
|
||||
}
|
||||
|
||||
private whereAccountId (options: {
|
||||
accountId: number
|
||||
includeCollaborations: boolean
|
||||
}) {
|
||||
if (options.includeCollaborations !== true) {
|
||||
this.and.push('"account"."id" = :accountId')
|
||||
this.replacements.accountId = options.accountId
|
||||
return
|
||||
}
|
||||
|
||||
this.joins.push(
|
||||
'LEFT JOIN "videoChannelCollaborator" ON "videoChannelCollaborator"."channelId" = "videoChannel".id ' +
|
||||
'AND "videoChannelCollaborator"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "videoChannelCollaborator"."accountId" = :accountId'
|
||||
)
|
||||
|
||||
this.and.push('("account"."id" = :accountId OR "videoChannelCollaborator"."accountId" = :accountId)')
|
||||
|
||||
this.replacements.accountId = options.accountId
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
}
|
||||
|
||||
private whereChannelId (channelId: number) {
|
||||
this.and.push('"videoChannel"."id" = :videoChannelId')
|
||||
this.replacements.videoChannelId = channelId
|
||||
}
|
||||
|
||||
private whereChannelOneOf (channelOneOf: string[]) {
|
||||
this.joinChannel()
|
||||
|
||||
this.and.push('"channelActor"."preferredUsername" IN (:channelOneOf)')
|
||||
this.replacements.channelOneOf = channelOneOf
|
||||
}
|
||||
|
||||
private whereFollowerActorId (options: { actorId: number, orLocalVideos: boolean, isCount: boolean }) {
|
||||
// EXISTS doesn't work very well with COUNT queries, so we use a CTE instead
|
||||
if (options.isCount) {
|
||||
// Don't use CTE on purpose, that seems to be slower in this case
|
||||
const targetActorIdQuery =
|
||||
`SELECT "targetActorId" FROM "actorFollow" WHERE "actorFollow"."actorId" = :followerActorId AND "actorFollow"."state" = 'accepted'`
|
||||
|
||||
let cteQuery = '"videoCandidates" AS (' +
|
||||
`SELECT "videoShare"."videoId" FROM "videoShare" WHERE "videoShare"."actorId" IN (${targetActorIdQuery}) ` +
|
||||
`UNION ` +
|
||||
`SELECT "video"."id" FROM "video" INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
|
||||
` INNER JOIN "actor" "channelActor" ON "videoChannel"."id" = "channelActor"."videoChannelId" ` +
|
||||
` WHERE "channelActor"."id" IN (${targetActorIdQuery}) ` +
|
||||
`UNION ` +
|
||||
`SELECT "video"."id" FROM "video" INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
|
||||
` INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ` +
|
||||
` INNER JOIN "actor" "accountActor" ON "account"."id" = "accountActor"."accountId" ` +
|
||||
` WHERE "accountActor"."id" IN (${targetActorIdQuery}) `
|
||||
|
||||
if (options.orLocalVideos) {
|
||||
cteQuery += 'UNION SELECT "video"."id" FROM "video" WHERE "remote" IS FALSE'
|
||||
}
|
||||
|
||||
cteQuery += ')'
|
||||
|
||||
this.cte.push(cteQuery)
|
||||
|
||||
this.joins.push('INNER JOIN "videoCandidates" ON "video"."id" = "videoCandidates"."videoId"')
|
||||
} else {
|
||||
this.joinChannel()
|
||||
|
||||
let query = ''
|
||||
|
||||
query = '(' +
|
||||
' EXISTS (' + // Videos shared by actors (instances, channels) we follow
|
||||
' SELECT 1 FROM "videoShare" ' +
|
||||
' INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' +
|
||||
' AND "actorFollowShare"."actorId" = :followerActorId AND "actorFollowShare"."state" = \'accepted\' ' +
|
||||
' WHERE "videoShare"."videoId" = "video"."id"' +
|
||||
' UNION ALL ' +
|
||||
' SELECT 1 from "actorFollow" ' + // Videos published by accounts we follow
|
||||
' WHERE "actorFollow"."targetActorId" = "accountActor"."id" ' +
|
||||
' AND "actorFollow"."actorId" = :followerActorId ' +
|
||||
' AND "actorFollow"."state" = \'accepted\'' +
|
||||
' UNION ALL ' +
|
||||
' SELECT 1 from "actorFollow" ' + // Videos published by channels we follow
|
||||
' WHERE "actorFollow"."targetActorId" = "channelActor"."id" ' +
|
||||
' AND "actorFollow"."actorId" = :followerActorId ' +
|
||||
' AND "actorFollow"."state" = \'accepted\'' +
|
||||
' LIMIT 1' +
|
||||
' )'
|
||||
|
||||
if (options.orLocalVideos) {
|
||||
query += ' OR "video"."remote" IS FALSE'
|
||||
}
|
||||
|
||||
query += ')'
|
||||
this.and.push(query)
|
||||
}
|
||||
|
||||
this.replacements.followerActorId = options.actorId
|
||||
}
|
||||
|
||||
private whereFileExists () {
|
||||
this.and.push(`(${this.buildWebVideoFileExistsQuery(true)} OR ${this.buildHLSFileExistsQuery(true)})`)
|
||||
}
|
||||
|
||||
private whereWebVideoFileExists (exists: boolean) {
|
||||
this.and.push(this.buildWebVideoFileExistsQuery(exists))
|
||||
}
|
||||
|
||||
private whereHLSFileExists (exists: boolean) {
|
||||
this.and.push(this.buildHLSFileExistsQuery(exists))
|
||||
}
|
||||
|
||||
private buildWebVideoFileExistsQuery (exists: boolean) {
|
||||
const prefix = exists ? '' : 'NOT '
|
||||
|
||||
return prefix + 'EXISTS (SELECT 1 FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id")'
|
||||
}
|
||||
|
||||
private buildHLSFileExistsQuery (exists: boolean) {
|
||||
const prefix = exists ? '' : 'NOT '
|
||||
|
||||
return prefix + 'EXISTS (' +
|
||||
' SELECT 1 FROM "videoStreamingPlaylist" ' +
|
||||
' INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ' +
|
||||
' WHERE "videoStreamingPlaylist"."videoId" = "video"."id"' +
|
||||
')'
|
||||
}
|
||||
|
||||
private whereTagsOneOf (tagsOneOf: string[]) {
|
||||
const tagsOneOfLower = tagsOneOf.map(t => t.toLowerCase())
|
||||
|
||||
this.cte.push(
|
||||
'"tagsOneOf" AS (' +
|
||||
' SELECT "videoTag"."videoId" AS "videoId" FROM "videoTag" ' +
|
||||
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
||||
' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsOneOfLower) + ') ' +
|
||||
')'
|
||||
)
|
||||
|
||||
this.joins.push('INNER JOIN "tagsOneOf" ON "video"."id" = "tagsOneOf"."videoId"')
|
||||
}
|
||||
|
||||
private whereAutoTagOneOf (autoTagOneOf: string[]) {
|
||||
const tags = autoTagOneOf.map(t => t.toLowerCase())
|
||||
|
||||
this.cte.push(
|
||||
'"autoTagsOneOf" AS (' +
|
||||
' SELECT "videoAutomaticTag"."videoId" AS "videoId" FROM "videoAutomaticTag" ' +
|
||||
' INNER JOIN "automaticTag" ON "automaticTag"."id" = "videoAutomaticTag"."automaticTagId" ' +
|
||||
' WHERE lower("automaticTag"."name") IN (' + createSafeIn(this.sequelize, tags) + ') ' +
|
||||
')'
|
||||
)
|
||||
|
||||
this.joins.push('INNER JOIN "autoTagsOneOf" ON "video"."id" = "autoTagsOneOf"."videoId"')
|
||||
}
|
||||
|
||||
private whereTagsAllOf (tagsAllOf: string[]) {
|
||||
const tagsAllOfLower = tagsAllOf.map(t => t.toLowerCase())
|
||||
|
||||
this.cte.push(
|
||||
'"tagsAllOf" AS (' +
|
||||
' SELECT "videoTag"."videoId" AS "videoId" FROM "videoTag" ' +
|
||||
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
||||
' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsAllOfLower) + ') ' +
|
||||
' GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length +
|
||||
')'
|
||||
)
|
||||
|
||||
this.joins.push('INNER JOIN "tagsAllOf" ON "video"."id" = "tagsAllOf"."videoId"')
|
||||
}
|
||||
|
||||
private wherePrivacyOneOf (privacyOneOf: VideoPrivacyType[]) {
|
||||
this.and.push('"video"."privacy" IN (:privacyOneOf)')
|
||||
this.replacements.privacyOneOf = privacyOneOf
|
||||
}
|
||||
|
||||
private whereUUIDs (uuids: string[]) {
|
||||
this.and.push('"video"."uuid" IN (' + createSafeIn(this.sequelize, uuids) + ')')
|
||||
}
|
||||
|
||||
private whereCategoryOneOf (categoryOneOf: number[]) {
|
||||
this.and.push('"video"."category" IN (:categoryOneOf)')
|
||||
this.replacements.categoryOneOf = categoryOneOf
|
||||
}
|
||||
|
||||
private whereLicenceOneOf (licenceOneOf: number[]) {
|
||||
this.and.push('"video"."licence" IN (:licenceOneOf)')
|
||||
this.replacements.licenceOneOf = licenceOneOf
|
||||
}
|
||||
|
||||
private whereLanguageOneOf (languageOneOf: string[]) {
|
||||
const languages = languageOneOf.filter(l => l && l !== '_unknown')
|
||||
const languagesQueryParts: string[] = []
|
||||
|
||||
if (languages.length !== 0) {
|
||||
languagesQueryParts.push('"video"."language" IN (:languageOneOf)')
|
||||
this.replacements.languageOneOf = languages
|
||||
|
||||
languagesQueryParts.push(
|
||||
'EXISTS (' +
|
||||
' SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' +
|
||||
' IN (' + createSafeIn(this.sequelize, languages) + ') AND ' +
|
||||
' "videoCaption"."videoId" = "video"."id"' +
|
||||
')'
|
||||
)
|
||||
}
|
||||
|
||||
if (languageOneOf.includes('_unknown')) {
|
||||
languagesQueryParts.push('"video"."language" IS NULL')
|
||||
}
|
||||
|
||||
if (languagesQueryParts.length !== 0) {
|
||||
this.and.push('(' + languagesQueryParts.join(' OR ') + ')')
|
||||
}
|
||||
}
|
||||
|
||||
private whereNSFW (nsfwFlagsExcluded?: number) {
|
||||
let filter = '"video"."nsfw" IS TRUE'
|
||||
|
||||
if (nsfwFlagsExcluded) {
|
||||
filter += ' AND "video"."nsfwFlags" & :nsfwFlagsExcluded = 0'
|
||||
this.replacements.nsfwFlagsExcluded = nsfwFlagsExcluded
|
||||
}
|
||||
|
||||
this.and.push(filter)
|
||||
}
|
||||
|
||||
private whereSFW (nsfwFlagsIncluded?: number) {
|
||||
let filter = '"video"."nsfw" IS FALSE'
|
||||
|
||||
if (nsfwFlagsIncluded) {
|
||||
filter = `(${filter} OR "video"."nsfwFlags" & :nsfwFlagsIncluded != 0)`
|
||||
this.replacements.nsfwFlagsIncluded = nsfwFlagsIncluded
|
||||
}
|
||||
|
||||
this.and.push(filter)
|
||||
}
|
||||
|
||||
private whereNSFWFlagsExcluded (nsfwFlagsExcluded: number) {
|
||||
this.and.push('"video"."nsfwFlags" & :nsfwFlagsExcluded = 0')
|
||||
this.replacements.nsfwFlagsExcluded = nsfwFlagsExcluded
|
||||
}
|
||||
|
||||
private whereLive () {
|
||||
this.and.push('"video"."isLive" IS TRUE')
|
||||
}
|
||||
|
||||
private whereVOD () {
|
||||
this.and.push('"video"."isLive" IS FALSE')
|
||||
}
|
||||
|
||||
private whereNotBlocked (serverAccountId: number, user?: MUserAccountId) {
|
||||
const blockerIds = [ serverAccountId ]
|
||||
if (user) blockerIds.push(user.Account.id)
|
||||
|
||||
const inClause = createSafeIn(this.sequelize, blockerIds)
|
||||
|
||||
this.and.push(
|
||||
'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"' +
|
||||
')'
|
||||
)
|
||||
}
|
||||
|
||||
private whereSearch (search?: string) {
|
||||
if (!search) {
|
||||
this.attributes.push('0 as similarity')
|
||||
return
|
||||
}
|
||||
|
||||
const escapedSearch = this.sequelize.escape(search)
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + search + '%')
|
||||
|
||||
this.queryConfig = 'SET pg_trgm.word_similarity_threshold = 0.40;'
|
||||
|
||||
this.cte.push(
|
||||
'"trigramSearch" AS (' +
|
||||
' SELECT "video"."id", ' +
|
||||
` word_similarity(lower(immutable_unaccent(${escapedSearch})), lower(immutable_unaccent("video"."name"))) as similarity ` +
|
||||
' FROM "video" ' +
|
||||
' WHERE lower(immutable_unaccent(' + escapedSearch + ')) <% lower(immutable_unaccent("video"."name")) OR ' +
|
||||
' lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
|
||||
')'
|
||||
)
|
||||
|
||||
this.joins.push('LEFT JOIN "trigramSearch" ON "video"."id" = "trigramSearch"."id"')
|
||||
|
||||
let base = '(' +
|
||||
' "trigramSearch"."id" IS NOT NULL OR ' +
|
||||
' EXISTS (' +
|
||||
' SELECT 1 FROM "videoTag" ' +
|
||||
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
|
||||
` WHERE lower("tag"."name") = lower(${escapedSearch}) ` +
|
||||
' AND "video"."id" = "videoTag"."videoId"' +
|
||||
' )'
|
||||
|
||||
if (validator.default.isUUID(search)) {
|
||||
base += ` OR "video"."uuid" = ${escapedSearch}`
|
||||
}
|
||||
|
||||
base += ')'
|
||||
|
||||
this.and.push(base)
|
||||
|
||||
let attribute = `COALESCE("trigramSearch"."similarity", 0)`
|
||||
if (this.group) attribute = `AVG(${attribute})`
|
||||
|
||||
this.attributes.push(`${attribute} as similarity`)
|
||||
}
|
||||
|
||||
private whereNotBlacklisted () {
|
||||
this.and.push('"video"."id" NOT IN (SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")')
|
||||
}
|
||||
|
||||
private whereStartDate (startDate: string) {
|
||||
this.and.push('"video"."publishedAt" >= :startDate')
|
||||
this.replacements.startDate = startDate
|
||||
}
|
||||
|
||||
private whereEndDate (endDate: string) {
|
||||
this.and.push('"video"."publishedAt" <= :endDate')
|
||||
this.replacements.endDate = endDate
|
||||
}
|
||||
|
||||
private whereOriginallyPublishedStartDate (startDate: string) {
|
||||
this.and.push('"video"."originallyPublishedAt" >= :originallyPublishedStartDate')
|
||||
this.replacements.originallyPublishedStartDate = startDate
|
||||
}
|
||||
|
||||
private whereOriginallyPublishedEndDate (endDate: string) {
|
||||
this.and.push('"video"."originallyPublishedAt" <= :originallyPublishedEndDate')
|
||||
this.replacements.originallyPublishedEndDate = endDate
|
||||
}
|
||||
|
||||
private whereDurationMin (durationMin: number) {
|
||||
this.and.push('"video"."duration" >= :durationMin')
|
||||
this.replacements.durationMin = durationMin
|
||||
}
|
||||
|
||||
private whereDurationMax (durationMax: number) {
|
||||
this.and.push('"video"."duration" <= :durationMax')
|
||||
this.replacements.durationMax = durationMax
|
||||
}
|
||||
|
||||
private whereExcludeAlreadyWatched (userId: number) {
|
||||
this.and.push(
|
||||
'NOT EXISTS (' +
|
||||
' SELECT 1' +
|
||||
' FROM "userVideoHistory"' +
|
||||
' WHERE "video"."id" = "userVideoHistory"."videoId"' +
|
||||
' AND "userVideoHistory"."userId" = :excludeAlreadyWatchedUserId' +
|
||||
')'
|
||||
)
|
||||
this.replacements.excludeAlreadyWatchedUserId = userId
|
||||
}
|
||||
|
||||
private groupForTrending (trendingDays: number) {
|
||||
const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
|
||||
|
||||
this.joins.push('LEFT JOIN "videoView" ON "video"."id" = "videoView"."videoId" AND "videoView"."startDate" >= :viewsGteDate')
|
||||
this.replacements.viewsGteDate = viewsGteDate
|
||||
|
||||
this.attributes.push('COALESCE(SUM("videoView"."views"), 0) AS "score"')
|
||||
|
||||
this.group = 'GROUP BY "video"."id"'
|
||||
}
|
||||
|
||||
private addAttributeForHotOrBest (sort: string, user?: MUserAccountId) {
|
||||
/**
|
||||
* "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 :)
|
||||
*
|
||||
* 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
|
||||
* see https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/_sorts.pyx#L47-L58
|
||||
* - we have less interactions than on reddit, so multiply weights by an arbitrary factor
|
||||
*/
|
||||
const weights = {
|
||||
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
|
||||
}
|
||||
|
||||
let attribute = `LOG(GREATEST(1, "video"."likes" - 1)) * ${weights.like} ` + // likes (+)
|
||||
`+ LOG(GREATEST(1, "video"."dislikes" - 1)) * ${weights.dislike} ` + // dislikes (-)
|
||||
`+ LOG("video"."views" + 1) * ${weights.view} ` + // views (+)
|
||||
`+ LOG(GREATEST(1, "video"."comments")) * ${weights.comment} ` + // comments (+)
|
||||
'+ (SELECT (EXTRACT(epoch FROM "video"."publishedAt") - 1446156582) / 47000) ' // base score (in number of half-days)
|
||||
|
||||
if (sort.endsWith('best') && user) {
|
||||
this.joins.push(
|
||||
'LEFT JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :bestUser'
|
||||
)
|
||||
this.replacements.bestUser = user.id
|
||||
|
||||
attribute += `+ POWER(CASE WHEN "userVideoHistory"."id" IS NULL THEN 0 ELSE 1 END, 2.0) * ${weights.history} `
|
||||
}
|
||||
|
||||
attribute += 'AS "score"'
|
||||
this.attributes.push(attribute)
|
||||
}
|
||||
|
||||
private setSort (sort: string) {
|
||||
if (sort === '-originallyPublishedAt' || sort === 'originallyPublishedAt') {
|
||||
this.attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"')
|
||||
}
|
||||
|
||||
if (sort === '-localVideoFilesSize' || sort === 'localVideoFilesSize') {
|
||||
this.attributes.push(
|
||||
'(' +
|
||||
'CASE ' +
|
||||
'WHEN "video"."remote" IS TRUE THEN 0 ' + // Consider remote videos with size of 0
|
||||
'ELSE (' +
|
||||
'(SELECT COALESCE(SUM(size), 0) FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id")' +
|
||||
' + ' +
|
||||
'(' +
|
||||
'SELECT COALESCE(SUM(size), 0) FROM "videoFile" ' +
|
||||
'INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoFile"."videoStreamingPlaylistId" ' +
|
||||
'AND "videoStreamingPlaylist"."videoId" = "video"."id"' +
|
||||
')' +
|
||||
' + ' +
|
||||
'(' +
|
||||
'SELECT COALESCE(SUM(size), 0) FROM "videoSource" ' +
|
||||
'WHERE "videoSource"."videoId" = "video"."id" AND "videoSource"."storage" IS NOT NULL' +
|
||||
')' +
|
||||
') END' +
|
||||
') AS "localVideoFilesSize"'
|
||||
)
|
||||
}
|
||||
|
||||
this.sort = this.buildOrder(sort)
|
||||
}
|
||||
|
||||
private buildOrder (value: string) {
|
||||
const { direction, field } = buildSortDirectionAndField(value)
|
||||
if (field.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + field)
|
||||
|
||||
if (field.toLowerCase() === 'random') return 'ORDER BY RANDOM()'
|
||||
if (field.toLowerCase() === 'total') return `ORDER BY "total" ${direction}`
|
||||
|
||||
if ([ 'trending', 'hot', 'best' ].includes(field.toLowerCase())) { // Sort by aggregation
|
||||
return `ORDER BY "score" ${direction}, "video"."views" ${direction}`
|
||||
}
|
||||
|
||||
let firstSort: string
|
||||
|
||||
if (field.toLowerCase() === 'match') { // Search
|
||||
firstSort = '"similarity"'
|
||||
} else if (field === 'originallyPublishedAt') {
|
||||
firstSort = '"publishedAtForOrder"'
|
||||
} else if (field === 'localVideoFilesSize') {
|
||||
firstSort = '"localVideoFilesSize"'
|
||||
} else if (field.includes('.')) {
|
||||
firstSort = field
|
||||
} else {
|
||||
firstSort = `"video"."${field}"`
|
||||
}
|
||||
|
||||
return `ORDER BY ${firstSort} ${direction}, "video"."id" ASC`
|
||||
}
|
||||
|
||||
private setLimit (countArg: number) {
|
||||
const count = forceNumber(countArg)
|
||||
this.limit = `LIMIT ${count}`
|
||||
}
|
||||
|
||||
private setOffset (startArg: number) {
|
||||
const start = forceNumber(startArg)
|
||||
this.offset = `OFFSET ${start}`
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
import { pick } from '@peertube/peertube-core-utils'
|
||||
import { VideoInclude } from '@peertube/peertube-models'
|
||||
import { getServerActor } from '@server/models/application/application.js'
|
||||
import { MActorAccount } from '@server/types/models/index.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { AbstractVideoQueryBuilder } from './shared/abstract-video-query-builder.js'
|
||||
import { VideoFileQueryBuilder } from './shared/video-file-query-builder.js'
|
||||
import { VideoModelBuilder } from './shared/video-model-builder.js'
|
||||
import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder.js'
|
||||
|
||||
/**
|
||||
* Build videos list SQL query and create video models
|
||||
*/
|
||||
|
||||
export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
|
||||
private innerQuery: string
|
||||
private innerSort: string
|
||||
|
||||
webVideoFilesQueryBuilder: VideoFileQueryBuilder
|
||||
streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
|
||||
|
||||
private readonly videoModelBuilder: VideoModelBuilder
|
||||
|
||||
constructor (protected readonly sequelize: Sequelize) {
|
||||
super(sequelize, 'list')
|
||||
|
||||
this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
|
||||
this.webVideoFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
|
||||
this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
|
||||
}
|
||||
|
||||
async queryVideos (options: BuildVideosListQueryOptions) {
|
||||
const serverActor = await getServerActor()
|
||||
|
||||
this.buildInnerQuery(options)
|
||||
this.buildMainQuery(options, serverActor)
|
||||
|
||||
const rows = await this.runQuery()
|
||||
|
||||
if (options.include & VideoInclude.FILES) {
|
||||
const videoIds = Array.from(new Set(rows.map(r => r.id)))
|
||||
|
||||
if (videoIds.length !== 0) {
|
||||
const fileQueryOptions = {
|
||||
...pick(options, [ 'transaction', 'logging' ]),
|
||||
|
||||
ids: videoIds,
|
||||
includeRedundancy: false
|
||||
}
|
||||
|
||||
const [ rowsWebVideoFiles, rowsStreamingPlaylist ] = await Promise.all([
|
||||
this.webVideoFilesQueryBuilder.queryWebVideos(fileQueryOptions),
|
||||
this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(fileQueryOptions)
|
||||
])
|
||||
|
||||
return this.videoModelBuilder.buildVideosFromRows({
|
||||
rows,
|
||||
include: options.include,
|
||||
addCaptions: false,
|
||||
rowsStreamingPlaylist,
|
||||
rowsWebVideoFiles
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include, addCaptions: false })
|
||||
}
|
||||
|
||||
private buildInnerQuery (options: BuildVideosListQueryOptions) {
|
||||
const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
|
||||
const { query, sort, replacements, queryConfig } = idsQueryBuilder.getQuery(options)
|
||||
|
||||
this.replacements = replacements
|
||||
this.innerQuery = query
|
||||
this.innerSort = sort
|
||||
this.queryConfig = queryConfig
|
||||
}
|
||||
|
||||
private buildMainQuery (options: BuildVideosListQueryOptions, serverActor: MActorAccount) {
|
||||
this.attributes = {
|
||||
'"video".*': ''
|
||||
}
|
||||
|
||||
this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"')
|
||||
|
||||
this.includeChannels()
|
||||
this.includeAccounts()
|
||||
this.includeThumbnails()
|
||||
|
||||
if (options.user) {
|
||||
this.includeUserHistory(options.user.id)
|
||||
}
|
||||
|
||||
if (options.videoPlaylistId) {
|
||||
this.includePlaylist(options.videoPlaylistId)
|
||||
}
|
||||
|
||||
if (options.isLive || options.includeScheduledLive) {
|
||||
this.includeLive()
|
||||
}
|
||||
|
||||
if (options.includeScheduledLive) {
|
||||
this.includeLiveSchedules()
|
||||
}
|
||||
|
||||
if (options.include & VideoInclude.BLACKLISTED) {
|
||||
this.includeBlacklisted()
|
||||
}
|
||||
|
||||
if (options.include & VideoInclude.BLOCKED_OWNER) {
|
||||
this.includeBlockedOwnerAndServer(options.serverAccountIdForBlock, options.user)
|
||||
}
|
||||
|
||||
if (options.include & VideoInclude.SOURCE) {
|
||||
this.includeVideoSource()
|
||||
}
|
||||
|
||||
if (options.include & VideoInclude.AUTOMATIC_TAGS) {
|
||||
this.includeAutomaticTags(serverActor.Account.id)
|
||||
}
|
||||
|
||||
if (options.include & VideoInclude.TAGS) {
|
||||
this.includeTags()
|
||||
}
|
||||
|
||||
if (options.include & VideoInclude.NOT_PUBLISHED_STATE) {
|
||||
this.includeScheduleUpdate()
|
||||
}
|
||||
|
||||
const select = this.buildSelect()
|
||||
|
||||
this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user