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