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