]>
Commit | Line | Data |
---|---|---|
690bb8f9 | 1 | import { splitIntoArray } from '@app/helpers' |
af7fd04a C |
2 | import { |
3 | BooleanBothQuery, | |
4 | BooleanQuery, | |
5 | SearchTargetType, | |
6 | VideoChannelsSearchQuery, | |
7 | VideoPlaylistsSearchQuery, | |
8 | VideosSearchQuery | |
9 | } from '@shared/models' | |
0b18f4aa | 10 | |
8cf43a65 C |
11 | export type AdvancedSearchResultType = 'videos' | 'playlists' | 'channels' |
12 | ||
0b18f4aa C |
13 | export class AdvancedSearch { |
14 | startDate: string // ISO 8601 | |
15 | endDate: string // ISO 8601 | |
16 | ||
31d065cc AM |
17 | originallyPublishedStartDate: string // ISO 8601 |
18 | originallyPublishedEndDate: string // ISO 8601 | |
19 | ||
1fd61899 | 20 | nsfw: BooleanBothQuery |
0b18f4aa C |
21 | |
22 | categoryOneOf: string | |
23 | ||
24 | licenceOneOf: string | |
25 | ||
26 | languageOneOf: string | |
27 | ||
9abd170d C |
28 | tagsOneOf: string[] |
29 | tagsAllOf: string[] | |
0b18f4aa C |
30 | |
31 | durationMin: number // seconds | |
32 | durationMax: number // seconds | |
33 | ||
7a22a0a5 C |
34 | isLive: BooleanQuery |
35 | ||
af7fd04a C |
36 | host: string |
37 | ||
cddf4503 C |
38 | sort: string |
39 | ||
5fb2e288 | 40 | searchTarget: SearchTargetType |
8cf43a65 | 41 | resultType: AdvancedSearchResultType |
5fb2e288 | 42 | |
2a4c0d8b W |
43 | excludeAlreadyWatched?: boolean |
44 | ||
0b18f4aa C |
45 | constructor (options?: { |
46 | startDate?: string | |
47 | endDate?: string | |
31d065cc AM |
48 | originallyPublishedStartDate?: string |
49 | originallyPublishedEndDate?: string | |
1fd61899 | 50 | nsfw?: BooleanBothQuery |
0b18f4aa C |
51 | categoryOneOf?: string |
52 | licenceOneOf?: string | |
53 | languageOneOf?: string | |
9abd170d C |
54 | |
55 | tagsOneOf?: any | |
56 | tagsAllOf?: any | |
57 | ||
7a22a0a5 C |
58 | isLive?: BooleanQuery |
59 | ||
af7fd04a C |
60 | host?: string |
61 | ||
0b18f4aa C |
62 | durationMin?: string |
63 | durationMax?: string | |
cddf4503 | 64 | sort?: string |
5fb2e288 | 65 | searchTarget?: SearchTargetType |
8cf43a65 | 66 | resultType?: AdvancedSearchResultType |
2a4c0d8b W |
67 | |
68 | excludeAlreadyWatched?: boolean | |
0b18f4aa C |
69 | }) { |
70 | if (!options) return | |
71 | ||
7afea880 C |
72 | this.startDate = options.startDate || undefined |
73 | this.endDate = options.endDate || undefined | |
31d065cc AM |
74 | this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined |
75 | this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined | |
76 | ||
7afea880 | 77 | this.nsfw = options.nsfw || undefined |
7a22a0a5 C |
78 | this.isLive = options.isLive || undefined |
79 | ||
7afea880 C |
80 | this.categoryOneOf = options.categoryOneOf || undefined |
81 | this.licenceOneOf = options.licenceOneOf || undefined | |
82 | this.languageOneOf = options.languageOneOf || undefined | |
690bb8f9 C |
83 | this.tagsOneOf = splitIntoArray(options.tagsOneOf) |
84 | this.tagsAllOf = splitIntoArray(options.tagsAllOf) | |
35762251 C |
85 | this.durationMin = options.durationMin ? parseInt(options.durationMin, 10) : undefined |
86 | this.durationMax = options.durationMax ? parseInt(options.durationMax, 10) : undefined | |
0b18f4aa | 87 | |
af7fd04a C |
88 | this.host = options.host || undefined |
89 | ||
5fb2e288 C |
90 | this.searchTarget = options.searchTarget || undefined |
91 | ||
8cf43a65 C |
92 | this.resultType = options.resultType || undefined |
93 | ||
2a4c0d8b W |
94 | this.excludeAlreadyWatched = options.excludeAlreadyWatched || undefined |
95 | ||
8cf43a65 C |
96 | if (!this.resultType && this.hasVideoFilter()) { |
97 | this.resultType = 'videos' | |
98 | } | |
99 | ||
0b18f4aa C |
100 | if (isNaN(this.durationMin)) this.durationMin = undefined |
101 | if (isNaN(this.durationMax)) this.durationMax = undefined | |
cddf4503 C |
102 | |
103 | this.sort = options.sort || '-match' | |
0b18f4aa C |
104 | } |
105 | ||
106 | containsValues () { | |
55269c04 | 107 | return this.size() !== 0 |
0b18f4aa C |
108 | } |
109 | ||
110 | reset () { | |
111 | this.startDate = undefined | |
112 | this.endDate = undefined | |
31d065cc AM |
113 | this.originallyPublishedStartDate = undefined |
114 | this.originallyPublishedEndDate = undefined | |
0b18f4aa C |
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 | |
7a22a0a5 | 123 | this.isLive = undefined |
af7fd04a | 124 | this.host = undefined |
cddf4503 C |
125 | |
126 | this.sort = '-match' | |
0b18f4aa C |
127 | } |
128 | ||
129 | toUrlObject () { | |
130 | return { | |
131 | startDate: this.startDate, | |
132 | endDate: this.endDate, | |
31d065cc AM |
133 | originallyPublishedStartDate: this.originallyPublishedStartDate, |
134 | originallyPublishedEndDate: this.originallyPublishedEndDate, | |
0b18f4aa C |
135 | nsfw: this.nsfw, |
136 | categoryOneOf: this.categoryOneOf, | |
137 | licenceOneOf: this.licenceOneOf, | |
138 | languageOneOf: this.languageOneOf, | |
139 | tagsOneOf: this.tagsOneOf, | |
140 | tagsAllOf: this.tagsAllOf, | |
141 | durationMin: this.durationMin, | |
cddf4503 | 142 | durationMax: this.durationMax, |
7a22a0a5 | 143 | isLive: this.isLive, |
af7fd04a | 144 | host: this.host, |
5fb2e288 | 145 | sort: this.sort, |
8cf43a65 | 146 | searchTarget: this.searchTarget, |
2a4c0d8b W |
147 | resultType: this.resultType, |
148 | excludeAlreadyWatched: this.excludeAlreadyWatched | |
0b18f4aa C |
149 | } |
150 | } | |
151 | ||
af7fd04a | 152 | toVideosAPIObject (): VideosSearchQuery { |
7a22a0a5 C |
153 | let isLive: boolean |
154 | if (this.isLive) isLive = this.isLive === 'true' | |
155 | ||
0b18f4aa C |
156 | return { |
157 | startDate: this.startDate, | |
158 | endDate: this.endDate, | |
31d065cc AM |
159 | originallyPublishedStartDate: this.originallyPublishedStartDate, |
160 | originallyPublishedEndDate: this.originallyPublishedEndDate, | |
0b18f4aa | 161 | nsfw: this.nsfw, |
690bb8f9 C |
162 | categoryOneOf: splitIntoArray(this.categoryOneOf), |
163 | licenceOneOf: splitIntoArray(this.licenceOneOf), | |
164 | languageOneOf: splitIntoArray(this.languageOneOf), | |
9abd170d C |
165 | tagsOneOf: this.tagsOneOf, |
166 | tagsAllOf: this.tagsAllOf, | |
0b18f4aa | 167 | durationMin: this.durationMin, |
cddf4503 | 168 | durationMax: this.durationMax, |
af7fd04a | 169 | host: this.host, |
7a22a0a5 | 170 | isLive, |
5fb2e288 | 171 | sort: this.sort, |
2a4c0d8b W |
172 | searchTarget: this.searchTarget, |
173 | excludeAlreadyWatched: this.excludeAlreadyWatched | |
0b18f4aa C |
174 | } |
175 | } | |
4278710d | 176 | |
af7fd04a C |
177 | toPlaylistAPIObject (): VideoPlaylistsSearchQuery { |
178 | return { | |
179 | host: this.host, | |
180 | searchTarget: this.searchTarget | |
181 | } | |
182 | } | |
183 | ||
184 | toChannelAPIObject (): VideoChannelsSearchQuery { | |
185 | return { | |
186 | host: this.host, | |
187 | searchTarget: this.searchTarget | |
188 | } | |
189 | } | |
190 | ||
c5d04b4f RK |
191 | size () { |
192 | let acc = 0 | |
193 | ||
55269c04 C |
194 | if (this.isValidValue(this.startDate) || this.isValidValue(this.endDate)) acc++ |
195 | if (this.isValidValue(this.originallyPublishedStartDate) || this.isValidValue(this.originallyPublishedEndDate)) acc++ | |
196 | ||
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++ | |
c5d04b4f RK |
207 | |
208 | return acc | |
209 | } | |
210 | ||
9abd170d C |
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 | |
215 | ||
216 | return true | |
217 | } | |
8cf43a65 C |
218 | |
219 | private hasVideoFilter () { | |
220 | return this.startDate !== undefined || | |
221 | this.endDate !== undefined || | |
222 | this.originallyPublishedStartDate !== undefined || | |
223 | this.originallyPublishedEndDate !== undefined || | |
35762251 | 224 | this.nsfw !== undefined || |
8cf43a65 C |
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 || | |
8cf43a65 C |
232 | this.isLive !== undefined |
233 | } | |
0b18f4aa | 234 | } |