]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/sql/videos-model-list-query-builder.ts
Merge branch 'release/4.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / videos-model-list-query-builder.ts
CommitLineData
2760b454 1import { VideoInclude } from '@shared/models'
e5dbd508 2import { Sequelize } from 'sequelize'
7e7d8e48 3import { AbstractVideoQueryBuilder } from './shared/abstract-video-query-builder'
1d43c3a6 4import { VideoModelBuilder } from './shared/video-model-builder'
e5dbd508
C
5import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder'
6
1d43c3a6
C
7/**
8 *
9 * Build videos list SQL query and create video models
10 *
11 */
12
7e7d8e48 13export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
d9bf974f 14 protected attributes: { [key: string]: string }
e5dbd508
C
15
16 private innerQuery: string
17 private innerSort: string
18
1d43c3a6
C
19 private readonly videoModelBuilder: VideoModelBuilder
20
e5dbd508 21 constructor (protected readonly sequelize: Sequelize) {
d9bf974f 22 super('list')
1d43c3a6 23
17bb4538 24 this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
e5dbd508
C
25 }
26
27 queryVideos (options: BuildVideosListQueryOptions) {
28 this.buildInnerQuery(options)
7e7d8e48 29 this.buildMainQuery(options)
e5dbd508 30
71d4af1e 31 return this.runQuery()
2760b454 32 .then(rows => this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include }))
e5dbd508
C
33 }
34
35 private buildInnerQuery (options: BuildVideosListQueryOptions) {
36 const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
7e7d8e48 37 const { query, sort, replacements } = idsQueryBuilder.getQuery(options)
e5dbd508
C
38
39 this.replacements = replacements
40 this.innerQuery = query
41 this.innerSort = sort
42 }
43
7e7d8e48 44 private buildMainQuery (options: BuildVideosListQueryOptions) {
e5dbd508
C
45 this.attributes = {
46 '"video".*': ''
47 }
48
3c79c2ce 49 this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"')
e5dbd508
C
50
51 this.includeChannels()
52 this.includeAccounts()
53 this.includeThumbnails()
54
3c10840f 55 if (options.include & VideoInclude.FILES) {
668f864f
C
56 this.includeWebtorrentFiles()
57 this.includeStreamingPlaylistFiles()
e5dbd508
C
58 }
59
60 if (options.user) {
d9bf974f 61 this.includeUserHistory(options.user.id)
e5dbd508
C
62 }
63
64 if (options.videoPlaylistId) {
65 this.includePlaylist(options.videoPlaylistId)
66 }
67
2760b454
C
68 if (options.include & VideoInclude.BLACKLISTED) {
69 this.includeBlacklisted()
70 }
71
72 if (options.include & VideoInclude.BLOCKED_OWNER) {
73 this.includeBlockedOwnerAndServer(options.serverAccountIdForBlock, options.user)
74 }
75
e5dbd508
C
76 const select = this.buildSelect()
77
3c79c2ce 78 this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
e5dbd508 79 }
e5dbd508 80}