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