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

107 lines
3.0 KiB
TypeScript
Raw Normal View History

2021-06-10 07:43:55 -05:00
import { Sequelize, Transaction } from 'sequelize'
import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-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-06-11 03:59:27 -05:00
import { VideoTables } from './shared/video-tables'
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
export type BuildVideoGetQueryOptions = {
id: number | string
transaction?: Transaction
userId?: number
forGetAPI?: boolean
}
2021-06-10 09:57:13 -05:00
export class VideosModelGetQueryBuilder {
videoQueryBuilder: VideosModelGetQuerySubBuilder
webtorrentFilesQueryBuilder: VideoFileQueryBuilder
streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
private readonly videoModelBuilder: VideoModelBuilder
constructor (protected readonly sequelize: Sequelize) {
this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
2021-06-11 03:59:27 -05:00
this.videoModelBuilder = new VideoModelBuilder('get', new VideoTables('get'))
2021-06-10 09:57:13 -05:00
}
async queryVideos (options: BuildVideoGetQueryOptions) {
const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
this.videoQueryBuilder.queryVideos(options),
this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options),
this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
])
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 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
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 03:59:27 -05:00
return this.runQuery(options.transaction)
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".*': ''
}
this.includeChannels()
this.includeAccounts()
this.includeTags()
this.includeThumbnails()
this.includeBlacklisted()
this.includeScheduleUpdate()
this.includeLive()
if (options.userId) {
this.includeUserHistory(options.userId)
}
if (options.forGetAPI === true) {
this.includeTrackers()
}
this.whereId(options.id)
2021-06-10 09:57:13 -05:00
this.query = this.buildQuery()
2021-06-10 07:43:55 -05:00
}
2021-06-10 09:57:13 -05:00
private buildQuery () {
const order = 'ORDER BY "Tags"."name" ASC'
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
}
}