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