1 import { Sequelize, Transaction } from 'sequelize'
2 import validator from 'validator'
3 import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
5 export type BuildVideoGetQueryOptions = {
7 transaction?: Transaction
12 export class VideosModelGetQueryBuilder extends AbstractVideosModelQueryBuilder {
13 protected attributes: { [key: string]: string }
14 protected joins: string[] = []
15 protected where: string
17 constructor (protected readonly sequelize: Sequelize) {
21 queryVideos (options: BuildVideoGetQueryOptions) {
22 this.buildGetQuery(options)
24 return this.runQuery(options.transaction, true).then(rows => {
25 const videos = this.videoModelBuilder.buildVideosFromRows(rows)
27 if (videos.length > 1) {
28 throw new Error('Video results is more than ')
31 if (videos.length === 0) return null
36 private buildGetQuery (options: BuildVideoGetQueryOptions) {
41 this.includeChannels()
42 this.includeAccounts()
46 this.includeThumbnails()
50 this.includeBlacklisted()
52 this.includeScheduleUpdate()
57 this.includeUserHistory(options.userId)
60 if (options.forGetAPI === true) {
61 this.includeTrackers()
62 this.includeRedundancies()
65 this.whereId(options.id)
67 const select = this.buildSelect()
68 const order = this.buildOrder()
70 this.query = `${select} FROM "video" ${this.joins.join(' ')} ${this.where} ${order}`
73 private whereId (id: string | number) {
74 if (validator.isInt('' + id)) {
75 this.where = 'WHERE "video".id = :videoId'
77 this.where = 'WHERE uuid = :videoId'
80 this.replacements.videoId = id
83 private buildOrder () {
84 return 'ORDER BY "Tags"."name" ASC'