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