]>
Commit | Line | Data |
---|---|---|
5fb2e288 | 1 | import { forkJoin, of, Subscription } from 'rxjs' |
57c36b27 | 2 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0b18f4aa | 3 | import { ActivatedRoute, Router } from '@angular/router' |
5fb2e288 C |
4 | import { AuthService, Notifier, ServerService } from '@app/core' |
5 | import { HooksService } from '@app/core/plugins/hooks.service' | |
6 | import { AdvancedSearch } from '@app/search/advanced-search.model' | |
57c36b27 | 7 | import { SearchService } from '@app/search/search.service' |
5fb2e288 | 8 | import { immutableAssign } from '@app/shared/misc/utils' |
57c36b27 | 9 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' |
f37dc0dd | 10 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
cf78883c | 11 | import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component' |
26fabbd6 | 12 | import { Video } from '@app/shared/video/video.model' |
5fb2e288 C |
13 | import { MetaService } from '@ngx-meta/core' |
14 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
15 | import { ServerConfig } from '@shared/models' | |
7c87746e | 16 | import { SearchTargetType } from '@shared/models/search/search-target-query.model' |
57c36b27 C |
17 | |
18 | @Component({ | |
19 | selector: 'my-search', | |
20 | styleUrls: [ './search.component.scss' ], | |
21 | templateUrl: './search.component.html' | |
22 | }) | |
23 | export class SearchComponent implements OnInit, OnDestroy { | |
26fabbd6 | 24 | results: (Video | VideoChannel)[] = [] |
f37dc0dd | 25 | |
57c36b27 C |
26 | pagination: ComponentPagination = { |
27 | currentPage: 1, | |
f37dc0dd | 28 | itemsPerPage: 10, // Only for videos, use another variable for channels |
57c36b27 C |
29 | totalItems: null |
30 | } | |
0b18f4aa C |
31 | advancedSearch: AdvancedSearch = new AdvancedSearch() |
32 | isSearchFilterCollapsed = true | |
f37dc0dd | 33 | currentSearch: string |
57c36b27 | 34 | |
cf78883c C |
35 | videoDisplayOptions: MiniatureDisplayOptions = { |
36 | date: true, | |
37 | views: true, | |
38 | by: true, | |
39 | avatar: false, | |
40 | privacyLabel: false, | |
41 | privacyText: false, | |
42 | state: false, | |
43 | blacklistInfo: false | |
44 | } | |
45 | ||
5fb2e288 C |
46 | errorMessage: string |
47 | serverConfig: ServerConfig | |
48 | ||
57c36b27 | 49 | private subActivatedRoute: Subscription |
c5d04b4f | 50 | private isInitialLoad = false // set to false to show the search filters on first arrival |
b1ee8526 | 51 | private firstSearch = true |
57c36b27 | 52 | |
f37dc0dd C |
53 | private channelsPerPage = 2 |
54 | ||
7c87746e C |
55 | private lastSearchTarget: SearchTargetType |
56 | ||
57c36b27 C |
57 | constructor ( |
58 | private i18n: I18n, | |
59 | private route: ActivatedRoute, | |
0b18f4aa | 60 | private router: Router, |
57c36b27 | 61 | private metaService: MetaService, |
f8b2c1b4 | 62 | private notifier: Notifier, |
26fabbd6 | 63 | private searchService: SearchService, |
93cae479 | 64 | private authService: AuthService, |
5fb2e288 C |
65 | private hooks: HooksService, |
66 | private serverService: ServerService | |
57c36b27 C |
67 | ) { } |
68 | ||
e2409062 C |
69 | get user () { |
70 | return this.authService.getUser() | |
71 | } | |
72 | ||
57c36b27 | 73 | ngOnInit () { |
5fb2e288 C |
74 | this.serverService.getConfig() |
75 | .subscribe(config => this.serverConfig = config) | |
76 | ||
57c36b27 | 77 | this.subActivatedRoute = this.route.queryParams.subscribe( |
5fb2e288 | 78 | async queryParams => { |
57c36b27 | 79 | const querySearch = queryParams['search'] |
7c87746e | 80 | const searchTarget = queryParams['searchTarget'] |
57c36b27 | 81 | |
0b18f4aa | 82 | // Search updated, reset filters |
7c87746e | 83 | if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) { |
7afea880 C |
84 | this.resetPagination() |
85 | this.advancedSearch.reset() | |
86 | ||
47879669 | 87 | this.currentSearch = querySearch || undefined |
7afea880 C |
88 | this.updateTitle() |
89 | } | |
90 | ||
91 | this.advancedSearch = new AdvancedSearch(queryParams) | |
5fb2e288 C |
92 | if (!this.advancedSearch.searchTarget) { |
93 | this.advancedSearch.searchTarget = await this.serverService.getDefaultSearchTarget() | |
94 | } | |
0b18f4aa | 95 | |
7afea880 C |
96 | // Don't hide filters if we have some of them AND the user just came on the webpage |
97 | this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues() | |
98 | this.isInitialLoad = false | |
57c36b27 | 99 | |
7afea880 | 100 | this.search() |
57c36b27 C |
101 | }, |
102 | ||
f8b2c1b4 | 103 | err => this.notifier.error(err.text) |
57c36b27 | 104 | ) |
7663e55a | 105 | |
c9e3eeed | 106 | this.hooks.runAction('action:search.init', 'search') |
57c36b27 C |
107 | } |
108 | ||
109 | ngOnDestroy () { | |
110 | if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() | |
111 | } | |
112 | ||
26fabbd6 C |
113 | isVideoChannel (d: VideoChannel | Video): d is VideoChannel { |
114 | return d instanceof VideoChannel | |
115 | } | |
116 | ||
117 | isVideo (v: VideoChannel | Video): v is Video { | |
118 | return v instanceof Video | |
119 | } | |
120 | ||
121 | isUserLoggedIn () { | |
122 | return this.authService.isLoggedIn() | |
123 | } | |
124 | ||
57c36b27 | 125 | search () { |
f37dc0dd | 126 | forkJoin([ |
93cae479 C |
127 | this.getVideosObs(), |
128 | this.getVideoChannelObs() | |
5fb2e288 C |
129 | ]).subscribe( |
130 | ([videosResult, videoChannelsResult]) => { | |
131 | this.results = this.results | |
132 | .concat(videoChannelsResult.data) | |
133 | .concat(videosResult.data) | |
134 | ||
135 | this.pagination.totalItems = videosResult.total + videoChannelsResult.total | |
7c87746e | 136 | this.lastSearchTarget = this.advancedSearch.searchTarget |
b1ee8526 | 137 | |
5fb2e288 C |
138 | // Focus on channels if there are no enough videos |
139 | if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) { | |
140 | this.resetPagination() | |
b1ee8526 | 141 | this.firstSearch = false |
57c36b27 | 142 | |
5fb2e288 C |
143 | this.channelsPerPage = 10 |
144 | this.search() | |
145 | } | |
146 | ||
147 | this.firstSearch = false | |
148 | }, | |
149 | ||
150 | err => { | |
151 | if (this.advancedSearch.searchTarget !== 'search-index') this.notifier.error(err.message) | |
152 | ||
153 | this.notifier.error( | |
154 | this.i18n('Search index is unavailable. Retrying with instance results instead.'), | |
155 | this.i18n('Search error') | |
156 | ) | |
157 | this.advancedSearch.searchTarget = 'local' | |
158 | this.search() | |
159 | } | |
160 | ) | |
57c36b27 C |
161 | } |
162 | ||
163 | onNearOfBottom () { | |
164 | // Last page | |
165 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
166 | ||
167 | this.pagination.currentPage += 1 | |
168 | this.search() | |
169 | } | |
170 | ||
0b18f4aa | 171 | onFiltered () { |
7afea880 | 172 | this.resetPagination() |
0b18f4aa | 173 | |
7afea880 | 174 | this.updateUrlFromAdvancedSearch() |
0b18f4aa C |
175 | } |
176 | ||
c5d04b4f RK |
177 | numberOfFilters () { |
178 | return this.advancedSearch.size() | |
179 | } | |
180 | ||
be27ef3b C |
181 | // Add VideoChannel for typings, but the template already checks "video" argument is a video |
182 | removeVideoFromArray (video: Video | VideoChannel) { | |
3a0fb65c C |
183 | this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id) |
184 | } | |
185 | ||
5fb2e288 C |
186 | getChannelUrl (channel: VideoChannel) { |
187 | if (this.advancedSearch.searchTarget === 'search-index' && channel.url) { | |
188 | const remoteUriConfig = this.serverConfig.search.remoteUri | |
189 | ||
190 | // Redirect on the external instance if not allowed to fetch remote data | |
191 | const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users | |
192 | const fromPath = window.location.pathname + window.location.search | |
193 | ||
194 | return [ '/search/lazy-load-channel', { url: channel.url, externalRedirect, fromPath } ] | |
195 | } | |
196 | ||
197 | return [ '/video-channels', channel.nameWithHost ] | |
198 | } | |
199 | ||
200 | hideActions () { | |
7c87746e | 201 | return this.lastSearchTarget === 'search-index' |
5fb2e288 C |
202 | } |
203 | ||
7afea880 | 204 | private resetPagination () { |
57c36b27 C |
205 | this.pagination.currentPage = 1 |
206 | this.pagination.totalItems = null | |
aa55a4da | 207 | this.channelsPerPage = 2 |
57c36b27 | 208 | |
26fabbd6 | 209 | this.results = [] |
57c36b27 C |
210 | } |
211 | ||
212 | private updateTitle () { | |
f107470e C |
213 | const suffix = this.currentSearch ? ' ' + this.currentSearch : '' |
214 | this.metaService.setTitle(this.i18n('Search') + suffix) | |
57c36b27 | 215 | } |
0b18f4aa C |
216 | |
217 | private updateUrlFromAdvancedSearch () { | |
47879669 | 218 | const search = this.currentSearch || undefined |
c5d04b4f | 219 | |
0b18f4aa C |
220 | this.router.navigate([], { |
221 | relativeTo: this.route, | |
c5d04b4f | 222 | queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search }) |
0b18f4aa C |
223 | }) |
224 | } | |
93cae479 C |
225 | |
226 | private getVideosObs () { | |
227 | const params = { | |
228 | search: this.currentSearch, | |
229 | componentPagination: this.pagination, | |
230 | advancedSearch: this.advancedSearch | |
231 | } | |
232 | ||
233 | return this.hooks.wrapObsFun( | |
234 | this.searchService.searchVideos.bind(this.searchService), | |
235 | params, | |
e8f902c0 | 236 | 'search', |
93cae479 C |
237 | 'filter:api.search.videos.list.params', |
238 | 'filter:api.search.videos.list.result' | |
239 | ) | |
240 | } | |
241 | ||
242 | private getVideoChannelObs () { | |
44df5c75 C |
243 | if (!this.currentSearch) return of({ data: [], total: 0 }) |
244 | ||
93cae479 C |
245 | const params = { |
246 | search: this.currentSearch, | |
5fb2e288 C |
247 | componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }), |
248 | searchTarget: this.advancedSearch.searchTarget | |
93cae479 C |
249 | } |
250 | ||
251 | return this.hooks.wrapObsFun( | |
252 | this.searchService.searchVideoChannels.bind(this.searchService), | |
253 | params, | |
e8f902c0 | 254 | 'search', |
93cae479 C |
255 | 'filter:api.search.video-channels.list.params', |
256 | 'filter:api.search.video-channels.list.result' | |
257 | ) | |
258 | } | |
57c36b27 | 259 | } |