]>
Commit | Line | Data |
---|---|---|
1 | import { Sequelize, Transaction } from 'sequelize' | |
2 | import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder' | |
3 | import { VideoFileQueryBuilder } from './shared/video-file-query-builder' | |
4 | import { VideoModelBuilder } from './shared/video-model-builder' | |
5 | import { VideoTables } from './shared/video-tables' | |
6 | ||
7 | /** | |
8 | * | |
9 | * Build a GET SQL query, fetch rows and create the video model | |
10 | * | |
11 | */ | |
12 | ||
13 | export type BuildVideoGetQueryOptions = { | |
14 | id: number | string | |
15 | transaction?: Transaction | |
16 | userId?: number | |
17 | forGetAPI?: boolean | |
18 | } | |
19 | ||
20 | export class VideosModelGetQueryBuilder { | |
21 | videoQueryBuilder: VideosModelGetQuerySubBuilder | |
22 | webtorrentFilesQueryBuilder: VideoFileQueryBuilder | |
23 | streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder | |
24 | ||
25 | private readonly videoModelBuilder: VideoModelBuilder | |
26 | ||
27 | constructor (protected readonly sequelize: Sequelize) { | |
28 | this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize) | |
29 | this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize) | |
30 | this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize) | |
31 | ||
32 | this.videoModelBuilder = new VideoModelBuilder('get', new VideoTables('get')) | |
33 | } | |
34 | ||
35 | async queryVideos (options: BuildVideoGetQueryOptions) { | |
36 | const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([ | |
37 | this.videoQueryBuilder.queryVideos(options), | |
38 | this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options), | |
39 | this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options) | |
40 | ]) | |
41 | ||
42 | const videos = this.videoModelBuilder.buildVideosFromRows(videoRows, webtorrentFilesRows, streamingPlaylistFilesRows) | |
43 | ||
44 | if (videos.length > 1) { | |
45 | throw new Error('Video results is more than ') | |
46 | } | |
47 | ||
48 | if (videos.length === 0) return null | |
49 | return videos[0] | |
50 | } | |
51 | } | |
52 | ||
53 | export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder { | |
54 | protected attributes: { [key: string]: string } | |
55 | ||
56 | protected webtorrentFilesQuery: string | |
57 | protected streamingPlaylistFilesQuery: string | |
58 | ||
59 | constructor (protected readonly sequelize: Sequelize) { | |
60 | super('get') | |
61 | } | |
62 | ||
63 | queryVideos (options: BuildVideoGetQueryOptions) { | |
64 | this.buildMainGetQuery(options) | |
65 | ||
66 | return this.runQuery(options.transaction) | |
67 | } | |
68 | ||
69 | private buildMainGetQuery (options: BuildVideoGetQueryOptions) { | |
70 | this.attributes = { | |
71 | '"video".*': '' | |
72 | } | |
73 | ||
74 | this.includeChannels() | |
75 | this.includeAccounts() | |
76 | ||
77 | this.includeTags() | |
78 | ||
79 | this.includeThumbnails() | |
80 | ||
81 | this.includeBlacklisted() | |
82 | ||
83 | this.includeScheduleUpdate() | |
84 | ||
85 | this.includeLive() | |
86 | ||
87 | if (options.userId) { | |
88 | this.includeUserHistory(options.userId) | |
89 | } | |
90 | ||
91 | if (options.forGetAPI === true) { | |
92 | this.includeTrackers() | |
93 | } | |
94 | ||
95 | this.whereId(options.id) | |
96 | ||
97 | this.query = this.buildQuery() | |
98 | } | |
99 | ||
100 | private buildQuery () { | |
101 | const order = 'ORDER BY "Tags"."name" ASC' | |
102 | const from = `SELECT * FROM "video" ${this.where} LIMIT 1` | |
103 | ||
104 | return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}` | |
105 | } | |
106 | } |