]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/sql/shared/video-file-query-builder.ts
Use raw SQL for most of video queries
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / shared / video-file-query-builder.ts
CommitLineData
1d43c3a6
C
1import { Sequelize } from 'sequelize'
2import { BuildVideoGetQueryOptions } from '../video-model-get-query-builder'
3import { AbstractVideosModelQueryBuilder } from './abstract-videos-model-query-builder'
4
5/**
6 *
7 * Fetch files (webtorrent and streaming playlist) according to a video
8 *
9 */
10
11export class VideoFileQueryBuilder extends AbstractVideosModelQueryBuilder {
12 protected attributes: { [key: string]: string }
1d43c3a6
C
13
14 constructor (protected readonly sequelize: Sequelize) {
15 super('get')
16 }
17
18 queryWebTorrentVideos (options: BuildVideoGetQueryOptions) {
19 this.buildWebtorrentFilesQuery(options)
20
71d4af1e 21 return this.runQuery(options)
1d43c3a6
C
22 }
23
24 queryStreamingPlaylistVideos (options: BuildVideoGetQueryOptions) {
25 this.buildVideoStreamingPlaylistFilesQuery(options)
26
71d4af1e 27 return this.runQuery(options)
1d43c3a6
C
28 }
29
30 private buildWebtorrentFilesQuery (options: BuildVideoGetQueryOptions) {
31 this.attributes = {
32 '"video"."id"': ''
33 }
34
35 this.includeWebtorrentFiles(true)
36
71d4af1e 37 if (this.shouldIncludeRedundancies(options)) {
1d43c3a6
C
38 this.includeWebTorrentRedundancies()
39 }
40
71d4af1e 41 this.whereId(options)
1d43c3a6
C
42
43 this.query = this.buildQuery()
44 }
45
46 private buildVideoStreamingPlaylistFilesQuery (options: BuildVideoGetQueryOptions) {
47 this.attributes = {
48 '"video"."id"': ''
49 }
50
51 this.includeStreamingPlaylistFiles(true)
52
71d4af1e 53 if (this.shouldIncludeRedundancies(options)) {
1d43c3a6
C
54 this.includeStreamingPlaylistRedundancies()
55 }
56
71d4af1e 57 this.whereId(options)
1d43c3a6
C
58
59 this.query = this.buildQuery()
60 }
61
62 private buildQuery () {
3c79c2ce 63 return `${this.buildSelect()} FROM "video" ${this.joins} ${this.where}`
1d43c3a6 64 }
71d4af1e
C
65
66 private shouldIncludeRedundancies (options: BuildVideoGetQueryOptions) {
67 return options.type === 'api'
68 }
1d43c3a6 69}