]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-search/advanced-search.model.ts
Add video filters to common video pages
[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
C
10
11export class AdvancedSearch {
12 startDate: string // ISO 8601
13 endDate: string // ISO 8601
14
31d065cc
AM
15 originallyPublishedStartDate: string // ISO 8601
16 originallyPublishedEndDate: string // ISO 8601
17
1fd61899 18 nsfw: BooleanBothQuery
0b18f4aa
C
19
20 categoryOneOf: string
21
22 licenceOneOf: string
23
24 languageOneOf: string
25
9abd170d
C
26 tagsOneOf: string[]
27 tagsAllOf: string[]
0b18f4aa
C
28
29 durationMin: number // seconds
30 durationMax: number // seconds
31
7a22a0a5
C
32 isLive: BooleanQuery
33
af7fd04a
C
34 host: string
35
cddf4503
C
36 sort: string
37
5fb2e288
C
38 searchTarget: SearchTargetType
39
40 // Filters we don't want to count, because they are mandatory
41 private silentFilters = new Set([ 'sort', 'searchTarget' ])
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
0b18f4aa
C
64 }) {
65 if (!options) return
66
7afea880
C
67 this.startDate = options.startDate || undefined
68 this.endDate = options.endDate || undefined
31d065cc
AM
69 this.originallyPublishedStartDate = options.originallyPublishedStartDate || undefined
70 this.originallyPublishedEndDate = options.originallyPublishedEndDate || undefined
71
7afea880 72 this.nsfw = options.nsfw || undefined
7a22a0a5
C
73 this.isLive = options.isLive || undefined
74
7afea880
C
75 this.categoryOneOf = options.categoryOneOf || undefined
76 this.licenceOneOf = options.licenceOneOf || undefined
77 this.languageOneOf = options.languageOneOf || undefined
dd24f1bb
C
78 this.tagsOneOf = intoArray(options.tagsOneOf)
79 this.tagsAllOf = intoArray(options.tagsAllOf)
0b18f4aa
C
80 this.durationMin = parseInt(options.durationMin, 10)
81 this.durationMax = parseInt(options.durationMax, 10)
82
af7fd04a
C
83 this.host = options.host || undefined
84
5fb2e288
C
85 this.searchTarget = options.searchTarget || undefined
86
0b18f4aa
C
87 if (isNaN(this.durationMin)) this.durationMin = undefined
88 if (isNaN(this.durationMax)) this.durationMax = undefined
cddf4503
C
89
90 this.sort = options.sort || '-match'
0b18f4aa
C
91 }
92
93 containsValues () {
c199c427 94 const obj = this.toUrlObject()
0b18f4aa 95 for (const k of Object.keys(obj)) {
5fb2e288 96 if (this.silentFilters.has(k)) continue
cddf4503 97
9abd170d 98 if (this.isValidValue(obj[k])) return true
0b18f4aa
C
99 }
100
101 return false
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
C
139 sort: this.sort,
140 searchTarget: this.searchTarget
0b18f4aa
C
141 }
142 }
143
af7fd04a 144 toVideosAPIObject (): VideosSearchQuery {
7a22a0a5
C
145 let isLive: boolean
146 if (this.isLive) isLive = this.isLive === 'true'
147
0b18f4aa
C
148 return {
149 startDate: this.startDate,
150 endDate: this.endDate,
31d065cc
AM
151 originallyPublishedStartDate: this.originallyPublishedStartDate,
152 originallyPublishedEndDate: this.originallyPublishedEndDate,
0b18f4aa 153 nsfw: this.nsfw,
dd24f1bb
C
154 categoryOneOf: intoArray(this.categoryOneOf),
155 licenceOneOf: intoArray(this.licenceOneOf),
156 languageOneOf: intoArray(this.languageOneOf),
9abd170d
C
157 tagsOneOf: this.tagsOneOf,
158 tagsAllOf: this.tagsAllOf,
0b18f4aa 159 durationMin: this.durationMin,
cddf4503 160 durationMax: this.durationMax,
af7fd04a 161 host: this.host,
7a22a0a5 162 isLive,
5fb2e288
C
163 sort: this.sort,
164 searchTarget: this.searchTarget
0b18f4aa
C
165 }
166 }
4278710d 167
af7fd04a
C
168 toPlaylistAPIObject (): VideoPlaylistsSearchQuery {
169 return {
170 host: this.host,
171 searchTarget: this.searchTarget
172 }
173 }
174
175 toChannelAPIObject (): VideoChannelsSearchQuery {
176 return {
177 host: this.host,
178 searchTarget: this.searchTarget
179 }
180 }
181
c5d04b4f
RK
182 size () {
183 let acc = 0
184
c199c427 185 const obj = this.toUrlObject()
c5d04b4f 186 for (const k of Object.keys(obj)) {
5fb2e288 187 if (this.silentFilters.has(k)) continue
c5d04b4f 188
9abd170d 189 if (this.isValidValue(obj[k])) acc++
c5d04b4f
RK
190 }
191
192 return acc
193 }
194
9abd170d
C
195 private isValidValue (val: any) {
196 if (val === undefined) return false
197 if (val === '') return false
198 if (Array.isArray(val) && val.length === 0) return false
199
200 return true
201 }
0b18f4aa 202}