]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-search/advanced-search.model.ts
Fix search filters count
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-search / advanced-search.model.ts
CommitLineData
dd24f1bb 1import { intoArray } from '@app/helpers'
af7fd04a
C
2import {
3 BooleanBothQuery,
4 BooleanQuery,
5 SearchTargetType,
6 VideoChannelsSearchQuery,
7 VideoPlaylistsSearchQuery,
8 VideosSearchQuery
9} from '@shared/models'
0b18f4aa 10
8cf43a65
C
11export type AdvancedSearchResultType = 'videos' | 'playlists' | 'channels'
12
0b18f4aa
C
13export class AdvancedSearch {
14 startDate: string // ISO 8601
15 endDate: string // ISO 8601
16
31d065cc
AM
17 originallyPublishedStartDate: string // ISO 8601
18 originallyPublishedEndDate: string // ISO 8601
19
1fd61899 20 nsfw: BooleanBothQuery
0b18f4aa
C
21
22 categoryOneOf: string
23
24 licenceOneOf: string
25
26 languageOneOf: string
27
9abd170d
C
28 tagsOneOf: string[]
29 tagsAllOf: string[]
0b18f4aa
C
30
31 durationMin: number // seconds
32 durationMax: number // seconds
33
7a22a0a5
C
34 isLive: BooleanQuery
35
af7fd04a
C
36 host: string
37
cddf4503
C
38 sort: string
39
5fb2e288 40 searchTarget: SearchTargetType
8cf43a65 41 resultType: AdvancedSearchResultType
5fb2e288 42
0b18f4aa
C
43 constructor (options?: {
44 startDate?: string
45 endDate?: string
31d065cc
AM
46 originallyPublishedStartDate?: string
47 originallyPublishedEndDate?: string
1fd61899 48 nsfw?: BooleanBothQuery
0b18f4aa
C
49 categoryOneOf?: string
50 licenceOneOf?: string
51 languageOneOf?: string
9abd170d
C
52
53 tagsOneOf?: any
54 tagsAllOf?: any
55
7a22a0a5
C
56 isLive?: BooleanQuery
57
af7fd04a
C
58 host?: string
59
0b18f4aa
C
60 durationMin?: string
61 durationMax?: string
cddf4503 62 sort?: string
5fb2e288 63 searchTarget?: SearchTargetType
8cf43a65 64 resultType?: AdvancedSearchResultType
0b18f4aa
C
65 }) {
66 if (!options) return
67
7afea880
C
68 this.startDate = options.startDate || undefined
69 this.endDate = options.endDate || undefined
31d065cc
AM
70 this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
71 this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
72
7afea880 73 this.nsfw = options.nsfw || undefined
7a22a0a5
C
74 this.isLive = options.isLive || undefined
75
7afea880
C
76 this.categoryOneOf = options.categoryOneOf || undefined
77 this.licenceOneOf = options.licenceOneOf || undefined
78 this.languageOneOf = options.languageOneOf || undefined
dd24f1bb
C
79 this.tagsOneOf = intoArray(options.tagsOneOf)
80 this.tagsAllOf = intoArray(options.tagsAllOf)
35762251
C
81 this.durationMin = options.durationMin ? parseInt(options.durationMin, 10) : undefined
82 this.durationMax = options.durationMax ? parseInt(options.durationMax, 10) : undefined
0b18f4aa 83
af7fd04a
C
84 this.host = options.host || undefined
85
5fb2e288
C
86 this.searchTarget = options.searchTarget || undefined
87
8cf43a65
C
88 this.resultType = options.resultType || undefined
89
90 if (!this.resultType && this.hasVideoFilter()) {
91 this.resultType = 'videos'
92 }
93
0b18f4aa
C
94 if (isNaN(this.durationMin)) this.durationMin = undefined
95 if (isNaN(this.durationMax)) this.durationMax = undefined
cddf4503
C
96
97 this.sort = options.sort || '-match'
0b18f4aa
C
98 }
99
100 containsValues () {
55269c04 101 return this.size() !== 0
0b18f4aa
C
102 }
103
104 reset () {
105 this.startDate = undefined
106 this.endDate = undefined
31d065cc
AM
107 this.originallyPublishedStartDate = undefined
108 this.originallyPublishedEndDate = undefined
0b18f4aa
C
109 this.nsfw = undefined
110 this.categoryOneOf = undefined
111 this.licenceOneOf = undefined
112 this.languageOneOf = undefined
113 this.tagsOneOf = undefined
114 this.tagsAllOf = undefined
115 this.durationMin = undefined
116 this.durationMax = undefined
7a22a0a5 117 this.isLive = undefined
af7fd04a 118 this.host = undefined
cddf4503
C
119
120 this.sort = '-match'
0b18f4aa
C
121 }
122
123 toUrlObject () {
124 return {
125 startDate: this.startDate,
126 endDate: this.endDate,
31d065cc
AM
127 originallyPublishedStartDate: this.originallyPublishedStartDate,
128 originallyPublishedEndDate: this.originallyPublishedEndDate,
0b18f4aa
C
129 nsfw: this.nsfw,
130 categoryOneOf: this.categoryOneOf,
131 licenceOneOf: this.licenceOneOf,
132 languageOneOf: this.languageOneOf,
133 tagsOneOf: this.tagsOneOf,
134 tagsAllOf: this.tagsAllOf,
135 durationMin: this.durationMin,
cddf4503 136 durationMax: this.durationMax,
7a22a0a5 137 isLive: this.isLive,
af7fd04a 138 host: this.host,
5fb2e288 139 sort: this.sort,
8cf43a65
C
140 searchTarget: this.searchTarget,
141 resultType: this.resultType
0b18f4aa
C
142 }
143 }
144
af7fd04a 145 toVideosAPIObject (): VideosSearchQuery {
7a22a0a5
C
146 let isLive: boolean
147 if (this.isLive) isLive = this.isLive === 'true'
148
0b18f4aa
C
149 return {
150 startDate: this.startDate,
151 endDate: this.endDate,
31d065cc
AM
152 originallyPublishedStartDate: this.originallyPublishedStartDate,
153 originallyPublishedEndDate: this.originallyPublishedEndDate,
0b18f4aa 154 nsfw: this.nsfw,
dd24f1bb
C
155 categoryOneOf: intoArray(this.categoryOneOf),
156 licenceOneOf: intoArray(this.licenceOneOf),
157 languageOneOf: intoArray(this.languageOneOf),
9abd170d
C
158 tagsOneOf: this.tagsOneOf,
159 tagsAllOf: this.tagsAllOf,
0b18f4aa 160 durationMin: this.durationMin,
cddf4503 161 durationMax: this.durationMax,
af7fd04a 162 host: this.host,
7a22a0a5 163 isLive,
5fb2e288
C
164 sort: this.sort,
165 searchTarget: this.searchTarget
0b18f4aa
C
166 }
167 }
4278710d 168
af7fd04a
C
169 toPlaylistAPIObject (): VideoPlaylistsSearchQuery {
170 return {
171 host: this.host,
172 searchTarget: this.searchTarget
173 }
174 }
175
176 toChannelAPIObject (): VideoChannelsSearchQuery {
177 return {
178 host: this.host,
179 searchTarget: this.searchTarget
180 }
181 }
182
c5d04b4f
RK
183 size () {
184 let acc = 0
185
55269c04
C
186 if (this.isValidValue(this.startDate) || this.isValidValue(this.endDate)) acc++
187 if (this.isValidValue(this.originallyPublishedStartDate) || this.isValidValue(this.originallyPublishedEndDate)) acc++
188
189 if (this.isValidValue(this.nsfw)) acc++
190 if (this.isValidValue(this.categoryOneOf)) acc++
191 if (this.isValidValue(this.licenceOneOf)) acc++
192 if (this.isValidValue(this.languageOneOf)) acc++
193 if (this.isValidValue(this.tagsOneOf)) acc++
194 if (this.isValidValue(this.tagsAllOf)) acc++
195 if (this.isValidValue(this.durationMin) || this.isValidValue(this.durationMax)) acc++
196 if (this.isValidValue(this.isLive)) acc++
197 if (this.isValidValue(this.host)) acc++
198 if (this.isValidValue(this.resultType)) acc++
c5d04b4f
RK
199
200 return acc
201 }
202
9abd170d
C
203 private isValidValue (val: any) {
204 if (val === undefined) return false
205 if (val === '') return false
206 if (Array.isArray(val) && val.length === 0) return false
207
208 return true
209 }
8cf43a65
C
210
211 private hasVideoFilter () {
212 return this.startDate !== undefined ||
213 this.endDate !== undefined ||
214 this.originallyPublishedStartDate !== undefined ||
215 this.originallyPublishedEndDate !== undefined ||
35762251 216 this.nsfw !== undefined ||
8cf43a65
C
217 this.categoryOneOf !== undefined ||
218 this.licenceOneOf !== undefined ||
219 this.languageOneOf !== undefined ||
220 this.tagsOneOf !== undefined ||
221 this.tagsAllOf !== undefined ||
222 this.durationMin !== undefined ||
223 this.durationMax !== undefined ||
224 this.host !== undefined ||
225 this.isLive !== undefined
226 }
0b18f4aa 227}