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