PeerTube/server/models/video/sql/shared/abstract-video-query-builde...

329 lines
11 KiB
TypeScript
Raw Normal View History

import { createSafeIn } from '@server/models/utils'
import { MUserAccountId } from '@server/types/models'
2021-06-10 09:57:13 -05:00
import validator from 'validator'
2021-11-02 05:00:40 -05:00
import { AbstractRunQuery } from './abstract-run-query'
import { VideoTableAttributes } from './video-table-attributes'
2021-06-10 09:57:13 -05:00
/**
*
* Abstract builder to create SQL query and fetch video models
*
*/
2021-06-10 07:43:55 -05:00
2021-11-02 05:00:40 -05:00
export class AbstractVideoQueryBuilder extends AbstractRunQuery {
2021-06-10 07:43:55 -05:00
protected attributes: { [key: string]: string } = {}
2021-06-11 04:27:45 -05:00
protected joins = ''
2021-06-10 09:57:13 -05:00
protected where: string
2021-06-10 07:43:55 -05:00
2021-11-02 05:00:40 -05:00
protected tables: VideoTableAttributes
2021-06-10 07:43:55 -05:00
2021-06-10 09:57:13 -05:00
constructor (protected readonly mode: 'list' | 'get') {
2021-06-10 07:43:55 -05:00
super()
2021-11-02 05:00:40 -05:00
this.tables = new VideoTableAttributes(this.mode)
2021-06-10 07:43:55 -05:00
}
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 () {
2021-06-11 04:27:45 -05:00
this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
this.addJoin('INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."actorId" = "VideoChannel->Actor"."id"')
2021-06-10 07:43:55 -05:00
2021-06-11 04:27:45 -05:00
this.addJoin(
'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"'
)
2021-06-10 07:43:55 -05:00
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Actor->Avatar" ' +
'ON "VideoChannel->Actor"."avatarId" = "VideoChannel->Actor->Avatar"."id"'
)
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
2021-06-10 07:43:55 -05:00
...this.buildActorInclude('VideoChannel->Actor'),
...this.buildAvatarInclude('VideoChannel->Actor->Avatar'),
...this.buildServerInclude('VideoChannel->Actor->Server')
}
}
protected includeAccounts () {
2021-06-11 04:27:45 -05:00
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"."actorId" = "VideoChannel->Account->Actor"."id"'
)
2021-06-10 07:43:55 -05:00
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' +
2021-06-11 04:27:45 -05:00
'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"'
)
2021-06-10 07:43:55 -05:00
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Account->Actor->Avatar" ' +
'ON "VideoChannel->Account->Actor"."avatarId" = "VideoChannel->Account->Actor->Avatar"."id"'
)
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('VideoChannel->Account', this.tables.getAccountAttributes()),
2021-06-10 07:43:55 -05:00
...this.buildActorInclude('VideoChannel->Account->Actor'),
...this.buildAvatarInclude('VideoChannel->Account->Actor->Avatar'),
...this.buildServerInclude('VideoChannel->Account->Actor->Server')
}
}
2021-06-11 07:09:33 -05:00
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())
}
}
2021-06-10 07:43:55 -05:00
protected includeThumbnails () {
2021-06-11 04:27:45 -05:00
this.addJoin('LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"')
2021-06-10 07:43:55 -05:00
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('Thumbnails', this.tables.getThumbnailAttributes())
2021-06-10 07:43:55 -05:00
}
}
2021-06-11 09:02:26 -05:00
protected includeWebtorrentFiles () {
this.addJoin('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')
2021-06-10 09:57:13 -05:00
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('VideoFiles', this.tables.getFileAttributes())
2021-06-10 09:57:13 -05:00
}
}
2021-06-11 09:02:26 -05:00
protected includeStreamingPlaylistFiles () {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-11 09:02:26 -05:00
'LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"'
2021-06-11 04:27:45 -05:00
)
2021-06-10 07:43:55 -05:00
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-11 09:02:26 -05:00
'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' +
2021-06-10 07:43:55 -05:00
'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"'
)
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('VideoStreamingPlaylists', this.tables.getStreamingPlaylistAttributes()),
...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.tables.getFileAttributes())
2021-06-10 07:43:55 -05:00
}
}
protected includeUserHistory (userId: number) {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'LEFT OUTER JOIN "userVideoHistory" ' +
'ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :userVideoHistoryId'
)
this.replacements.userVideoHistoryId = userId
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('userVideoHistory', this.tables.getUserHistoryAttributes())
2021-06-10 07:43:55 -05:00
}
}
protected includePlaylist (playlistId: number) {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'INNER JOIN "videoPlaylistElement" as "VideoPlaylistElement" ON "videoPlaylistElement"."videoId" = "video"."id" ' +
'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
)
this.replacements.videoPlaylistId = playlistId
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('VideoPlaylistElement', this.tables.getPlaylistAttributes())
2021-06-10 07:43:55 -05:00
}
}
protected includeTags () {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'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,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('Tags', this.tables.getTagAttributes()),
...this.buildAttributesObject('Tags->VideoTagModel', this.tables.getVideoTagAttributes())
2021-06-10 07:43:55 -05:00
}
}
protected includeBlacklisted () {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'LEFT OUTER JOIN "videoBlacklist" AS "VideoBlacklist" ON "video"."id" = "VideoBlacklist"."videoId"'
)
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('VideoBlacklist', this.tables.getBlacklistedAttributes())
2021-06-10 07:43:55 -05:00
}
}
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())
}
}
2021-06-10 07:43:55 -05:00
protected includeScheduleUpdate () {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'LEFT OUTER JOIN "scheduleVideoUpdate" AS "ScheduleVideoUpdate" ON "video"."id" = "ScheduleVideoUpdate"."videoId"'
)
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('ScheduleVideoUpdate', this.tables.getScheduleUpdateAttributes())
2021-06-10 07:43:55 -05:00
}
}
protected includeLive () {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'LEFT OUTER JOIN "videoLive" AS "VideoLive" ON "video"."id" = "VideoLive"."videoId"'
)
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('VideoLive', this.tables.getLiveAttributes())
2021-06-10 07:43:55 -05:00
}
}
protected includeTrackers () {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'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,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('Trackers', this.tables.getTrackerAttributes()),
...this.buildAttributesObject('Trackers->VideoTrackerModel', this.tables.getVideoTrackerAttributes())
2021-06-10 07:43:55 -05:00
}
}
2021-06-10 09:57:13 -05:00
protected includeWebTorrentRedundancies () {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 07:43:55 -05:00
'LEFT OUTER JOIN "videoRedundancy" AS "VideoFiles->RedundancyVideos" ON ' +
'"VideoFiles"."id" = "VideoFiles->RedundancyVideos"."videoFileId"'
)
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('VideoFiles->RedundancyVideos', this.tables.getRedundancyAttributes())
2021-06-10 09:57:13 -05:00
}
}
protected includeStreamingPlaylistRedundancies () {
2021-06-11 04:27:45 -05:00
this.addJoin(
2021-06-10 09:57:13 -05:00
'LEFT OUTER JOIN "videoRedundancy" AS "VideoStreamingPlaylists->RedundancyVideos" ' +
'ON "VideoStreamingPlaylists"."id" = "VideoStreamingPlaylists->RedundancyVideos"."videoStreamingPlaylistId"'
)
this.attributes = {
...this.attributes,
2021-06-11 03:59:27 -05:00
...this.buildAttributesObject('VideoStreamingPlaylists->RedundancyVideos', this.tables.getRedundancyAttributes())
2021-06-10 07:43:55 -05:00
}
}
protected buildActorInclude (prefixKey: string) {
2021-06-11 03:59:27 -05:00
return this.buildAttributesObject(prefixKey, this.tables.getActorAttributes())
2021-06-10 07:43:55 -05:00
}
protected buildAvatarInclude (prefixKey: string) {
2021-06-11 03:59:27 -05:00
return this.buildAttributesObject(prefixKey, this.tables.getAvatarAttributes())
2021-06-10 07:43:55 -05:00
}
protected buildServerInclude (prefixKey: string) {
2021-06-11 03:59:27 -05:00
return this.buildAttributesObject(prefixKey, this.tables.getServerAttributes())
2021-06-10 07:43:55 -05:00
}
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
}
2021-06-10 09:57:13 -05:00
2021-06-11 07:09:33 -05:00
protected whereId (options: { id?: string | number, url?: string }) {
if (options.url) {
this.where = 'WHERE "video"."url" = :videoUrl'
this.replacements.videoUrl = options.url
return
}
if (validator.isInt('' + options.id)) {
2021-06-10 09:57:13 -05:00
this.where = 'WHERE "video".id = :videoId'
} else {
this.where = 'WHERE uuid = :videoId'
}
2021-06-11 07:09:33 -05:00
this.replacements.videoId = options.id
2021-06-10 09:57:13 -05:00
}
2021-06-11 04:27:45 -05:00
protected addJoin (join: string) {
this.joins += join + ' '
}
2021-06-10 07:43:55 -05:00
}