1 import { intoArray } from '@app/helpers'
6 VideoChannelsSearchQuery,
7 VideoPlaylistsSearchQuery,
9 } from '@shared/models'
11 export class AdvancedSearch {
12 startDate: string // ISO 8601
13 endDate: string // ISO 8601
15 originallyPublishedStartDate: string // ISO 8601
16 originallyPublishedEndDate: string // ISO 8601
18 nsfw: BooleanBothQuery
29 durationMin: number // seconds
30 durationMax: number // seconds
38 searchTarget: SearchTargetType
40 // Filters we don't want to count, because they are mandatory
41 private silentFilters = new Set([ 'sort', 'searchTarget' ])
43 constructor (options?: {
46 originallyPublishedStartDate?: string
47 originallyPublishedEndDate?: string
48 nsfw?: BooleanBothQuery
49 categoryOneOf?: string
51 languageOneOf?: string
63 searchTarget?: SearchTargetType
67 this.startDate = options.startDate || undefined
68 this.endDate = options.endDate || undefined
69 this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
70 this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
72 this.nsfw = options.nsfw || undefined
73 this.isLive = options.isLive || undefined
75 this.categoryOneOf = options.categoryOneOf || undefined
76 this.licenceOneOf = options.licenceOneOf || undefined
77 this.languageOneOf = options.languageOneOf || undefined
78 this.tagsOneOf = intoArray(options.tagsOneOf)
79 this.tagsAllOf = intoArray(options.tagsAllOf)
80 this.durationMin = parseInt(options.durationMin, 10)
81 this.durationMax = parseInt(options.durationMax, 10)
83 this.host = options.host || undefined
85 this.searchTarget = options.searchTarget || undefined
87 if (isNaN(this.durationMin)) this.durationMin = undefined
88 if (isNaN(this.durationMax)) this.durationMax = undefined
90 this.sort = options.sort || '-match'
94 const obj = this.toUrlObject()
95 for (const k of Object.keys(obj)) {
96 if (this.silentFilters.has(k)) continue
98 if (this.isValidValue(obj[k])) return true
105 this.startDate = undefined
106 this.endDate = undefined
107 this.originallyPublishedStartDate = undefined
108 this.originallyPublishedEndDate = undefined
109 this.nsfw = undefined
110 this.categoryOneOf = undefined
111 this.licenceOneOf = undefined
112 this.languageOneOf = undefined
113 this.tagsOneOf = undefined
114 this.tagsAllOf = undefined
115 this.durationMin = undefined
116 this.durationMax = undefined
117 this.isLive = undefined
118 this.host = undefined
125 startDate: this.startDate,
126 endDate: this.endDate,
127 originallyPublishedStartDate: this.originallyPublishedStartDate,
128 originallyPublishedEndDate: this.originallyPublishedEndDate,
130 categoryOneOf: this.categoryOneOf,
131 licenceOneOf: this.licenceOneOf,
132 languageOneOf: this.languageOneOf,
133 tagsOneOf: this.tagsOneOf,
134 tagsAllOf: this.tagsAllOf,
135 durationMin: this.durationMin,
136 durationMax: this.durationMax,
140 searchTarget: this.searchTarget
144 toVideosAPIObject (): VideosSearchQuery {
146 if (this.isLive) isLive = this.isLive === 'true'
149 startDate: this.startDate,
150 endDate: this.endDate,
151 originallyPublishedStartDate: this.originallyPublishedStartDate,
152 originallyPublishedEndDate: this.originallyPublishedEndDate,
154 categoryOneOf: intoArray(this.categoryOneOf),
155 licenceOneOf: intoArray(this.licenceOneOf),
156 languageOneOf: intoArray(this.languageOneOf),
157 tagsOneOf: this.tagsOneOf,
158 tagsAllOf: this.tagsAllOf,
159 durationMin: this.durationMin,
160 durationMax: this.durationMax,
164 searchTarget: this.searchTarget
168 toPlaylistAPIObject (): VideoPlaylistsSearchQuery {
171 searchTarget: this.searchTarget
175 toChannelAPIObject (): VideoChannelsSearchQuery {
178 searchTarget: this.searchTarget
185 const obj = this.toUrlObject()
186 for (const k of Object.keys(obj)) {
187 if (this.silentFilters.has(k)) continue
189 if (this.isValidValue(obj[k])) acc++
195 private isValidValue (val: any) {
196 if (val === undefined) return false
197 if (val === '') return false
198 if (Array.isArray(val) && val.length === 0) return false