]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/sql/video/videos-model-list-query-builder.ts
Feature/Add replay privacy (#5692)
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / video / videos-model-list-query-builder.ts
CommitLineData
e5dbd508 1import { Sequelize } from 'sequelize'
2ec349aa 2import { pick } from '@shared/core-utils'
7fb45bda 3import { VideoInclude } from '@shared/models'
7e7d8e48 4import { AbstractVideoQueryBuilder } from './shared/abstract-video-query-builder'
7fb45bda 5import { VideoFileQueryBuilder } from './shared/video-file-query-builder'
1d43c3a6 6import { VideoModelBuilder } from './shared/video-model-builder'
e5dbd508
C
7import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder'
8
1d43c3a6
C
9/**
10 *
11 * Build videos list SQL query and create video models
12 *
13 */
14
7e7d8e48 15export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
d9bf974f 16 protected attributes: { [key: string]: string }
e5dbd508
C
17
18 private innerQuery: string
19 private innerSort: string
20
7fb45bda
C
21 webtorrentFilesQueryBuilder: VideoFileQueryBuilder
22 streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
23
1d43c3a6
C
24 private readonly videoModelBuilder: VideoModelBuilder
25
e5dbd508 26 constructor (protected readonly sequelize: Sequelize) {
156c44c8 27 super(sequelize, 'list')
1d43c3a6 28
17bb4538 29 this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
7fb45bda
C
30 this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
31 this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
e5dbd508
C
32 }
33
7fb45bda 34 async queryVideos (options: BuildVideosListQueryOptions) {
e5dbd508 35 this.buildInnerQuery(options)
7e7d8e48 36 this.buildMainQuery(options)
e5dbd508 37
7fb45bda
C
38 const rows = await this.runQuery()
39
40 if (options.include & VideoInclude.FILES) {
41 const videoIds = Array.from(new Set(rows.map(r => r.id)))
42
43 if (videoIds.length !== 0) {
44 const fileQueryOptions = {
45 ...pick(options, [ 'transaction', 'logging' ]),
46
47 ids: videoIds,
48 includeRedundancy: false
49 }
50
51 const [ rowsWebTorrentFiles, rowsStreamingPlaylist ] = await Promise.all([
52 this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(fileQueryOptions),
53 this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(fileQueryOptions)
54 ])
55
56 return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include, rowsStreamingPlaylist, rowsWebTorrentFiles })
57 }
58 }
59
60 return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include })
e5dbd508
C
61 }
62
63 private buildInnerQuery (options: BuildVideosListQueryOptions) {
64 const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
7e7d8e48 65 const { query, sort, replacements } = idsQueryBuilder.getQuery(options)
e5dbd508
C
66
67 this.replacements = replacements
68 this.innerQuery = query
69 this.innerSort = sort
70 }
71
7e7d8e48 72 private buildMainQuery (options: BuildVideosListQueryOptions) {
e5dbd508
C
73 this.attributes = {
74 '"video".*': ''
75 }
76
3c79c2ce 77 this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"')
e5dbd508
C
78
79 this.includeChannels()
80 this.includeAccounts()
81 this.includeThumbnails()
82
e5dbd508 83 if (options.user) {
d9bf974f 84 this.includeUserHistory(options.user.id)
e5dbd508
C
85 }
86
87 if (options.videoPlaylistId) {
88 this.includePlaylist(options.videoPlaylistId)
89 }
90
2760b454
C
91 if (options.include & VideoInclude.BLACKLISTED) {
92 this.includeBlacklisted()
93 }
94
95 if (options.include & VideoInclude.BLOCKED_OWNER) {
96 this.includeBlockedOwnerAndServer(options.serverAccountIdForBlock, options.user)
97 }
98
e5dbd508
C
99 const select = this.buildSelect()
100
3c79c2ce 101 this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
e5dbd508 102 }
e5dbd508 103}