PeerTube/server/models/video/sql/videos-model-list-query-bui...

81 lines
2.3 KiB
TypeScript
Raw Normal View History

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