]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.component.ts
Fix search error loop
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.component.ts
CommitLineData
5fb2e288 1import { forkJoin, of, Subscription } from 'rxjs'
57c36b27 2import { Component, OnDestroy, OnInit } from '@angular/core'
0b18f4aa 3import { ActivatedRoute, Router } from '@angular/router'
5fb2e288
C
4import { AuthService, Notifier, ServerService } from '@app/core'
5import { HooksService } from '@app/core/plugins/hooks.service'
6import { AdvancedSearch } from '@app/search/advanced-search.model'
57c36b27 7import { SearchService } from '@app/search/search.service'
5fb2e288 8import { immutableAssign } from '@app/shared/misc/utils'
57c36b27 9import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
f37dc0dd 10import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
cf78883c 11import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
26fabbd6 12import { Video } from '@app/shared/video/video.model'
5fb2e288
C
13import { MetaService } from '@ngx-meta/core'
14import { I18n } from '@ngx-translate/i18n-polyfill'
15import { ServerConfig } from '@shared/models'
7c87746e 16import { 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})
23export 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 => {
12e6b314
C
151 if (this.advancedSearch.searchTarget !== 'search-index') {
152 this.notifier.error(err.message)
153 return
154 }
5fb2e288
C
155
156 this.notifier.error(
157 this.i18n('Search index is unavailable. Retrying with instance results instead.'),
158 this.i18n('Search error')
159 )
160 this.advancedSearch.searchTarget = 'local'
161 this.search()
162 }
163 )
57c36b27
C
164 }
165
166 onNearOfBottom () {
167 // Last page
168 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
169
170 this.pagination.currentPage += 1
171 this.search()
172 }
173
0b18f4aa 174 onFiltered () {
7afea880 175 this.resetPagination()
0b18f4aa 176
7afea880 177 this.updateUrlFromAdvancedSearch()
0b18f4aa
C
178 }
179
c5d04b4f
RK
180 numberOfFilters () {
181 return this.advancedSearch.size()
182 }
183
be27ef3b
C
184 // Add VideoChannel for typings, but the template already checks "video" argument is a video
185 removeVideoFromArray (video: Video | VideoChannel) {
3a0fb65c
C
186 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
187 }
188
5fb2e288
C
189 getChannelUrl (channel: VideoChannel) {
190 if (this.advancedSearch.searchTarget === 'search-index' && channel.url) {
191 const remoteUriConfig = this.serverConfig.search.remoteUri
192
193 // Redirect on the external instance if not allowed to fetch remote data
194 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
195 const fromPath = window.location.pathname + window.location.search
196
197 return [ '/search/lazy-load-channel', { url: channel.url, externalRedirect, fromPath } ]
198 }
199
200 return [ '/video-channels', channel.nameWithHost ]
201 }
202
203 hideActions () {
7c87746e 204 return this.lastSearchTarget === 'search-index'
5fb2e288
C
205 }
206
7afea880 207 private resetPagination () {
57c36b27
C
208 this.pagination.currentPage = 1
209 this.pagination.totalItems = null
aa55a4da 210 this.channelsPerPage = 2
57c36b27 211
26fabbd6 212 this.results = []
57c36b27
C
213 }
214
215 private updateTitle () {
f107470e
C
216 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
217 this.metaService.setTitle(this.i18n('Search') + suffix)
57c36b27 218 }
0b18f4aa
C
219
220 private updateUrlFromAdvancedSearch () {
47879669 221 const search = this.currentSearch || undefined
c5d04b4f 222
0b18f4aa
C
223 this.router.navigate([], {
224 relativeTo: this.route,
c5d04b4f 225 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
0b18f4aa
C
226 })
227 }
93cae479
C
228
229 private getVideosObs () {
230 const params = {
231 search: this.currentSearch,
232 componentPagination: this.pagination,
233 advancedSearch: this.advancedSearch
234 }
235
236 return this.hooks.wrapObsFun(
237 this.searchService.searchVideos.bind(this.searchService),
238 params,
e8f902c0 239 'search',
93cae479
C
240 'filter:api.search.videos.list.params',
241 'filter:api.search.videos.list.result'
242 )
243 }
244
245 private getVideoChannelObs () {
44df5c75
C
246 if (!this.currentSearch) return of({ data: [], total: 0 })
247
93cae479
C
248 const params = {
249 search: this.currentSearch,
5fb2e288
C
250 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
251 searchTarget: this.advancedSearch.searchTarget
93cae479
C
252 }
253
254 return this.hooks.wrapObsFun(
255 this.searchService.searchVideoChannels.bind(this.searchService),
256 params,
e8f902c0 257 'search',
93cae479
C
258 'filter:api.search.video-channels.list.params',
259 'filter:api.search.video-channels.list.result'
260 )
261 }
57c36b27 262}