]>
Commit | Line | Data |
---|---|---|
1 | import { VideoInclude } from '@shared/models' | |
2 | import { Sequelize } from 'sequelize' | |
3 | import { AbstractVideoQueryBuilder } from './shared/abstract-video-query-builder' | |
4 | import { VideoModelBuilder } from './shared/video-model-builder' | |
5 | import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder' | |
6 | ||
7 | /** | |
8 | * | |
9 | * Build videos list SQL query and create video models | |
10 | * | |
11 | */ | |
12 | ||
13 | export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder { | |
14 | protected attributes: { [key: string]: string } | |
15 | ||
16 | private innerQuery: string | |
17 | private innerSort: string | |
18 | ||
19 | private readonly videoModelBuilder: VideoModelBuilder | |
20 | ||
21 | constructor (protected readonly sequelize: Sequelize) { | |
22 | super('list') | |
23 | ||
24 | this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables) | |
25 | } | |
26 | ||
27 | queryVideos (options: BuildVideosListQueryOptions) { | |
28 | this.buildInnerQuery(options) | |
29 | this.buildMainQuery(options) | |
30 | ||
31 | return this.runQuery() | |
32 | .then(rows => this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include })) | |
33 | } | |
34 | ||
35 | private buildInnerQuery (options: BuildVideosListQueryOptions) { | |
36 | const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize) | |
37 | const { query, sort, replacements } = idsQueryBuilder.getQuery(options) | |
38 | ||
39 | this.replacements = replacements | |
40 | this.innerQuery = query | |
41 | this.innerSort = sort | |
42 | } | |
43 | ||
44 | private buildMainQuery (options: BuildVideosListQueryOptions) { | |
45 | this.attributes = { | |
46 | '"video".*': '' | |
47 | } | |
48 | ||
49 | this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"') | |
50 | ||
51 | this.includeChannels() | |
52 | this.includeAccounts() | |
53 | this.includeThumbnails() | |
54 | ||
55 | if (options.include & VideoInclude.FILES) { | |
56 | this.includeWebtorrentFiles() | |
57 | this.includeStreamingPlaylistFiles() | |
58 | } | |
59 | ||
60 | if (options.user) { | |
61 | this.includeUserHistory(options.user.id) | |
62 | } | |
63 | ||
64 | if (options.videoPlaylistId) { | |
65 | this.includePlaylist(options.videoPlaylistId) | |
66 | } | |
67 | ||
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 | ||
76 | const select = this.buildSelect() | |
77 | ||
78 | this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}` | |
79 | } | |
80 | } |