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