]>
Commit | Line | Data |
---|---|---|
af7fd04a C |
1 | import { |
2 | BooleanBothQuery, | |
3 | BooleanQuery, | |
4 | SearchTargetType, | |
5 | VideoChannelsSearchQuery, | |
6 | VideoPlaylistsSearchQuery, | |
7 | VideosSearchQuery | |
8 | } from '@shared/models' | |
0b18f4aa C |
9 | |
10 | export class AdvancedSearch { | |
11 | startDate: string // ISO 8601 | |
12 | endDate: string // ISO 8601 | |
13 | ||
31d065cc AM |
14 | originallyPublishedStartDate: string // ISO 8601 |
15 | originallyPublishedEndDate: string // ISO 8601 | |
16 | ||
1fd61899 | 17 | nsfw: BooleanBothQuery |
0b18f4aa C |
18 | |
19 | categoryOneOf: string | |
20 | ||
21 | licenceOneOf: string | |
22 | ||
23 | languageOneOf: string | |
24 | ||
9abd170d C |
25 | tagsOneOf: string[] |
26 | tagsAllOf: string[] | |
0b18f4aa C |
27 | |
28 | durationMin: number // seconds | |
29 | durationMax: number // seconds | |
30 | ||
7a22a0a5 C |
31 | isLive: BooleanQuery |
32 | ||
af7fd04a C |
33 | host: string |
34 | ||
cddf4503 C |
35 | sort: string |
36 | ||
5fb2e288 C |
37 | searchTarget: SearchTargetType |
38 | ||
39 | // Filters we don't want to count, because they are mandatory | |
40 | private silentFilters = new Set([ 'sort', 'searchTarget' ]) | |
41 | ||
0b18f4aa C |
42 | constructor (options?: { |
43 | startDate?: string | |
44 | endDate?: string | |
31d065cc AM |
45 | originallyPublishedStartDate?: string |
46 | originallyPublishedEndDate?: string | |
1fd61899 | 47 | nsfw?: BooleanBothQuery |
0b18f4aa C |
48 | categoryOneOf?: string |
49 | licenceOneOf?: string | |
50 | languageOneOf?: string | |
9abd170d C |
51 | |
52 | tagsOneOf?: any | |
53 | tagsAllOf?: any | |
54 | ||
7a22a0a5 C |
55 | isLive?: BooleanQuery |
56 | ||
af7fd04a C |
57 | host?: string |
58 | ||
0b18f4aa C |
59 | durationMin?: string |
60 | durationMax?: string | |
cddf4503 | 61 | sort?: string |
5fb2e288 | 62 | searchTarget?: SearchTargetType |
0b18f4aa C |
63 | }) { |
64 | if (!options) return | |
65 | ||
7afea880 C |
66 | this.startDate = options.startDate || undefined |
67 | this.endDate = options.endDate || undefined | |
31d065cc AM |
68 | this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined |
69 | this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined | |
70 | ||
7afea880 | 71 | this.nsfw = options.nsfw || undefined |
7a22a0a5 C |
72 | this.isLive = options.isLive || undefined |
73 | ||
7afea880 C |
74 | this.categoryOneOf = options.categoryOneOf || undefined |
75 | this.licenceOneOf = options.licenceOneOf || undefined | |
76 | this.languageOneOf = options.languageOneOf || undefined | |
9abd170d C |
77 | this.tagsOneOf = this.intoArray(options.tagsOneOf) |
78 | this.tagsAllOf = this.intoArray(options.tagsAllOf) | |
0b18f4aa C |
79 | this.durationMin = parseInt(options.durationMin, 10) |
80 | this.durationMax = parseInt(options.durationMax, 10) | |
81 | ||
af7fd04a C |
82 | this.host = options.host || undefined |
83 | ||
5fb2e288 C |
84 | this.searchTarget = options.searchTarget || undefined |
85 | ||
0b18f4aa C |
86 | if (isNaN(this.durationMin)) this.durationMin = undefined |
87 | if (isNaN(this.durationMax)) this.durationMax = undefined | |
cddf4503 C |
88 | |
89 | this.sort = options.sort || '-match' | |
0b18f4aa C |
90 | } |
91 | ||
92 | containsValues () { | |
c199c427 | 93 | const obj = this.toUrlObject() |
0b18f4aa | 94 | for (const k of Object.keys(obj)) { |
5fb2e288 | 95 | if (this.silentFilters.has(k)) continue |
cddf4503 | 96 | |
9abd170d | 97 | if (this.isValidValue(obj[k])) return true |
0b18f4aa C |
98 | } |
99 | ||
100 | return false | |
101 | } | |
102 | ||
103 | reset () { | |
104 | this.startDate = undefined | |
105 | this.endDate = undefined | |
31d065cc AM |
106 | this.originallyPublishedStartDate = undefined |
107 | this.originallyPublishedEndDate = undefined | |
0b18f4aa C |
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 | |
7a22a0a5 | 116 | this.isLive = undefined |
af7fd04a | 117 | this.host = undefined |
cddf4503 C |
118 | |
119 | this.sort = '-match' | |
0b18f4aa C |
120 | } |
121 | ||
122 | toUrlObject () { | |
123 | return { | |
124 | startDate: this.startDate, | |
125 | endDate: this.endDate, | |
31d065cc AM |
126 | originallyPublishedStartDate: this.originallyPublishedStartDate, |
127 | originallyPublishedEndDate: this.originallyPublishedEndDate, | |
0b18f4aa C |
128 | nsfw: this.nsfw, |
129 | categoryOneOf: this.categoryOneOf, | |
130 | licenceOneOf: this.licenceOneOf, | |
131 | languageOneOf: this.languageOneOf, | |
132 | tagsOneOf: this.tagsOneOf, | |
133 | tagsAllOf: this.tagsAllOf, | |
134 | durationMin: this.durationMin, | |
cddf4503 | 135 | durationMax: this.durationMax, |
7a22a0a5 | 136 | isLive: this.isLive, |
af7fd04a | 137 | host: this.host, |
5fb2e288 C |
138 | sort: this.sort, |
139 | searchTarget: this.searchTarget | |
0b18f4aa C |
140 | } |
141 | } | |
142 | ||
af7fd04a | 143 | toVideosAPIObject (): VideosSearchQuery { |
7a22a0a5 C |
144 | let isLive: boolean |
145 | if (this.isLive) isLive = this.isLive === 'true' | |
146 | ||
0b18f4aa C |
147 | return { |
148 | startDate: this.startDate, | |
149 | endDate: this.endDate, | |
31d065cc AM |
150 | originallyPublishedStartDate: this.originallyPublishedStartDate, |
151 | originallyPublishedEndDate: this.originallyPublishedEndDate, | |
0b18f4aa | 152 | nsfw: this.nsfw, |
4278710d C |
153 | categoryOneOf: this.intoArray(this.categoryOneOf), |
154 | licenceOneOf: this.intoArray(this.licenceOneOf), | |
155 | languageOneOf: this.intoArray(this.languageOneOf), | |
9abd170d C |
156 | tagsOneOf: this.tagsOneOf, |
157 | tagsAllOf: this.tagsAllOf, | |
0b18f4aa | 158 | durationMin: this.durationMin, |
cddf4503 | 159 | durationMax: this.durationMax, |
af7fd04a | 160 | host: this.host, |
7a22a0a5 | 161 | isLive, |
5fb2e288 C |
162 | sort: this.sort, |
163 | searchTarget: this.searchTarget | |
0b18f4aa C |
164 | } |
165 | } | |
4278710d | 166 | |
af7fd04a C |
167 | toPlaylistAPIObject (): VideoPlaylistsSearchQuery { |
168 | return { | |
169 | host: this.host, | |
170 | searchTarget: this.searchTarget | |
171 | } | |
172 | } | |
173 | ||
174 | toChannelAPIObject (): VideoChannelsSearchQuery { | |
175 | return { | |
176 | host: this.host, | |
177 | searchTarget: this.searchTarget | |
178 | } | |
179 | } | |
180 | ||
c5d04b4f RK |
181 | size () { |
182 | let acc = 0 | |
183 | ||
c199c427 | 184 | const obj = this.toUrlObject() |
c5d04b4f | 185 | for (const k of Object.keys(obj)) { |
5fb2e288 | 186 | if (this.silentFilters.has(k)) continue |
c5d04b4f | 187 | |
9abd170d | 188 | if (this.isValidValue(obj[k])) acc++ |
c5d04b4f RK |
189 | } |
190 | ||
191 | return acc | |
192 | } | |
193 | ||
9abd170d C |
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 | |
198 | ||
199 | return true | |
200 | } | |
201 | ||
4278710d C |
202 | private intoArray (value: any) { |
203 | if (!value) return undefined | |
baeb429d | 204 | if (Array.isArray(value)) return value |
4278710d C |
205 | |
206 | if (typeof value === 'string') return value.split(',') | |
207 | ||
208 | return [ value ] | |
209 | } | |
0b18f4aa | 210 | } |