aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/sql/video/videos-model-list-query-builder.ts
blob: 4fe6bc321bbe0c9851d21e07c659be02676c139a (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
import { pick } from 'lodash'
import { Sequelize } from 'sequelize'
import { VideoInclude } from '@shared/models'
import { AbstractVideoQueryBuilder } from './shared/abstract-video-query-builder'
import { VideoFileQueryBuilder } from './shared/video-file-query-builder'
import { VideoModelBuilder } from './shared/video-model-builder'
import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder'

/**
 *
 * Build videos list SQL query and create video models
 *
 */

export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
  protected attributes: { [key: string]: string }

  private innerQuery: string
  private innerSort: string

  webtorrentFilesQueryBuilder: VideoFileQueryBuilder
  streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder

  private readonly videoModelBuilder: VideoModelBuilder

  constructor (protected readonly sequelize: Sequelize) {
    super(sequelize, 'list')

    this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
    this.webtorrentFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
    this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
  }

  async queryVideos (options: BuildVideosListQueryOptions) {
    this.buildInnerQuery(options)
    this.buildMainQuery(options)

    const rows = await this.runQuery()

    if (options.include & VideoInclude.FILES) {
      const videoIds = Array.from(new Set(rows.map(r => r.id)))

      if (videoIds.length !== 0) {
        const fileQueryOptions = {
          ...pick(options, [ 'transaction', 'logging' ]),

          ids: videoIds,
          includeRedundancy: false
        }

        const [ rowsWebTorrentFiles, rowsStreamingPlaylist ] = await Promise.all([
          this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(fileQueryOptions),
          this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(fileQueryOptions)
        ])

        return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include, rowsStreamingPlaylist, rowsWebTorrentFiles })
      }
    }

    return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include })
  }

  private buildInnerQuery (options: BuildVideosListQueryOptions) {
    const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
    const { query, sort, replacements } = idsQueryBuilder.getQuery(options)

    this.replacements = replacements
    this.innerQuery = query
    this.innerSort = sort
  }

  private buildMainQuery (options: BuildVideosListQueryOptions) {
    this.attributes = {
      '"video".*': ''
    }

    this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"')

    this.includeChannels()
    this.includeAccounts()
    this.includeThumbnails()

    if (options.user) {
      this.includeUserHistory(options.user.id)
    }

    if (options.videoPlaylistId) {
      this.includePlaylist(options.videoPlaylistId)
    }

    if (options.include & VideoInclude.BLACKLISTED) {
      this.includeBlacklisted()
    }

    if (options.include & VideoInclude.BLOCKED_OWNER) {
      this.includeBlockedOwnerAndServer(options.serverAccountIdForBlock, options.user)
    }

    const select = this.buildSelect()

    this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
  }
}