]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/sql/video-model-get-query-builder.ts
Deprecate filter video query
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / video-model-get-query-builder.ts
CommitLineData
d9bf974f 1import { Sequelize, Transaction } from 'sequelize'
d9bf974f 2import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
1d43c3a6
C
3import { VideoFileQueryBuilder } from './shared/video-file-query-builder'
4import { VideoModelBuilder } from './shared/video-model-builder'
17bb4538 5import { VideoTables } from './shared/video-tables'
1d43c3a6
C
6
7/**
8 *
9 * Build a GET SQL query, fetch rows and create the video model
10 *
11 */
d9bf974f 12
20a206c3
C
13export type GetType =
14 'api' |
15 'full-light' |
16 'account-blacklist-files' |
17 'all-files' |
18 'thumbnails' |
19 'thumbnails-blacklist' |
20 'id' |
21 'blacklist-rights'
22
d9bf974f 23export type BuildVideoGetQueryOptions = {
71d4af1e
C
24 id?: number | string
25 url?: string
26
20a206c3 27 type: GetType
71d4af1e 28
d9bf974f 29 userId?: number
71d4af1e
C
30 transaction?: Transaction
31
32 logging?: boolean
d9bf974f
C
33}
34
1d43c3a6
C
35export class VideosModelGetQueryBuilder {
36 videoQueryBuilder: VideosModelGetQuerySubBuilder
37 webtorrentFilesQueryBuilder: VideoFileQueryBuilder
38 streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
39
40 private readonly videoModelBuilder: VideoModelBuilder
41
20a206c3
C
42 private static readonly videoFilesInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files', 'all-files' ])
43
1d43c3a6
C
44 constructor (protected readonly sequelize: Sequelize) {
45 this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
46 this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
47 this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
48
17bb4538 49 this.videoModelBuilder = new VideoModelBuilder('get', new VideoTables('get'))
1d43c3a6
C
50 }
51
71d4af1e 52 async queryVideo (options: BuildVideoGetQueryOptions) {
1d43c3a6
C
53 const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
54 this.videoQueryBuilder.queryVideos(options),
71d4af1e 55
20a206c3 56 VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
71d4af1e
C
57 ? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options)
58 : Promise.resolve(undefined),
59
20a206c3 60 VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
71d4af1e
C
61 ? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
62 : Promise.resolve(undefined)
1d43c3a6
C
63 ])
64
2760b454
C
65 const videos = this.videoModelBuilder.buildVideosFromRows({
66 rows: videoRows,
67 rowsWebTorrentFiles: webtorrentFilesRows,
68 rowsStreamingPlaylist: streamingPlaylistFilesRows
69 })
1d43c3a6
C
70
71 if (videos.length > 1) {
72 throw new Error('Video results is more than ')
73 }
74
75 if (videos.length === 0) return null
76 return videos[0]
77 }
78}
79
80export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder {
d9bf974f 81 protected attributes: { [key: string]: string }
1d43c3a6
C
82
83 protected webtorrentFilesQuery: string
84 protected streamingPlaylistFilesQuery: string
d9bf974f 85
20a206c3
C
86 private static readonly trackersInclude = new Set<GetType>([ 'api' ])
87 private static readonly liveInclude = new Set<GetType>([ 'api', 'full-light' ])
88 private static readonly scheduleUpdateInclude = new Set<GetType>([ 'api', 'full-light' ])
89 private static readonly tagsInclude = new Set<GetType>([ 'api', 'full-light' ])
90 private static readonly userHistoryInclude = new Set<GetType>([ 'api', 'full-light' ])
91 private static readonly accountInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files' ])
92 private static readonly ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])
93
94 private static readonly blacklistedInclude = new Set<GetType>([
95 'api',
96 'full-light',
97 'account-blacklist-files',
98 'thumbnails-blacklist',
99 'blacklist-rights'
100 ])
101
102 private static readonly thumbnailsInclude = new Set<GetType>([
103 'api',
104 'full-light',
105 'account-blacklist-files',
adddb12b 106 'all-files',
20a206c3
C
107 'thumbnails',
108 'thumbnails-blacklist'
109 ])
110
d9bf974f
C
111 constructor (protected readonly sequelize: Sequelize) {
112 super('get')
113 }
114
115 queryVideos (options: BuildVideoGetQueryOptions) {
1d43c3a6 116 this.buildMainGetQuery(options)
d9bf974f 117
71d4af1e 118 return this.runQuery(options)
d9bf974f
C
119 }
120
1d43c3a6 121 private buildMainGetQuery (options: BuildVideoGetQueryOptions) {
d9bf974f
C
122 this.attributes = {
123 '"video".*': ''
124 }
125
20a206c3 126 if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) {
71d4af1e
C
127 this.includeThumbnails()
128 }
d9bf974f 129
20a206c3 130 if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) {
71d4af1e
C
131 this.includeBlacklisted()
132 }
d9bf974f 133
20a206c3 134 if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) {
71d4af1e
C
135 this.includeChannels()
136 this.includeAccounts()
137 }
d9bf974f 138
20a206c3 139 if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) {
71d4af1e
C
140 this.includeTags()
141 }
d9bf974f 142
20a206c3 143 if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) {
71d4af1e
C
144 this.includeScheduleUpdate()
145 }
d9bf974f 146
20a206c3 147 if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) {
71d4af1e
C
148 this.includeLive()
149 }
d9bf974f 150
20a206c3 151 if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) {
d9bf974f
C
152 this.includeUserHistory(options.userId)
153 }
154
20a206c3 155 if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) {
71d4af1e
C
156 this.includeOwnerUser()
157 }
158
20a206c3 159 if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) {
d9bf974f 160 this.includeTrackers()
d9bf974f
C
161 }
162
71d4af1e 163 this.whereId(options)
d9bf974f 164
71d4af1e 165 this.query = this.buildQuery(options)
d9bf974f
C
166 }
167
71d4af1e 168 private buildQuery (options: BuildVideoGetQueryOptions) {
20a206c3 169 const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)
71d4af1e
C
170 ? 'ORDER BY "Tags"."name" ASC'
171 : ''
172
1d43c3a6 173 const from = `SELECT * FROM "video" ${this.where} LIMIT 1`
d9bf974f 174
3c79c2ce 175 return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`
d9bf974f
C
176 }
177}