]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+search/search.component.ts
Add ability for plugins to add metadata
[github/Chocobozzz/PeerTube.git] / client / src / app / +search / search.component.ts
CommitLineData
09a7ce0c 1import { forkJoin, Subject, Subscription } from 'rxjs'
37a44fc9 2import { LinkType } from 'src/types/link.type'
57c36b27 3import { Component, OnDestroy, OnInit } from '@angular/core'
0b18f4aa 4import { ActivatedRoute, Router } from '@angular/router'
37a44fc9 5import { AuthService, HooksService, MetaService, Notifier, ServerService, User, UserService } from '@app/core'
67ed6552 6import { immutableAssign } from '@app/helpers'
af7fd04a 7import { validateHost } from '@app/shared/form-validators/host-validators'
67ed6552 8import { Video, VideoChannel } from '@app/shared/shared-main'
1942f11d 9import { AdvancedSearch, SearchService } from '@app/shared/shared-search'
37a44fc9
C
10import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
11import { VideoPlaylist } from '@app/shared/shared-video-playlist'
2989628b 12import { HTMLServerConfig, SearchTargetType } from '@shared/models'
57c36b27
C
13
14@Component({
15 selector: 'my-search',
16 styleUrls: [ './search.component.scss' ],
17 templateUrl: './search.component.html'
18})
19export class SearchComponent implements OnInit, OnDestroy {
af7fd04a
C
20 error: string
21
916bf528 22 results: (Video | VideoChannel | VideoPlaylist)[] = []
f37dc0dd 23
37a44fc9 24 pagination = {
57c36b27 25 currentPage: 1,
37a44fc9 26 totalItems: null as number
57c36b27 27 }
0b18f4aa
C
28 advancedSearch: AdvancedSearch = new AdvancedSearch()
29 isSearchFilterCollapsed = true
f37dc0dd 30 currentSearch: string
57c36b27 31
cf78883c
C
32 videoDisplayOptions: MiniatureDisplayOptions = {
33 date: true,
34 views: true,
35 by: true,
36 avatar: false,
37 privacyLabel: false,
38 privacyText: false,
39 state: false,
40 blacklistInfo: false
41 }
42
5fb2e288 43 errorMessage: string
5fb2e288 44
5c20a455
C
45 userMiniature: User
46
09a7ce0c
C
47 onSearchDataSubject = new Subject<any>()
48
57c36b27 49 private subActivatedRoute: Subscription
c5d04b4f 50 private isInitialLoad = false // set to false to show the search filters on first arrival
57c36b27 51
37a44fc9
C
52 private hasMoreResults = true
53 private isSearching = false
f37dc0dd 54
7c87746e
C
55 private lastSearchTarget: SearchTargetType
56
2989628b
C
57 private serverConfig: HTMLServerConfig
58
57c36b27 59 constructor (
57c36b27 60 private route: ActivatedRoute,
0b18f4aa 61 private router: Router,
57c36b27 62 private metaService: MetaService,
f8b2c1b4 63 private notifier: Notifier,
26fabbd6 64 private searchService: SearchService,
93cae479 65 private authService: AuthService,
5c20a455 66 private userService: UserService,
5fb2e288
C
67 private hooks: HooksService,
68 private serverService: ServerService
57c36b27
C
69 ) { }
70
71 ngOnInit () {
2989628b 72 this.serverConfig = this.serverService.getHTMLConfig()
5fb2e288 73
1378c0d3
C
74 this.subActivatedRoute = this.route.queryParams
75 .subscribe({
98ab5dc8 76 next: queryParams => {
1378c0d3
C
77 const querySearch = queryParams['search']
78 const searchTarget = queryParams['searchTarget']
7afea880 79
1378c0d3
C
80 // Search updated, reset filters
81 if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) {
82 this.resetPagination()
83 this.advancedSearch.reset()
0b18f4aa 84
1378c0d3
C
85 this.currentSearch = querySearch || undefined
86 this.updateTitle()
87 }
af7fd04a 88
1378c0d3
C
89 this.advancedSearch = new AdvancedSearch(queryParams)
90 if (!this.advancedSearch.searchTarget) {
91 this.advancedSearch.searchTarget = this.getDefaultSearchTarget()
92 }
57c36b27 93
1378c0d3 94 this.error = this.checkFieldsAndGetError()
57c36b27 95
1378c0d3
C
96 // Don't hide filters if we have some of them AND the user just came on the webpage, or we have an error
97 this.isSearchFilterCollapsed = !this.error && (this.isInitialLoad === false || !this.advancedSearch.containsValues())
98 this.isInitialLoad = false
99
100 this.search()
101 },
102
255c0030 103 error: err => this.notifier.error(err.message)
1378c0d3 104 })
7663e55a 105
5c20a455
C
106 this.userService.getAnonymousOrLoggedUser()
107 .subscribe(user => this.userMiniature = user)
108
c9e3eeed 109 this.hooks.runAction('action:search.init', 'search')
57c36b27
C
110 }
111
112 ngOnDestroy () {
113 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
114 }
115
37a44fc9 116 isVideoChannel (d: VideoChannel | Video | VideoPlaylist): d is VideoChannel {
26fabbd6
C
117 return d instanceof VideoChannel
118 }
119
37a44fc9 120 isVideo (v: VideoChannel | Video | VideoPlaylist): v is Video {
26fabbd6
C
121 return v instanceof Video
122 }
123
37a44fc9
C
124 isPlaylist (v: VideoChannel | Video | VideoPlaylist): v is VideoPlaylist {
125 return v instanceof VideoPlaylist
126 }
127
26fabbd6
C
128 isUserLoggedIn () {
129 return this.authService.isLoggedIn()
130 }
131
37a44fc9 132 search () {
af7fd04a
C
133 this.error = this.checkFieldsAndGetError()
134 if (this.error) return
135
37a44fc9 136 this.isSearching = true
0bdad52f 137
37a44fc9
C
138 forkJoin([
139 this.getVideoChannelObs(),
140 this.getVideoPlaylistObs(),
141 this.getVideosObs()
1378c0d3
C
142 ]).subscribe({
143 next: results => {
144 for (const result of results) {
145 this.results = this.results.concat(result.data)
146 }
0bdad52f 147
1378c0d3
C
148 this.pagination.totalItems = results.reduce((p, r) => p += r.total, 0)
149 this.lastSearchTarget = this.advancedSearch.searchTarget
0bdad52f 150
1378c0d3 151 this.hasMoreResults = this.results.length < this.pagination.totalItems
09a7ce0c
C
152
153 this.onSearchDataSubject.next(results)
1378c0d3 154 },
0bdad52f 155
1378c0d3
C
156 error: err => {
157 if (this.advancedSearch.searchTarget !== 'search-index') {
158 this.notifier.error(err.message)
159 return
160 }
5fb2e288 161
1378c0d3
C
162 this.notifier.error(
163 $localize`Search index is unavailable. Retrying with instance results instead.`,
164 $localize`Search error`
165 )
166 this.advancedSearch.searchTarget = 'local'
167 this.search()
168 },
5fb2e288 169
1378c0d3
C
170 complete: () => {
171 this.isSearching = false
172 }
37a44fc9 173 })
57c36b27
C
174 }
175
176 onNearOfBottom () {
177 // Last page
37a44fc9 178 if (!this.hasMoreResults || this.isSearching) return
57c36b27
C
179
180 this.pagination.currentPage += 1
181 this.search()
182 }
183
0b18f4aa 184 onFiltered () {
7afea880 185 this.resetPagination()
0b18f4aa 186
7afea880 187 this.updateUrlFromAdvancedSearch()
0b18f4aa
C
188 }
189
c5d04b4f
RK
190 numberOfFilters () {
191 return this.advancedSearch.size()
192 }
193
37a44fc9
C
194 // Add VideoChannel/VideoPlaylist for typings, but the template already checks "video" argument is a video
195 removeVideoFromArray (video: Video | VideoChannel | VideoPlaylist) {
3a0fb65c
C
196 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
197 }
198
37a44fc9
C
199 getLinkType (): LinkType {
200 if (this.advancedSearch.searchTarget === 'search-index') {
201 const remoteUriConfig = this.serverConfig.search.remoteUri
202
203 // Redirect on the external instance if not allowed to fetch remote data
204 if ((!this.isUserLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users) {
205 return 'external'
206 }
207
208 return 'lazy-load'
209 }
210
211 return 'internal'
212 }
213
746018f6 214 isExternalChannelUrl () {
37a44fc9 215 return this.getLinkType() === 'external'
746018f6
C
216 }
217
218 getExternalChannelUrl (channel: VideoChannel) {
0bdad52f 219 // Same algorithm than videos
37a44fc9 220 if (this.getLinkType() === 'external') {
0bdad52f
C
221 return channel.url
222 }
5fb2e288 223
746018f6
C
224 // lazy-load or internal
225 return undefined
226 }
227
228 getInternalChannelUrl (channel: VideoChannel) {
37a44fc9 229 const linkType = this.getLinkType()
746018f6
C
230
231 if (linkType === 'internal') {
71887396 232 return [ '/c', channel.nameWithHost ]
5fb2e288
C
233 }
234
746018f6
C
235 if (linkType === 'lazy-load') {
236 return [ '/search/lazy-load-channel', { url: channel.url } ]
237 }
238
239 // external
240 return undefined
5fb2e288
C
241 }
242
243 hideActions () {
7c87746e 244 return this.lastSearchTarget === 'search-index'
5fb2e288
C
245 }
246
7afea880 247 private resetPagination () {
57c36b27
C
248 this.pagination.currentPage = 1
249 this.pagination.totalItems = null
250
26fabbd6 251 this.results = []
57c36b27
C
252 }
253
254 private updateTitle () {
eaa52952
C
255 const title = this.currentSearch
256 ? $localize`Search ${this.currentSearch}`
257 : $localize`Search`
0f01a8ba 258
eaa52952 259 this.metaService.setTitle(title)
57c36b27 260 }
0b18f4aa
C
261
262 private updateUrlFromAdvancedSearch () {
47879669 263 const search = this.currentSearch || undefined
c5d04b4f 264
0b18f4aa
C
265 this.router.navigate([], {
266 relativeTo: this.route,
c5d04b4f 267 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
0b18f4aa
C
268 })
269 }
93cae479
C
270
271 private getVideosObs () {
272 const params = {
273 search: this.currentSearch,
8cf43a65 274 componentPagination: immutableAssign(this.pagination, { itemsPerPage: 10 }),
93cae479
C
275 advancedSearch: this.advancedSearch
276 }
277
278 return this.hooks.wrapObsFun(
279 this.searchService.searchVideos.bind(this.searchService),
280 params,
e8f902c0 281 'search',
93cae479
C
282 'filter:api.search.videos.list.params',
283 'filter:api.search.videos.list.result'
284 )
285 }
286
287 private getVideoChannelObs () {
288 const params = {
289 search: this.currentSearch,
8cf43a65 290 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.buildChannelsPerPage() }),
af7fd04a 291 advancedSearch: this.advancedSearch
93cae479
C
292 }
293
294 return this.hooks.wrapObsFun(
295 this.searchService.searchVideoChannels.bind(this.searchService),
296 params,
e8f902c0 297 'search',
93cae479
C
298 'filter:api.search.video-channels.list.params',
299 'filter:api.search.video-channels.list.result'
300 )
301 }
2989628b 302
37a44fc9 303 private getVideoPlaylistObs () {
37a44fc9
C
304 const params = {
305 search: this.currentSearch,
8cf43a65 306 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.buildPlaylistsPerPage() }),
af7fd04a 307 advancedSearch: this.advancedSearch
37a44fc9
C
308 }
309
310 return this.hooks.wrapObsFun(
311 this.searchService.searchVideoPlaylists.bind(this.searchService),
312 params,
313 'search',
314 'filter:api.search.video-playlists.list.params',
315 'filter:api.search.video-playlists.list.result'
316 )
317 }
318
fc21ef5c 319 private getDefaultSearchTarget (): SearchTargetType {
2989628b
C
320 const searchIndexConfig = this.serverConfig.search.searchIndex
321
322 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
323 return 'search-index'
324 }
325
326 return 'local'
327 }
af7fd04a
C
328
329 private checkFieldsAndGetError () {
330 if (this.advancedSearch.host && !validateHost(this.advancedSearch.host)) {
331 return $localize`PeerTube instance host filter is invalid`
332 }
333
334 return undefined
335 }
8cf43a65
C
336
337 private buildChannelsPerPage () {
338 if (this.advancedSearch.resultType === 'channels') return 10
339
340 return 2
341 }
342
343 private buildPlaylistsPerPage () {
344 if (this.advancedSearch.resultType === 'playlists') return 10
345
346 return 2
347 }
57c36b27 348}