diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-11 14:26:37 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-11 14:26:37 +0200 |
commit | 20a206c3d12ad285c31411cd506cede791958322 (patch) | |
tree | a7f8dd5b4cb8978086c8d441098058a2effec450 /server/models/video | |
parent | 71d4af1efc810f853e1a0d986bf758c201692594 (diff) | |
download | PeerTube-20a206c3d12ad285c31411cd506cede791958322.tar.gz PeerTube-20a206c3d12ad285c31411cd506cede791958322.tar.zst PeerTube-20a206c3d12ad285c31411cd506cede791958322.zip |
Refactor include checks
Diffstat (limited to 'server/models/video')
-rw-r--r-- | server/models/video/sql/video-model-get-query-builder.ts | 102 |
1 files changed, 49 insertions, 53 deletions
diff --git a/server/models/video/sql/video-model-get-query-builder.ts b/server/models/video/sql/video-model-get-query-builder.ts index f56fdd474..2545f887e 100644 --- a/server/models/video/sql/video-model-get-query-builder.ts +++ b/server/models/video/sql/video-model-get-query-builder.ts | |||
@@ -10,11 +10,21 @@ import { VideoTables } from './shared/video-tables' | |||
10 | * | 10 | * |
11 | */ | 11 | */ |
12 | 12 | ||
13 | export 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 | |||
13 | export type BuildVideoGetQueryOptions = { | 23 | export type BuildVideoGetQueryOptions = { |
14 | id?: number | string | 24 | id?: number | string |
15 | url?: string | 25 | url?: string |
16 | 26 | ||
17 | type: 'api' | 'full-light' | 'account-blacklist-files' | 'all-files' | 'thumbnails' | 'thumbnails-blacklist' | 'id' | 'blacklist-rights' | 27 | type: GetType |
18 | 28 | ||
19 | userId?: number | 29 | userId?: number |
20 | transaction?: Transaction | 30 | transaction?: Transaction |
@@ -29,6 +39,8 @@ export class VideosModelGetQueryBuilder { | |||
29 | 39 | ||
30 | private readonly videoModelBuilder: VideoModelBuilder | 40 | private readonly videoModelBuilder: VideoModelBuilder |
31 | 41 | ||
42 | private static readonly videoFilesInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files', 'all-files' ]) | ||
43 | |||
32 | constructor (protected readonly sequelize: Sequelize) { | 44 | constructor (protected readonly sequelize: Sequelize) { |
33 | this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize) | 45 | this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize) |
34 | this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize) | 46 | this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize) |
@@ -41,11 +53,11 @@ export class VideosModelGetQueryBuilder { | |||
41 | const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([ | 53 | const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([ |
42 | this.videoQueryBuilder.queryVideos(options), | 54 | this.videoQueryBuilder.queryVideos(options), |
43 | 55 | ||
44 | this.shouldQueryVideoFiles(options) | 56 | VideosModelGetQueryBuilder.videoFilesInclude.has(options.type) |
45 | ? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options) | 57 | ? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options) |
46 | : Promise.resolve(undefined), | 58 | : Promise.resolve(undefined), |
47 | 59 | ||
48 | this.shouldQueryVideoFiles(options) | 60 | VideosModelGetQueryBuilder.videoFilesInclude.has(options.type) |
49 | ? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options) | 61 | ? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options) |
50 | : Promise.resolve(undefined) | 62 | : Promise.resolve(undefined) |
51 | ]) | 63 | ]) |
@@ -59,10 +71,6 @@ export class VideosModelGetQueryBuilder { | |||
59 | if (videos.length === 0) return null | 71 | if (videos.length === 0) return null |
60 | return videos[0] | 72 | return videos[0] |
61 | } | 73 | } |
62 | |||
63 | private shouldQueryVideoFiles (options: BuildVideoGetQueryOptions) { | ||
64 | return [ 'api', 'full-light', 'account-blacklist-files', 'all-files' ].includes(options.type) | ||
65 | } | ||
66 | } | 74 | } |
67 | 75 | ||
68 | export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder { | 76 | export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder { |
@@ -71,6 +79,30 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild | |||
71 | protected webtorrentFilesQuery: string | 79 | protected webtorrentFilesQuery: string |
72 | protected streamingPlaylistFilesQuery: string | 80 | protected streamingPlaylistFilesQuery: string |
73 | 81 | ||
82 | private static readonly trackersInclude = new Set<GetType>([ 'api' ]) | ||
83 | private static readonly liveInclude = new Set<GetType>([ 'api', 'full-light' ]) | ||
84 | private static readonly scheduleUpdateInclude = new Set<GetType>([ 'api', 'full-light' ]) | ||
85 | private static readonly tagsInclude = new Set<GetType>([ 'api', 'full-light' ]) | ||
86 | private static readonly userHistoryInclude = new Set<GetType>([ 'api', 'full-light' ]) | ||
87 | private static readonly accountInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files' ]) | ||
88 | private static readonly ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ]) | ||
89 | |||
90 | private static readonly blacklistedInclude = new Set<GetType>([ | ||
91 | 'api', | ||
92 | 'full-light', | ||
93 | 'account-blacklist-files', | ||
94 | 'thumbnails-blacklist', | ||
95 | 'blacklist-rights' | ||
96 | ]) | ||
97 | |||
98 | private static readonly thumbnailsInclude = new Set<GetType>([ | ||
99 | 'api', | ||
100 | 'full-light', | ||
101 | 'account-blacklist-files', | ||
102 | 'thumbnails', | ||
103 | 'thumbnails-blacklist' | ||
104 | ]) | ||
105 | |||
74 | constructor (protected readonly sequelize: Sequelize) { | 106 | constructor (protected readonly sequelize: Sequelize) { |
75 | super('get') | 107 | super('get') |
76 | } | 108 | } |
@@ -86,40 +118,40 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild | |||
86 | '"video".*': '' | 118 | '"video".*': '' |
87 | } | 119 | } |
88 | 120 | ||
89 | if (this.shouldIncludeThumbnails(options)) { | 121 | if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) { |
90 | this.includeThumbnails() | 122 | this.includeThumbnails() |
91 | } | 123 | } |
92 | 124 | ||
93 | if (this.shouldIncludeBlacklisted(options)) { | 125 | if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) { |
94 | this.includeBlacklisted() | 126 | this.includeBlacklisted() |
95 | } | 127 | } |
96 | 128 | ||
97 | if (this.shouldIncludeAccount(options)) { | 129 | if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) { |
98 | this.includeChannels() | 130 | this.includeChannels() |
99 | this.includeAccounts() | 131 | this.includeAccounts() |
100 | } | 132 | } |
101 | 133 | ||
102 | if (this.shouldIncludeTags(options)) { | 134 | if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) { |
103 | this.includeTags() | 135 | this.includeTags() |
104 | } | 136 | } |
105 | 137 | ||
106 | if (this.shouldIncludeScheduleUpdate(options)) { | 138 | if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) { |
107 | this.includeScheduleUpdate() | 139 | this.includeScheduleUpdate() |
108 | } | 140 | } |
109 | 141 | ||
110 | if (this.shouldIncludeLive(options)) { | 142 | if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) { |
111 | this.includeLive() | 143 | this.includeLive() |
112 | } | 144 | } |
113 | 145 | ||
114 | if (options.userId && this.shouldIncludeUserHistory(options)) { | 146 | if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) { |
115 | this.includeUserHistory(options.userId) | 147 | this.includeUserHistory(options.userId) |
116 | } | 148 | } |
117 | 149 | ||
118 | if (this.shouldIncludeOwnerUser(options)) { | 150 | if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) { |
119 | this.includeOwnerUser() | 151 | this.includeOwnerUser() |
120 | } | 152 | } |
121 | 153 | ||
122 | if (this.shouldIncludeTrackers(options)) { | 154 | if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) { |
123 | this.includeTrackers() | 155 | this.includeTrackers() |
124 | } | 156 | } |
125 | 157 | ||
@@ -129,7 +161,7 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild | |||
129 | } | 161 | } |
130 | 162 | ||
131 | private buildQuery (options: BuildVideoGetQueryOptions) { | 163 | private buildQuery (options: BuildVideoGetQueryOptions) { |
132 | const order = this.shouldIncludeTags(options) | 164 | const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type) |
133 | ? 'ORDER BY "Tags"."name" ASC' | 165 | ? 'ORDER BY "Tags"."name" ASC' |
134 | : '' | 166 | : '' |
135 | 167 | ||
@@ -137,40 +169,4 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild | |||
137 | 169 | ||
138 | return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}` | 170 | return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}` |
139 | } | 171 | } |
140 | |||
141 | private shouldIncludeTrackers (options: BuildVideoGetQueryOptions) { | ||
142 | return options.type === 'api' | ||
143 | } | ||
144 | |||
145 | private shouldIncludeLive (options: BuildVideoGetQueryOptions) { | ||
146 | return [ 'api', 'full-light' ].includes(options.type) | ||
147 | } | ||
148 | |||
149 | private shouldIncludeScheduleUpdate (options: BuildVideoGetQueryOptions) { | ||
150 | return [ 'api', 'full-light' ].includes(options.type) | ||
151 | } | ||
152 | |||
153 | private shouldIncludeTags (options: BuildVideoGetQueryOptions) { | ||
154 | return [ 'api', 'full-light' ].includes(options.type) | ||
155 | } | ||
156 | |||
157 | private shouldIncludeUserHistory (options: BuildVideoGetQueryOptions) { | ||
158 | return [ 'api', 'full-light' ].includes(options.type) | ||
159 | } | ||
160 | |||
161 | private shouldIncludeAccount (options: BuildVideoGetQueryOptions) { | ||
162 | return [ 'api', 'full-light', 'account-blacklist-files' ].includes(options.type) | ||
163 | } | ||
164 | |||
165 | private shouldIncludeBlacklisted (options: BuildVideoGetQueryOptions) { | ||
166 | return [ 'api', 'full-light', 'account-blacklist-files', 'thumbnails-blacklist', 'blacklist-rights' ].includes(options.type) | ||
167 | } | ||
168 | |||
169 | private shouldIncludeOwnerUser (options: BuildVideoGetQueryOptions) { | ||
170 | return options.type === 'blacklist-rights' | ||
171 | } | ||
172 | |||
173 | private shouldIncludeThumbnails (options: BuildVideoGetQueryOptions) { | ||
174 | return [ 'api', 'full-light', 'account-blacklist-files', 'thumbnails', 'thumbnails-blacklist' ].includes(options.type) | ||
175 | } | ||
176 | } | 172 | } |