2021-06-10 14:43:55 +02:00
|
|
|
import { Sequelize, Transaction } from 'sequelize'
|
|
|
|
import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
|
2021-06-10 16:57:13 +02:00
|
|
|
import { VideoFileQueryBuilder } from './shared/video-file-query-builder'
|
|
|
|
import { VideoModelBuilder } from './shared/video-model-builder'
|
2021-06-11 10:59:27 +02:00
|
|
|
import { VideoTables } from './shared/video-tables'
|
2021-06-10 16:57:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Build a GET SQL query, fetch rows and create the video model
|
|
|
|
*
|
|
|
|
*/
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
export type GetType =
|
|
|
|
'api' |
|
|
|
|
'full-light' |
|
|
|
|
'account-blacklist-files' |
|
|
|
|
'all-files' |
|
|
|
|
'thumbnails' |
|
|
|
|
'thumbnails-blacklist' |
|
|
|
|
'id' |
|
|
|
|
'blacklist-rights'
|
|
|
|
|
2021-06-10 14:43:55 +02:00
|
|
|
export type BuildVideoGetQueryOptions = {
|
2021-06-11 14:09:33 +02:00
|
|
|
id?: number | string
|
|
|
|
url?: string
|
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
type: GetType
|
2021-06-11 14:09:33 +02:00
|
|
|
|
2021-06-10 14:43:55 +02:00
|
|
|
userId?: number
|
2021-06-11 14:09:33 +02:00
|
|
|
transaction?: Transaction
|
|
|
|
|
|
|
|
logging?: boolean
|
2021-06-10 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 16:57:13 +02:00
|
|
|
export class VideosModelGetQueryBuilder {
|
|
|
|
videoQueryBuilder: VideosModelGetQuerySubBuilder
|
|
|
|
webtorrentFilesQueryBuilder: VideoFileQueryBuilder
|
|
|
|
streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
|
|
|
|
|
|
|
|
private readonly videoModelBuilder: VideoModelBuilder
|
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
private static readonly videoFilesInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files', 'all-files' ])
|
|
|
|
|
2021-06-10 16:57:13 +02:00
|
|
|
constructor (protected readonly sequelize: Sequelize) {
|
|
|
|
this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
|
|
|
|
this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
|
|
|
|
this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
|
|
|
|
|
2021-06-11 10:59:27 +02:00
|
|
|
this.videoModelBuilder = new VideoModelBuilder('get', new VideoTables('get'))
|
2021-06-10 16:57:13 +02:00
|
|
|
}
|
|
|
|
|
2021-06-11 14:09:33 +02:00
|
|
|
async queryVideo (options: BuildVideoGetQueryOptions) {
|
2021-06-10 16:57:13 +02:00
|
|
|
const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
|
|
|
|
this.videoQueryBuilder.queryVideos(options),
|
2021-06-11 14:09:33 +02:00
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
|
2021-06-11 14:09:33 +02:00
|
|
|
? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options)
|
|
|
|
: Promise.resolve(undefined),
|
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
|
2021-06-11 14:09:33 +02:00
|
|
|
? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
|
|
|
|
: Promise.resolve(undefined)
|
2021-06-10 16:57:13 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
const videos = this.videoModelBuilder.buildVideosFromRows(videoRows, webtorrentFilesRows, streamingPlaylistFilesRows)
|
|
|
|
|
|
|
|
if (videos.length > 1) {
|
|
|
|
throw new Error('Video results is more than ')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videos.length === 0) return null
|
|
|
|
return videos[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder {
|
2021-06-10 14:43:55 +02:00
|
|
|
protected attributes: { [key: string]: string }
|
2021-06-10 16:57:13 +02:00
|
|
|
|
|
|
|
protected webtorrentFilesQuery: string
|
|
|
|
protected streamingPlaylistFilesQuery: string
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
private static readonly trackersInclude = new Set<GetType>([ 'api' ])
|
|
|
|
private static readonly liveInclude = new Set<GetType>([ 'api', 'full-light' ])
|
|
|
|
private static readonly scheduleUpdateInclude = new Set<GetType>([ 'api', 'full-light' ])
|
|
|
|
private static readonly tagsInclude = new Set<GetType>([ 'api', 'full-light' ])
|
|
|
|
private static readonly userHistoryInclude = new Set<GetType>([ 'api', 'full-light' ])
|
|
|
|
private static readonly accountInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files' ])
|
|
|
|
private static readonly ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])
|
|
|
|
|
|
|
|
private static readonly blacklistedInclude = new Set<GetType>([
|
|
|
|
'api',
|
|
|
|
'full-light',
|
|
|
|
'account-blacklist-files',
|
|
|
|
'thumbnails-blacklist',
|
|
|
|
'blacklist-rights'
|
|
|
|
])
|
|
|
|
|
|
|
|
private static readonly thumbnailsInclude = new Set<GetType>([
|
|
|
|
'api',
|
|
|
|
'full-light',
|
|
|
|
'account-blacklist-files',
|
2021-06-11 16:05:32 +02:00
|
|
|
'all-files',
|
2021-06-11 14:26:37 +02:00
|
|
|
'thumbnails',
|
|
|
|
'thumbnails-blacklist'
|
|
|
|
])
|
|
|
|
|
2021-06-10 14:43:55 +02:00
|
|
|
constructor (protected readonly sequelize: Sequelize) {
|
|
|
|
super('get')
|
|
|
|
}
|
|
|
|
|
|
|
|
queryVideos (options: BuildVideoGetQueryOptions) {
|
2021-06-10 16:57:13 +02:00
|
|
|
this.buildMainGetQuery(options)
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:09:33 +02:00
|
|
|
return this.runQuery(options)
|
2021-06-10 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 16:57:13 +02:00
|
|
|
private buildMainGetQuery (options: BuildVideoGetQueryOptions) {
|
2021-06-10 14:43:55 +02:00
|
|
|
this.attributes = {
|
|
|
|
'"video".*': ''
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) {
|
2021-06-11 14:09:33 +02:00
|
|
|
this.includeThumbnails()
|
|
|
|
}
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) {
|
2021-06-11 14:09:33 +02:00
|
|
|
this.includeBlacklisted()
|
|
|
|
}
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) {
|
2021-06-11 14:09:33 +02:00
|
|
|
this.includeChannels()
|
|
|
|
this.includeAccounts()
|
|
|
|
}
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) {
|
2021-06-11 14:09:33 +02:00
|
|
|
this.includeTags()
|
|
|
|
}
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) {
|
2021-06-11 14:09:33 +02:00
|
|
|
this.includeScheduleUpdate()
|
|
|
|
}
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) {
|
2021-06-11 14:09:33 +02:00
|
|
|
this.includeLive()
|
|
|
|
}
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) {
|
2021-06-10 14:43:55 +02:00
|
|
|
this.includeUserHistory(options.userId)
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) {
|
2021-06-11 14:09:33 +02:00
|
|
|
this.includeOwnerUser()
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:26:37 +02:00
|
|
|
if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) {
|
2021-06-10 14:43:55 +02:00
|
|
|
this.includeTrackers()
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:09:33 +02:00
|
|
|
this.whereId(options)
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 14:09:33 +02:00
|
|
|
this.query = this.buildQuery(options)
|
2021-06-10 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2021-06-11 14:09:33 +02:00
|
|
|
private buildQuery (options: BuildVideoGetQueryOptions) {
|
2021-06-11 14:26:37 +02:00
|
|
|
const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)
|
2021-06-11 14:09:33 +02:00
|
|
|
? 'ORDER BY "Tags"."name" ASC'
|
|
|
|
: ''
|
|
|
|
|
2021-06-10 16:57:13 +02:00
|
|
|
const from = `SELECT * FROM "video" ${this.where} LIMIT 1`
|
2021-06-10 14:43:55 +02:00
|
|
|
|
2021-06-11 11:27:45 +02:00
|
|
|
return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`
|
2021-06-10 14:43:55 +02:00
|
|
|
}
|
|
|
|
}
|