]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-search/advanced-search.model.ts
Add additional checks when importing a video
[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
C
42
43 // Filters we don't want to count, because they are mandatory
44 private silentFilters = new Set([ 'sort', 'searchTarget' ])
45
0b18f4aa
C
46 constructor (options?: {
47 startDate?: string
48 endDate?: string
31d065cc
AM
49 originallyPublishedStartDate?: string
50 originallyPublishedEndDate?: string
1fd61899 51 nsfw?: BooleanBothQuery
0b18f4aa
C
52 categoryOneOf?: string
53 licenceOneOf?: string
54 languageOneOf?: string
9abd170d
C
55
56 tagsOneOf?: any
57 tagsAllOf?: any
58
7a22a0a5
C
59 isLive?: BooleanQuery
60
af7fd04a
C
61 host?: string
62
0b18f4aa
C
63 durationMin?: string
64 durationMax?: string
cddf4503 65 sort?: string
5fb2e288 66 searchTarget?: SearchTargetType
8cf43a65 67 resultType?: AdvancedSearchResultType
0b18f4aa
C
68 }) {
69 if (!options) return
70
7afea880
C
71 this.startDate = options.startDate || undefined
72 this.endDate = options.endDate || undefined
31d065cc
AM
73 this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
74 this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
75
7afea880 76 this.nsfw = options.nsfw || undefined
7a22a0a5
C
77 this.isLive = options.isLive || undefined
78
7afea880
C
79 this.categoryOneOf = options.categoryOneOf || undefined
80 this.licenceOneOf = options.licenceOneOf || undefined
81 this.languageOneOf = options.languageOneOf || undefined
dd24f1bb
C
82 this.tagsOneOf = intoArray(options.tagsOneOf)
83 this.tagsAllOf = intoArray(options.tagsAllOf)
35762251
C
84 this.durationMin = options.durationMin ? parseInt(options.durationMin, 10) : undefined
85 this.durationMax = options.durationMax ? parseInt(options.durationMax, 10) : undefined
0b18f4aa 86
af7fd04a
C
87 this.host = options.host || undefined
88
5fb2e288
C
89 this.searchTarget = options.searchTarget || undefined
90
8cf43a65
C
91 this.resultType = options.resultType || undefined
92
93 if (!this.resultType && this.hasVideoFilter()) {
94 this.resultType = 'videos'
95 }
96
0b18f4aa
C
97 if (isNaN(this.durationMin)) this.durationMin = undefined
98 if (isNaN(this.durationMax)) this.durationMax = undefined
cddf4503
C
99
100 this.sort = options.sort || '-match'
0b18f4aa
C
101 }
102
103 containsValues () {
c199c427 104 const obj = this.toUrlObject()
0b18f4aa 105 for (const k of Object.keys(obj)) {
5fb2e288 106 if (this.silentFilters.has(k)) continue
cddf4503 107
9abd170d 108 if (this.isValidValue(obj[k])) return true
0b18f4aa
C
109 }
110
111 return false
112 }
113
114 reset () {
115 this.startDate = undefined
116 this.endDate = undefined
31d065cc
AM
117 this.originallyPublishedStartDate = undefined
118 this.originallyPublishedEndDate = undefined
0b18f4aa
C
119 this.nsfw = undefined
120 this.categoryOneOf = undefined
121 this.licenceOneOf = undefined
122 this.languageOneOf = undefined
123 this.tagsOneOf = undefined
124 this.tagsAllOf = undefined
125 this.durationMin = undefined
126 this.durationMax = undefined
7a22a0a5 127 this.isLive = undefined
af7fd04a 128 this.host = undefined
cddf4503
C
129
130 this.sort = '-match'
0b18f4aa
C
131 }
132
133 toUrlObject () {
134 return {
135 startDate: this.startDate,
136 endDate: this.endDate,
31d065cc
AM
137 originallyPublishedStartDate: this.originallyPublishedStartDate,
138 originallyPublishedEndDate: this.originallyPublishedEndDate,
0b18f4aa
C
139 nsfw: this.nsfw,
140 categoryOneOf: this.categoryOneOf,
141 licenceOneOf: this.licenceOneOf,
142 languageOneOf: this.languageOneOf,
143 tagsOneOf: this.tagsOneOf,
144 tagsAllOf: this.tagsAllOf,
145 durationMin: this.durationMin,
cddf4503 146 durationMax: this.durationMax,
7a22a0a5 147 isLive: this.isLive,
af7fd04a 148 host: this.host,
5fb2e288 149 sort: this.sort,
8cf43a65
C
150 searchTarget: this.searchTarget,
151 resultType: this.resultType
0b18f4aa
C
152 }
153 }
154
af7fd04a 155 toVideosAPIObject (): VideosSearchQuery {
7a22a0a5
C
156 let isLive: boolean
157 if (this.isLive) isLive = this.isLive === 'true'
158
0b18f4aa
C
159 return {
160 startDate: this.startDate,
161 endDate: this.endDate,
31d065cc
AM
162 originallyPublishedStartDate: this.originallyPublishedStartDate,
163 originallyPublishedEndDate: this.originallyPublishedEndDate,
0b18f4aa 164 nsfw: this.nsfw,
dd24f1bb
C
165 categoryOneOf: intoArray(this.categoryOneOf),
166 licenceOneOf: intoArray(this.licenceOneOf),
167 languageOneOf: intoArray(this.languageOneOf),
9abd170d
C
168 tagsOneOf: this.tagsOneOf,
169 tagsAllOf: this.tagsAllOf,
0b18f4aa 170 durationMin: this.durationMin,
cddf4503 171 durationMax: this.durationMax,
af7fd04a 172 host: this.host,
7a22a0a5 173 isLive,
5fb2e288
C
174 sort: this.sort,
175 searchTarget: this.searchTarget
0b18f4aa
C
176 }
177 }
4278710d 178
af7fd04a
C
179 toPlaylistAPIObject (): VideoPlaylistsSearchQuery {
180 return {
181 host: this.host,
182 searchTarget: this.searchTarget
183 }
184 }
185
186 toChannelAPIObject (): VideoChannelsSearchQuery {
187 return {
188 host: this.host,
189 searchTarget: this.searchTarget
190 }
191 }
192
c5d04b4f
RK
193 size () {
194 let acc = 0
195
c199c427 196 const obj = this.toUrlObject()
c5d04b4f 197 for (const k of Object.keys(obj)) {
5fb2e288 198 if (this.silentFilters.has(k)) continue
c5d04b4f 199
9abd170d 200 if (this.isValidValue(obj[k])) acc++
c5d04b4f
RK
201 }
202
203 return acc
204 }
205
9abd170d
C
206 private isValidValue (val: any) {
207 if (val === undefined) return false
208 if (val === '') return false
209 if (Array.isArray(val) && val.length === 0) return false
210
211 return true
212 }
8cf43a65
C
213
214 private hasVideoFilter () {
215 return this.startDate !== undefined ||
216 this.endDate !== undefined ||
217 this.originallyPublishedStartDate !== undefined ||
218 this.originallyPublishedEndDate !== undefined ||
35762251 219 this.nsfw !== undefined ||
8cf43a65
C
220 this.categoryOneOf !== undefined ||
221 this.licenceOneOf !== undefined ||
222 this.languageOneOf !== undefined ||
223 this.tagsOneOf !== undefined ||
224 this.tagsAllOf !== undefined ||
225 this.durationMin !== undefined ||
226 this.durationMax !== undefined ||
227 this.host !== undefined ||
228 this.isLive !== undefined
229 }
0b18f4aa 230}