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