PeerTube/server/models/video/sql/video-model-get-query-build...

179 lines
5.4 KiB
TypeScript
Raw Normal View History

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