1 import { forkJoin, Subscription } from 'rxjs'
2 import { LinkType } from 'src/types/link.type'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { AuthService, HooksService, MetaService, Notifier, ServerService, User, UserService } from '@app/core'
6 import { immutableAssign } from '@app/helpers'
7 import { validateHost } from '@app/shared/form-validators/host-validators'
8 import { Video, VideoChannel } from '@app/shared/shared-main'
9 import { AdvancedSearch, SearchService } from '@app/shared/shared-search'
10 import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
11 import { VideoPlaylist } from '@app/shared/shared-video-playlist'
12 import { HTMLServerConfig, SearchTargetType } from '@shared/models'
15 selector: 'my-search',
16 styleUrls: [ './search.component.scss' ],
17 templateUrl: './search.component.html'
19 export class SearchComponent implements OnInit, OnDestroy {
22 results: (Video | VideoChannel | VideoPlaylist)[] = []
26 totalItems: null as number
28 advancedSearch: AdvancedSearch = new AdvancedSearch()
29 isSearchFilterCollapsed = true
32 videoDisplayOptions: MiniatureDisplayOptions = {
47 private subActivatedRoute: Subscription
48 private isInitialLoad = false // set to false to show the search filters on first arrival
50 private channelsPerPage = 2
51 private playlistsPerPage = 2
52 private videosPerPage = 10
54 private hasMoreResults = true
55 private isSearching = false
57 private lastSearchTarget: SearchTargetType
59 private serverConfig: HTMLServerConfig
62 private route: ActivatedRoute,
63 private router: Router,
64 private metaService: MetaService,
65 private notifier: Notifier,
66 private searchService: SearchService,
67 private authService: AuthService,
68 private userService: UserService,
69 private hooks: HooksService,
70 private serverService: ServerService
74 this.serverConfig = this.serverService.getHTMLConfig()
76 this.subActivatedRoute = this.route.queryParams
78 next: queryParams => {
79 const querySearch = queryParams['search']
80 const searchTarget = queryParams['searchTarget']
82 // Search updated, reset filters
83 if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) {
84 this.resetPagination()
85 this.advancedSearch.reset()
87 this.currentSearch = querySearch || undefined
91 this.advancedSearch = new AdvancedSearch(queryParams)
92 if (!this.advancedSearch.searchTarget) {
93 this.advancedSearch.searchTarget = this.getDefaultSearchTarget()
96 this.error = this.checkFieldsAndGetError()
98 // Don't hide filters if we have some of them AND the user just came on the webpage, or we have an error
99 this.isSearchFilterCollapsed = !this.error && (this.isInitialLoad === false || !this.advancedSearch.containsValues())
100 this.isInitialLoad = false
105 error: err => this.notifier.error(err.text)
108 this.userService.getAnonymousOrLoggedUser()
109 .subscribe(user => this.userMiniature = user)
111 this.hooks.runAction('action:search.init', 'search')
115 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
118 isVideoChannel (d: VideoChannel | Video | VideoPlaylist): d is VideoChannel {
119 return d instanceof VideoChannel
122 isVideo (v: VideoChannel | Video | VideoPlaylist): v is Video {
123 return v instanceof Video
126 isPlaylist (v: VideoChannel | Video | VideoPlaylist): v is VideoPlaylist {
127 return v instanceof VideoPlaylist
131 return this.authService.isLoggedIn()
135 this.error = this.checkFieldsAndGetError()
136 if (this.error) return
138 this.isSearching = true
141 this.getVideoChannelObs(),
142 this.getVideoPlaylistObs(),
146 for (const result of results) {
147 this.results = this.results.concat(result.data)
150 this.pagination.totalItems = results.reduce((p, r) => p += r.total, 0)
151 this.lastSearchTarget = this.advancedSearch.searchTarget
153 this.hasMoreResults = this.results.length < this.pagination.totalItems
157 if (this.advancedSearch.searchTarget !== 'search-index') {
158 this.notifier.error(err.message)
163 $localize`Search index is unavailable. Retrying with instance results instead.`,
164 $localize`Search error`
166 this.advancedSearch.searchTarget = 'local'
171 this.isSearching = false
178 if (!this.hasMoreResults || this.isSearching) return
180 this.pagination.currentPage += 1
185 this.resetPagination()
187 this.updateUrlFromAdvancedSearch()
191 return this.advancedSearch.size()
194 // Add VideoChannel/VideoPlaylist for typings, but the template already checks "video" argument is a video
195 removeVideoFromArray (video: Video | VideoChannel | VideoPlaylist) {
196 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
199 getLinkType (): LinkType {
200 if (this.advancedSearch.searchTarget === 'search-index') {
201 const remoteUriConfig = this.serverConfig.search.remoteUri
203 // Redirect on the external instance if not allowed to fetch remote data
204 if ((!this.isUserLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users) {
214 isExternalChannelUrl () {
215 return this.getLinkType() === 'external'
218 getExternalChannelUrl (channel: VideoChannel) {
219 // Same algorithm than videos
220 if (this.getLinkType() === 'external') {
224 // lazy-load or internal
228 getInternalChannelUrl (channel: VideoChannel) {
229 const linkType = this.getLinkType()
231 if (linkType === 'internal') {
232 return [ '/c', channel.nameWithHost ]
235 if (linkType === 'lazy-load') {
236 return [ '/search/lazy-load-channel', { url: channel.url } ]
244 return this.lastSearchTarget === 'search-index'
247 private resetPagination () {
248 this.pagination.currentPage = 1
249 this.pagination.totalItems = null
250 this.channelsPerPage = 2
255 private updateTitle () {
256 const suffix = this.currentSearch
257 ? ' ' + this.currentSearch
260 this.metaService.setTitle($localize`Search` + suffix)
263 private updateUrlFromAdvancedSearch () {
264 const search = this.currentSearch || undefined
266 this.router.navigate([], {
267 relativeTo: this.route,
268 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
272 private getVideosObs () {
274 search: this.currentSearch,
275 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.videosPerPage }),
276 advancedSearch: this.advancedSearch
279 return this.hooks.wrapObsFun(
280 this.searchService.searchVideos.bind(this.searchService),
283 'filter:api.search.videos.list.params',
284 'filter:api.search.videos.list.result'
288 private getVideoChannelObs () {
290 search: this.currentSearch,
291 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
292 advancedSearch: this.advancedSearch
295 return this.hooks.wrapObsFun(
296 this.searchService.searchVideoChannels.bind(this.searchService),
299 'filter:api.search.video-channels.list.params',
300 'filter:api.search.video-channels.list.result'
304 private getVideoPlaylistObs () {
306 search: this.currentSearch,
307 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.playlistsPerPage }),
308 advancedSearch: this.advancedSearch
311 return this.hooks.wrapObsFun(
312 this.searchService.searchVideoPlaylists.bind(this.searchService),
315 'filter:api.search.video-playlists.list.params',
316 'filter:api.search.video-playlists.list.result'
320 private getDefaultSearchTarget (): SearchTargetType {
321 const searchIndexConfig = this.serverConfig.search.searchIndex
323 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
324 return 'search-index'
330 private checkFieldsAndGetError () {
331 if (this.advancedSearch.host && !validateHost(this.advancedSearch.host)) {
332 return $localize`PeerTube instance host filter is invalid`