1 import { splitIntoArray } from '@app/helpers'
6 VideoChannelsSearchQuery,
7 VideoPlaylistsSearchQuery,
9 } from '@shared/models'
11 export type AdvancedSearchResultType = 'videos' | 'playlists' | 'channels'
13 export class AdvancedSearch {
14 startDate: string // ISO 8601
15 endDate: string // ISO 8601
17 originallyPublishedStartDate: string // ISO 8601
18 originallyPublishedEndDate: string // ISO 8601
20 nsfw: BooleanBothQuery
31 durationMin: number // seconds
32 durationMax: number // seconds
40 searchTarget: SearchTargetType
41 resultType: AdvancedSearchResultType
43 excludeAlreadyWatched?: boolean
45 constructor (options?: {
48 originallyPublishedStartDate?: string
49 originallyPublishedEndDate?: string
50 nsfw?: BooleanBothQuery
51 categoryOneOf?: string
53 languageOneOf?: string
65 searchTarget?: SearchTargetType
66 resultType?: AdvancedSearchResultType
68 excludeAlreadyWatched?: boolean
72 this.startDate = options.startDate || undefined
73 this.endDate = options.endDate || undefined
74 this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
75 this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
77 this.nsfw = options.nsfw || undefined
78 this.isLive = options.isLive || undefined
80 this.categoryOneOf = options.categoryOneOf || undefined
81 this.licenceOneOf = options.licenceOneOf || undefined
82 this.languageOneOf = options.languageOneOf || undefined
83 this.tagsOneOf = splitIntoArray(options.tagsOneOf)
84 this.tagsAllOf = splitIntoArray(options.tagsAllOf)
85 this.durationMin = options.durationMin ? parseInt(options.durationMin, 10) : undefined
86 this.durationMax = options.durationMax ? parseInt(options.durationMax, 10) : undefined
88 this.host = options.host || undefined
90 this.searchTarget = options.searchTarget || undefined
92 this.resultType = options.resultType || undefined
94 this.excludeAlreadyWatched = options.excludeAlreadyWatched || undefined
96 if (!this.resultType && this.hasVideoFilter()) {
97 this.resultType = 'videos'
100 if (isNaN(this.durationMin)) this.durationMin = undefined
101 if (isNaN(this.durationMax)) this.durationMax = undefined
103 this.sort = options.sort || '-match'
107 return this.size() !== 0
111 this.startDate = undefined
112 this.endDate = undefined
113 this.originallyPublishedStartDate = undefined
114 this.originallyPublishedEndDate = undefined
115 this.nsfw = undefined
116 this.categoryOneOf = undefined
117 this.licenceOneOf = undefined
118 this.languageOneOf = undefined
119 this.tagsOneOf = undefined
120 this.tagsAllOf = undefined
121 this.durationMin = undefined
122 this.durationMax = undefined
123 this.isLive = undefined
124 this.host = undefined
131 startDate: this.startDate,
132 endDate: this.endDate,
133 originallyPublishedStartDate: this.originallyPublishedStartDate,
134 originallyPublishedEndDate: this.originallyPublishedEndDate,
136 categoryOneOf: this.categoryOneOf,
137 licenceOneOf: this.licenceOneOf,
138 languageOneOf: this.languageOneOf,
139 tagsOneOf: this.tagsOneOf,
140 tagsAllOf: this.tagsAllOf,
141 durationMin: this.durationMin,
142 durationMax: this.durationMax,
146 searchTarget: this.searchTarget,
147 resultType: this.resultType,
148 excludeAlreadyWatched: this.excludeAlreadyWatched
152 toVideosAPIObject (): VideosSearchQuery {
154 if (this.isLive) isLive = this.isLive === 'true'
157 startDate: this.startDate,
158 endDate: this.endDate,
159 originallyPublishedStartDate: this.originallyPublishedStartDate,
160 originallyPublishedEndDate: this.originallyPublishedEndDate,
162 categoryOneOf: splitIntoArray(this.categoryOneOf),
163 licenceOneOf: splitIntoArray(this.licenceOneOf),
164 languageOneOf: splitIntoArray(this.languageOneOf),
165 tagsOneOf: this.tagsOneOf,
166 tagsAllOf: this.tagsAllOf,
167 durationMin: this.durationMin,
168 durationMax: this.durationMax,
172 searchTarget: this.searchTarget,
173 excludeAlreadyWatched: this.excludeAlreadyWatched
177 toPlaylistAPIObject (): VideoPlaylistsSearchQuery {
180 searchTarget: this.searchTarget
184 toChannelAPIObject (): VideoChannelsSearchQuery {
187 searchTarget: this.searchTarget
194 if (this.isValidValue(this.startDate) || this.isValidValue(this.endDate)) acc++
195 if (this.isValidValue(this.originallyPublishedStartDate) || this.isValidValue(this.originallyPublishedEndDate)) acc++
197 if (this.isValidValue(this.nsfw)) acc++
198 if (this.isValidValue(this.categoryOneOf)) acc++
199 if (this.isValidValue(this.licenceOneOf)) acc++
200 if (this.isValidValue(this.languageOneOf)) acc++
201 if (this.isValidValue(this.tagsOneOf)) acc++
202 if (this.isValidValue(this.tagsAllOf)) acc++
203 if (this.isValidValue(this.durationMin) || this.isValidValue(this.durationMax)) acc++
204 if (this.isValidValue(this.isLive)) acc++
205 if (this.isValidValue(this.host)) acc++
206 if (this.isValidValue(this.resultType)) acc++
211 private isValidValue (val: any) {
212 if (val === undefined) return false
213 if (val === '') return false
214 if (Array.isArray(val) && val.length === 0) return false
219 private hasVideoFilter () {
220 return this.startDate !== undefined ||
221 this.endDate !== undefined ||
222 this.originallyPublishedStartDate !== undefined ||
223 this.originallyPublishedEndDate !== undefined ||
224 this.nsfw !== undefined ||
225 this.categoryOneOf !== undefined ||
226 this.licenceOneOf !== undefined ||
227 this.languageOneOf !== undefined ||
228 this.tagsOneOf !== undefined ||
229 this.tagsAllOf !== undefined ||
230 this.durationMin !== undefined ||
231 this.durationMax !== undefined ||
232 this.isLive !== undefined