1 import validator from 'validator'
2 import { AbstractVideosQueryBuilder } from './abstract-videos-query-builder'
3 import { VideoTables } from './video-tables'
7 * Abstract builder to create SQL query and fetch video models
11 export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder {
12 protected attributes: { [key: string]: string } = {}
13 protected joins: string[] = []
14 protected where: string
16 protected tables: VideoTables
18 constructor (protected readonly mode: 'list' | 'get') {
21 this.tables = new VideoTables(this.mode)
24 protected buildSelect () {
25 return 'SELECT ' + Object.keys(this.attributes).map(key => {
26 const value = this.attributes[key]
27 if (value) return `${key} AS ${value}`
33 protected includeChannels () {
35 'INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"',
36 'INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."actorId" = "VideoChannel->Actor"."id"',
38 'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"',
40 'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Actor->Avatar" ' +
41 'ON "VideoChannel->Actor"."avatarId" = "VideoChannel->Actor->Avatar"."id"'
47 ...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
48 ...this.buildActorInclude('VideoChannel->Actor'),
49 ...this.buildAvatarInclude('VideoChannel->Actor->Avatar'),
50 ...this.buildServerInclude('VideoChannel->Actor->Server')
54 protected includeAccounts () {
56 'INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"',
57 'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."actorId" = "VideoChannel->Account->Actor"."id"',
59 'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' +
60 'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"',
62 'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Account->Actor->Avatar" ' +
63 'ON "VideoChannel->Account->Actor"."avatarId" = "VideoChannel->Account->Actor->Avatar"."id"'
69 ...this.buildAttributesObject('VideoChannel->Account', this.tables.getAccountAttributes()),
70 ...this.buildActorInclude('VideoChannel->Account->Actor'),
71 ...this.buildAvatarInclude('VideoChannel->Account->Actor->Avatar'),
72 ...this.buildServerInclude('VideoChannel->Account->Actor->Server')
76 protected includeThumbnails () {
77 this.joins.push('LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"')
82 ...this.buildAttributesObject('Thumbnails', this.tables.getThumbnailAttributes())
86 protected includeWebtorrentFiles (required: boolean) {
87 const joinType = required ? 'INNER' : 'LEFT'
88 this.joins.push(joinType + ' JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')
93 ...this.buildAttributesObject('VideoFiles', this.tables.getFileAttributes())
97 protected includeStreamingPlaylistFiles (required: boolean) {
98 const joinType = required ? 'INNER' : 'LEFT'
101 joinType + ' JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"',
103 joinType + ' JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' +
104 'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"'
110 ...this.buildAttributesObject('VideoStreamingPlaylists', this.tables.getStreamingPlaylistAttributes()),
111 ...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.tables.getFileAttributes())
115 protected includeUserHistory (userId: number) {
117 'LEFT OUTER JOIN "userVideoHistory" ' +
118 'ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :userVideoHistoryId'
121 this.replacements.userVideoHistoryId = userId
126 ...this.buildAttributesObject('userVideoHistory', this.tables.getUserHistoryAttributes())
130 protected includePlaylist (playlistId: number) {
132 'INNER JOIN "videoPlaylistElement" as "VideoPlaylistElement" ON "videoPlaylistElement"."videoId" = "video"."id" ' +
133 'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
136 this.replacements.videoPlaylistId = playlistId
141 ...this.buildAttributesObject('VideoPlaylistElement', this.tables.getPlaylistAttributes())
145 protected includeTags () {
147 'LEFT OUTER JOIN (' +
148 '"videoTag" AS "Tags->VideoTagModel" INNER JOIN "tag" AS "Tags" ON "Tags"."id" = "Tags->VideoTagModel"."tagId"' +
150 'ON "video"."id" = "Tags->VideoTagModel"."videoId"'
156 ...this.buildAttributesObject('Tags', this.tables.getTagAttributes()),
157 ...this.buildAttributesObject('Tags->VideoTagModel', this.tables.getVideoTagAttributes())
161 protected includeBlacklisted () {
163 'LEFT OUTER JOIN "videoBlacklist" AS "VideoBlacklist" ON "video"."id" = "VideoBlacklist"."videoId"'
169 ...this.buildAttributesObject('VideoBlacklist', this.tables.getBlacklistedAttributes())
173 protected includeScheduleUpdate () {
175 'LEFT OUTER JOIN "scheduleVideoUpdate" AS "ScheduleVideoUpdate" ON "video"."id" = "ScheduleVideoUpdate"."videoId"'
181 ...this.buildAttributesObject('ScheduleVideoUpdate', this.tables.getScheduleUpdateAttributes())
185 protected includeLive () {
187 'LEFT OUTER JOIN "videoLive" AS "VideoLive" ON "video"."id" = "VideoLive"."videoId"'
193 ...this.buildAttributesObject('VideoLive', this.tables.getLiveAttributes())
197 protected includeTrackers () {
199 'LEFT OUTER JOIN (' +
200 '"videoTracker" AS "Trackers->VideoTrackerModel" ' +
201 'INNER JOIN "tracker" AS "Trackers" ON "Trackers"."id" = "Trackers->VideoTrackerModel"."trackerId"' +
202 ') ON "video"."id" = "Trackers->VideoTrackerModel"."videoId"'
208 ...this.buildAttributesObject('Trackers', this.tables.getTrackerAttributes()),
209 ...this.buildAttributesObject('Trackers->VideoTrackerModel', this.tables.getVideoTrackerAttributes())
213 protected includeWebTorrentRedundancies () {
215 'LEFT OUTER JOIN "videoRedundancy" AS "VideoFiles->RedundancyVideos" ON ' +
216 '"VideoFiles"."id" = "VideoFiles->RedundancyVideos"."videoFileId"'
222 ...this.buildAttributesObject('VideoFiles->RedundancyVideos', this.tables.getRedundancyAttributes())
226 protected includeStreamingPlaylistRedundancies () {
228 'LEFT OUTER JOIN "videoRedundancy" AS "VideoStreamingPlaylists->RedundancyVideos" ' +
229 'ON "VideoStreamingPlaylists"."id" = "VideoStreamingPlaylists->RedundancyVideos"."videoStreamingPlaylistId"'
235 ...this.buildAttributesObject('VideoStreamingPlaylists->RedundancyVideos', this.tables.getRedundancyAttributes())
239 protected buildActorInclude (prefixKey: string) {
240 return this.buildAttributesObject(prefixKey, this.tables.getActorAttributes())
243 protected buildAvatarInclude (prefixKey: string) {
244 return this.buildAttributesObject(prefixKey, this.tables.getAvatarAttributes())
247 protected buildServerInclude (prefixKey: string) {
248 return this.buildAttributesObject(prefixKey, this.tables.getServerAttributes())
251 protected buildAttributesObject (prefixKey: string, attributeKeys: string[]) {
252 const result: { [id: string]: string} = {}
254 const prefixValue = prefixKey.replace(/->/g, '.')
256 for (const attribute of attributeKeys) {
257 result[`"${prefixKey}"."${attribute}"`] = `"${prefixValue}.${attribute}"`
263 protected whereId (id: string | number) {
264 if (validator.isInt('' + id)) {
265 this.where = 'WHERE "video".id = :videoId'
267 this.where = 'WHERE uuid = :videoId'
270 this.replacements.videoId = id