aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/sql/video-model-get-query-builder.ts
blob: d18ddae6781a8c66eeeac0d312b82ffb0383f9f5 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Sequelize, Transaction } from 'sequelize'
import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
import { VideoFileQueryBuilder } from './shared/video-file-query-builder'
import { VideoModelBuilder } from './shared/video-model-builder'
import { VideoTables } from './shared/video-tables'

/**
 *
 * Build a GET SQL query, fetch rows and create the video model
 *
 */

export type GetType =
  'api' |
  'full-light' |
  'account-blacklist-files' |
  'all-files' |
  'thumbnails' |
  'thumbnails-blacklist' |
  'id' |
  'blacklist-rights'

export type BuildVideoGetQueryOptions = {
  id?: number | string
  url?: string

  type: GetType

  userId?: number
  transaction?: Transaction

  logging?: boolean
}

export class VideosModelGetQueryBuilder {
  videoQueryBuilder: VideosModelGetQuerySubBuilder
  webtorrentFilesQueryBuilder: VideoFileQueryBuilder
  streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder

  private readonly videoModelBuilder: VideoModelBuilder

  private static readonly videoFilesInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files', 'all-files' ])

  constructor (protected readonly sequelize: Sequelize) {
    this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
    this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
    this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)

    this.videoModelBuilder = new VideoModelBuilder('get', new VideoTables('get'))
  }

  async queryVideo (options: BuildVideoGetQueryOptions) {
    const [ videoRows, webtorrentFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
      this.videoQueryBuilder.queryVideos(options),

      VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
        ? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(options)
        : Promise.resolve(undefined),

      VideosModelGetQueryBuilder.videoFilesInclude.has(options.type)
        ? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(options)
        : Promise.resolve(undefined)
    ])

    const videos = this.videoModelBuilder.buildVideosFromRows({
      rows: videoRows,
      rowsWebTorrentFiles: webtorrentFilesRows,
      rowsStreamingPlaylist: streamingPlaylistFilesRows
    })

    if (videos.length > 1) {
      throw new Error('Video results is more than ')
    }

    if (videos.length === 0) return null
    return videos[0]
  }
}

export class VideosModelGetQuerySubBuilder extends AbstractVideosModelQueryBuilder {
  protected attributes: { [key: string]: string }

  protected webtorrentFilesQuery: string
  protected streamingPlaylistFilesQuery: string

  private static readonly trackersInclude = new Set<GetType>([ 'api' ])
  private static readonly liveInclude = new Set<GetType>([ 'api', 'full-light' ])
  private static readonly scheduleUpdateInclude = new Set<GetType>([ 'api', 'full-light' ])
  private static readonly tagsInclude = new Set<GetType>([ 'api', 'full-light' ])
  private static readonly userHistoryInclude = new Set<GetType>([ 'api', 'full-light' ])
  private static readonly accountInclude = new Set<GetType>([ 'api', 'full-light', 'account-blacklist-files' ])
  private static readonly ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])

  private static readonly blacklistedInclude = new Set<GetType>([
    'api',
    'full-light',
    'account-blacklist-files',
    'thumbnails-blacklist',
    'blacklist-rights'
  ])

  private static readonly thumbnailsInclude = new Set<GetType>([
    'api',
    'full-light',
    'account-blacklist-files',
    'all-files',
    'thumbnails',
    'thumbnails-blacklist'
  ])

  constructor (protected readonly sequelize: Sequelize) {
    super('get')
  }

  queryVideos (options: BuildVideoGetQueryOptions) {
    this.buildMainGetQuery(options)

    return this.runQuery(options)
  }

  private buildMainGetQuery (options: BuildVideoGetQueryOptions) {
    this.attributes = {
      '"video".*': ''
    }

    if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) {
      this.includeThumbnails()
    }

    if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) {
      this.includeBlacklisted()
    }

    if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) {
      this.includeChannels()
      this.includeAccounts()
    }

    if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) {
      this.includeTags()
    }

    if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) {
      this.includeScheduleUpdate()
    }

    if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) {
      this.includeLive()
    }

    if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) {
      this.includeUserHistory(options.userId)
    }

    if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) {
      this.includeOwnerUser()
    }

    if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) {
      this.includeTrackers()
    }

    this.whereId(options)

    this.query = this.buildQuery(options)
  }

  private buildQuery (options: BuildVideoGetQueryOptions) {
    const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)
      ? 'ORDER BY "Tags"."name" ASC'
      : ''

    const from = `SELECT * FROM "video" ${this.where} LIMIT 1`

    return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`
  }
}