+ private static buildAPIResult (rows: any[]) {
+ const videosMemo: { [ id: number ]: VideoModel } = {}
+ const videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel } = {}
+
+ const thumbnailsDone = new Set<number>()
+ const historyDone = new Set<number>()
+ const videoFilesDone = new Set<number>()
+
+ const videos: VideoModel[] = []
+
+ const avatarKeys = [ 'id', 'filename', 'fileUrl', 'onDisk', 'createdAt', 'updatedAt' ]
+ const actorKeys = [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ]
+ const serverKeys = [ 'id', 'host' ]
+ const videoFileKeys = [ 'id', 'createdAt', 'updatedAt', 'resolution', 'size', 'extname', 'infoHash', 'fps', 'videoId' ]
+ const videoStreamingPlaylistKeys = [ 'id' ]
+ const videoKeys = [
+ 'id',
+ 'uuid',
+ 'name',
+ 'category',
+ 'licence',
+ 'language',
+ 'privacy',
+ 'nsfw',
+ 'description',
+ 'support',
+ 'duration',
+ 'views',
+ 'likes',
+ 'dislikes',
+ 'remote',
+ 'url',
+ 'commentsEnabled',
+ 'downloadEnabled',
+ 'waitTranscoding',
+ 'state',
+ 'publishedAt',
+ 'originallyPublishedAt',
+ 'channelId',
+ 'createdAt',
+ 'updatedAt'
+ ]
+
+ function buildActor (rowActor: any) {
+ const avatarModel = rowActor.Avatar.id !== null
+ ? new AvatarModel(pick(rowActor.Avatar, avatarKeys))
+ : null
+
+ const serverModel = rowActor.Server.id !== null
+ ? new ServerModel(pick(rowActor.Server, serverKeys))
+ : null
+
+ const actorModel = new ActorModel(pick(rowActor, actorKeys))
+ actorModel.Avatar = avatarModel
+ actorModel.Server = serverModel
+
+ return actorModel
+ }
+
+ for (const row of rows) {
+ if (!videosMemo[row.id]) {
+ // Build Channel
+ const channel = row.VideoChannel
+ const channelModel = new VideoChannelModel(pick(channel, [ 'id', 'name', 'description', 'actorId' ]))
+ channelModel.Actor = buildActor(channel.Actor)
+
+ const account = row.VideoChannel.Account
+ const accountModel = new AccountModel(pick(account, [ 'id', 'name' ]))
+ accountModel.Actor = buildActor(account.Actor)
+
+ channelModel.Account = accountModel
+
+ const videoModel = new VideoModel(pick(row, videoKeys))
+ videoModel.VideoChannel = channelModel
+
+ videoModel.UserVideoHistories = []
+ videoModel.Thumbnails = []
+ videoModel.VideoFiles = []
+ videoModel.VideoStreamingPlaylists = []
+
+ videosMemo[row.id] = videoModel
+ // Don't take object value to have a sorted array
+ videos.push(videoModel)
+ }
+
+ const videoModel = videosMemo[row.id]
+
+ if (row.userVideoHistory?.id && !historyDone.has(row.userVideoHistory.id)) {
+ const historyModel = new UserVideoHistoryModel(pick(row.userVideoHistory, [ 'id', 'currentTime' ]))
+ videoModel.UserVideoHistories.push(historyModel)
+
+ historyDone.add(row.userVideoHistory.id)
+ }
+
+ if (row.Thumbnails?.id && !thumbnailsDone.has(row.Thumbnails.id)) {
+ const thumbnailModel = new ThumbnailModel(pick(row.Thumbnails, [ 'id', 'type', 'filename' ]))
+ videoModel.Thumbnails.push(thumbnailModel)
+
+ thumbnailsDone.add(row.Thumbnails.id)
+ }
+
+ if (row.VideoFiles?.id && !videoFilesDone.has(row.VideoFiles.id)) {
+ const videoFileModel = new VideoFileModel(pick(row.VideoFiles, videoFileKeys))
+ videoModel.VideoFiles.push(videoFileModel)
+
+ videoFilesDone.add(row.VideoFiles.id)
+ }
+
+ if (row.VideoFiles?.id && !videoFilesDone.has(row.VideoFiles.id)) {
+ const videoFileModel = new VideoFileModel(pick(row.VideoFiles, videoFileKeys))
+ videoModel.VideoFiles.push(videoFileModel)
+
+ videoFilesDone.add(row.VideoFiles.id)
+ }
+
+ if (row.VideoStreamingPlaylists?.id && !videoStreamingPlaylistMemo[row.VideoStreamingPlaylists.id]) {
+ const streamingPlaylist = new VideoStreamingPlaylistModel(pick(row.VideoStreamingPlaylists, videoStreamingPlaylistKeys))
+ streamingPlaylist.VideoFiles = []
+
+ videoModel.VideoStreamingPlaylists.push(streamingPlaylist)
+
+ videoStreamingPlaylistMemo[streamingPlaylist.id] = streamingPlaylist
+ }
+
+ if (row.VideoStreamingPlaylists?.VideoFiles?.id && !videoFilesDone.has(row.VideoStreamingPlaylists.VideoFiles.id)) {
+ const streamingPlaylist = videoStreamingPlaylistMemo[row.VideoStreamingPlaylists.id]
+
+ const videoFileModel = new VideoFileModel(pick(row.VideoStreamingPlaylists.VideoFiles, videoFileKeys))
+ streamingPlaylist.VideoFiles.push(videoFileModel)
+
+ videoFilesDone.add(row.VideoStreamingPlaylists.VideoFiles.id)
+ }
+ }
+
+ return videos
+ }
+