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