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