diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-11 14:09:33 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-11 14:09:52 +0200 |
commit | 71d4af1efc810f853e1a0d986bf758c201692594 (patch) | |
tree | 2066053638baefb6430772c2e0a0aa1774019a51 /server/models/video/sql/video-model-get-query-builder.ts | |
parent | 3c79c2ce86eaf9e151ab6c2c9d1f646968a16744 (diff) | |
download | PeerTube-71d4af1efc810f853e1a0d986bf758c201692594.tar.gz PeerTube-71d4af1efc810f853e1a0d986bf758c201692594.tar.zst PeerTube-71d4af1efc810f853e1a0d986bf758c201692594.zip |
Use raw SQL for most of video queries
Diffstat (limited to 'server/models/video/sql/video-model-get-query-builder.ts')
-rw-r--r-- | server/models/video/sql/video-model-get-query-builder.ts | 110 |
1 files changed, 90 insertions, 20 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 4aab9ff1d..f56fdd474 100644 --- a/server/models/video/sql/video-model-get-query-builder.ts +++ b/server/models/video/sql/video-model-get-query-builder.ts | |||
@@ -11,10 +11,15 @@ import { VideoTables } from './shared/video-tables' | |||
11 | */ | 11 | */ |
12 | 12 | ||
13 | export type BuildVideoGetQueryOptions = { | 13 | export type BuildVideoGetQueryOptions = { |
14 | id: number | string | 14 | id?: number | string |
15 | transaction?: Transaction | 15 | url?: string |
16 | |||
17 | type: 'api' | 'full-light' | 'account-blacklist-files' | 'all-files' | 'thumbnails' | 'thumbnails-blacklist' | 'id' | 'blacklist-rights' | ||
18 | |||
16 | userId?: number | 19 | userId?: number |
17 | forGetAPI?: boolean | 20 | transaction?: Transaction |
21 | |||
22 | logging?: boolean | ||
18 | } | 23 | } |
19 | 24 | ||
20 | export class VideosModelGetQueryBuilder { | 25 | export class VideosModelGetQueryBuilder { |
@@ -32,11 +37,17 @@ export class VideosModelGetQueryBuilder { | |||
32 | this.videoModelBuilder = new VideoModelBuilder('get', new VideoTables('get')) | 37 | this.videoModelBuilder = new VideoModelBuilder('get', new VideoTables('get')) |
33 | } | 38 | } |
34 | 39 | ||
35 | async queryVideos (options: BuildVideoGetQueryOptions) { | 40 | async queryVideo (options: BuildVideoGetQueryOptions) { |
36 | const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([ | 41 | const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([ |
37 | this.videoQueryBuilder.queryVideos(options), | 42 | this.videoQueryBuilder.queryVideos(options), |
38 | this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options), | 43 | |
39 | this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options) | 44 | this.shouldQueryVideoFiles(options) |
45 | ? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options) | ||
46 | : Promise.resolve(undefined), | ||
47 | |||
48 | this.shouldQueryVideoFiles(options) | ||
49 | ? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options) | ||
50 | : Promise.resolve(undefined) | ||
40 | ]) | 51 | ]) |
41 | 52 | ||
42 | const videos = this.videoModelBuilder.buildVideosFromRows(videoRows, webtorrentFilesRows, streamingPlaylistFilesRows) | 53 | const videos = this.videoModelBuilder.buildVideosFromRows(videoRows, webtorrentFilesRows, streamingPlaylistFilesRows) |
@@ -48,6 +59,10 @@ export class VideosModelGetQueryBuilder { | |||
48 | if (videos.length === 0) return null | 59 | if (videos.length === 0) return null |
49 | return videos[0] | 60 | return videos[0] |
50 | } | 61 | } |
62 | |||
63 | private shouldQueryVideoFiles (options: BuildVideoGetQueryOptions) { | ||
64 | return [ 'api', 'full-light', 'account-blacklist-files', 'all-files' ].includes(options.type) | ||
65 | } | ||
51 | } | 66 | } |
52 | 67 | ||
53 | export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder { | 68 | export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder { |
@@ -63,7 +78,7 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild | |||
63 | queryVideos (options: BuildVideoGetQueryOptions) { | 78 | queryVideos (options: BuildVideoGetQueryOptions) { |
64 | this.buildMainGetQuery(options) | 79 | this.buildMainGetQuery(options) |
65 | 80 | ||
66 | return this.runQuery(options.transaction) | 81 | return this.runQuery(options) |
67 | } | 82 | } |
68 | 83 | ||
69 | private buildMainGetQuery (options: BuildVideoGetQueryOptions) { | 84 | private buildMainGetQuery (options: BuildVideoGetQueryOptions) { |
@@ -71,36 +86,91 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuild | |||
71 | '"video".*': '' | 86 | '"video".*': '' |
72 | } | 87 | } |
73 | 88 | ||
74 | this.includeChannels() | 89 | if (this.shouldIncludeThumbnails(options)) { |
75 | this.includeAccounts() | 90 | this.includeThumbnails() |
91 | } | ||
76 | 92 | ||
77 | this.includeTags() | 93 | if (this.shouldIncludeBlacklisted(options)) { |
94 | this.includeBlacklisted() | ||
95 | } | ||
78 | 96 | ||
79 | this.includeThumbnails() | 97 | if (this.shouldIncludeAccount(options)) { |
98 | this.includeChannels() | ||
99 | this.includeAccounts() | ||
100 | } | ||
80 | 101 | ||
81 | this.includeBlacklisted() | 102 | if (this.shouldIncludeTags(options)) { |
103 | this.includeTags() | ||
104 | } | ||
82 | 105 | ||
83 | this.includeScheduleUpdate() | 106 | if (this.shouldIncludeScheduleUpdate(options)) { |
107 | this.includeScheduleUpdate() | ||
108 | } | ||
84 | 109 | ||
85 | this.includeLive() | 110 | if (this.shouldIncludeLive(options)) { |
111 | this.includeLive() | ||
112 | } | ||
86 | 113 | ||
87 | if (options.userId) { | 114 | if (options.userId && this.shouldIncludeUserHistory(options)) { |
88 | this.includeUserHistory(options.userId) | 115 | this.includeUserHistory(options.userId) |
89 | } | 116 | } |
90 | 117 | ||
91 | if (options.forGetAPI === true) { | 118 | if (this.shouldIncludeOwnerUser(options)) { |
119 | this.includeOwnerUser() | ||
120 | } | ||
121 | |||
122 | if (this.shouldIncludeTrackers(options)) { | ||
92 | this.includeTrackers() | 123 | this.includeTrackers() |
93 | } | 124 | } |
94 | 125 | ||
95 | this.whereId(options.id) | 126 | this.whereId(options) |
96 | 127 | ||
97 | this.query = this.buildQuery() | 128 | this.query = this.buildQuery(options) |
98 | } | 129 | } |
99 | 130 | ||
100 | private buildQuery () { | 131 | private buildQuery (options: BuildVideoGetQueryOptions) { |
101 | const order = 'ORDER BY "Tags"."name" ASC' | 132 | const order = this.shouldIncludeTags(options) |
133 | ? 'ORDER BY "Tags"."name" ASC' | ||
134 | : '' | ||
135 | |||
102 | const from = `SELECT * FROM "video" ${this.where} LIMIT 1` | 136 | const from = `SELECT * FROM "video" ${this.where} LIMIT 1` |
103 | 137 | ||
104 | return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}` | 138 | return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}` |
105 | } | 139 | } |
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 | } | ||
106 | } | 176 | } |