2 import { logger } from '@server/helpers/logger'
3 import { AccountModel } from '@server/models/account/account'
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 { TrackerModel } from '@server/models/server/tracker'
9 import { UserVideoHistoryModel } from '@server/models/user/user-video-history'
10 import { ScheduleVideoUpdateModel } from '../../schedule-video-update'
11 import { TagModel } from '../../tag'
12 import { ThumbnailModel } from '../../thumbnail'
13 import { VideoModel } from '../../video'
14 import { VideoBlacklistModel } from '../../video-blacklist'
15 import { VideoChannelModel } from '../../video-channel'
16 import { VideoFileModel } from '../../video-file'
17 import { VideoLiveModel } from '../../video-live'
18 import { VideoStreamingPlaylistModel } from '../../video-streaming-playlist'
19 import { VideoTables } from './video-tables'
21 type SQLRow = { [id: string]: string | number }
25 * Build video models from SQL rows
29 export class VideoModelBuilder {
30 private videosMemo: { [ id: number ]: VideoModel }
31 private videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel }
32 private videoFileMemo: { [ id: number ]: VideoFileModel }
34 private thumbnailsDone: Set<any>
35 private historyDone: Set<any>
36 private blacklistDone: Set<any>
37 private liveDone: Set<any>
38 private redundancyDone: Set<any>
39 private scheduleVideoUpdateDone: Set<any>
41 private trackersDone: Set<string>
42 private tagsDone: Set<string>
44 private videos: VideoModel[]
46 private readonly buildOpts = { raw: true, isNewRecord: false }
49 readonly mode: 'get' | 'list',
50 readonly tables: VideoTables
55 buildVideosFromRows (rows: SQLRow[], rowsWebTorrentFiles?: SQLRow[], rowsStreamingPlaylist?: SQLRow[]) {
58 for (const row of rows) {
61 const videoModel = this.videosMemo[row.id]
63 this.setUserHistory(row, videoModel)
64 this.addThumbnail(row, videoModel)
66 if (!rowsWebTorrentFiles) {
67 this.addWebTorrentFile(row, videoModel)
70 if (!rowsStreamingPlaylist) {
71 this.addStreamingPlaylist(row, videoModel)
72 this.addStreamingPlaylistFile(row)
75 if (this.mode === 'get') {
76 this.addTag(row, videoModel)
77 this.addTracker(row, videoModel)
78 this.setBlacklisted(row, videoModel)
79 this.setScheduleVideoUpdate(row, videoModel)
80 this.setLive(row, videoModel)
84 this.grabSeparateWebTorrentFiles(rowsWebTorrentFiles)
85 this.grabSeparateStreamingPlaylistFiles(rowsStreamingPlaylist)
92 this.videoStreamingPlaylistMemo = {}
93 this.videoFileMemo = {}
95 this.thumbnailsDone = new Set<number>()
96 this.historyDone = new Set<number>()
97 this.blacklistDone = new Set<number>()
98 this.liveDone = new Set<number>()
99 this.redundancyDone = new Set<number>()
100 this.scheduleVideoUpdateDone = new Set<number>()
102 this.trackersDone = new Set<string>()
103 this.tagsDone = new Set<string>()
108 private grabSeparateWebTorrentFiles (rowsWebTorrentFiles?: SQLRow[]) {
109 if (!rowsWebTorrentFiles) return
111 for (const row of rowsWebTorrentFiles) {
112 const videoModel = this.videosMemo[row.id]
113 this.addWebTorrentFile(row, videoModel)
114 this.addRedundancy(row, 'VideoFiles.RedundancyVideos', this.videoFileMemo[row['VideoFiles.id']])
118 private grabSeparateStreamingPlaylistFiles (rowsStreamingPlaylist?: SQLRow[]) {
119 if (!rowsStreamingPlaylist) return
121 for (const row of rowsStreamingPlaylist || []) {
122 const videoModel = this.videosMemo[row.id]
124 this.addStreamingPlaylist(row, videoModel)
125 this.addStreamingPlaylistFile(row)
128 'VideoStreamingPlaylists.RedundancyVideos',
129 this.videoStreamingPlaylistMemo[row['VideoStreamingPlaylists.id']]
134 private buildVideo (row: SQLRow) {
135 if (this.videosMemo[row.id]) return
138 const channelModel = new VideoChannelModel(this.grab(row, this.tables.getChannelAttributes(), 'VideoChannel'), this.buildOpts)
139 channelModel.Actor = this.buildActor(row, 'VideoChannel')
141 const accountModel = new AccountModel(this.grab(row, this.tables.getAccountAttributes(), 'VideoChannel.Account'), this.buildOpts)
142 accountModel.Actor = this.buildActor(row, 'VideoChannel.Account')
144 channelModel.Account = accountModel
146 const videoModel = new VideoModel(this.grab(row, this.tables.getVideoAttributes(), ''), this.buildOpts)
147 videoModel.VideoChannel = channelModel
149 this.videosMemo[row.id] = videoModel
151 videoModel.UserVideoHistories = []
152 videoModel.Thumbnails = []
153 videoModel.VideoFiles = []
154 videoModel.VideoStreamingPlaylists = []
156 videoModel.Trackers = []
159 this.videos.push(videoModel)
162 private buildActor (row: SQLRow, prefix: string) {
163 const actorPrefix = `${prefix}.Actor`
164 const avatarPrefix = `${actorPrefix}.Avatar`
165 const serverPrefix = `${actorPrefix}.Server`
167 const avatarModel = row[`${avatarPrefix}.id`] !== null
168 ? new ActorImageModel(this.grab(row, this.tables.getAvatarAttributes(), avatarPrefix), this.buildOpts)
171 const serverModel = row[`${serverPrefix}.id`] !== null
172 ? new ServerModel(this.grab(row, this.tables.getServerAttributes(), serverPrefix), this.buildOpts)
175 const actorModel = new ActorModel(this.grab(row, this.tables.getActorAttributes(), actorPrefix), this.buildOpts)
176 actorModel.Avatar = avatarModel
177 actorModel.Server = serverModel
182 private setUserHistory (row: SQLRow, videoModel: VideoModel) {
183 const id = row['userVideoHistory.id']
184 if (!id || this.historyDone.has(id)) return
186 const attributes = this.grab(row, this.tables.getUserHistoryAttributes(), 'userVideoHistory')
187 const historyModel = new UserVideoHistoryModel(attributes, this.buildOpts)
188 videoModel.UserVideoHistories.push(historyModel)
190 this.historyDone.add(id)
193 private addThumbnail (row: SQLRow, videoModel: VideoModel) {
194 const id = row['Thumbnails.id']
195 if (!id || this.thumbnailsDone.has(id)) return
197 const attributes = this.grab(row, this.tables.getThumbnailAttributes(), 'Thumbnails')
198 const thumbnailModel = new ThumbnailModel(attributes, this.buildOpts)
199 videoModel.Thumbnails.push(thumbnailModel)
201 this.thumbnailsDone.add(id)
204 private addWebTorrentFile (row: SQLRow, videoModel: VideoModel) {
205 const id = row['VideoFiles.id']
206 if (!id || this.videoFileMemo[id]) return
208 const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoFiles')
209 const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
210 videoModel.VideoFiles.push(videoFileModel)
212 this.videoFileMemo[id] = videoFileModel
215 private addStreamingPlaylist (row: SQLRow, videoModel: VideoModel) {
216 const id = row['VideoStreamingPlaylists.id']
217 if (!id || this.videoStreamingPlaylistMemo[id]) return
219 const attributes = this.grab(row, this.tables.getStreamingPlaylistAttributes(), 'VideoStreamingPlaylists')
220 const streamingPlaylist = new VideoStreamingPlaylistModel(attributes, this.buildOpts)
221 streamingPlaylist.VideoFiles = []
223 videoModel.VideoStreamingPlaylists.push(streamingPlaylist)
225 this.videoStreamingPlaylistMemo[id] = streamingPlaylist
228 private addStreamingPlaylistFile (row: SQLRow) {
229 const id = row['VideoStreamingPlaylists.VideoFiles.id']
230 if (!id || this.videoFileMemo[id]) return
232 const streamingPlaylist = this.videoStreamingPlaylistMemo[row['VideoStreamingPlaylists.id']]
234 const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoStreamingPlaylists.VideoFiles')
235 const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
236 streamingPlaylist.VideoFiles.push(videoFileModel)
238 this.videoFileMemo[id] = videoFileModel
241 private addRedundancy (row: SQLRow, prefix: string, to: VideoFileModel | VideoStreamingPlaylistModel) {
242 if (!to.RedundancyVideos) to.RedundancyVideos = []
244 const redundancyPrefix = `${prefix}.RedundancyVideos`
245 const id = row[`${redundancyPrefix}.id`]
247 if (!id || this.redundancyDone.has(id)) return
249 const attributes = this.grab(row, this.tables.getRedundancyAttributes(), redundancyPrefix)
250 const redundancyModel = new VideoRedundancyModel(attributes, this.buildOpts)
251 to.RedundancyVideos.push(redundancyModel)
253 this.redundancyDone.add(id)
256 private addTag (row: SQLRow, videoModel: VideoModel) {
257 if (!row['Tags.name']) return
259 const key = `${row['Tags.VideoTagModel.videoId']}-${row['Tags.VideoTagModel.tagId']}`
260 if (this.tagsDone.has(key)) return
262 const attributes = this.grab(row, this.tables.getTagAttributes(), 'Tags')
263 const tagModel = new TagModel(attributes, this.buildOpts)
264 videoModel.Tags.push(tagModel)
266 this.tagsDone.add(key)
269 private addTracker (row: SQLRow, videoModel: VideoModel) {
270 if (!row['Trackers.id']) return
272 const key = `${row['Trackers.VideoTrackerModel.videoId']}-${row['Trackers.VideoTrackerModel.trackerId']}`
273 if (this.trackersDone.has(key)) return
275 const attributes = this.grab(row, this.tables.getTrackerAttributes(), 'Trackers')
276 const trackerModel = new TrackerModel(attributes, this.buildOpts)
277 videoModel.Trackers.push(trackerModel)
279 this.trackersDone.add(key)
282 private setBlacklisted (row: SQLRow, videoModel: VideoModel) {
283 const id = row['VideoBlacklist.id']
284 if (!id || this.blacklistDone.has(id)) return
286 const attributes = this.grab(row, this.tables.getBlacklistedAttributes(), 'VideoBlacklist')
287 videoModel.VideoBlacklist = new VideoBlacklistModel(attributes, this.buildOpts)
289 this.blacklistDone.add(id)
292 private setScheduleVideoUpdate (row: SQLRow, videoModel: VideoModel) {
293 const id = row['ScheduleVideoUpdate.id']
294 if (!id || this.scheduleVideoUpdateDone.has(id)) return
296 const attributes = this.grab(row, this.tables.getScheduleUpdateAttributes(), 'ScheduleVideoUpdate')
297 videoModel.ScheduleVideoUpdate = new ScheduleVideoUpdateModel(attributes, this.buildOpts)
299 this.scheduleVideoUpdateDone.add(id)
302 private setLive (row: SQLRow, videoModel: VideoModel) {
303 const id = row['VideoLive.id']
304 if (!id || this.liveDone.has(id)) return
306 const attributes = this.grab(row, this.tables.getLiveAttributes(), 'VideoLive')
307 videoModel.VideoLive = new VideoLiveModel(attributes, this.buildOpts)
309 this.liveDone.add(id)
312 private grab (row: SQLRow, attributes: string[], prefix: string) {
313 const result: { [ id: string ]: string | number } = {}
315 for (const a of attributes) {