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