]>
Commit | Line | Data |
---|---|---|
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(undefined).then(rows => this.videoModelBuilder.buildVideosFromRows(rows)) | |
31 | } | |
32 | ||
33 | private buildInnerQuery (options: BuildVideosListQueryOptions) { | |
34 | const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize) | |
35 | const { query, sort, replacements } = idsQueryBuilder.getIdsListQueryAndSort(options) | |
36 | ||
37 | this.replacements = replacements | |
38 | this.innerQuery = query | |
39 | this.innerSort = sort | |
40 | } | |
41 | ||
42 | private buildListQueryFromIdsQuery (options: BuildVideosListQueryOptions) { | |
43 | this.attributes = { | |
44 | '"video".*': '' | |
45 | } | |
46 | ||
47 | this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"') | |
48 | ||
49 | this.includeChannels() | |
50 | this.includeAccounts() | |
51 | this.includeThumbnails() | |
52 | ||
53 | if (options.withFiles) { | |
54 | this.includeWebtorrentFiles(false) | |
55 | this.includeStreamingPlaylistFiles(false) | |
56 | } | |
57 | ||
58 | if (options.user) { | |
59 | this.includeUserHistory(options.user.id) | |
60 | } | |
61 | ||
62 | if (options.videoPlaylistId) { | |
63 | this.includePlaylist(options.videoPlaylistId) | |
64 | } | |
65 | ||
66 | const select = this.buildSelect() | |
67 | ||
68 | this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}` | |
69 | } | |
70 | } |