5 VideoChannelsSearchQuery,
6 VideoPlaylistsSearchQuery,
8 } from '@shared/models'
10 export class AdvancedSearch {
11 startDate: string // ISO 8601
12 endDate: string // ISO 8601
14 originallyPublishedStartDate: string // ISO 8601
15 originallyPublishedEndDate: string // ISO 8601
17 nsfw: BooleanBothQuery
28 durationMin: number // seconds
29 durationMax: number // seconds
37 searchTarget: SearchTargetType
39 // Filters we don't want to count, because they are mandatory
40 private silentFilters = new Set([ 'sort', 'searchTarget' ])
42 constructor (options?: {
45 originallyPublishedStartDate?: string
46 originallyPublishedEndDate?: string
47 nsfw?: BooleanBothQuery
48 categoryOneOf?: string
50 languageOneOf?: string
62 searchTarget?: SearchTargetType
66 this.startDate = options.startDate || undefined
67 this.endDate = options.endDate || undefined
68 this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
69 this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
71 this.nsfw = options.nsfw || undefined
72 this.isLive = options.isLive || undefined
74 this.categoryOneOf = options.categoryOneOf || undefined
75 this.licenceOneOf = options.licenceOneOf || undefined
76 this.languageOneOf = options.languageOneOf || undefined
77 this.tagsOneOf = this.intoArray(options.tagsOneOf)
78 this.tagsAllOf = this.intoArray(options.tagsAllOf)
79 this.durationMin = parseInt(options.durationMin, 10)
80 this.durationMax = parseInt(options.durationMax, 10)
82 this.host = options.host || undefined
84 this.searchTarget = options.searchTarget || undefined
86 if (isNaN(this.durationMin)) this.durationMin = undefined
87 if (isNaN(this.durationMax)) this.durationMax = undefined
89 this.sort = options.sort || '-match'
93 const obj = this.toUrlObject()
94 for (const k of Object.keys(obj)) {
95 if (this.silentFilters.has(k)) continue
97 if (this.isValidValue(obj[k])) return true
104 this.startDate = undefined
105 this.endDate = undefined
106 this.originallyPublishedStartDate = undefined
107 this.originallyPublishedEndDate = undefined
108 this.nsfw = undefined
109 this.categoryOneOf = undefined
110 this.licenceOneOf = undefined
111 this.languageOneOf = undefined
112 this.tagsOneOf = undefined
113 this.tagsAllOf = undefined
114 this.durationMin = undefined
115 this.durationMax = undefined
116 this.isLive = undefined
117 this.host = undefined
124 startDate: this.startDate,
125 endDate: this.endDate,
126 originallyPublishedStartDate: this.originallyPublishedStartDate,
127 originallyPublishedEndDate: this.originallyPublishedEndDate,
129 categoryOneOf: this.categoryOneOf,
130 licenceOneOf: this.licenceOneOf,
131 languageOneOf: this.languageOneOf,
132 tagsOneOf: this.tagsOneOf,
133 tagsAllOf: this.tagsAllOf,
134 durationMin: this.durationMin,
135 durationMax: this.durationMax,
139 searchTarget: this.searchTarget
143 toVideosAPIObject (): VideosSearchQuery {
145 if (this.isLive) isLive = this.isLive === 'true'
148 startDate: this.startDate,
149 endDate: this.endDate,
150 originallyPublishedStartDate: this.originallyPublishedStartDate,
151 originallyPublishedEndDate: this.originallyPublishedEndDate,
153 categoryOneOf: this.intoArray(this.categoryOneOf),
154 licenceOneOf: this.intoArray(this.licenceOneOf),
155 languageOneOf: this.intoArray(this.languageOneOf),
156 tagsOneOf: this.tagsOneOf,
157 tagsAllOf: this.tagsAllOf,
158 durationMin: this.durationMin,
159 durationMax: this.durationMax,
163 searchTarget: this.searchTarget
167 toPlaylistAPIObject (): VideoPlaylistsSearchQuery {
170 searchTarget: this.searchTarget
174 toChannelAPIObject (): VideoChannelsSearchQuery {
177 searchTarget: this.searchTarget
184 const obj = this.toUrlObject()
185 for (const k of Object.keys(obj)) {
186 if (this.silentFilters.has(k)) continue
188 if (this.isValidValue(obj[k])) acc++
194 private isValidValue (val: any) {
195 if (val === undefined) return false
196 if (val === '') return false
197 if (Array.isArray(val) && val.length === 0) return false
202 private intoArray (value: any) {
203 if (!value) return undefined
204 if (Array.isArray(value)) return value
206 if (typeof value === 'string') return value.split(',')