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'
23 type SQLRow = { [id: string]: string | number }
27 * Build video models from SQL rows
31 export class VideoModelBuilder {
32 private videosMemo: { [ id: number ]: VideoModel }
33 private videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel }
34 private videoFileMemo: { [ id: number ]: VideoFileModel }
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>
45 private trackersDone: Set<string>
46 private tagsDone: Set<string>
48 private videos: VideoModel[]
50 private readonly buildOpts = { raw: true, isNewRecord: false }
53 readonly mode: 'get' | 'list',
54 readonly tables: VideoTableAttributes
59 buildVideosFromRows (options: {
61 include?: VideoInclude
62 rowsWebTorrentFiles?: SQLRow[]
63 rowsStreamingPlaylist?: SQLRow[]
65 const { rows, rowsWebTorrentFiles, rowsStreamingPlaylist, include } = options
69 for (const row of rows) {
70 this.buildVideoAndAccount(row)
72 const videoModel = this.videosMemo[row.id]
74 this.setUserHistory(row, videoModel)
75 this.addThumbnail(row, videoModel)
77 if (!rowsWebTorrentFiles) {
78 this.addWebTorrentFile(row, videoModel)
81 if (!rowsStreamingPlaylist) {
82 this.addStreamingPlaylist(row, videoModel)
83 this.addStreamingPlaylistFile(row)
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)
93 if (include & VideoInclude.BLACKLISTED) {
94 this.setBlacklisted(row, videoModel)
97 if (include & VideoInclude.BLOCKED_OWNER) {
98 this.setBlockedOwner(row, videoModel)
99 this.setBlockedServer(row, videoModel)
104 this.grabSeparateWebTorrentFiles(rowsWebTorrentFiles)
105 this.grabSeparateStreamingPlaylistFiles(rowsStreamingPlaylist)
112 this.videoStreamingPlaylistMemo = {}
113 this.videoFileMemo = {}
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()
122 this.accountBlocklistDone = new Set()
123 this.serverBlocklistDone = new Set()
125 this.trackersDone = new Set()
126 this.tagsDone = new Set()
131 private grabSeparateWebTorrentFiles (rowsWebTorrentFiles?: SQLRow[]) {
132 if (!rowsWebTorrentFiles) return
134 for (const row of rowsWebTorrentFiles) {
135 const id = row['VideoFiles.id']
138 const videoModel = this.videosMemo[row.id]
139 this.addWebTorrentFile(row, videoModel)
140 this.addRedundancy(row, 'VideoFiles', this.videoFileMemo[id])
144 private grabSeparateStreamingPlaylistFiles (rowsStreamingPlaylist?: SQLRow[]) {
145 if (!rowsStreamingPlaylist) return
147 for (const row of rowsStreamingPlaylist) {
148 const id = row['VideoStreamingPlaylists.id']
151 const videoModel = this.videosMemo[row.id]
153 this.addStreamingPlaylist(row, videoModel)
154 this.addStreamingPlaylistFile(row)
155 this.addRedundancy(row, 'VideoStreamingPlaylists', this.videoStreamingPlaylistMemo[id])
159 private buildVideoAndAccount (row: SQLRow) {
160 if (this.videosMemo[row.id]) return
162 const videoModel = new VideoModel(this.grab(row, this.tables.getVideoAttributes(), ''), this.buildOpts)
164 videoModel.UserVideoHistories = []
165 videoModel.Thumbnails = []
166 videoModel.VideoFiles = []
167 videoModel.VideoStreamingPlaylists = []
169 videoModel.Trackers = []
171 this.buildAccount(row, videoModel)
173 this.videosMemo[row.id] = videoModel
176 this.videos.push(videoModel)
179 private buildAccount (row: SQLRow, videoModel: VideoModel) {
180 const id = row['VideoChannel.Account.id']
183 const channelModel = new VideoChannelModel(this.grab(row, this.tables.getChannelAttributes(), 'VideoChannel'), this.buildOpts)
184 channelModel.Actor = this.buildActor(row, 'VideoChannel')
186 const accountModel = new AccountModel(this.grab(row, this.tables.getAccountAttributes(), 'VideoChannel.Account'), this.buildOpts)
187 accountModel.Actor = this.buildActor(row, 'VideoChannel.Account')
189 accountModel.BlockedBy = []
191 channelModel.Account = accountModel
193 videoModel.VideoChannel = channelModel
196 private buildActor (row: SQLRow, prefix: string) {
197 const actorPrefix = `${prefix}.Actor`
198 const avatarPrefix = `${actorPrefix}.Avatar`
199 const serverPrefix = `${actorPrefix}.Server`
201 const avatarModel = row[`${avatarPrefix}.id`] !== null
202 ? new ActorImageModel(this.grab(row, this.tables.getAvatarAttributes(), avatarPrefix), this.buildOpts)
205 const serverModel = row[`${serverPrefix}.id`] !== null
206 ? new ServerModel(this.grab(row, this.tables.getServerAttributes(), serverPrefix), this.buildOpts)
209 if (serverModel) serverModel.BlockedBy = []
211 const actorModel = new ActorModel(this.grab(row, this.tables.getActorAttributes(), actorPrefix), this.buildOpts)
212 actorModel.Avatar = avatarModel
213 actorModel.Server = serverModel
218 private setUserHistory (row: SQLRow, videoModel: VideoModel) {
219 const id = row['userVideoHistory.id']
220 if (!id || this.historyDone.has(id)) return
222 const attributes = this.grab(row, this.tables.getUserHistoryAttributes(), 'userVideoHistory')
223 const historyModel = new UserVideoHistoryModel(attributes, this.buildOpts)
224 videoModel.UserVideoHistories.push(historyModel)
226 this.historyDone.add(id)
229 private addThumbnail (row: SQLRow, videoModel: VideoModel) {
230 const id = row['Thumbnails.id']
231 if (!id || this.thumbnailsDone.has(id)) return
233 const attributes = this.grab(row, this.tables.getThumbnailAttributes(), 'Thumbnails')
234 const thumbnailModel = new ThumbnailModel(attributes, this.buildOpts)
235 videoModel.Thumbnails.push(thumbnailModel)
237 this.thumbnailsDone.add(id)
240 private addWebTorrentFile (row: SQLRow, videoModel: VideoModel) {
241 const id = row['VideoFiles.id']
242 if (!id || this.videoFileMemo[id]) return
244 const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoFiles')
245 const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
246 videoModel.VideoFiles.push(videoFileModel)
248 this.videoFileMemo[id] = videoFileModel
251 private addStreamingPlaylist (row: SQLRow, videoModel: VideoModel) {
252 const id = row['VideoStreamingPlaylists.id']
253 if (!id || this.videoStreamingPlaylistMemo[id]) return
255 const attributes = this.grab(row, this.tables.getStreamingPlaylistAttributes(), 'VideoStreamingPlaylists')
256 const streamingPlaylist = new VideoStreamingPlaylistModel(attributes, this.buildOpts)
257 streamingPlaylist.VideoFiles = []
259 videoModel.VideoStreamingPlaylists.push(streamingPlaylist)
261 this.videoStreamingPlaylistMemo[id] = streamingPlaylist
264 private addStreamingPlaylistFile (row: SQLRow) {
265 const id = row['VideoStreamingPlaylists.VideoFiles.id']
266 if (!id || this.videoFileMemo[id]) return
268 const streamingPlaylist = this.videoStreamingPlaylistMemo[row['VideoStreamingPlaylists.id']]
270 const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoStreamingPlaylists.VideoFiles')
271 const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
272 streamingPlaylist.VideoFiles.push(videoFileModel)
274 this.videoFileMemo[id] = videoFileModel
277 private addRedundancy (row: SQLRow, prefix: string, to: VideoFileModel | VideoStreamingPlaylistModel) {
278 if (!to.RedundancyVideos) to.RedundancyVideos = []
280 const redundancyPrefix = `${prefix}.RedundancyVideos`
281 const id = row[`${redundancyPrefix}.id`]
283 if (!id || this.redundancyDone.has(id)) return
285 const attributes = this.grab(row, this.tables.getRedundancyAttributes(), redundancyPrefix)
286 const redundancyModel = new VideoRedundancyModel(attributes, this.buildOpts)
287 to.RedundancyVideos.push(redundancyModel)
289 this.redundancyDone.add(id)
292 private addTag (row: SQLRow, videoModel: VideoModel) {
293 if (!row['Tags.name']) return
295 const key = `${row['Tags.VideoTagModel.videoId']}-${row['Tags.VideoTagModel.tagId']}`
296 if (this.tagsDone.has(key)) return
298 const attributes = this.grab(row, this.tables.getTagAttributes(), 'Tags')
299 const tagModel = new TagModel(attributes, this.buildOpts)
300 videoModel.Tags.push(tagModel)
302 this.tagsDone.add(key)
305 private addTracker (row: SQLRow, videoModel: VideoModel) {
306 if (!row['Trackers.id']) return
308 const key = `${row['Trackers.VideoTrackerModel.videoId']}-${row['Trackers.VideoTrackerModel.trackerId']}`
309 if (this.trackersDone.has(key)) return
311 const attributes = this.grab(row, this.tables.getTrackerAttributes(), 'Trackers')
312 const trackerModel = new TrackerModel(attributes, this.buildOpts)
313 videoModel.Trackers.push(trackerModel)
315 this.trackersDone.add(key)
318 private setBlacklisted (row: SQLRow, videoModel: VideoModel) {
319 const id = row['VideoBlacklist.id']
320 if (!id || this.blacklistDone.has(id)) return
322 const attributes = this.grab(row, this.tables.getBlacklistedAttributes(), 'VideoBlacklist')
323 videoModel.VideoBlacklist = new VideoBlacklistModel(attributes, this.buildOpts)
325 this.blacklistDone.add(id)
328 private setBlockedOwner (row: SQLRow, videoModel: VideoModel) {
329 const id = row['VideoChannel.Account.AccountBlocklist.id']
332 const key = `${videoModel.id}-${id}`
333 if (this.accountBlocklistDone.has(key)) return
335 const attributes = this.grab(row, this.tables.getBlocklistAttributes(), 'VideoChannel.Account.AccountBlocklist')
336 videoModel.VideoChannel.Account.BlockedBy.push(new AccountBlocklistModel(attributes, this.buildOpts))
338 this.accountBlocklistDone.add(key)
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
345 const key = `${videoModel.id}-${id}`
346 if (this.serverBlocklistDone.has(key)) return
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))
351 this.serverBlocklistDone.add(key)
354 private setScheduleVideoUpdate (row: SQLRow, videoModel: VideoModel) {
355 const id = row['ScheduleVideoUpdate.id']
356 if (!id || this.scheduleVideoUpdateDone.has(id)) return
358 const attributes = this.grab(row, this.tables.getScheduleUpdateAttributes(), 'ScheduleVideoUpdate')
359 videoModel.ScheduleVideoUpdate = new ScheduleVideoUpdateModel(attributes, this.buildOpts)
361 this.scheduleVideoUpdateDone.add(id)
364 private setLive (row: SQLRow, videoModel: VideoModel) {
365 const id = row['VideoLive.id']
366 if (!id || this.liveDone.has(id)) return
368 const attributes = this.grab(row, this.tables.getLiveAttributes(), 'VideoLive')
369 videoModel.VideoLive = new VideoLiveModel(attributes, this.buildOpts)
371 this.liveDone.add(id)
374 private grab (row: SQLRow, attributes: string[], prefix: string) {
375 const result: { [ id: string ]: string | number } = {}
377 for (const a of attributes) {