2021-06-10 01:53:32 -05:00
|
|
|
import { Sequelize } from 'sequelize'
|
2021-06-10 07:43:55 -05:00
|
|
|
import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
|
2021-06-10 09:57:13 -05:00
|
|
|
import { VideoModelBuilder } from './shared/video-model-builder'
|
2021-06-10 01:53:32 -05:00
|
|
|
import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder'
|
|
|
|
|
2021-06-10 09:57:13 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Build videos list SQL query and create video models
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-06-10 07:43:55 -05:00
|
|
|
export class VideosModelListQueryBuilder extends AbstractVideosModelQueryBuilder {
|
|
|
|
protected attributes: { [key: string]: string }
|
2021-06-10 01:53:32 -05:00
|
|
|
|
|
|
|
private innerQuery: string
|
|
|
|
private innerSort: string
|
|
|
|
|
2021-06-10 09:57:13 -05:00
|
|
|
private readonly videoModelBuilder: VideoModelBuilder
|
|
|
|
|
2021-06-10 01:53:32 -05:00
|
|
|
constructor (protected readonly sequelize: Sequelize) {
|
2021-06-10 07:43:55 -05:00
|
|
|
super('list')
|
2021-06-10 09:57:13 -05:00
|
|
|
|
2021-06-11 03:59:27 -05:00
|
|
|
this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
|
2021-06-10 01:53:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
queryVideos (options: BuildVideosListQueryOptions) {
|
|
|
|
this.buildInnerQuery(options)
|
|
|
|
this.buildListQueryFromIdsQuery(options)
|
|
|
|
|
2021-06-11 07:09:33 -05:00
|
|
|
return this.runQuery()
|
|
|
|
.then(rows => this.videoModelBuilder.buildVideosFromRows(rows))
|
2021-06-10 01:53:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private buildInnerQuery (options: BuildVideosListQueryOptions) {
|
|
|
|
const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
|
|
|
|
const { query, sort, replacements } = idsQueryBuilder.getIdsListQueryAndSort(options)
|
|
|
|
|
|
|
|
this.replacements = replacements
|
|
|
|
this.innerQuery = query
|
|
|
|
this.innerSort = sort
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildListQueryFromIdsQuery (options: BuildVideosListQueryOptions) {
|
|
|
|
this.attributes = {
|
|
|
|
'"video".*': ''
|
|
|
|
}
|
|
|
|
|
2021-06-11 04:27:45 -05:00
|
|
|
this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"')
|
2021-06-10 01:53:32 -05:00
|
|
|
|
|
|
|
this.includeChannels()
|
|
|
|
this.includeAccounts()
|
|
|
|
this.includeThumbnails()
|
|
|
|
|
|
|
|
if (options.withFiles) {
|
2021-06-11 09:02:26 -05:00
|
|
|
this.includeWebtorrentFiles()
|
|
|
|
this.includeStreamingPlaylistFiles()
|
2021-06-10 01:53:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.user) {
|
2021-06-10 07:43:55 -05:00
|
|
|
this.includeUserHistory(options.user.id)
|
2021-06-10 01:53:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.videoPlaylistId) {
|
|
|
|
this.includePlaylist(options.videoPlaylistId)
|
|
|
|
}
|
|
|
|
|
|
|
|
const select = this.buildSelect()
|
|
|
|
|
2021-06-11 04:27:45 -05:00
|
|
|
this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
|
2021-06-10 01:53:32 -05:00
|
|
|
}
|
|
|
|
}
|