aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/sql/video-model-builder.ts
blob: c428312fe14769a81c7cc6a41f74eb2a9e6d559f (plain) (blame)
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
import { pick } from 'lodash'
import { AccountModel } from '@server/models/account/account'
import { ActorModel } from '@server/models/actor/actor'
import { ActorImageModel } from '@server/models/actor/actor-image'
import { ServerModel } from '@server/models/server/server'
import { UserVideoHistoryModel } from '@server/models/user/user-video-history'
import { ThumbnailModel } from '../thumbnail'
import { VideoModel } from '../video'
import { VideoChannelModel } from '../video-channel'
import { VideoFileModel } from '../video-file'
import { VideoStreamingPlaylistModel } from '../video-streaming-playlist'

function buildVideosFromRows (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',
    'filename',
    'fileUrl',
    'torrentFilename',
    'torrentUrl',
    'infoHash',
    'fps',
    'videoId',
    'videoStreamingPlaylistId'
  ]
  const videoStreamingPlaylistKeys = [ 'id', 'type', 'playlistUrl' ]
  const videoKeys = [
    'id',
    'uuid',
    'name',
    'category',
    'licence',
    'language',
    'privacy',
    'nsfw',
    'description',
    'support',
    'duration',
    'views',
    'likes',
    'dislikes',
    'remote',
    'isLive',
    'url',
    'commentsEnabled',
    'downloadEnabled',
    'waitTranscoding',
    'state',
    'publishedAt',
    'originallyPublishedAt',
    'channelId',
    'createdAt',
    'updatedAt'
  ]
  const buildOpts = { raw: true }

  function buildActor (rowActor: any) {
    const avatarModel = rowActor.Avatar.id !== null
      ? new ActorImageModel(pick(rowActor.Avatar, avatarKeys), buildOpts)
      : null

    const serverModel = rowActor.Server.id !== null
      ? new ServerModel(pick(rowActor.Server, serverKeys), buildOpts)
      : null

    const actorModel = new ActorModel(pick(rowActor, actorKeys), buildOpts)
    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' ]), buildOpts)
      channelModel.Actor = buildActor(channel.Actor)

      const account = row.VideoChannel.Account
      const accountModel = new AccountModel(pick(account, [ 'id', 'name' ]), buildOpts)
      accountModel.Actor = buildActor(account.Actor)

      channelModel.Account = accountModel

      const videoModel = new VideoModel(pick(row, videoKeys), buildOpts)
      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' ]), buildOpts)
      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' ]), buildOpts)
      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), buildOpts)
      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), buildOpts)
      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), buildOpts)
      streamingPlaylist.VideoFiles.push(videoFileModel)

      videoFilesDone.add(row.VideoStreamingPlaylists.VideoFiles.id)
    }
  }

  return videos
}

export {
  buildVideosFromRows
}