]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/sql/shared/video-model-builder.ts
Use different p2p policy for embeds and webapp
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / shared / video-model-builder.ts
1
2 import { AccountModel } from '@server/models/account/account'
3 import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
4 import { ActorModel } from '@server/models/actor/actor'
5 import { ActorImageModel } from '@server/models/actor/actor-image'
6 import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
7 import { ServerModel } from '@server/models/server/server'
8 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
9 import { TrackerModel } from '@server/models/server/tracker'
10 import { UserVideoHistoryModel } from '@server/models/user/user-video-history'
11 import { VideoInclude } from '@shared/models'
12 import { ScheduleVideoUpdateModel } from '../../schedule-video-update'
13 import { TagModel } from '../../tag'
14 import { ThumbnailModel } from '../../thumbnail'
15 import { VideoModel } from '../../video'
16 import { VideoBlacklistModel } from '../../video-blacklist'
17 import { VideoChannelModel } from '../../video-channel'
18 import { VideoFileModel } from '../../video-file'
19 import { VideoLiveModel } from '../../video-live'
20 import { VideoStreamingPlaylistModel } from '../../video-streaming-playlist'
21 import { VideoTableAttributes } from './video-table-attributes'
22
23 type SQLRow = { [id: string]: string | number }
24
25 /**
26 *
27 * Build video models from SQL rows
28 *
29 */
30
31 export class VideoModelBuilder {
32 private videosMemo: { [ id: number ]: VideoModel }
33 private videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel }
34 private videoFileMemo: { [ id: number ]: VideoFileModel }
35
36 private thumbnailsDone: Set<any>
37 private historyDone: Set<any>
38 private blacklistDone: Set<any>
39 private accountBlocklistDone: Set<any>
40 private serverBlocklistDone: Set<any>
41 private liveDone: Set<any>
42 private redundancyDone: Set<any>
43 private scheduleVideoUpdateDone: Set<any>
44
45 private trackersDone: Set<string>
46 private tagsDone: Set<string>
47
48 private videos: VideoModel[]
49
50 private readonly buildOpts = { raw: true, isNewRecord: false }
51
52 constructor (
53 readonly mode: 'get' | 'list',
54 readonly tables: VideoTableAttributes
55 ) {
56
57 }
58
59 buildVideosFromRows (options: {
60 rows: SQLRow[]
61 include?: VideoInclude
62 rowsWebTorrentFiles?: SQLRow[]
63 rowsStreamingPlaylist?: SQLRow[]
64 }) {
65 const { rows, rowsWebTorrentFiles, rowsStreamingPlaylist, include } = options
66
67 this.reinit()
68
69 for (const row of rows) {
70 this.buildVideoAndAccount(row)
71
72 const videoModel = this.videosMemo[row.id]
73
74 this.setUserHistory(row, videoModel)
75 this.addThumbnail(row, videoModel)
76
77 if (!rowsWebTorrentFiles) {
78 this.addWebTorrentFile(row, videoModel)
79 }
80
81 if (!rowsStreamingPlaylist) {
82 this.addStreamingPlaylist(row, videoModel)
83 this.addStreamingPlaylistFile(row)
84 }
85
86 if (this.mode === 'get') {
87 this.addTag(row, videoModel)
88 this.addTracker(row, videoModel)
89 this.setBlacklisted(row, videoModel)
90 this.setScheduleVideoUpdate(row, videoModel)
91 this.setLive(row, videoModel)
92 } else {
93 if (include & VideoInclude.BLACKLISTED) {
94 this.setBlacklisted(row, videoModel)
95 }
96
97 if (include & VideoInclude.BLOCKED_OWNER) {
98 this.setBlockedOwner(row, videoModel)
99 this.setBlockedServer(row, videoModel)
100 }
101 }
102 }
103
104 this.grabSeparateWebTorrentFiles(rowsWebTorrentFiles)
105 this.grabSeparateStreamingPlaylistFiles(rowsStreamingPlaylist)
106
107 return this.videos
108 }
109
110 private reinit () {
111 this.videosMemo = {}
112 this.videoStreamingPlaylistMemo = {}
113 this.videoFileMemo = {}
114
115 this.thumbnailsDone = new Set()
116 this.historyDone = new Set()
117 this.blacklistDone = new Set()
118 this.liveDone = new Set()
119 this.redundancyDone = new Set()
120 this.scheduleVideoUpdateDone = new Set()
121
122 this.accountBlocklistDone = new Set()
123 this.serverBlocklistDone = new Set()
124
125 this.trackersDone = new Set()
126 this.tagsDone = new Set()
127
128 this.videos = []
129 }
130
131 private grabSeparateWebTorrentFiles (rowsWebTorrentFiles?: SQLRow[]) {
132 if (!rowsWebTorrentFiles) return
133
134 for (const row of rowsWebTorrentFiles) {
135 const id = row['VideoFiles.id']
136 if (!id) continue
137
138 const videoModel = this.videosMemo[row.id]
139 this.addWebTorrentFile(row, videoModel)
140 this.addRedundancy(row, 'VideoFiles', this.videoFileMemo[id])
141 }
142 }
143
144 private grabSeparateStreamingPlaylistFiles (rowsStreamingPlaylist?: SQLRow[]) {
145 if (!rowsStreamingPlaylist) return
146
147 for (const row of rowsStreamingPlaylist) {
148 const id = row['VideoStreamingPlaylists.id']
149 if (!id) continue
150
151 const videoModel = this.videosMemo[row.id]
152
153 this.addStreamingPlaylist(row, videoModel)
154 this.addStreamingPlaylistFile(row)
155 this.addRedundancy(row, 'VideoStreamingPlaylists', this.videoStreamingPlaylistMemo[id])
156 }
157 }
158
159 private buildVideoAndAccount (row: SQLRow) {
160 if (this.videosMemo[row.id]) return
161
162 const videoModel = new VideoModel(this.grab(row, this.tables.getVideoAttributes(), ''), this.buildOpts)
163
164 videoModel.UserVideoHistories = []
165 videoModel.Thumbnails = []
166 videoModel.VideoFiles = []
167 videoModel.VideoStreamingPlaylists = []
168 videoModel.Tags = []
169 videoModel.Trackers = []
170
171 this.buildAccount(row, videoModel)
172
173 this.videosMemo[row.id] = videoModel
174
175 // Keep rows order
176 this.videos.push(videoModel)
177 }
178
179 private buildAccount (row: SQLRow, videoModel: VideoModel) {
180 const id = row['VideoChannel.Account.id']
181 if (!id) return
182
183 const channelModel = new VideoChannelModel(this.grab(row, this.tables.getChannelAttributes(), 'VideoChannel'), this.buildOpts)
184 channelModel.Actor = this.buildActor(row, 'VideoChannel')
185
186 const accountModel = new AccountModel(this.grab(row, this.tables.getAccountAttributes(), 'VideoChannel.Account'), this.buildOpts)
187 accountModel.Actor = this.buildActor(row, 'VideoChannel.Account')
188
189 accountModel.BlockedBy = []
190
191 channelModel.Account = accountModel
192
193 videoModel.VideoChannel = channelModel
194 }
195
196 private buildActor (row: SQLRow, prefix: string) {
197 const actorPrefix = `${prefix}.Actor`
198 const avatarPrefix = `${actorPrefix}.Avatar`
199 const serverPrefix = `${actorPrefix}.Server`
200
201 const avatarModel = row[`${avatarPrefix}.id`] !== null
202 ? new ActorImageModel(this.grab(row, this.tables.getAvatarAttributes(), avatarPrefix), this.buildOpts)
203 : null
204
205 const serverModel = row[`${serverPrefix}.id`] !== null
206 ? new ServerModel(this.grab(row, this.tables.getServerAttributes(), serverPrefix), this.buildOpts)
207 : null
208
209 if (serverModel) serverModel.BlockedBy = []
210
211 const actorModel = new ActorModel(this.grab(row, this.tables.getActorAttributes(), actorPrefix), this.buildOpts)
212 actorModel.Avatar = avatarModel
213 actorModel.Server = serverModel
214
215 return actorModel
216 }
217
218 private setUserHistory (row: SQLRow, videoModel: VideoModel) {
219 const id = row['userVideoHistory.id']
220 if (!id || this.historyDone.has(id)) return
221
222 const attributes = this.grab(row, this.tables.getUserHistoryAttributes(), 'userVideoHistory')
223 const historyModel = new UserVideoHistoryModel(attributes, this.buildOpts)
224 videoModel.UserVideoHistories.push(historyModel)
225
226 this.historyDone.add(id)
227 }
228
229 private addThumbnail (row: SQLRow, videoModel: VideoModel) {
230 const id = row['Thumbnails.id']
231 if (!id || this.thumbnailsDone.has(id)) return
232
233 const attributes = this.grab(row, this.tables.getThumbnailAttributes(), 'Thumbnails')
234 const thumbnailModel = new ThumbnailModel(attributes, this.buildOpts)
235 videoModel.Thumbnails.push(thumbnailModel)
236
237 this.thumbnailsDone.add(id)
238 }
239
240 private addWebTorrentFile (row: SQLRow, videoModel: VideoModel) {
241 const id = row['VideoFiles.id']
242 if (!id || this.videoFileMemo[id]) return
243
244 const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoFiles')
245 const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
246 videoModel.VideoFiles.push(videoFileModel)
247
248 this.videoFileMemo[id] = videoFileModel
249 }
250
251 private addStreamingPlaylist (row: SQLRow, videoModel: VideoModel) {
252 const id = row['VideoStreamingPlaylists.id']
253 if (!id || this.videoStreamingPlaylistMemo[id]) return
254
255 const attributes = this.grab(row, this.tables.getStreamingPlaylistAttributes(), 'VideoStreamingPlaylists')
256 const streamingPlaylist = new VideoStreamingPlaylistModel(attributes, this.buildOpts)
257 streamingPlaylist.VideoFiles = []
258
259 videoModel.VideoStreamingPlaylists.push(streamingPlaylist)
260
261 this.videoStreamingPlaylistMemo[id] = streamingPlaylist
262 }
263
264 private addStreamingPlaylistFile (row: SQLRow) {
265 const id = row['VideoStreamingPlaylists.VideoFiles.id']
266 if (!id || this.videoFileMemo[id]) return
267
268 const streamingPlaylist = this.videoStreamingPlaylistMemo[row['VideoStreamingPlaylists.id']]
269
270 const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoStreamingPlaylists.VideoFiles')
271 const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
272 streamingPlaylist.VideoFiles.push(videoFileModel)
273
274 this.videoFileMemo[id] = videoFileModel
275 }
276
277 private addRedundancy (row: SQLRow, prefix: string, to: VideoFileModel | VideoStreamingPlaylistModel) {
278 if (!to.RedundancyVideos) to.RedundancyVideos = []
279
280 const redundancyPrefix = `${prefix}.RedundancyVideos`
281 const id = row[`${redundancyPrefix}.id`]
282
283 if (!id || this.redundancyDone.has(id)) return
284
285 const attributes = this.grab(row, this.tables.getRedundancyAttributes(), redundancyPrefix)
286 const redundancyModel = new VideoRedundancyModel(attributes, this.buildOpts)
287 to.RedundancyVideos.push(redundancyModel)
288
289 this.redundancyDone.add(id)
290 }
291
292 private addTag (row: SQLRow, videoModel: VideoModel) {
293 if (!row['Tags.name']) return
294
295 const key = `${row['Tags.VideoTagModel.videoId']}-${row['Tags.VideoTagModel.tagId']}`
296 if (this.tagsDone.has(key)) return
297
298 const attributes = this.grab(row, this.tables.getTagAttributes(), 'Tags')
299 const tagModel = new TagModel(attributes, this.buildOpts)
300 videoModel.Tags.push(tagModel)
301
302 this.tagsDone.add(key)
303 }
304
305 private addTracker (row: SQLRow, videoModel: VideoModel) {
306 if (!row['Trackers.id']) return
307
308 const key = `${row['Trackers.VideoTrackerModel.videoId']}-${row['Trackers.VideoTrackerModel.trackerId']}`
309 if (this.trackersDone.has(key)) return
310
311 const attributes = this.grab(row, this.tables.getTrackerAttributes(), 'Trackers')
312 const trackerModel = new TrackerModel(attributes, this.buildOpts)
313 videoModel.Trackers.push(trackerModel)
314
315 this.trackersDone.add(key)
316 }
317
318 private setBlacklisted (row: SQLRow, videoModel: VideoModel) {
319 const id = row['VideoBlacklist.id']
320 if (!id || this.blacklistDone.has(id)) return
321
322 const attributes = this.grab(row, this.tables.getBlacklistedAttributes(), 'VideoBlacklist')
323 videoModel.VideoBlacklist = new VideoBlacklistModel(attributes, this.buildOpts)
324
325 this.blacklistDone.add(id)
326 }
327
328 private setBlockedOwner (row: SQLRow, videoModel: VideoModel) {
329 const id = row['VideoChannel.Account.AccountBlocklist.id']
330 if (!id) return
331
332 const key = `${videoModel.id}-${id}`
333 if (this.accountBlocklistDone.has(key)) return
334
335 const attributes = this.grab(row, this.tables.getBlocklistAttributes(), 'VideoChannel.Account.AccountBlocklist')
336 videoModel.VideoChannel.Account.BlockedBy.push(new AccountBlocklistModel(attributes, this.buildOpts))
337
338 this.accountBlocklistDone.add(key)
339 }
340
341 private setBlockedServer (row: SQLRow, videoModel: VideoModel) {
342 const id = row['VideoChannel.Account.Actor.Server.ServerBlocklist.id']
343 if (!id || this.serverBlocklistDone.has(id)) return
344
345 const key = `${videoModel.id}-${id}`
346 if (this.serverBlocklistDone.has(key)) return
347
348 const attributes = this.grab(row, this.tables.getBlocklistAttributes(), 'VideoChannel.Account.Actor.Server.ServerBlocklist')
349 videoModel.VideoChannel.Account.Actor.Server.BlockedBy.push(new ServerBlocklistModel(attributes, this.buildOpts))
350
351 this.serverBlocklistDone.add(key)
352 }
353
354 private setScheduleVideoUpdate (row: SQLRow, videoModel: VideoModel) {
355 const id = row['ScheduleVideoUpdate.id']
356 if (!id || this.scheduleVideoUpdateDone.has(id)) return
357
358 const attributes = this.grab(row, this.tables.getScheduleUpdateAttributes(), 'ScheduleVideoUpdate')
359 videoModel.ScheduleVideoUpdate = new ScheduleVideoUpdateModel(attributes, this.buildOpts)
360
361 this.scheduleVideoUpdateDone.add(id)
362 }
363
364 private setLive (row: SQLRow, videoModel: VideoModel) {
365 const id = row['VideoLive.id']
366 if (!id || this.liveDone.has(id)) return
367
368 const attributes = this.grab(row, this.tables.getLiveAttributes(), 'VideoLive')
369 videoModel.VideoLive = new VideoLiveModel(attributes, this.buildOpts)
370
371 this.liveDone.add(id)
372 }
373
374 private grab (row: SQLRow, attributes: string[], prefix: string) {
375 const result: { [ id: string ]: string | number } = {}
376
377 for (const a of attributes) {
378 const key = prefix
379 ? prefix + '.' + a
380 : a
381
382 result[a] = row[key]
383 }
384
385 return result
386 }
387 }