1
0
Fork 0
peertube/server/models/video/sql/video-model-get-query-builder.ts

108 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-06-10 08:43:55 -04:00
import { Sequelize, Transaction } from 'sequelize'
import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
2021-06-10 10:57:13 -04:00
import { VideoAttributes } from './shared/video-attributes'
import { VideoFileQueryBuilder } from './shared/video-file-query-builder'
import { VideoModelBuilder } from './shared/video-model-builder'
/**
*
* Build a GET SQL query, fetch rows and create the video model
*
*/
2021-06-10 08:43:55 -04:00
export type BuildVideoGetQueryOptions = {
id: number | string
transaction?: Transaction
userId?: number
forGetAPI?: boolean
}
2021-06-10 10:57:13 -04: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)
this.videoModelBuilder = new VideoModelBuilder('get', new VideoAttributes('get'))
}
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 08:43:55 -04:00
protected attributes: { [key: string]: string }
protected joins: string[] = []
2021-06-10 10:57:13 -04:00
protected webtorrentFilesQuery: string
protected streamingPlaylistFilesQuery: string
2021-06-10 08:43:55 -04:00
constructor (protected readonly sequelize: Sequelize) {
super('get')
}
queryVideos (options: BuildVideoGetQueryOptions) {
2021-06-10 10:57:13 -04:00
this.buildMainGetQuery(options)
2021-06-10 08:43:55 -04:00
2021-06-10 10:57:13 -04:00
return this.runQuery(options.transaction, true)
2021-06-10 08:43:55 -04:00
}
2021-06-10 10:57:13 -04:00
private buildMainGetQuery (options: BuildVideoGetQueryOptions) {
2021-06-10 08:43:55 -04: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 10:57:13 -04:00
this.query = this.buildQuery()
2021-06-10 08:43:55 -04:00
}
2021-06-10 10:57:13 -04:00
private buildQuery () {
const order = 'ORDER BY "Tags"."name" ASC'
const from = `SELECT * FROM "video" ${this.where} LIMIT 1`
2021-06-10 08:43:55 -04:00
2021-06-10 10:57:13 -04:00
return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins.join(' ')} ${this.where} ${order}`
2021-06-10 08:43:55 -04:00
}
}