1
0
Fork 0
peertube/server/models/video/sql/shared/video-model-builder.ts

338 lines
11 KiB
TypeScript
Raw Normal View History

2021-06-11 04:59:27 -04:00
2021-06-10 08:43:55 -04:00
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'
2021-06-11 04:59:27 -04:00
import { VideoTables } from './video-tables'
type SQLRow = { [id: string]: string | number }
2021-06-10 08:43:55 -04:00
2021-06-10 10:57:13 -04:00
/**
*
* Build video models from SQL rows
*
*/
2021-06-10 08:43:55 -04:00
export class VideoModelBuilder {
private videosMemo: { [ id: number ]: VideoModel }
private videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel }
private videoFileMemo: { [ id: number ]: VideoFileModel }
2021-06-11 04:59:27 -04:00
private thumbnailsDone: Set<any>
private historyDone: Set<any>
private blacklistDone: Set<any>
private liveDone: Set<any>
private redundancyDone: Set<any>
private scheduleVideoUpdateDone: Set<any>
2021-06-10 08:43:55 -04:00
private trackersDone: Set<string>
private tagsDone: Set<string>
private videos: VideoModel[]
private readonly buildOpts = { raw: true, isNewRecord: false }
constructor (
readonly mode: 'get' | 'list',
2021-06-11 04:59:27 -04:00
readonly tables: VideoTables
2021-06-10 08:43:55 -04:00
) {
}
2021-06-11 04:59:27 -04:00
buildVideosFromRows (rows: SQLRow[], rowsWebTorrentFiles?: SQLRow[], rowsStreamingPlaylist?: SQLRow[]) {
2021-06-10 08:43:55 -04:00
this.reinit()
for (const row of rows) {
2021-06-11 08:09:33 -04:00
this.buildVideoAndAccount(row)
2021-06-10 08:43:55 -04:00
const videoModel = this.videosMemo[row.id]
this.setUserHistory(row, videoModel)
this.addThumbnail(row, videoModel)
2021-06-11 04:59:27 -04:00
if (!rowsWebTorrentFiles) {
2021-06-10 10:57:13 -04:00
this.addWebTorrentFile(row, videoModel)
}
if (!rowsStreamingPlaylist) {
this.addStreamingPlaylist(row, videoModel)
this.addStreamingPlaylistFile(row)
}
2021-06-10 08:43:55 -04:00
if (this.mode === 'get') {
this.addTag(row, videoModel)
this.addTracker(row, videoModel)
this.setBlacklisted(row, videoModel)
this.setScheduleVideoUpdate(row, videoModel)
this.setLive(row, videoModel)
}
}
2021-06-11 04:59:27 -04:00
this.grabSeparateWebTorrentFiles(rowsWebTorrentFiles)
this.grabSeparateStreamingPlaylistFiles(rowsStreamingPlaylist)
2021-06-10 10:57:13 -04:00
2021-06-10 08:43:55 -04:00
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 = []
}
2021-06-11 04:59:27 -04:00
private grabSeparateWebTorrentFiles (rowsWebTorrentFiles?: SQLRow[]) {
if (!rowsWebTorrentFiles) return
for (const row of rowsWebTorrentFiles) {
2021-06-11 10:02:26 -04:00
const id = row['VideoFiles.id']
if (!id) continue
2021-06-11 04:59:27 -04:00
const videoModel = this.videosMemo[row.id]
this.addWebTorrentFile(row, videoModel)
2021-06-11 10:02:26 -04:00
this.addRedundancy(row, 'VideoFiles.RedundancyVideos', this.videoFileMemo[id])
2021-06-11 04:59:27 -04:00
}
}
private grabSeparateStreamingPlaylistFiles (rowsStreamingPlaylist?: SQLRow[]) {
if (!rowsStreamingPlaylist) return
for (const row of rowsStreamingPlaylist || []) {
2021-06-11 10:02:26 -04:00
const id = row['VideoStreamingPlaylists.id']
if (!id) continue
2021-06-11 04:59:27 -04:00
const videoModel = this.videosMemo[row.id]
this.addStreamingPlaylist(row, videoModel)
this.addStreamingPlaylistFile(row)
this.addRedundancy(
row,
'VideoStreamingPlaylists.RedundancyVideos',
2021-06-11 10:02:26 -04:00
this.videoStreamingPlaylistMemo[id]
2021-06-11 04:59:27 -04:00
)
}
}
2021-06-11 08:09:33 -04:00
private buildVideoAndAccount (row: SQLRow) {
2021-06-10 08:43:55 -04:00
if (this.videosMemo[row.id]) return
2021-06-11 04:59:27 -04:00
const videoModel = new VideoModel(this.grab(row, this.tables.getVideoAttributes(), ''), this.buildOpts)
2021-06-10 08:43:55 -04:00
videoModel.UserVideoHistories = []
videoModel.Thumbnails = []
videoModel.VideoFiles = []
videoModel.VideoStreamingPlaylists = []
videoModel.Tags = []
videoModel.Trackers = []
2021-06-11 08:09:33 -04:00
this.buildAccount(row, videoModel)
this.videosMemo[row.id] = videoModel
2021-06-10 08:43:55 -04:00
// Keep rows order
this.videos.push(videoModel)
}
2021-06-11 08:09:33 -04:00
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
}
2021-06-11 04:59:27 -04:00
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)
2021-06-10 08:43:55 -04:00
: null
2021-06-11 04:59:27 -04:00
const serverModel = row[`${serverPrefix}.id`] !== null
? new ServerModel(this.grab(row, this.tables.getServerAttributes(), serverPrefix), this.buildOpts)
2021-06-10 08:43:55 -04:00
: null
2021-06-11 04:59:27 -04:00
const actorModel = new ActorModel(this.grab(row, this.tables.getActorAttributes(), actorPrefix), this.buildOpts)
2021-06-10 08:43:55 -04:00
actorModel.Avatar = avatarModel
actorModel.Server = serverModel
return actorModel
}
2021-06-11 04:59:27 -04:00
private setUserHistory (row: SQLRow, videoModel: VideoModel) {
const id = row['userVideoHistory.id']
if (!id || this.historyDone.has(id)) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getUserHistoryAttributes(), 'userVideoHistory')
2021-06-10 08:43:55 -04:00
const historyModel = new UserVideoHistoryModel(attributes, this.buildOpts)
videoModel.UserVideoHistories.push(historyModel)
2021-06-11 04:59:27 -04:00
this.historyDone.add(id)
2021-06-10 08:43:55 -04:00
}
2021-06-11 04:59:27 -04:00
private addThumbnail (row: SQLRow, videoModel: VideoModel) {
const id = row['Thumbnails.id']
if (!id || this.thumbnailsDone.has(id)) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getThumbnailAttributes(), 'Thumbnails')
2021-06-10 08:43:55 -04:00
const thumbnailModel = new ThumbnailModel(attributes, this.buildOpts)
videoModel.Thumbnails.push(thumbnailModel)
2021-06-11 04:59:27 -04:00
this.thumbnailsDone.add(id)
2021-06-10 08:43:55 -04:00
}
2021-06-11 04:59:27 -04:00
private addWebTorrentFile (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoFiles.id']
if (!id || this.videoFileMemo[id]) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoFiles')
2021-06-10 08:43:55 -04:00
const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
videoModel.VideoFiles.push(videoFileModel)
2021-06-11 04:59:27 -04:00
this.videoFileMemo[id] = videoFileModel
2021-06-10 08:43:55 -04:00
}
2021-06-11 04:59:27 -04:00
private addStreamingPlaylist (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoStreamingPlaylists.id']
if (!id || this.videoStreamingPlaylistMemo[id]) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getStreamingPlaylistAttributes(), 'VideoStreamingPlaylists')
2021-06-10 08:43:55 -04:00
const streamingPlaylist = new VideoStreamingPlaylistModel(attributes, this.buildOpts)
streamingPlaylist.VideoFiles = []
videoModel.VideoStreamingPlaylists.push(streamingPlaylist)
2021-06-11 04:59:27 -04:00
this.videoStreamingPlaylistMemo[id] = streamingPlaylist
2021-06-10 08:43:55 -04:00
}
2021-06-11 04:59:27 -04:00
private addStreamingPlaylistFile (row: SQLRow) {
const id = row['VideoStreamingPlaylists.VideoFiles.id']
if (!id || this.videoFileMemo[id]) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const streamingPlaylist = this.videoStreamingPlaylistMemo[row['VideoStreamingPlaylists.id']]
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoStreamingPlaylists.VideoFiles')
2021-06-10 08:43:55 -04:00
const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
streamingPlaylist.VideoFiles.push(videoFileModel)
2021-06-11 04:59:27 -04:00
this.videoFileMemo[id] = videoFileModel
2021-06-10 08:43:55 -04:00
}
2021-06-11 04:59:27 -04:00
private addRedundancy (row: SQLRow, prefix: string, to: VideoFileModel | VideoStreamingPlaylistModel) {
2021-06-10 08:43:55 -04:00
if (!to.RedundancyVideos) to.RedundancyVideos = []
2021-06-11 04:59:27 -04:00
const redundancyPrefix = `${prefix}.RedundancyVideos`
const id = row[`${redundancyPrefix}.id`]
if (!id || this.redundancyDone.has(id)) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getRedundancyAttributes(), redundancyPrefix)
2021-06-10 08:43:55 -04:00
const redundancyModel = new VideoRedundancyModel(attributes, this.buildOpts)
to.RedundancyVideos.push(redundancyModel)
2021-06-11 04:59:27 -04:00
this.redundancyDone.add(id)
2021-06-10 08:43:55 -04:00
}
2021-06-11 04:59:27 -04:00
private addTag (row: SQLRow, videoModel: VideoModel) {
if (!row['Tags.name']) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const key = `${row['Tags.VideoTagModel.videoId']}-${row['Tags.VideoTagModel.tagId']}`
2021-06-10 08:43:55 -04:00
if (this.tagsDone.has(key)) return
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getTagAttributes(), 'Tags')
2021-06-10 08:43:55 -04:00
const tagModel = new TagModel(attributes, this.buildOpts)
videoModel.Tags.push(tagModel)
this.tagsDone.add(key)
}
2021-06-11 04:59:27 -04:00
private addTracker (row: SQLRow, videoModel: VideoModel) {
if (!row['Trackers.id']) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const key = `${row['Trackers.VideoTrackerModel.videoId']}-${row['Trackers.VideoTrackerModel.trackerId']}`
2021-06-10 08:43:55 -04:00
if (this.trackersDone.has(key)) return
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getTrackerAttributes(), 'Trackers')
2021-06-10 08:43:55 -04:00
const trackerModel = new TrackerModel(attributes, this.buildOpts)
videoModel.Trackers.push(trackerModel)
this.trackersDone.add(key)
}
2021-06-11 04:59:27 -04:00
private setBlacklisted (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoBlacklist.id']
if (!id || this.blacklistDone.has(id)) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getBlacklistedAttributes(), 'VideoBlacklist')
2021-06-10 08:43:55 -04:00
videoModel.VideoBlacklist = new VideoBlacklistModel(attributes, this.buildOpts)
2021-06-11 04:59:27 -04:00
this.blacklistDone.add(id)
2021-06-10 08:43:55 -04:00
}
2021-06-11 04:59:27 -04:00
private setScheduleVideoUpdate (row: SQLRow, videoModel: VideoModel) {
const id = row['ScheduleVideoUpdate.id']
if (!id || this.scheduleVideoUpdateDone.has(id)) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getScheduleUpdateAttributes(), 'ScheduleVideoUpdate')
2021-06-10 08:43:55 -04:00
videoModel.ScheduleVideoUpdate = new ScheduleVideoUpdateModel(attributes, this.buildOpts)
2021-06-11 04:59:27 -04:00
this.scheduleVideoUpdateDone.add(id)
2021-06-10 08:43:55 -04:00
}
2021-06-11 04:59:27 -04:00
private setLive (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoLive.id']
if (!id || this.liveDone.has(id)) return
2021-06-10 08:43:55 -04:00
2021-06-11 04:59:27 -04:00
const attributes = this.grab(row, this.tables.getLiveAttributes(), 'VideoLive')
2021-06-10 08:43:55 -04:00
videoModel.VideoLive = new VideoLiveModel(attributes, this.buildOpts)
2021-06-11 04:59:27 -04:00
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
2021-06-10 08:43:55 -04:00
}
}