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