]>
Commit | Line | Data |
---|---|---|
1 | import { NSFWQuery, SearchTargetType } 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: NSFWQuery | |
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 | sort: string | |
25 | ||
26 | searchTarget: SearchTargetType | |
27 | ||
28 | // Filters we don't want to count, because they are mandatory | |
29 | private silentFilters = new Set([ 'sort', 'searchTarget' ]) | |
30 | ||
31 | constructor (options?: { | |
32 | startDate?: string | |
33 | endDate?: string | |
34 | originallyPublishedStartDate?: string | |
35 | originallyPublishedEndDate?: string | |
36 | nsfw?: NSFWQuery | |
37 | categoryOneOf?: string | |
38 | licenceOneOf?: string | |
39 | languageOneOf?: string | |
40 | ||
41 | tagsOneOf?: any | |
42 | tagsAllOf?: any | |
43 | ||
44 | durationMin?: string | |
45 | durationMax?: string | |
46 | sort?: string | |
47 | searchTarget?: SearchTargetType | |
48 | }) { | |
49 | if (!options) return | |
50 | ||
51 | this.startDate = options.startDate || undefined | |
52 | this.endDate = options.endDate || undefined | |
53 | this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined | |
54 | this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined | |
55 | ||
56 | this.nsfw = options.nsfw || undefined | |
57 | this.categoryOneOf = options.categoryOneOf || undefined | |
58 | this.licenceOneOf = options.licenceOneOf || undefined | |
59 | this.languageOneOf = options.languageOneOf || undefined | |
60 | this.tagsOneOf = this.intoArray(options.tagsOneOf) | |
61 | this.tagsAllOf = this.intoArray(options.tagsAllOf) | |
62 | this.durationMin = parseInt(options.durationMin, 10) | |
63 | this.durationMax = parseInt(options.durationMax, 10) | |
64 | ||
65 | this.searchTarget = options.searchTarget || undefined | |
66 | ||
67 | if (isNaN(this.durationMin)) this.durationMin = undefined | |
68 | if (isNaN(this.durationMax)) this.durationMax = undefined | |
69 | ||
70 | this.sort = options.sort || '-match' | |
71 | } | |
72 | ||
73 | containsValues () { | |
74 | const obj = this.toUrlObject() | |
75 | for (const k of Object.keys(obj)) { | |
76 | if (this.silentFilters.has(k)) continue | |
77 | ||
78 | if (this.isValidValue(obj[k])) return true | |
79 | } | |
80 | ||
81 | return false | |
82 | } | |
83 | ||
84 | reset () { | |
85 | this.startDate = undefined | |
86 | this.endDate = undefined | |
87 | this.originallyPublishedStartDate = undefined | |
88 | this.originallyPublishedEndDate = undefined | |
89 | this.nsfw = undefined | |
90 | this.categoryOneOf = undefined | |
91 | this.licenceOneOf = undefined | |
92 | this.languageOneOf = undefined | |
93 | this.tagsOneOf = undefined | |
94 | this.tagsAllOf = undefined | |
95 | this.durationMin = undefined | |
96 | this.durationMax = undefined | |
97 | ||
98 | this.sort = '-match' | |
99 | } | |
100 | ||
101 | toUrlObject () { | |
102 | return { | |
103 | startDate: this.startDate, | |
104 | endDate: this.endDate, | |
105 | originallyPublishedStartDate: this.originallyPublishedStartDate, | |
106 | originallyPublishedEndDate: this.originallyPublishedEndDate, | |
107 | nsfw: this.nsfw, | |
108 | categoryOneOf: this.categoryOneOf, | |
109 | licenceOneOf: this.licenceOneOf, | |
110 | languageOneOf: this.languageOneOf, | |
111 | tagsOneOf: this.tagsOneOf, | |
112 | tagsAllOf: this.tagsAllOf, | |
113 | durationMin: this.durationMin, | |
114 | durationMax: this.durationMax, | |
115 | sort: this.sort, | |
116 | searchTarget: this.searchTarget | |
117 | } | |
118 | } | |
119 | ||
120 | toAPIObject () { | |
121 | return { | |
122 | startDate: this.startDate, | |
123 | endDate: this.endDate, | |
124 | originallyPublishedStartDate: this.originallyPublishedStartDate, | |
125 | originallyPublishedEndDate: this.originallyPublishedEndDate, | |
126 | nsfw: this.nsfw, | |
127 | categoryOneOf: this.intoArray(this.categoryOneOf), | |
128 | licenceOneOf: this.intoArray(this.licenceOneOf), | |
129 | languageOneOf: this.intoArray(this.languageOneOf), | |
130 | tagsOneOf: this.tagsOneOf, | |
131 | tagsAllOf: this.tagsAllOf, | |
132 | durationMin: this.durationMin, | |
133 | durationMax: this.durationMax, | |
134 | sort: this.sort, | |
135 | searchTarget: this.searchTarget | |
136 | } | |
137 | } | |
138 | ||
139 | size () { | |
140 | let acc = 0 | |
141 | ||
142 | const obj = this.toUrlObject() | |
143 | for (const k of Object.keys(obj)) { | |
144 | if (this.silentFilters.has(k)) continue | |
145 | ||
146 | if (this.isValidValue(obj[k])) acc++ | |
147 | } | |
148 | ||
149 | return acc | |
150 | } | |
151 | ||
152 | private isValidValue (val: any) { | |
153 | if (val === undefined) return false | |
154 | if (val === '') return false | |
155 | if (Array.isArray(val) && val.length === 0) return false | |
156 | ||
157 | return true | |
158 | } | |
159 | ||
160 | private intoArray (value: any) { | |
161 | if (!value) return undefined | |
162 | if (Array.isArray(value)) return value | |
163 | ||
164 | if (typeof value === 'string') return value.split(',') | |
165 | ||
166 | return [ value ] | |
167 | } | |
168 | } |