]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.component.ts
First implem global search
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.component.ts
CommitLineData
5fb2e288 1import { forkJoin, of, Subscription } from 'rxjs'
57c36b27 2import { Component, OnDestroy, OnInit } from '@angular/core'
0b18f4aa 3import { ActivatedRoute, Router } from '@angular/router'
5fb2e288
C
4import { AuthService, Notifier, ServerService } from '@app/core'
5import { HooksService } from '@app/core/plugins/hooks.service'
6import { AdvancedSearch } from '@app/search/advanced-search.model'
57c36b27 7import { SearchService } from '@app/search/search.service'
5fb2e288 8import { immutableAssign } from '@app/shared/misc/utils'
57c36b27 9import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
f37dc0dd 10import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
26fabbd6 11import { Video } from '@app/shared/video/video.model'
5fb2e288
C
12import { MetaService } from '@ngx-meta/core'
13import { I18n } from '@ngx-translate/i18n-polyfill'
14import { ServerConfig } from '@shared/models'
15import { UserService } from '@app/shared'
57c36b27
C
16
17@Component({
18 selector: 'my-search',
19 styleUrls: [ './search.component.scss' ],
20 templateUrl: './search.component.html'
21})
22export class SearchComponent implements OnInit, OnDestroy {
26fabbd6 23 results: (Video | VideoChannel)[] = []
f37dc0dd 24
57c36b27
C
25 pagination: ComponentPagination = {
26 currentPage: 1,
f37dc0dd 27 itemsPerPage: 10, // Only for videos, use another variable for channels
57c36b27
C
28 totalItems: null
29 }
0b18f4aa
C
30 advancedSearch: AdvancedSearch = new AdvancedSearch()
31 isSearchFilterCollapsed = true
f37dc0dd 32 currentSearch: string
57c36b27 33
5fb2e288
C
34 errorMessage: string
35 serverConfig: ServerConfig
36
57c36b27 37 private subActivatedRoute: Subscription
c5d04b4f 38 private isInitialLoad = false // set to false to show the search filters on first arrival
b1ee8526 39 private firstSearch = true
57c36b27 40
f37dc0dd
C
41 private channelsPerPage = 2
42
57c36b27
C
43 constructor (
44 private i18n: I18n,
45 private route: ActivatedRoute,
0b18f4aa 46 private router: Router,
57c36b27 47 private metaService: MetaService,
f8b2c1b4 48 private notifier: Notifier,
26fabbd6 49 private searchService: SearchService,
93cae479 50 private authService: AuthService,
5fb2e288
C
51 private hooks: HooksService,
52 private serverService: ServerService
57c36b27
C
53 ) { }
54
e2409062
C
55 get user () {
56 return this.authService.getUser()
57 }
58
57c36b27 59 ngOnInit () {
5fb2e288
C
60 this.serverService.getConfig()
61 .subscribe(config => this.serverConfig = config)
62
57c36b27 63 this.subActivatedRoute = this.route.queryParams.subscribe(
5fb2e288 64 async queryParams => {
57c36b27
C
65 const querySearch = queryParams['search']
66
0b18f4aa 67 // Search updated, reset filters
7afea880
C
68 if (this.currentSearch !== querySearch) {
69 this.resetPagination()
70 this.advancedSearch.reset()
71
47879669 72 this.currentSearch = querySearch || undefined
7afea880
C
73 this.updateTitle()
74 }
75
76 this.advancedSearch = new AdvancedSearch(queryParams)
5fb2e288
C
77 if (!this.advancedSearch.searchTarget) {
78 this.advancedSearch.searchTarget = await this.serverService.getDefaultSearchTarget()
79 }
0b18f4aa 80
7afea880
C
81 // Don't hide filters if we have some of them AND the user just came on the webpage
82 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
83 this.isInitialLoad = false
57c36b27 84
7afea880 85 this.search()
57c36b27
C
86 },
87
f8b2c1b4 88 err => this.notifier.error(err.text)
57c36b27 89 )
7663e55a 90
c9e3eeed 91 this.hooks.runAction('action:search.init', 'search')
57c36b27
C
92 }
93
94 ngOnDestroy () {
95 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
96 }
97
26fabbd6
C
98 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
99 return d instanceof VideoChannel
100 }
101
102 isVideo (v: VideoChannel | Video): v is Video {
103 return v instanceof Video
104 }
105
106 isUserLoggedIn () {
107 return this.authService.isLoggedIn()
108 }
109
57c36b27 110 search () {
f37dc0dd 111 forkJoin([
93cae479
C
112 this.getVideosObs(),
113 this.getVideoChannelObs()
5fb2e288
C
114 ]).subscribe(
115 ([videosResult, videoChannelsResult]) => {
116 this.results = this.results
117 .concat(videoChannelsResult.data)
118 .concat(videosResult.data)
119
120 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
b1ee8526 121
5fb2e288
C
122 // Focus on channels if there are no enough videos
123 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
124 this.resetPagination()
b1ee8526 125 this.firstSearch = false
57c36b27 126
5fb2e288
C
127 this.channelsPerPage = 10
128 this.search()
129 }
130
131 this.firstSearch = false
132 },
133
134 err => {
135 if (this.advancedSearch.searchTarget !== 'search-index') this.notifier.error(err.message)
136
137 this.notifier.error(
138 this.i18n('Search index is unavailable. Retrying with instance results instead.'),
139 this.i18n('Search error')
140 )
141 this.advancedSearch.searchTarget = 'local'
142 this.search()
143 }
144 )
57c36b27
C
145 }
146
147 onNearOfBottom () {
148 // Last page
149 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
150
151 this.pagination.currentPage += 1
152 this.search()
153 }
154
0b18f4aa 155 onFiltered () {
7afea880 156 this.resetPagination()
0b18f4aa 157
7afea880 158 this.updateUrlFromAdvancedSearch()
0b18f4aa
C
159 }
160
c5d04b4f
RK
161 numberOfFilters () {
162 return this.advancedSearch.size()
163 }
164
be27ef3b
C
165 // Add VideoChannel for typings, but the template already checks "video" argument is a video
166 removeVideoFromArray (video: Video | VideoChannel) {
3a0fb65c
C
167 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
168 }
169
5fb2e288
C
170 getChannelUrl (channel: VideoChannel) {
171 if (this.advancedSearch.searchTarget === 'search-index' && channel.url) {
172 const remoteUriConfig = this.serverConfig.search.remoteUri
173
174 // Redirect on the external instance if not allowed to fetch remote data
175 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
176 const fromPath = window.location.pathname + window.location.search
177
178 return [ '/search/lazy-load-channel', { url: channel.url, externalRedirect, fromPath } ]
179 }
180
181 return [ '/video-channels', channel.nameWithHost ]
182 }
183
184 hideActions () {
185 return this.advancedSearch.searchTarget === 'search-index'
186 }
187
7afea880 188 private resetPagination () {
57c36b27
C
189 this.pagination.currentPage = 1
190 this.pagination.totalItems = null
aa55a4da 191 this.channelsPerPage = 2
57c36b27 192
26fabbd6 193 this.results = []
57c36b27
C
194 }
195
196 private updateTitle () {
f107470e
C
197 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
198 this.metaService.setTitle(this.i18n('Search') + suffix)
57c36b27 199 }
0b18f4aa
C
200
201 private updateUrlFromAdvancedSearch () {
47879669 202 const search = this.currentSearch || undefined
c5d04b4f 203
0b18f4aa
C
204 this.router.navigate([], {
205 relativeTo: this.route,
c5d04b4f 206 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
0b18f4aa
C
207 })
208 }
93cae479
C
209
210 private getVideosObs () {
211 const params = {
212 search: this.currentSearch,
213 componentPagination: this.pagination,
214 advancedSearch: this.advancedSearch
215 }
216
217 return this.hooks.wrapObsFun(
218 this.searchService.searchVideos.bind(this.searchService),
219 params,
e8f902c0 220 'search',
93cae479
C
221 'filter:api.search.videos.list.params',
222 'filter:api.search.videos.list.result'
223 )
224 }
225
226 private getVideoChannelObs () {
44df5c75
C
227 if (!this.currentSearch) return of({ data: [], total: 0 })
228
93cae479
C
229 const params = {
230 search: this.currentSearch,
5fb2e288
C
231 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
232 searchTarget: this.advancedSearch.searchTarget
93cae479
C
233 }
234
235 return this.hooks.wrapObsFun(
236 this.searchService.searchVideoChannels.bind(this.searchService),
237 params,
e8f902c0 238 'search',
93cae479
C
239 'filter:api.search.video-channels.list.params',
240 'filter:api.search.video-channels.list.result'
241 )
242 }
57c36b27 243}