]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/header/header.component.ts
5805356691ec261bf1f423ad97cf36140cb14239
[github/Chocobozzz/PeerTube.git] / client / src / app / header / header.component.ts
1 import { filter, first, map, tap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, NavigationEnd, Params, Router } from '@angular/router'
4 import { getParameterByName } from '../shared/misc/utils'
5 import { AuthService, ServerService, Notifier } from '@app/core'
6 import { of } from 'rxjs'
7 import { ServerConfig } from '@shared/models'
8
9 @Component({
10 selector: 'my-header',
11 templateUrl: './header.component.html',
12 styleUrls: [ './header.component.scss' ]
13 })
14
15 export class HeaderComponent implements OnInit {
16 searchValue = ''
17
18 private serverConfig: ServerConfig
19
20 constructor (
21 private router: Router,
22 private route: ActivatedRoute,
23 private auth: AuthService,
24 private serverService: ServerService,
25 private authService: AuthService,
26 private notifier: Notifier
27 ) {}
28
29 ngOnInit () {
30 this.router.events
31 .pipe(
32 filter(e => e instanceof NavigationEnd),
33 map(() => getParameterByName('search', window.location.href))
34 )
35 .subscribe(searchQuery => this.searchValue = searchQuery || '')
36
37 this.serverConfig = this.serverService.getTmpConfig()
38 this.serverService.getConfig().subscribe(
39 config => this.serverConfig = config,
40
41 err => this.notifier.error(err.message)
42 )
43 }
44
45 get routerLink () {
46 if (this.isUserLoggedIn()) {
47 return [ '/videos/upload' ]
48 } else if (this.isRegistrationAllowed()) {
49 return [ '/signup' ]
50 } else {
51 return [ '/login', { fromUpload: true } ]
52 }
53 }
54
55 doSearch () {
56 const queryParams: Params = {}
57
58 if (window.location.pathname === '/search' && this.route.snapshot.queryParams) {
59 Object.assign(queryParams, this.route.snapshot.queryParams)
60 }
61
62 Object.assign(queryParams, { search: this.searchValue })
63
64 const o = this.auth.isLoggedIn()
65 ? this.loadUserLanguagesIfNeeded(queryParams)
66 : of(true)
67
68 o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
69 }
70
71 isUserLoggedIn () {
72 return this.authService.isLoggedIn()
73 }
74
75 isRegistrationAllowed () {
76 return this.serverConfig.signup.allowed &&
77 this.serverConfig.signup.allowedForCurrentIP
78 }
79
80 private loadUserLanguagesIfNeeded (queryParams: any) {
81 if (queryParams && queryParams.languageOneOf) return of(queryParams)
82
83 return this.auth.userInformationLoaded
84 .pipe(
85 first(),
86 tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages }))
87 )
88 }
89 }