]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/sql/videos-model-list-query-builder.ts
Fix notification on create transcoding job
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / videos-model-list-query-builder.ts
1 import { Sequelize } from 'sequelize'
2 import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
3 import { VideoModelBuilder } from './shared/video-model-builder'
4 import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder'
5
6 /**
7 *
8 * Build videos list SQL query and create video models
9 *
10 */
11
12 export class VideosModelListQueryBuilder extends AbstractVideosModelQueryBuilder {
13 protected attributes: { [key: string]: string }
14
15 private innerQuery: string
16 private innerSort: string
17
18 private readonly videoModelBuilder: VideoModelBuilder
19
20 constructor (protected readonly sequelize: Sequelize) {
21 super('list')
22
23 this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
24 }
25
26 queryVideos (options: BuildVideosListQueryOptions) {
27 this.buildInnerQuery(options)
28 this.buildListQueryFromIdsQuery(options)
29
30 return this.runQuery()
31 .then(rows => this.videoModelBuilder.buildVideosFromRows(rows))
32 }
33
34 private buildInnerQuery (options: BuildVideosListQueryOptions) {
35 const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
36 const { query, sort, replacements } = idsQueryBuilder.getIdsListQueryAndSort(options)
37
38 this.replacements = replacements
39 this.innerQuery = query
40 this.innerSort = sort
41 }
42
43 private buildListQueryFromIdsQuery (options: BuildVideosListQueryOptions) {
44 this.attributes = {
45 '"video".*': ''
46 }
47
48 this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"')
49
50 this.includeChannels()
51 this.includeAccounts()
52 this.includeThumbnails()
53
54 if (options.withFiles) {
55 this.includeWebtorrentFiles()
56 this.includeStreamingPlaylistFiles()
57 }
58
59 if (options.user) {
60 this.includeUserHistory(options.user.id)
61 }
62
63 if (options.videoPlaylistId) {
64 this.includePlaylist(options.videoPlaylistId)
65 }
66
67 const select = this.buildSelect()
68
69 this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
70 }
71 }