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 { immutableAssign } from '@app/shared/misc/utils'
9 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
10 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
11 import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
12 import { Video } from '@app/shared/video/video.model'
13 import { MetaService } from '@ngx-meta/core'
14 import { I18n } from '@ngx-translate/i18n-polyfill'
15 import { ServerConfig } from '@shared/models'
16 import { SearchTargetType } from '@shared/models/search/search-target-query.model'
19 selector: 'my-search',
20 styleUrls: [ './search.component.scss' ],
21 templateUrl: './search.component.html'
23 export class SearchComponent implements OnInit, OnDestroy {
24 results: (Video | VideoChannel)[] = []
26 pagination: ComponentPagination = {
28 itemsPerPage: 10, // Only for videos, use another variable for channels
31 advancedSearch: AdvancedSearch = new AdvancedSearch()
32 isSearchFilterCollapsed = true
35 videoDisplayOptions: MiniatureDisplayOptions = {
47 serverConfig: ServerConfig
49 private subActivatedRoute: Subscription
50 private isInitialLoad = false // set to false to show the search filters on first arrival
51 private firstSearch = true
53 private channelsPerPage = 2
55 private lastSearchTarget: SearchTargetType
59 private route: ActivatedRoute,
60 private router: Router,
61 private metaService: MetaService,
62 private notifier: Notifier,
63 private searchService: SearchService,
64 private authService: AuthService,
65 private hooks: HooksService,
66 private serverService: ServerService
70 return this.authService.getUser()
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.hooks.runAction('action:search.init', 'search')
110 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
113 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
114 return d instanceof VideoChannel
117 isVideo (v: VideoChannel | Video): v is Video {
118 return v instanceof Video
122 return this.authService.isLoggedIn()
128 this.getVideoChannelObs()
130 ([videosResult, videoChannelsResult]) => {
131 this.results = this.results
132 .concat(videoChannelsResult.data)
133 .concat(videosResult.data)
135 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
136 this.lastSearchTarget = this.advancedSearch.searchTarget
138 // Focus on channels if there are no enough videos
139 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
140 this.resetPagination()
141 this.firstSearch = false
143 this.channelsPerPage = 10
147 this.firstSearch = false
151 if (this.advancedSearch.searchTarget !== 'search-index') {
152 this.notifier.error(err.message)
157 this.i18n('Search index is unavailable. Retrying with instance results instead.'),
158 this.i18n('Search error')
160 this.advancedSearch.searchTarget = 'local'
168 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
170 this.pagination.currentPage += 1
175 this.resetPagination()
177 this.updateUrlFromAdvancedSearch()
181 return this.advancedSearch.size()
184 // Add VideoChannel for typings, but the template already checks "video" argument is a video
185 removeVideoFromArray (video: Video | VideoChannel) {
186 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
189 getChannelUrl (channel: VideoChannel) {
190 if (this.advancedSearch.searchTarget === 'search-index' && channel.url) {
191 const remoteUriConfig = this.serverConfig.search.remoteUri
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
197 return [ '/search/lazy-load-channel', { url: channel.url, externalRedirect, fromPath } ]
200 return [ '/video-channels', channel.nameWithHost ]
204 return this.lastSearchTarget === 'search-index'
207 private resetPagination () {
208 this.pagination.currentPage = 1
209 this.pagination.totalItems = null
210 this.channelsPerPage = 2
215 private updateTitle () {
216 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
217 this.metaService.setTitle(this.i18n('Search') + suffix)
220 private updateUrlFromAdvancedSearch () {
221 const search = this.currentSearch || undefined
223 this.router.navigate([], {
224 relativeTo: this.route,
225 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
229 private getVideosObs () {
231 search: this.currentSearch,
232 componentPagination: this.pagination,
233 advancedSearch: this.advancedSearch
236 return this.hooks.wrapObsFun(
237 this.searchService.searchVideos.bind(this.searchService),
240 'filter:api.search.videos.list.params',
241 'filter:api.search.videos.list.result'
245 private getVideoChannelObs () {
246 if (!this.currentSearch) return of({ data: [], total: 0 })
249 search: this.currentSearch,
250 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
251 searchTarget: this.advancedSearch.searchTarget
254 return this.hooks.wrapObsFun(
255 this.searchService.searchVideoChannels.bind(this.searchService),
258 'filter:api.search.video-channels.list.params',
259 'filter:api.search.video-channels.list.result'