]>
Commit | Line | Data |
---|---|---|
916bf528 | 1 | import { forkJoin, Subscription } from 'rxjs' |
37a44fc9 | 2 | import { LinkType } from 'src/types/link.type' |
57c36b27 | 3 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0b18f4aa | 4 | import { ActivatedRoute, Router } from '@angular/router' |
37a44fc9 | 5 | import { AuthService, HooksService, MetaService, Notifier, ServerService, User, UserService } from '@app/core' |
67ed6552 | 6 | import { immutableAssign } from '@app/helpers' |
af7fd04a | 7 | import { validateHost } from '@app/shared/form-validators/host-validators' |
67ed6552 | 8 | import { Video, VideoChannel } from '@app/shared/shared-main' |
1942f11d | 9 | import { AdvancedSearch, SearchService } from '@app/shared/shared-search' |
37a44fc9 C |
10 | import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature' |
11 | import { VideoPlaylist } from '@app/shared/shared-video-playlist' | |
2989628b | 12 | import { HTMLServerConfig, SearchTargetType } from '@shared/models' |
57c36b27 C |
13 | |
14 | @Component({ | |
15 | selector: 'my-search', | |
16 | styleUrls: [ './search.component.scss' ], | |
17 | templateUrl: './search.component.html' | |
18 | }) | |
19 | export class SearchComponent implements OnInit, OnDestroy { | |
af7fd04a C |
20 | error: string |
21 | ||
916bf528 | 22 | results: (Video | VideoChannel | VideoPlaylist)[] = [] |
f37dc0dd | 23 | |
37a44fc9 | 24 | pagination = { |
57c36b27 | 25 | currentPage: 1, |
37a44fc9 | 26 | totalItems: null as number |
57c36b27 | 27 | } |
0b18f4aa C |
28 | advancedSearch: AdvancedSearch = new AdvancedSearch() |
29 | isSearchFilterCollapsed = true | |
f37dc0dd | 30 | currentSearch: string |
57c36b27 | 31 | |
cf78883c C |
32 | videoDisplayOptions: MiniatureDisplayOptions = { |
33 | date: true, | |
34 | views: true, | |
35 | by: true, | |
36 | avatar: false, | |
37 | privacyLabel: false, | |
38 | privacyText: false, | |
39 | state: false, | |
40 | blacklistInfo: false | |
41 | } | |
42 | ||
5fb2e288 | 43 | errorMessage: string |
5fb2e288 | 44 | |
5c20a455 C |
45 | userMiniature: User |
46 | ||
57c36b27 | 47 | private subActivatedRoute: Subscription |
c5d04b4f | 48 | private isInitialLoad = false // set to false to show the search filters on first arrival |
57c36b27 | 49 | |
f37dc0dd | 50 | private channelsPerPage = 2 |
37a44fc9 C |
51 | private playlistsPerPage = 2 |
52 | private videosPerPage = 10 | |
53 | ||
54 | private hasMoreResults = true | |
55 | private isSearching = false | |
f37dc0dd | 56 | |
7c87746e C |
57 | private lastSearchTarget: SearchTargetType |
58 | ||
2989628b C |
59 | private serverConfig: HTMLServerConfig |
60 | ||
57c36b27 | 61 | constructor ( |
57c36b27 | 62 | private route: ActivatedRoute, |
0b18f4aa | 63 | private router: Router, |
57c36b27 | 64 | private metaService: MetaService, |
f8b2c1b4 | 65 | private notifier: Notifier, |
26fabbd6 | 66 | private searchService: SearchService, |
93cae479 | 67 | private authService: AuthService, |
5c20a455 | 68 | private userService: UserService, |
5fb2e288 C |
69 | private hooks: HooksService, |
70 | private serverService: ServerService | |
57c36b27 C |
71 | ) { } |
72 | ||
73 | ngOnInit () { | |
2989628b | 74 | this.serverConfig = this.serverService.getHTMLConfig() |
5fb2e288 | 75 | |
57c36b27 | 76 | this.subActivatedRoute = this.route.queryParams.subscribe( |
5fb2e288 | 77 | async queryParams => { |
57c36b27 | 78 | const querySearch = queryParams['search'] |
7c87746e | 79 | const searchTarget = queryParams['searchTarget'] |
57c36b27 | 80 | |
0b18f4aa | 81 | // Search updated, reset filters |
7c87746e | 82 | if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) { |
7afea880 C |
83 | this.resetPagination() |
84 | this.advancedSearch.reset() | |
85 | ||
47879669 | 86 | this.currentSearch = querySearch || undefined |
7afea880 C |
87 | this.updateTitle() |
88 | } | |
89 | ||
90 | this.advancedSearch = new AdvancedSearch(queryParams) | |
5fb2e288 | 91 | if (!this.advancedSearch.searchTarget) { |
2989628b | 92 | this.advancedSearch.searchTarget = this.getDefaultSearchTarget() |
5fb2e288 | 93 | } |
0b18f4aa | 94 | |
af7fd04a C |
95 | this.error = this.checkFieldsAndGetError() |
96 | ||
97 | // Don't hide filters if we have some of them AND the user just came on the webpage, or we have an error | |
98 | this.isSearchFilterCollapsed = !this.error && (this.isInitialLoad === false || !this.advancedSearch.containsValues()) | |
7afea880 | 99 | this.isInitialLoad = false |
57c36b27 | 100 | |
7afea880 | 101 | this.search() |
57c36b27 C |
102 | }, |
103 | ||
f8b2c1b4 | 104 | err => this.notifier.error(err.text) |
57c36b27 | 105 | ) |
7663e55a | 106 | |
5c20a455 C |
107 | this.userService.getAnonymousOrLoggedUser() |
108 | .subscribe(user => this.userMiniature = user) | |
109 | ||
c9e3eeed | 110 | this.hooks.runAction('action:search.init', 'search') |
57c36b27 C |
111 | } |
112 | ||
113 | ngOnDestroy () { | |
114 | if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() | |
115 | } | |
116 | ||
37a44fc9 | 117 | isVideoChannel (d: VideoChannel | Video | VideoPlaylist): d is VideoChannel { |
26fabbd6 C |
118 | return d instanceof VideoChannel |
119 | } | |
120 | ||
37a44fc9 | 121 | isVideo (v: VideoChannel | Video | VideoPlaylist): v is Video { |
26fabbd6 C |
122 | return v instanceof Video |
123 | } | |
124 | ||
37a44fc9 C |
125 | isPlaylist (v: VideoChannel | Video | VideoPlaylist): v is VideoPlaylist { |
126 | return v instanceof VideoPlaylist | |
127 | } | |
128 | ||
26fabbd6 C |
129 | isUserLoggedIn () { |
130 | return this.authService.isLoggedIn() | |
131 | } | |
132 | ||
37a44fc9 | 133 | search () { |
af7fd04a C |
134 | this.error = this.checkFieldsAndGetError() |
135 | if (this.error) return | |
136 | ||
37a44fc9 | 137 | this.isSearching = true |
0bdad52f | 138 | |
37a44fc9 C |
139 | forkJoin([ |
140 | this.getVideoChannelObs(), | |
141 | this.getVideoPlaylistObs(), | |
142 | this.getVideosObs() | |
143 | ]).subscribe(results => { | |
144 | for (const result of results) { | |
145 | this.results = this.results.concat(result.data) | |
0bdad52f C |
146 | } |
147 | ||
37a44fc9 C |
148 | this.pagination.totalItems = results.reduce((p, r) => p += r.total, 0) |
149 | this.lastSearchTarget = this.advancedSearch.searchTarget | |
0bdad52f | 150 | |
37a44fc9 C |
151 | this.hasMoreResults = this.results.length < this.pagination.totalItems |
152 | }, | |
0bdad52f | 153 | |
37a44fc9 C |
154 | err => { |
155 | if (this.advancedSearch.searchTarget !== 'search-index') { | |
156 | this.notifier.error(err.message) | |
157 | return | |
158 | } | |
5fb2e288 | 159 | |
37a44fc9 C |
160 | this.notifier.error( |
161 | $localize`Search index is unavailable. Retrying with instance results instead.`, | |
162 | $localize`Search error` | |
163 | ) | |
164 | this.advancedSearch.searchTarget = 'local' | |
165 | this.search() | |
166 | }, | |
5fb2e288 | 167 | |
37a44fc9 C |
168 | () => { |
169 | this.isSearching = false | |
170 | }) | |
57c36b27 C |
171 | } |
172 | ||
173 | onNearOfBottom () { | |
174 | // Last page | |
37a44fc9 | 175 | if (!this.hasMoreResults || this.isSearching) return |
57c36b27 C |
176 | |
177 | this.pagination.currentPage += 1 | |
178 | this.search() | |
179 | } | |
180 | ||
0b18f4aa | 181 | onFiltered () { |
7afea880 | 182 | this.resetPagination() |
0b18f4aa | 183 | |
7afea880 | 184 | this.updateUrlFromAdvancedSearch() |
0b18f4aa C |
185 | } |
186 | ||
c5d04b4f RK |
187 | numberOfFilters () { |
188 | return this.advancedSearch.size() | |
189 | } | |
190 | ||
37a44fc9 C |
191 | // Add VideoChannel/VideoPlaylist for typings, but the template already checks "video" argument is a video |
192 | removeVideoFromArray (video: Video | VideoChannel | VideoPlaylist) { | |
3a0fb65c C |
193 | this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id) |
194 | } | |
195 | ||
37a44fc9 C |
196 | getLinkType (): LinkType { |
197 | if (this.advancedSearch.searchTarget === 'search-index') { | |
198 | const remoteUriConfig = this.serverConfig.search.remoteUri | |
199 | ||
200 | // Redirect on the external instance if not allowed to fetch remote data | |
201 | if ((!this.isUserLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users) { | |
202 | return 'external' | |
203 | } | |
204 | ||
205 | return 'lazy-load' | |
206 | } | |
207 | ||
208 | return 'internal' | |
209 | } | |
210 | ||
746018f6 | 211 | isExternalChannelUrl () { |
37a44fc9 | 212 | return this.getLinkType() === 'external' |
746018f6 C |
213 | } |
214 | ||
215 | getExternalChannelUrl (channel: VideoChannel) { | |
0bdad52f | 216 | // Same algorithm than videos |
37a44fc9 | 217 | if (this.getLinkType() === 'external') { |
0bdad52f C |
218 | return channel.url |
219 | } | |
5fb2e288 | 220 | |
746018f6 C |
221 | // lazy-load or internal |
222 | return undefined | |
223 | } | |
224 | ||
225 | getInternalChannelUrl (channel: VideoChannel) { | |
37a44fc9 | 226 | const linkType = this.getLinkType() |
746018f6 C |
227 | |
228 | if (linkType === 'internal') { | |
71887396 | 229 | return [ '/c', channel.nameWithHost ] |
5fb2e288 C |
230 | } |
231 | ||
746018f6 C |
232 | if (linkType === 'lazy-load') { |
233 | return [ '/search/lazy-load-channel', { url: channel.url } ] | |
234 | } | |
235 | ||
236 | // external | |
237 | return undefined | |
5fb2e288 C |
238 | } |
239 | ||
240 | hideActions () { | |
7c87746e | 241 | return this.lastSearchTarget === 'search-index' |
5fb2e288 C |
242 | } |
243 | ||
7afea880 | 244 | private resetPagination () { |
57c36b27 C |
245 | this.pagination.currentPage = 1 |
246 | this.pagination.totalItems = null | |
aa55a4da | 247 | this.channelsPerPage = 2 |
57c36b27 | 248 | |
26fabbd6 | 249 | this.results = [] |
57c36b27 C |
250 | } |
251 | ||
252 | private updateTitle () { | |
0f01a8ba C |
253 | const suffix = this.currentSearch |
254 | ? ' ' + this.currentSearch | |
255 | : '' | |
256 | ||
66357162 | 257 | this.metaService.setTitle($localize`Search` + suffix) |
57c36b27 | 258 | } |
0b18f4aa C |
259 | |
260 | private updateUrlFromAdvancedSearch () { | |
47879669 | 261 | const search = this.currentSearch || undefined |
c5d04b4f | 262 | |
0b18f4aa C |
263 | this.router.navigate([], { |
264 | relativeTo: this.route, | |
c5d04b4f | 265 | queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search }) |
0b18f4aa C |
266 | }) |
267 | } | |
93cae479 C |
268 | |
269 | private getVideosObs () { | |
270 | const params = { | |
271 | search: this.currentSearch, | |
37a44fc9 | 272 | componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.videosPerPage }), |
93cae479 C |
273 | advancedSearch: this.advancedSearch |
274 | } | |
275 | ||
276 | return this.hooks.wrapObsFun( | |
277 | this.searchService.searchVideos.bind(this.searchService), | |
278 | params, | |
e8f902c0 | 279 | 'search', |
93cae479 C |
280 | 'filter:api.search.videos.list.params', |
281 | 'filter:api.search.videos.list.result' | |
282 | ) | |
283 | } | |
284 | ||
285 | private getVideoChannelObs () { | |
286 | const params = { | |
287 | search: this.currentSearch, | |
5fb2e288 | 288 | componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }), |
af7fd04a | 289 | advancedSearch: this.advancedSearch |
93cae479 C |
290 | } |
291 | ||
292 | return this.hooks.wrapObsFun( | |
293 | this.searchService.searchVideoChannels.bind(this.searchService), | |
294 | params, | |
e8f902c0 | 295 | 'search', |
93cae479 C |
296 | 'filter:api.search.video-channels.list.params', |
297 | 'filter:api.search.video-channels.list.result' | |
298 | ) | |
299 | } | |
2989628b | 300 | |
37a44fc9 | 301 | private getVideoPlaylistObs () { |
37a44fc9 C |
302 | const params = { |
303 | search: this.currentSearch, | |
304 | componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.playlistsPerPage }), | |
af7fd04a | 305 | advancedSearch: this.advancedSearch |
37a44fc9 C |
306 | } |
307 | ||
308 | return this.hooks.wrapObsFun( | |
309 | this.searchService.searchVideoPlaylists.bind(this.searchService), | |
310 | params, | |
311 | 'search', | |
312 | 'filter:api.search.video-playlists.list.params', | |
313 | 'filter:api.search.video-playlists.list.result' | |
314 | ) | |
315 | } | |
316 | ||
fc21ef5c | 317 | private getDefaultSearchTarget (): SearchTargetType { |
2989628b C |
318 | const searchIndexConfig = this.serverConfig.search.searchIndex |
319 | ||
320 | if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) { | |
321 | return 'search-index' | |
322 | } | |
323 | ||
324 | return 'local' | |
325 | } | |
af7fd04a C |
326 | |
327 | private checkFieldsAndGetError () { | |
328 | if (this.advancedSearch.host && !validateHost(this.advancedSearch.host)) { | |
329 | return $localize`PeerTube instance host filter is invalid` | |
330 | } | |
331 | ||
332 | return undefined | |
333 | } | |
57c36b27 | 334 | } |