1 import { Sequelize, Transaction } from 'sequelize'
2 import { AbstractVideoQueryBuilder } from './shared/abstract-video-query-builder'
3 import { VideoFileQueryBuilder } from './shared/video-file-query-builder'
4 import { VideoModelBuilder } from './shared/video-model-builder'
5 import { VideoTableAttributes } from './shared/video-table-attributes'
9 * Build a GET SQL query, fetch rows and create the video model
16 'account-blacklist-files' |
19 'thumbnails-blacklist' |
23 export type BuildVideoGetQueryOptions = {
30 transaction?: Transaction
35 export class VideoModelGetQueryBuilder {
36 videoQueryBuilder: VideosModelGetQuerySubBuilder
37 webtorrentFilesQueryBuilder: VideoFileQueryBuilder
38 streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
40 private readonly videoModelBuilder: VideoModelBuilder
42 private static readonly videoFilesInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files', 'all-files' ])
44 constructor (protected readonly sequelize: Sequelize) {
45 this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
46 this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
47 this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
49 this.videoModelBuilder = new VideoModelBuilder('get', new VideoTableAttributes('get'))
52 async queryVideo (options: BuildVideoGetQueryOptions) {
53 const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
54 this.videoQueryBuilder.queryVideos(options),
56 VideoModelGetQueryBuilder.videoFilesInclude.has(options.type)
57 ? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options)
58 : Promise.resolve(undefined),
60 VideoModelGetQueryBuilder.videoFilesInclude.has(options.type)
61 ? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
62 : Promise.resolve(undefined)
65 const videos = this.videoModelBuilder.buildVideosFromRows({
67 rowsWebTorrentFiles: webtorrentFilesRows,
68 rowsStreamingPlaylist: streamingPlaylistFilesRows
71 if (videos.length > 1) {
72 throw new Error('Video results is more than 1')
75 if (videos.length === 0) return null
81 export class VideosModelGetQuerySubBuilder extends AbstractVideoQueryBuilder {
82 protected attributes: { [key: string]: string }
84 protected webtorrentFilesQuery: string
85 protected streamingPlaylistFilesQuery: string
87 private static readonly trackersInclude = new Set<GetType>([ 'api' ])
88 private static readonly liveInclude = new Set<GetType>([ 'api', 'full-light' ])
89 private static readonly scheduleUpdateInclude = new Set<GetType>([ 'api', 'full-light' ])
90 private static readonly tagsInclude = new Set<GetType>([ 'api', 'full-light' ])
91 private static readonly userHistoryInclude = new Set<GetType>([ 'api', 'full-light' ])
92 private static readonly accountInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files' ])
93 private static readonly ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])
95 private static readonly blacklistedInclude = new Set<GetType>([
98 'account-blacklist-files',
99 'thumbnails-blacklist',
103 private static readonly thumbnailsInclude = new Set<GetType>([
106 'account-blacklist-files',
109 'thumbnails-blacklist'
112 constructor (protected readonly sequelize: Sequelize) {
113 super(sequelize, 'get')
116 queryVideos (options: BuildVideoGetQueryOptions) {
117 this.buildMainGetQuery(options)
119 return this.runQuery(options)
122 private buildMainGetQuery (options: BuildVideoGetQueryOptions) {
127 if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) {
128 this.includeThumbnails()
131 if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) {
132 this.includeBlacklisted()
135 if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) {
136 this.includeChannels()
137 this.includeAccounts()
140 if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) {
144 if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) {
145 this.includeScheduleUpdate()
148 if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) {
152 if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) {
153 this.includeUserHistory(options.userId)
156 if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) {
157 this.includeOwnerUser()
160 if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) {
161 this.includeTrackers()
164 this.whereId(options)
166 this.query = this.buildQuery(options)
169 private buildQuery (options: BuildVideoGetQueryOptions) {
170 const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)
171 ? 'ORDER BY "Tags"."name" ASC'
174 const from = `SELECT * FROM "video" ${this.where} LIMIT 1`
176 return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`