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, VideoLinkType } 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()
122 getVideoLinkType (): VideoLinkType {
123 if (this.advancedSearch.searchTarget === 'search-index') {
124 const remoteUriConfig = this.serverConfig.search.remoteUri
126 // Redirect on the external instance if not allowed to fetch remote data
127 if ((!this.isUserLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users) {
137 isExternalChannelUrl () {
138 return this.getVideoLinkType() === 'external'
144 this.getVideoChannelObs()
146 ([videosResult, videoChannelsResult]) => {
147 this.results = this.results
148 .concat(videoChannelsResult.data)
149 .concat(videosResult.data)
151 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
152 this.lastSearchTarget = this.advancedSearch.searchTarget
154 // Focus on channels if there are no enough videos
155 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
156 this.resetPagination()
157 this.firstSearch = false
159 this.channelsPerPage = 10
163 this.firstSearch = false
167 if (this.advancedSearch.searchTarget !== 'search-index') {
168 this.notifier.error(err.message)
173 this.i18n('Search index is unavailable. Retrying with instance results instead.'),
174 this.i18n('Search error')
176 this.advancedSearch.searchTarget = 'local'
184 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
186 this.pagination.currentPage += 1
191 this.resetPagination()
193 this.updateUrlFromAdvancedSearch()
197 return this.advancedSearch.size()
200 // Add VideoChannel for typings, but the template already checks "video" argument is a video
201 removeVideoFromArray (video: Video | VideoChannel) {
202 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
205 getChannelUrl (channel: VideoChannel) {
206 // Same algorithm than videos
207 if (this.getVideoLinkType() === 'external') {
211 if (this.getVideoLinkType() === 'internal') {
212 return [ '/video-channels', channel.nameWithHost ]
215 return [ '/search/lazy-load-channel', { url: channel.url } ]
219 return this.lastSearchTarget === 'search-index'
222 private resetPagination () {
223 this.pagination.currentPage = 1
224 this.pagination.totalItems = null
225 this.channelsPerPage = 2
230 private updateTitle () {
231 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
232 this.metaService.setTitle(this.i18n('Search') + suffix)
235 private updateUrlFromAdvancedSearch () {
236 const search = this.currentSearch || undefined
238 this.router.navigate([], {
239 relativeTo: this.route,
240 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
244 private getVideosObs () {
246 search: this.currentSearch,
247 componentPagination: this.pagination,
248 advancedSearch: this.advancedSearch
251 return this.hooks.wrapObsFun(
252 this.searchService.searchVideos.bind(this.searchService),
255 'filter:api.search.videos.list.params',
256 'filter:api.search.videos.list.result'
260 private getVideoChannelObs () {
261 if (!this.currentSearch) return of({ data: [], total: 0 })
264 search: this.currentSearch,
265 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
266 searchTarget: this.advancedSearch.searchTarget
269 return this.hooks.wrapObsFun(
270 this.searchService.searchVideoChannels.bind(this.searchService),
273 'filter:api.search.video-channels.list.params',
274 'filter:api.search.video-channels.list.result'