]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/sql/video-model-get-query-builder.ts
Use raw SQL for video get request
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / video-model-get-query-builder.ts
CommitLineData
d9bf974f
C
1import { Sequelize, Transaction } from 'sequelize'
2import validator from 'validator'
3import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
4
5export type BuildVideoGetQueryOptions = {
6 id: number | string
7 transaction?: Transaction
8 userId?: number
9 forGetAPI?: boolean
10}
11
12export class VideosModelGetQueryBuilder extends AbstractVideosModelQueryBuilder {
13 protected attributes: { [key: string]: string }
14 protected joins: string[] = []
15 protected where: string
16
17 constructor (protected readonly sequelize: Sequelize) {
18 super('get')
19 }
20
21 queryVideos (options: BuildVideoGetQueryOptions) {
22 this.buildGetQuery(options)
23
24 return this.runQuery(options.transaction, true).then(rows => {
25 const videos = this.videoModelBuilder.buildVideosFromRows(rows)
26
27 if (videos.length > 1) {
28 throw new Error('Video results is more than ')
29 }
30
31 if (videos.length === 0) return null
32 return videos[0]
33 })
34 }
35
36 private buildGetQuery (options: BuildVideoGetQueryOptions) {
37 this.attributes = {
38 '"video".*': ''
39 }
40
41 this.includeChannels()
42 this.includeAccounts()
43
44 this.includeTags()
45
46 this.includeThumbnails()
47
48 this.includeFiles()
49
50 this.includeBlacklisted()
51
52 this.includeScheduleUpdate()
53
54 this.includeLive()
55
56 if (options.userId) {
57 this.includeUserHistory(options.userId)
58 }
59
60 if (options.forGetAPI === true) {
61 this.includeTrackers()
62 this.includeRedundancies()
63 }
64
65 this.whereId(options.id)
66
67 const select = this.buildSelect()
68 const order = this.buildOrder()
69
70 this.query = `${select} FROM "video" ${this.joins.join(' ')} ${this.where} ${order}`
71 }
72
73 private whereId (id: string | number) {
74 if (validator.isInt('' + id)) {
75 this.where = 'WHERE "video".id = :videoId'
76 } else {
77 this.where = 'WHERE uuid = :videoId'
78 }
79
80 this.replacements.videoId = id
81 }
82
83 private buildOrder () {
84 return 'ORDER BY "Tags"."name" ASC'
85 }
86}