2021-06-10 09:57:13 -05:00
|
|
|
import validator from 'validator'
|
2021-06-10 07:43:55 -05:00
|
|
|
import { AbstractVideosQueryBuilder } from './abstract-videos-query-builder'
|
2021-06-11 03:59:27 -05:00
|
|
|
import { VideoTables } from './video-tables'
|
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
|
|
|
|
|
|
|
export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder {
|
|
|
|
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-06-11 03:59:27 -05:00
|
|
|
protected tables: VideoTables
|
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-06-11 03:59:27 -05:00
|
|
|
this.tables = new VideoTables(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')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 09:57:13 -05:00
|
|
|
protected includeWebtorrentFiles (required: boolean) {
|
|
|
|
const joinType = required ? 'INNER' : 'LEFT'
|
2021-06-11 04:27:45 -05:00
|
|
|
this.addJoin(joinType + ' 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected includeStreamingPlaylistFiles (required: boolean) {
|
|
|
|
const joinType = required ? 'INNER' : 'LEFT'
|
2021-06-10 07:43:55 -05:00
|
|
|
|
2021-06-11 04:27:45 -05:00
|
|
|
this.addJoin(
|
|
|
|
joinType + ' JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"'
|
|
|
|
)
|
2021-06-10 07:43:55 -05:00
|
|
|
|
2021-06-11 04:27:45 -05:00
|
|
|
this.addJoin(
|
2021-06-10 09:57:13 -05:00
|
|
|
joinType + ' 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 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
|
|
|
|
|
|
|
protected whereId (id: string | number) {
|
|
|
|
if (validator.isInt('' + id)) {
|
|
|
|
this.where = 'WHERE "video".id = :videoId'
|
|
|
|
} else {
|
|
|
|
this.where = 'WHERE uuid = :videoId'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.replacements.videoId = id
|
|
|
|
}
|
2021-06-11 04:27:45 -05:00
|
|
|
|
|
|
|
protected addJoin (join: string) {
|
|
|
|
this.joins += join + ' '
|
|
|
|
}
|
2021-06-10 07:43:55 -05:00
|
|
|
}
|