]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+search/search.component.ts
Move to stylelint
[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
57c36b27 135 search () {
f37dc0dd 136 forkJoin([
93cae479
C
137 this.getVideosObs(),
138 this.getVideoChannelObs()
5fb2e288
C
139 ]).subscribe(
140 ([videosResult, videoChannelsResult]) => {
141 this.results = this.results
142 .concat(videoChannelsResult.data)
143 .concat(videosResult.data)
144
145 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
7c87746e 146 this.lastSearchTarget = this.advancedSearch.searchTarget
b1ee8526 147
5fb2e288
C
148 // Focus on channels if there are no enough videos
149 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
150 this.resetPagination()
b1ee8526 151 this.firstSearch = false
57c36b27 152
5fb2e288
C
153 this.channelsPerPage = 10
154 this.search()
155 }
156
157 this.firstSearch = false
158 },
159
160 err => {
12e6b314
C
161 if (this.advancedSearch.searchTarget !== 'search-index') {
162 this.notifier.error(err.message)
163 return
164 }
5fb2e288
C
165
166 this.notifier.error(
66357162
C
167 $localize`Search index is unavailable. Retrying with instance results instead.`,
168 $localize`Search error`
5fb2e288
C
169 )
170 this.advancedSearch.searchTarget = 'local'
171 this.search()
172 }
173 )
57c36b27
C
174 }
175
176 onNearOfBottom () {
177 // Last page
178 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
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
be27ef3b
C
194 // Add VideoChannel for typings, but the template already checks "video" argument is a video
195 removeVideoFromArray (video: Video | VideoChannel) {
3a0fb65c
C
196 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
197 }
198
746018f6
C
199 isExternalChannelUrl () {
200 return this.getVideoLinkType() === 'external'
201 }
202
203 getExternalChannelUrl (channel: VideoChannel) {
0bdad52f
C
204 // Same algorithm than videos
205 if (this.getVideoLinkType() === 'external') {
206 return channel.url
207 }
5fb2e288 208
746018f6
C
209 // lazy-load or internal
210 return undefined
211 }
212
213 getInternalChannelUrl (channel: VideoChannel) {
214 const linkType = this.getVideoLinkType()
215
216 if (linkType === 'internal') {
0bdad52f 217 return [ '/video-channels', channel.nameWithHost ]
5fb2e288
C
218 }
219
746018f6
C
220 if (linkType === 'lazy-load') {
221 return [ '/search/lazy-load-channel', { url: channel.url } ]
222 }
223
224 // external
225 return undefined
5fb2e288
C
226 }
227
228 hideActions () {
7c87746e 229 return this.lastSearchTarget === 'search-index'
5fb2e288
C
230 }
231
7afea880 232 private resetPagination () {
57c36b27
C
233 this.pagination.currentPage = 1
234 this.pagination.totalItems = null
aa55a4da 235 this.channelsPerPage = 2
57c36b27 236
26fabbd6 237 this.results = []
57c36b27
C
238 }
239
240 private updateTitle () {
f107470e 241 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
66357162 242 this.metaService.setTitle($localize`Search` + suffix)
57c36b27 243 }
0b18f4aa
C
244
245 private updateUrlFromAdvancedSearch () {
47879669 246 const search = this.currentSearch || undefined
c5d04b4f 247
0b18f4aa
C
248 this.router.navigate([], {
249 relativeTo: this.route,
c5d04b4f 250 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
0b18f4aa
C
251 })
252 }
93cae479
C
253
254 private getVideosObs () {
255 const params = {
256 search: this.currentSearch,
257 componentPagination: this.pagination,
258 advancedSearch: this.advancedSearch
259 }
260
261 return this.hooks.wrapObsFun(
262 this.searchService.searchVideos.bind(this.searchService),
263 params,
e8f902c0 264 'search',
93cae479
C
265 'filter:api.search.videos.list.params',
266 'filter:api.search.videos.list.result'
267 )
268 }
269
270 private getVideoChannelObs () {
44df5c75
C
271 if (!this.currentSearch) return of({ data: [], total: 0 })
272
93cae479
C
273 const params = {
274 search: this.currentSearch,
5fb2e288
C
275 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }),
276 searchTarget: this.advancedSearch.searchTarget
93cae479
C
277 }
278
279 return this.hooks.wrapObsFun(
280 this.searchService.searchVideoChannels.bind(this.searchService),
281 params,
e8f902c0 282 'search',
93cae479
C
283 'filter:api.search.video-channels.list.params',
284 'filter:api.search.video-channels.list.result'
285 )
286 }
57c36b27 287}