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