]>
Commit | Line | Data |
---|---|---|
1 | import { intoArray } from '@app/helpers' | |
2 | import { | |
3 | BooleanBothQuery, | |
4 | BooleanQuery, | |
5 | SearchTargetType, | |
6 | VideoChannelsSearchQuery, | |
7 | VideoPlaylistsSearchQuery, | |
8 | VideosSearchQuery | |
9 | } from '@shared/models' | |
10 | ||
11 | export class AdvancedSearch { | |
12 | startDate: string // ISO 8601 | |
13 | endDate: string // ISO 8601 | |
14 | ||
15 | originallyPublishedStartDate: string // ISO 8601 | |
16 | originallyPublishedEndDate: string // ISO 8601 | |
17 | ||
18 | nsfw: BooleanBothQuery | |
19 | ||
20 | categoryOneOf: string | |
21 | ||
22 | licenceOneOf: string | |
23 | ||
24 | languageOneOf: string | |
25 | ||
26 | tagsOneOf: string[] | |
27 | tagsAllOf: string[] | |
28 | ||
29 | durationMin: number // seconds | |
30 | durationMax: number // seconds | |
31 | ||
32 | isLive: BooleanQuery | |
33 | ||
34 | host: string | |
35 | ||
36 | sort: string | |
37 | ||
38 | searchTarget: SearchTargetType | |
39 | ||
40 | // Filters we don't want to count, because they are mandatory | |
41 | private silentFilters = new Set([ 'sort', 'searchTarget' ]) | |
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 | }) { | |
65 | if (!options) return | |
66 | ||
67 | this.startDate = options.startDate || undefined | |
68 | this.endDate = options.endDate || undefined | |
69 | this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined | |
70 | this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined | |
71 | ||
72 | this.nsfw = options.nsfw || undefined | |
73 | this.isLive = options.isLive || undefined | |
74 | ||
75 | this.categoryOneOf = options.categoryOneOf || undefined | |
76 | this.licenceOneOf = options.licenceOneOf || undefined | |
77 | this.languageOneOf = options.languageOneOf || undefined | |
78 | this.tagsOneOf = intoArray(options.tagsOneOf) | |
79 | this.tagsAllOf = intoArray(options.tagsAllOf) | |
80 | this.durationMin = parseInt(options.durationMin, 10) | |
81 | this.durationMax = parseInt(options.durationMax, 10) | |
82 | ||
83 | this.host = options.host || undefined | |
84 | ||
85 | this.searchTarget = options.searchTarget || undefined | |
86 | ||
87 | if (isNaN(this.durationMin)) this.durationMin = undefined | |
88 | if (isNaN(this.durationMax)) this.durationMax = undefined | |
89 | ||
90 | this.sort = options.sort || '-match' | |
91 | } | |
92 | ||
93 | containsValues () { | |
94 | const obj = this.toUrlObject() | |
95 | for (const k of Object.keys(obj)) { | |
96 | if (this.silentFilters.has(k)) continue | |
97 | ||
98 | if (this.isValidValue(obj[k])) return true | |
99 | } | |
100 | ||
101 | return false | |
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 | } | |
142 | } | |
143 | ||
144 | toVideosAPIObject (): VideosSearchQuery { | |
145 | let isLive: boolean | |
146 | if (this.isLive) isLive = this.isLive === 'true' | |
147 | ||
148 | return { | |
149 | startDate: this.startDate, | |
150 | endDate: this.endDate, | |
151 | originallyPublishedStartDate: this.originallyPublishedStartDate, | |
152 | originallyPublishedEndDate: this.originallyPublishedEndDate, | |
153 | nsfw: this.nsfw, | |
154 | categoryOneOf: intoArray(this.categoryOneOf), | |
155 | licenceOneOf: intoArray(this.licenceOneOf), | |
156 | languageOneOf: intoArray(this.languageOneOf), | |
157 | tagsOneOf: this.tagsOneOf, | |
158 | tagsAllOf: this.tagsAllOf, | |
159 | durationMin: this.durationMin, | |
160 | durationMax: this.durationMax, | |
161 | host: this.host, | |
162 | isLive, | |
163 | sort: this.sort, | |
164 | searchTarget: this.searchTarget | |
165 | } | |
166 | } | |
167 | ||
168 | toPlaylistAPIObject (): VideoPlaylistsSearchQuery { | |
169 | return { | |
170 | host: this.host, | |
171 | searchTarget: this.searchTarget | |
172 | } | |
173 | } | |
174 | ||
175 | toChannelAPIObject (): VideoChannelsSearchQuery { | |
176 | return { | |
177 | host: this.host, | |
178 | searchTarget: this.searchTarget | |
179 | } | |
180 | } | |
181 | ||
182 | size () { | |
183 | let acc = 0 | |
184 | ||
185 | const obj = this.toUrlObject() | |
186 | for (const k of Object.keys(obj)) { | |
187 | if (this.silentFilters.has(k)) continue | |
188 | ||
189 | if (this.isValidValue(obj[k])) acc++ | |
190 | } | |
191 | ||
192 | return acc | |
193 | } | |
194 | ||
195 | private isValidValue (val: any) { | |
196 | if (val === undefined) return false | |
197 | if (val === '') return false | |
198 | if (Array.isArray(val) && val.length === 0) return false | |
199 | ||
200 | return true | |
201 | } | |
202 | } |