]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+search/search.component.ts
distinct style for rows of banned users in listing, saving space
[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'
67ed6552 8import { MiniatureDisplayOptions } 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
57c36b27 122 search () {
f37dc0dd 123 forkJoin([
93cae479
C
124 this.getVideosObs(),
125 this.getVideoChannelObs()
5fb2e288
C
126 ]).subscribe(
127 ([videosResult, videoChannelsResult]) => {
128 this.results = this.results
129 .concat(videoChannelsResult.data)
130 .concat(videosResult.data)
131
132 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
7c87746e 133 this.lastSearchTarget = this.advancedSearch.searchTarget
b1ee8526 134
5fb2e288
C
135 // Focus on channels if there are no enough videos
136 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
137 this.resetPagination()
b1ee8526 138 this.firstSearch = false
57c36b27 139
5fb2e288
C
140 this.channelsPerPage = 10
141 this.search()
142 }
143
144 this.firstSearch = false
145 },
146
147 err => {
12e6b314
C
148 if (this.advancedSearch.searchTarget !== 'search-index') {
149 this.notifier.error(err.message)
150 return
151 }
5fb2e288
C
152
153 this.notifier.error(
154 this.i18n('Search index is unavailable. Retrying with instance results instead.'),
155 this.i18n('Search error')
156 )
157 this.advancedSearch.searchTarget = 'local'
158 this.search()
159 }
160 )
57c36b27
C
161 }
162
163 onNearOfBottom () {
164 // Last page
165 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
166
167 this.pagination.currentPage += 1
168 this.search()
169 }
170
0b18f4aa 171 onFiltered () {
7afea880 172 this.resetPagination()
0b18f4aa 173
7afea880 174 this.updateUrlFromAdvancedSearch()
0b18f4aa
C
175 }
176
c5d04b4f
RK
177 numberOfFilters () {
178 return this.advancedSearch.size()
179 }
180
be27ef3b
C
181 // Add VideoChannel for typings, but the template already checks "video" argument is a video
182 removeVideoFromArray (video: Video | VideoChannel) {
3a0fb65c
C
183 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
184 }
185
5fb2e288
C
186 getChannelUrl (channel: VideoChannel) {
187 if (this.advancedSearch.searchTarget === 'search-index' && channel.url) {
188 const remoteUriConfig = this.serverConfig.search.remoteUri
189
190 // Redirect on the external instance if not allowed to fetch remote data
191 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
192 const fromPath = window.location.pathname + window.location.search
193
194 return [ '/search/lazy-load-channel', { url: channel.url, externalRedirect, fromPath } ]
195 }
196
197 return [ '/video-channels', channel.nameWithHost ]
198 }
199
200 hideActions () {
7c87746e 201 return this.lastSearchTarget === 'search-index'
5fb2e288
C
202 }
203
7afea880 204 private resetPagination () {
57c36b27
C
205 this.pagination.currentPage = 1
206 this.pagination.totalItems = null
aa55a4da 207 this.channelsPerPage = 2
57c36b27 208
26fabbd6 209 this.results = []
57c36b27
C
210 }
211
212 private updateTitle () {
f107470e
C
213 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
214 this.metaService.setTitle(this.i18n('Search') + suffix)
57c36b27 215 }
0b18f4aa
C
216
217 private updateUrlFromAdvancedSearch () {
47879669 218 const search = this.currentSearch || undefined
c5d04b4f 219
0b18f4aa
C
220 this.router.navigate([], {
221 relativeTo: this.route,
c5d04b4f 222 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
0b18f4aa
C
223 })
224 }
93cae479
C
225
226 private getVideosObs () {
227 const params = {
228 search: this.currentSearch,
229 componentPagination: this.pagination,
230 advancedSearch: this.advancedSearch
231 }
232
233 return this.hooks.wrapObsFun(
234 this.searchService.searchVideos.bind(this.searchService),
235 params,
e8f902c0 236 'search',
93cae479
C
237 'filter:api.search.videos.list.params',
238 'filter:api.search.videos.list.result'
239 )
240 }
241
242 private getVideoChannelObs () {
44df5c75
C
243 if (!this.currentSearch) return of({ data: [], total: 0 })
244
93cae479
C
245 const params = {
246 search: this.currentSearch,
5fb2e288
C
247 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
248 searchTarget: this.advancedSearch.searchTarget
93cae479
C
249 }
250
251 return this.hooks.wrapObsFun(
252 this.searchService.searchVideoChannels.bind(this.searchService),
253 params,
e8f902c0 254 'search',
93cae479
C
255 'filter:api.search.video-channels.list.params',
256 'filter:api.search.video-channels.list.result'
257 )
258 }
57c36b27 259}