]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/menu.component.ts
Try to improve infinite pagination
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { UserRight } from '../../../../shared/models/users/user-right.enum'
3 import { AuthService, AuthStatus, RedirectService, ServerService, ThemeService } from '../core'
4 import { User } from '../shared/users/user.model'
5 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
6
7 @Component({
8 selector: 'my-menu',
9 templateUrl: './menu.component.html',
10 styleUrls: [ './menu.component.scss' ]
11 })
12 export class MenuComponent implements OnInit {
13 @ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
14
15 user: User
16 isLoggedIn: boolean
17 userHasAdminAccess = false
18
19 private routesPerRight = {
20 [UserRight.MANAGE_USERS]: '/admin/users',
21 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
22 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses',
23 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist'
24 }
25
26 constructor (
27 private authService: AuthService,
28 private serverService: ServerService,
29 private redirectService: RedirectService,
30 private themeService: ThemeService
31 ) {}
32
33 ngOnInit () {
34 this.isLoggedIn = this.authService.isLoggedIn()
35 if (this.isLoggedIn === true) this.user = this.authService.getUser()
36 this.computeIsUserHasAdminAccess()
37
38 this.authService.loginChangedSource.subscribe(
39 status => {
40 if (status === AuthStatus.LoggedIn) {
41 this.isLoggedIn = true
42 this.user = this.authService.getUser()
43 this.computeIsUserHasAdminAccess()
44 console.log('Logged in.')
45 } else if (status === AuthStatus.LoggedOut) {
46 this.isLoggedIn = false
47 this.user = undefined
48 this.computeIsUserHasAdminAccess()
49 console.log('Logged out.')
50 } else {
51 console.error('Unknown auth status: ' + status)
52 }
53 }
54 )
55 }
56
57 isRegistrationAllowed () {
58 return this.serverService.getConfig().signup.allowed &&
59 this.serverService.getConfig().signup.allowedForCurrentIP
60 }
61
62 getFirstAdminRightAvailable () {
63 const user = this.authService.getUser()
64 if (!user) return undefined
65
66 const adminRights = [
67 UserRight.MANAGE_USERS,
68 UserRight.MANAGE_SERVER_FOLLOW,
69 UserRight.MANAGE_VIDEO_ABUSES,
70 UserRight.MANAGE_VIDEO_BLACKLIST
71 ]
72
73 for (const adminRight of adminRights) {
74 if (user.hasRight(adminRight)) {
75 return adminRight
76 }
77 }
78
79 return undefined
80 }
81
82 getFirstAdminRouteAvailable () {
83 const right = this.getFirstAdminRightAvailable()
84
85 return this.routesPerRight[right]
86 }
87
88 logout (event: Event) {
89 event.preventDefault()
90
91 this.authService.logout()
92 // Redirect to home page
93 this.redirectService.redirectToHomepage()
94 }
95
96 openLanguageChooser () {
97 this.languageChooserModal.show()
98 }
99
100 toggleDarkTheme () {
101 this.themeService.toggleDarkTheme()
102 }
103
104 private computeIsUserHasAdminAccess () {
105 const right = this.getFirstAdminRightAvailable()
106
107 this.userHasAdminAccess = right !== undefined
108 }
109 }