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, Notifier, ServerService } from '@app/core'
5 import { HooksService } from '@app/core/plugins/hooks.service'
6 import { AdvancedSearch } from '@app/search/advanced-search.model'
7 import { SearchService } from '@app/search/search.service'
8 import { UserService } from '@app/shared'
9 import { immutableAssign } from '@app/shared/misc/utils'
10 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
11 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
12 import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
13 import { Video } from '@app/shared/video/video.model'
14 import { MetaService } from '@ngx-meta/core'
15 import { I18n } from '@ngx-translate/i18n-polyfill'
16 import { ServerConfig, User } from '@shared/models'
17 import { SearchTargetType } from '@shared/models/search/search-target-query.model'
20 selector: 'my-search',
21 styleUrls: [ './search.component.scss' ],
22 templateUrl: './search.component.html'
24 export class SearchComponent implements OnInit, OnDestroy {
25 results: (Video | VideoChannel)[] = []
27 pagination: ComponentPagination = {
29 itemsPerPage: 10, // Only for videos, use another variable for channels
32 advancedSearch: AdvancedSearch = new AdvancedSearch()
33 isSearchFilterCollapsed = true
36 videoDisplayOptions: MiniatureDisplayOptions = {
48 serverConfig: ServerConfig
52 private subActivatedRoute: Subscription
53 private isInitialLoad = false // set to false to show the search filters on first arrival
54 private firstSearch = true
56 private channelsPerPage = 2
58 private lastSearchTarget: SearchTargetType
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.serverService.getConfig()
75 .subscribe(config => this.serverConfig = config)
77 this.subActivatedRoute = this.route.queryParams.subscribe(
78 async 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 = await this.serverService.getDefaultSearchTarget()
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
103 err => this.notifier.error(err.text)
106 this.userService.getAnonymousOrLoggedUser()
107 .subscribe(user => this.userMiniature = user)
109 this.hooks.runAction('action:search.init', 'search')
113 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
116 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
117 return d instanceof VideoChannel
120 isVideo (v: VideoChannel | Video): v is Video {
121 return v instanceof Video
125 return this.authService.isLoggedIn()
131 this.getVideoChannelObs()
133 ([videosResult, videoChannelsResult]) => {
134 this.results = this.results
135 .concat(videoChannelsResult.data)
136 .concat(videosResult.data)
138 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
139 this.lastSearchTarget = this.advancedSearch.searchTarget
141 // Focus on channels if there are no enough videos
142 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
143 this.resetPagination()
144 this.firstSearch = false
146 this.channelsPerPage = 10
150 this.firstSearch = false
154 if (this.advancedSearch.searchTarget !== 'search-index') {
155 this.notifier.error(err.message)
160 this.i18n('Search index is unavailable. Retrying with instance results instead.'),
161 this.i18n('Search error')
163 this.advancedSearch.searchTarget = 'local'
171 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
173 this.pagination.currentPage += 1
178 this.resetPagination()
180 this.updateUrlFromAdvancedSearch()
184 return this.advancedSearch.size()
187 // Add VideoChannel for typings, but the template already checks "video" argument is a video
188 removeVideoFromArray (video: Video | VideoChannel) {
189 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
192 getChannelUrl (channel: VideoChannel) {
193 if (this.advancedSearch.searchTarget === 'search-index' && channel.url) {
194 const remoteUriConfig = this.serverConfig.search.remoteUri
196 // Redirect on the external instance if not allowed to fetch remote data
197 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
198 const fromPath = window.location.pathname + window.location.search
200 return [ '/search/lazy-load-channel', { url: channel.url, externalRedirect, fromPath } ]
203 return [ '/video-channels', channel.nameWithHost ]
207 return this.lastSearchTarget === 'search-index'
210 private resetPagination () {
211 this.pagination.currentPage = 1
212 this.pagination.totalItems = null
213 this.channelsPerPage = 2
218 private updateTitle () {
219 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
220 this.metaService.setTitle(this.i18n('Search') + suffix)
223 private updateUrlFromAdvancedSearch () {
224 const search = this.currentSearch || undefined
226 this.router.navigate([], {
227 relativeTo: this.route,
228 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
232 private getVideosObs () {
234 search: this.currentSearch,
235 componentPagination: this.pagination,
236 advancedSearch: this.advancedSearch
239 return this.hooks.wrapObsFun(
240 this.searchService.searchVideos.bind(this.searchService),
243 'filter:api.search.videos.list.params',
244 'filter:api.search.videos.list.result'
248 private getVideoChannelObs () {
249 if (!this.currentSearch) return of({ data: [], total: 0 })
252 search: this.currentSearch,
253 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
254 searchTarget: this.advancedSearch.searchTarget
257 return this.hooks.wrapObsFun(
258 this.searchService.searchVideoChannels.bind(this.searchService),
261 'filter:api.search.video-channels.list.params',
262 'filter:api.search.video-channels.list.result'