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 { SearchTargetType, ServerConfig } from '@shared/models'
13 selector: 'my-search',
14 styleUrls: [ './search.component.scss' ],
15 templateUrl: './search.component.html'
17 export class SearchComponent implements OnInit, OnDestroy {
18 results: (Video | VideoChannel)[] = []
20 pagination: ComponentPagination = {
22 itemsPerPage: 10, // Only for videos, use another variable for channels
25 advancedSearch: AdvancedSearch = new AdvancedSearch()
26 isSearchFilterCollapsed = true
29 videoDisplayOptions: MiniatureDisplayOptions = {
41 serverConfig: ServerConfig
45 private subActivatedRoute: Subscription
46 private isInitialLoad = false // set to false to show the search filters on first arrival
47 private firstSearch = true
49 private channelsPerPage = 2
51 private lastSearchTarget: SearchTargetType
54 private route: ActivatedRoute,
55 private router: Router,
56 private metaService: MetaService,
57 private notifier: Notifier,
58 private searchService: SearchService,
59 private authService: AuthService,
60 private userService: UserService,
61 private hooks: HooksService,
62 private serverService: ServerService
66 this.serverService.getConfig()
67 .subscribe(config => this.serverConfig = config)
69 this.subActivatedRoute = this.route.queryParams.subscribe(
70 async queryParams => {
71 const querySearch = queryParams['search']
72 const searchTarget = queryParams['searchTarget']
74 // Search updated, reset filters
75 if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) {
76 this.resetPagination()
77 this.advancedSearch.reset()
79 this.currentSearch = querySearch || undefined
83 this.advancedSearch = new AdvancedSearch(queryParams)
84 if (!this.advancedSearch.searchTarget) {
85 this.advancedSearch.searchTarget = await this.serverService.getDefaultSearchTarget()
88 // Don't hide filters if we have some of them AND the user just came on the webpage
89 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
90 this.isInitialLoad = false
95 err => this.notifier.error(err.text)
98 this.userService.getAnonymousOrLoggedUser()
99 .subscribe(user => this.userMiniature = user)
101 this.hooks.runAction('action:search.init', 'search')
105 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
108 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
109 return d instanceof VideoChannel
112 isVideo (v: VideoChannel | Video): v is Video {
113 return v instanceof Video
117 return this.authService.isLoggedIn()
120 getVideoLinkType (): VideoLinkType {
121 if (this.advancedSearch.searchTarget === 'search-index') {
122 const remoteUriConfig = this.serverConfig.search.remoteUri
124 // Redirect on the external instance if not allowed to fetch remote data
125 if ((!this.isUserLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users) {
135 isExternalChannelUrl () {
136 return this.getVideoLinkType() === 'external'
142 this.getVideoChannelObs()
144 ([videosResult, videoChannelsResult]) => {
145 this.results = this.results
146 .concat(videoChannelsResult.data)
147 .concat(videosResult.data)
149 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
150 this.lastSearchTarget = this.advancedSearch.searchTarget
152 // Focus on channels if there are no enough videos
153 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
154 this.resetPagination()
155 this.firstSearch = false
157 this.channelsPerPage = 10
161 this.firstSearch = false
165 if (this.advancedSearch.searchTarget !== 'search-index') {
166 this.notifier.error(err.message)
171 $localize`Search index is unavailable. Retrying with instance results instead.`,
172 $localize`Search error`
174 this.advancedSearch.searchTarget = 'local'
182 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
184 this.pagination.currentPage += 1
189 this.resetPagination()
191 this.updateUrlFromAdvancedSearch()
195 return this.advancedSearch.size()
198 // Add VideoChannel for typings, but the template already checks "video" argument is a video
199 removeVideoFromArray (video: Video | VideoChannel) {
200 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
203 getChannelUrl (channel: VideoChannel) {
204 // Same algorithm than videos
205 if (this.getVideoLinkType() === 'external') {
209 if (this.getVideoLinkType() === 'internal') {
210 return [ '/video-channels', channel.nameWithHost ]
213 return [ '/search/lazy-load-channel', { url: channel.url } ]
217 return this.lastSearchTarget === 'search-index'
220 private resetPagination () {
221 this.pagination.currentPage = 1
222 this.pagination.totalItems = null
223 this.channelsPerPage = 2
228 private updateTitle () {
229 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
230 this.metaService.setTitle($localize`Search` + suffix)
233 private updateUrlFromAdvancedSearch () {
234 const search = this.currentSearch || undefined
236 this.router.navigate([], {
237 relativeTo: this.route,
238 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
242 private getVideosObs () {
244 search: this.currentSearch,
245 componentPagination: this.pagination,
246 advancedSearch: this.advancedSearch
249 return this.hooks.wrapObsFun(
250 this.searchService.searchVideos.bind(this.searchService),
253 'filter:api.search.videos.list.params',
254 'filter:api.search.videos.list.result'
258 private getVideoChannelObs () {
259 if (!this.currentSearch) return of({ data: [], total: 0 })
262 search: this.currentSearch,
263 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
264 searchTarget: this.advancedSearch.searchTarget
267 return this.hooks.wrapObsFun(
268 this.searchService.searchVideoChannels.bind(this.searchService),
271 'filter:api.search.video-channels.list.params',
272 'filter:api.search.video-channels.list.result'