]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+search/search.component.ts
Add hls to prune storage script
[github/Chocobozzz/PeerTube.git] / client / src / app / +search / search.component.ts
CommitLineData
916bf528 1import { forkJoin, 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
57c36b27 47 private subActivatedRoute: Subscription
c5d04b4f 48 private isInitialLoad = false // set to false to show the search filters on first arrival
57c36b27 49
f37dc0dd 50 private channelsPerPage = 2
37a44fc9
C
51 private playlistsPerPage = 2
52 private videosPerPage = 10
53
54 private hasMoreResults = true
55 private isSearching = false
f37dc0dd 56
7c87746e
C
57 private lastSearchTarget: SearchTargetType
58
2989628b
C
59 private serverConfig: HTMLServerConfig
60
57c36b27 61 constructor (
57c36b27 62 private route: ActivatedRoute,
0b18f4aa 63 private router: Router,
57c36b27 64 private metaService: MetaService,
f8b2c1b4 65 private notifier: Notifier,
26fabbd6 66 private searchService: SearchService,
93cae479 67 private authService: AuthService,
5c20a455 68 private userService: UserService,
5fb2e288
C
69 private hooks: HooksService,
70 private serverService: ServerService
57c36b27
C
71 ) { }
72
73 ngOnInit () {
2989628b 74 this.serverConfig = this.serverService.getHTMLConfig()
5fb2e288 75
1378c0d3
C
76 this.subActivatedRoute = this.route.queryParams
77 .subscribe({
98ab5dc8 78 next: queryParams => {
1378c0d3
C
79 const querySearch = queryParams['search']
80 const searchTarget = queryParams['searchTarget']
7afea880 81
1378c0d3
C
82 // Search updated, reset filters
83 if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) {
84 this.resetPagination()
85 this.advancedSearch.reset()
0b18f4aa 86
1378c0d3
C
87 this.currentSearch = querySearch || undefined
88 this.updateTitle()
89 }
af7fd04a 90
1378c0d3
C
91 this.advancedSearch = new AdvancedSearch(queryParams)
92 if (!this.advancedSearch.searchTarget) {
93 this.advancedSearch.searchTarget = this.getDefaultSearchTarget()
94 }
57c36b27 95
1378c0d3 96 this.error = this.checkFieldsAndGetError()
57c36b27 97
1378c0d3
C
98 // Don't hide filters if we have some of them AND the user just came on the webpage, or we have an error
99 this.isSearchFilterCollapsed = !this.error && (this.isInitialLoad === false || !this.advancedSearch.containsValues())
100 this.isInitialLoad = false
101
102 this.search()
103 },
104
105 error: err => this.notifier.error(err.text)
106 })
7663e55a 107
5c20a455
C
108 this.userService.getAnonymousOrLoggedUser()
109 .subscribe(user => this.userMiniature = user)
110
c9e3eeed 111 this.hooks.runAction('action:search.init', 'search')
57c36b27
C
112 }
113
114 ngOnDestroy () {
115 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
116 }
117
37a44fc9 118 isVideoChannel (d: VideoChannel | Video | VideoPlaylist): d is VideoChannel {
26fabbd6
C
119 return d instanceof VideoChannel
120 }
121
37a44fc9 122 isVideo (v: VideoChannel | Video | VideoPlaylist): v is Video {
26fabbd6
C
123 return v instanceof Video
124 }
125
37a44fc9
C
126 isPlaylist (v: VideoChannel | Video | VideoPlaylist): v is VideoPlaylist {
127 return v instanceof VideoPlaylist
128 }
129
26fabbd6
C
130 isUserLoggedIn () {
131 return this.authService.isLoggedIn()
132 }
133
37a44fc9 134 search () {
af7fd04a
C
135 this.error = this.checkFieldsAndGetError()
136 if (this.error) return
137
37a44fc9 138 this.isSearching = true
0bdad52f 139
37a44fc9
C
140 forkJoin([
141 this.getVideoChannelObs(),
142 this.getVideoPlaylistObs(),
143 this.getVideosObs()
1378c0d3
C
144 ]).subscribe({
145 next: results => {
146 for (const result of results) {
147 this.results = this.results.concat(result.data)
148 }
0bdad52f 149
1378c0d3
C
150 this.pagination.totalItems = results.reduce((p, r) => p += r.total, 0)
151 this.lastSearchTarget = this.advancedSearch.searchTarget
0bdad52f 152
1378c0d3
C
153 this.hasMoreResults = this.results.length < this.pagination.totalItems
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
aa55a4da 250 this.channelsPerPage = 2
57c36b27 251
26fabbd6 252 this.results = []
57c36b27
C
253 }
254
255 private updateTitle () {
0f01a8ba
C
256 const suffix = this.currentSearch
257 ? ' ' + this.currentSearch
258 : ''
259
66357162 260 this.metaService.setTitle($localize`Search` + suffix)
57c36b27 261 }
0b18f4aa
C
262
263 private updateUrlFromAdvancedSearch () {
47879669 264 const search = this.currentSearch || undefined
c5d04b4f 265
0b18f4aa
C
266 this.router.navigate([], {
267 relativeTo: this.route,
c5d04b4f 268 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
0b18f4aa
C
269 })
270 }
93cae479
C
271
272 private getVideosObs () {
273 const params = {
274 search: this.currentSearch,
37a44fc9 275 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.videosPerPage }),
93cae479
C
276 advancedSearch: this.advancedSearch
277 }
278
279 return this.hooks.wrapObsFun(
280 this.searchService.searchVideos.bind(this.searchService),
281 params,
e8f902c0 282 'search',
93cae479
C
283 'filter:api.search.videos.list.params',
284 'filter:api.search.videos.list.result'
285 )
286 }
287
288 private getVideoChannelObs () {
289 const params = {
290 search: this.currentSearch,
5fb2e288 291 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
af7fd04a 292 advancedSearch: this.advancedSearch
93cae479
C
293 }
294
295 return this.hooks.wrapObsFun(
296 this.searchService.searchVideoChannels.bind(this.searchService),
297 params,
e8f902c0 298 'search',
93cae479
C
299 'filter:api.search.video-channels.list.params',
300 'filter:api.search.video-channels.list.result'
301 )
302 }
2989628b 303
37a44fc9 304 private getVideoPlaylistObs () {
37a44fc9
C
305 const params = {
306 search: this.currentSearch,
307 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.playlistsPerPage }),
af7fd04a 308 advancedSearch: this.advancedSearch
37a44fc9
C
309 }
310
311 return this.hooks.wrapObsFun(
312 this.searchService.searchVideoPlaylists.bind(this.searchService),
313 params,
314 'search',
315 'filter:api.search.video-playlists.list.params',
316 'filter:api.search.video-playlists.list.result'
317 )
318 }
319
fc21ef5c 320 private getDefaultSearchTarget (): SearchTargetType {
2989628b
C
321 const searchIndexConfig = this.serverConfig.search.searchIndex
322
323 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
324 return 'search-index'
325 }
326
327 return 'local'
328 }
af7fd04a
C
329
330 private checkFieldsAndGetError () {
331 if (this.advancedSearch.host && !validateHost(this.advancedSearch.host)) {
332 return $localize`PeerTube instance host filter is invalid`
333 }
334
335 return undefined
336 }
57c36b27 337}