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