]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/header/header.component.ts
Add informational message at login for visitors coming from upload button
[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 doSearch () {
46 const queryParams: Params = {}
47
48 if (window.location.pathname === '/search' && this.route.snapshot.queryParams) {
49 Object.assign(queryParams, this.route.snapshot.queryParams)
50 }
51
52 Object.assign(queryParams, { search: this.searchValue })
53
54 const o = this.auth.isLoggedIn()
55 ? this.loadUserLanguagesIfNeeded(queryParams)
56 : of(true)
57
58 o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
59 }
60
61 isUserLoggedIn () {
62 return this.authService.isLoggedIn()
63 }
64
65 isRegistrationAllowed () {
66 return this.serverConfig.signup.allowed &&
67 this.serverConfig.signup.allowedForCurrentIP
68 }
69
70 goToUpload () {
71 if (this.isUserLoggedIn()) {
72 this.router.navigate([ '/videos/upload' ])
73 } else if (this.isRegistrationAllowed()) {
74 this.router.navigate([ '/signup' ])
75 } else {
76 this.router.navigate([ '/login', { fromUpload: true } ])
77 }
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 }