1 import { Sequelize } from 'sequelize'
2 import validator from 'validator'
3 import { MUserAccountId } from '@server/types/models'
4 import { ActorImageType } from '@shared/models'
5 import { AbstractRunQuery } from '../../../../shared/abstract-run-query'
6 import { createSafeIn } from '../../../../shared'
7 import { VideoTableAttributes } from './video-table-attributes'
11 * Abstract builder to create SQL query and fetch video models
15 export class AbstractVideoQueryBuilder extends AbstractRunQuery {
16 protected attributes: { [key: string]: string } = {}
19 protected where: string
21 protected tables: VideoTableAttributes
24 protected readonly sequelize: Sequelize,
25 protected readonly mode: 'list' | 'get'
29 this.tables = new VideoTableAttributes(this.mode)
32 protected buildSelect () {
33 return 'SELECT ' + Object.keys(this.attributes).map(key => {
34 const value = this.attributes[key]
35 if (value) return `${key} AS ${value}`
41 protected includeChannels () {
42 this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
43 this.addJoin('INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."actorId" = "VideoChannel->Actor"."id"')
46 'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"'
50 'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Actor->Avatars" ' +
51 'ON "VideoChannel->Actor"."id" = "VideoChannel->Actor->Avatars"."actorId" ' +
52 `AND "VideoChannel->Actor->Avatars"."type" = ${ActorImageType.AVATAR}`
58 ...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
59 ...this.buildActorInclude('VideoChannel->Actor'),
60 ...this.buildAvatarInclude('VideoChannel->Actor->Avatars'),
61 ...this.buildServerInclude('VideoChannel->Actor->Server')
65 protected includeAccounts () {
66 this.addJoin('INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"')
68 'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."actorId" = "VideoChannel->Account->Actor"."id"'
72 'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' +
73 'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"'
77 'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Account->Actor->Avatars" ' +
78 'ON "VideoChannel->Account"."actorId"= "VideoChannel->Account->Actor->Avatars"."actorId" ' +
79 `AND "VideoChannel->Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}`
85 ...this.buildAttributesObject('VideoChannel->Account', this.tables.getAccountAttributes()),
86 ...this.buildActorInclude('VideoChannel->Account->Actor'),
87 ...this.buildAvatarInclude('VideoChannel->Account->Actor->Avatars'),
88 ...this.buildServerInclude('VideoChannel->Account->Actor->Server')
92 protected includeOwnerUser () {
93 this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
94 this.addJoin('INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"')
99 ...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
100 ...this.buildAttributesObject('VideoChannel->Account', this.tables.getUserAccountAttributes())
104 protected includeThumbnails () {
105 this.addJoin('LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"')
110 ...this.buildAttributesObject('Thumbnails', this.tables.getThumbnailAttributes())
114 protected includeWebtorrentFiles () {
115 this.addJoin('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')
120 ...this.buildAttributesObject('VideoFiles', this.tables.getFileAttributes())
124 protected includeStreamingPlaylistFiles () {
126 'LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"'
130 'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' +
131 'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"'
137 ...this.buildAttributesObject('VideoStreamingPlaylists', this.tables.getStreamingPlaylistAttributes()),
138 ...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.tables.getFileAttributes())
142 protected includeUserHistory (userId: number) {
144 'LEFT OUTER JOIN "userVideoHistory" ' +
145 'ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :userVideoHistoryId'
148 this.replacements.userVideoHistoryId = userId
153 ...this.buildAttributesObject('userVideoHistory', this.tables.getUserHistoryAttributes())
157 protected includePlaylist (playlistId: number) {
159 'INNER JOIN "videoPlaylistElement" as "VideoPlaylistElement" ON "videoPlaylistElement"."videoId" = "video"."id" ' +
160 'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
163 this.replacements.videoPlaylistId = playlistId
168 ...this.buildAttributesObject('VideoPlaylistElement', this.tables.getPlaylistAttributes())
172 protected includeTags () {
174 'LEFT OUTER JOIN (' +
175 '"videoTag" AS "Tags->VideoTagModel" INNER JOIN "tag" AS "Tags" ON "Tags"."id" = "Tags->VideoTagModel"."tagId"' +
177 'ON "video"."id" = "Tags->VideoTagModel"."videoId"'
183 ...this.buildAttributesObject('Tags', this.tables.getTagAttributes()),
184 ...this.buildAttributesObject('Tags->VideoTagModel', this.tables.getVideoTagAttributes())
188 protected includeBlacklisted () {
190 'LEFT OUTER JOIN "videoBlacklist" AS "VideoBlacklist" ON "video"."id" = "VideoBlacklist"."videoId"'
196 ...this.buildAttributesObject('VideoBlacklist', this.tables.getBlacklistedAttributes())
200 protected includeBlockedOwnerAndServer (serverAccountId: number, user?: MUserAccountId) {
201 const blockerIds = [ serverAccountId ]
202 if (user) blockerIds.push(user.Account.id)
204 const inClause = createSafeIn(this.sequelize, blockerIds)
207 'LEFT JOIN "accountBlocklist" AS "VideoChannel->Account->AccountBlocklist" ' +
208 'ON "VideoChannel->Account"."id" = "VideoChannel->Account->AccountBlocklist"."targetAccountId" ' +
209 'AND "VideoChannel->Account->AccountBlocklist"."accountId" IN (' + inClause + ')'
213 'LEFT JOIN "serverBlocklist" AS "VideoChannel->Account->Actor->Server->ServerBlocklist" ' +
214 'ON "VideoChannel->Account->Actor->Server->ServerBlocklist"."targetServerId" = "VideoChannel->Account->Actor"."serverId" ' +
215 'AND "VideoChannel->Account->Actor->Server->ServerBlocklist"."accountId" IN (' + inClause + ') '
221 ...this.buildAttributesObject('VideoChannel->Account->AccountBlocklist', this.tables.getBlocklistAttributes()),
222 ...this.buildAttributesObject('VideoChannel->Account->Actor->Server->ServerBlocklist', this.tables.getBlocklistAttributes())
226 protected includeScheduleUpdate () {
228 'LEFT OUTER JOIN "scheduleVideoUpdate" AS "ScheduleVideoUpdate" ON "video"."id" = "ScheduleVideoUpdate"."videoId"'
234 ...this.buildAttributesObject('ScheduleVideoUpdate', this.tables.getScheduleUpdateAttributes())
238 protected includeLive () {
240 'LEFT OUTER JOIN "videoLive" AS "VideoLive" ON "video"."id" = "VideoLive"."videoId"'
246 ...this.buildAttributesObject('VideoLive', this.tables.getLiveAttributes())
250 protected includeTrackers () {
252 'LEFT OUTER JOIN (' +
253 '"videoTracker" AS "Trackers->VideoTrackerModel" ' +
254 'INNER JOIN "tracker" AS "Trackers" ON "Trackers"."id" = "Trackers->VideoTrackerModel"."trackerId"' +
255 ') ON "video"."id" = "Trackers->VideoTrackerModel"."videoId"'
261 ...this.buildAttributesObject('Trackers', this.tables.getTrackerAttributes()),
262 ...this.buildAttributesObject('Trackers->VideoTrackerModel', this.tables.getVideoTrackerAttributes())
266 protected includeWebTorrentRedundancies () {
268 'LEFT OUTER JOIN "videoRedundancy" AS "VideoFiles->RedundancyVideos" ON ' +
269 '"VideoFiles"."id" = "VideoFiles->RedundancyVideos"."videoFileId"'
275 ...this.buildAttributesObject('VideoFiles->RedundancyVideos', this.tables.getRedundancyAttributes())
279 protected includeStreamingPlaylistRedundancies () {
281 'LEFT OUTER JOIN "videoRedundancy" AS "VideoStreamingPlaylists->RedundancyVideos" ' +
282 'ON "VideoStreamingPlaylists"."id" = "VideoStreamingPlaylists->RedundancyVideos"."videoStreamingPlaylistId"'
288 ...this.buildAttributesObject('VideoStreamingPlaylists->RedundancyVideos', this.tables.getRedundancyAttributes())
292 protected buildActorInclude (prefixKey: string) {
293 return this.buildAttributesObject(prefixKey, this.tables.getActorAttributes())
296 protected buildAvatarInclude (prefixKey: string) {
297 return this.buildAttributesObject(prefixKey, this.tables.getAvatarAttributes())
300 protected buildServerInclude (prefixKey: string) {
301 return this.buildAttributesObject(prefixKey, this.tables.getServerAttributes())
304 protected buildAttributesObject (prefixKey: string, attributeKeys: string[]) {
305 const result: { [id: string]: string } = {}
307 const prefixValue = prefixKey.replace(/->/g, '.')
309 for (const attribute of attributeKeys) {
310 result[`"${prefixKey}"."${attribute}"`] = `"${prefixValue}.${attribute}"`
316 protected whereId (options: { ids?: number[], id?: string | number, url?: string }) {
318 this.where = `WHERE "video"."id" IN (${createSafeIn(this.sequelize, options.ids)})`
323 this.where = 'WHERE "video"."url" = :videoUrl'
324 this.replacements.videoUrl = options.url
328 if (validator.isInt('' + options.id)) {
329 this.where = 'WHERE "video".id = :videoId'
331 this.where = 'WHERE uuid = :videoId'
334 this.replacements.videoId = options.id
337 protected addJoin (join: string) {
338 this.joins += join + ' '