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 } from '@app/shared/shared-video-miniature'
9 import { MetaService } from '@ngx-meta/core'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { SearchTargetType, ServerConfig } from '@shared/models'
14 selector: 'my-search',
15 styleUrls: [ './search.component.scss' ],
16 templateUrl: './search.component.html'
18 export class SearchComponent implements OnInit, OnDestroy {
19 results: (Video | VideoChannel)[] = []
21 pagination: ComponentPagination = {
23 itemsPerPage: 10, // Only for videos, use another variable for channels
26 advancedSearch: AdvancedSearch = new AdvancedSearch()
27 isSearchFilterCollapsed = true
30 videoDisplayOptions: MiniatureDisplayOptions = {
42 serverConfig: ServerConfig
46 private subActivatedRoute: Subscription
47 private isInitialLoad = false // set to false to show the search filters on first arrival
48 private firstSearch = true
50 private channelsPerPage = 2
52 private lastSearchTarget: SearchTargetType
56 private route: ActivatedRoute,
57 private router: Router,
58 private metaService: MetaService,
59 private notifier: Notifier,
60 private searchService: SearchService,
61 private authService: AuthService,
62 private userService: UserService,
63 private hooks: HooksService,
64 private serverService: ServerService
68 this.serverService.getConfig()
69 .subscribe(config => this.serverConfig = config)
71 this.subActivatedRoute = this.route.queryParams.subscribe(
72 async queryParams => {
73 const querySearch = queryParams['search']
74 const searchTarget = queryParams['searchTarget']
76 // Search updated, reset filters
77 if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) {
78 this.resetPagination()
79 this.advancedSearch.reset()
81 this.currentSearch = querySearch || undefined
85 this.advancedSearch = new AdvancedSearch(queryParams)
86 if (!this.advancedSearch.searchTarget) {
87 this.advancedSearch.searchTarget = await this.serverService.getDefaultSearchTarget()
90 // Don't hide filters if we have some of them AND the user just came on the webpage
91 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
92 this.isInitialLoad = false
97 err => this.notifier.error(err.text)
100 this.userService.getAnonymousOrLoggedUser()
101 .subscribe(user => this.userMiniature = user)
103 this.hooks.runAction('action:search.init', 'search')
107 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
110 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
111 return d instanceof VideoChannel
114 isVideo (v: VideoChannel | Video): v is Video {
115 return v instanceof Video
119 return this.authService.isLoggedIn()
125 this.getVideoChannelObs()
127 ([videosResult, videoChannelsResult]) => {
128 this.results = this.results
129 .concat(videoChannelsResult.data)
130 .concat(videosResult.data)
132 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
133 this.lastSearchTarget = this.advancedSearch.searchTarget
135 // Focus on channels if there are no enough videos
136 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
137 this.resetPagination()
138 this.firstSearch = false
140 this.channelsPerPage = 10
144 this.firstSearch = false
148 if (this.advancedSearch.searchTarget !== 'search-index') {
149 this.notifier.error(err.message)
154 this.i18n('Search index is unavailable. Retrying with instance results instead.'),
155 this.i18n('Search error')
157 this.advancedSearch.searchTarget = 'local'
165 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
167 this.pagination.currentPage += 1
172 this.resetPagination()
174 this.updateUrlFromAdvancedSearch()
178 return this.advancedSearch.size()
181 // Add VideoChannel for typings, but the template already checks "video" argument is a video
182 removeVideoFromArray (video: Video | VideoChannel) {
183 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
186 getChannelUrl (channel: VideoChannel) {
187 if (this.advancedSearch.searchTarget === 'search-index' && channel.url) {
188 const remoteUriConfig = this.serverConfig.search.remoteUri
190 // Redirect on the external instance if not allowed to fetch remote data
191 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
192 const fromPath = window.location.pathname + window.location.search
194 return [ '/search/lazy-load-channel', { url: channel.url, externalRedirect, fromPath } ]
197 return [ '/video-channels', channel.nameWithHost ]
201 return this.lastSearchTarget === 'search-index'
204 private resetPagination () {
205 this.pagination.currentPage = 1
206 this.pagination.totalItems = null
207 this.channelsPerPage = 2
212 private updateTitle () {
213 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
214 this.metaService.setTitle(this.i18n('Search') + suffix)
217 private updateUrlFromAdvancedSearch () {
218 const search = this.currentSearch || undefined
220 this.router.navigate([], {
221 relativeTo: this.route,
222 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
226 private getVideosObs () {
228 search: this.currentSearch,
229 componentPagination: this.pagination,
230 advancedSearch: this.advancedSearch
233 return this.hooks.wrapObsFun(
234 this.searchService.searchVideos.bind(this.searchService),
237 'filter:api.search.videos.list.params',
238 'filter:api.search.videos.list.result'
242 private getVideoChannelObs () {
243 if (!this.currentSearch) return of({ data: [], total: 0 })
246 search: this.currentSearch,
247 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
248 searchTarget: this.advancedSearch.searchTarget
251 return this.hooks.wrapObsFun(
252 this.searchService.searchVideoChannels.bind(this.searchService),
255 'filter:api.search.video-channels.list.params',
256 'filter:api.search.video-channels.list.result'