]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/menu.component.ts
5f3dfc52a599acd87ebfb5faecd751090405326b
[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 } from '../core'
4 import { User } from '@app/shared/users/user.model'
5 import { UserService } from '@app/shared/users/user.service'
6 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
7 import { HotkeysService } from 'angular2-hotkeys'
8 import { ServerConfig, VideoConstant } from '@shared/models'
9 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11
12 @Component({
13 selector: 'my-menu',
14 templateUrl: './menu.component.html',
15 styleUrls: [ './menu.component.scss' ]
16 })
17 export class MenuComponent implements OnInit {
18 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
19 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
20
21 user: User
22 isLoggedIn: boolean
23
24 userHasAdminAccess = false
25 helpVisible = false
26 languages: VideoConstant<string>[] = []
27
28 private serverConfig: ServerConfig
29 private routesPerRight: { [ role in UserRight ]?: string } = {
30 [UserRight.MANAGE_USERS]: '/admin/users',
31 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
32 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
33 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist',
34 [UserRight.MANAGE_JOBS]: '/admin/jobs',
35 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
36 }
37
38 constructor (
39 private authService: AuthService,
40 private userService: UserService,
41 private serverService: ServerService,
42 private redirectService: RedirectService,
43 private hotkeysService: HotkeysService,
44 private i18n: I18n
45 ) {}
46
47 ngOnInit () {
48 this.serverConfig = this.serverService.getTmpConfig()
49 this.serverService.getConfig()
50 .subscribe(config => this.serverConfig = config)
51
52 this.isLoggedIn = this.authService.isLoggedIn()
53 if (this.isLoggedIn === true) this.user = this.authService.getUser()
54 this.computeIsUserHasAdminAccess()
55
56 this.authService.loginChangedSource.subscribe(
57 status => {
58 if (status === AuthStatus.LoggedIn) {
59 this.isLoggedIn = true
60 this.user = this.authService.getUser()
61 this.computeIsUserHasAdminAccess()
62 console.log('Logged in.')
63 } else if (status === AuthStatus.LoggedOut) {
64 this.isLoggedIn = false
65 this.user = undefined
66 this.computeIsUserHasAdminAccess()
67 console.log('Logged out.')
68 } else {
69 console.error('Unknown auth status: ' + status)
70 }
71 }
72 )
73
74 this.hotkeysService.cheatSheetToggle.subscribe(isOpen => this.helpVisible = isOpen)
75
76 this.serverService.getVideoLanguages().subscribe(languages => this.languages = languages)
77 }
78
79 get language () {
80 return this.languageChooserModal.getCurrentLanguage()
81 }
82
83 get videoLanguages (): string[] {
84 if (!this.user) return
85 if (!this.user.videoLanguages) return [this.i18n('any language')]
86 return this.user.videoLanguages
87 .map(locale => this.langForLocale(locale))
88 .map(value => value === undefined ? '?' : value)
89 }
90
91 get nsfwPolicy () {
92 if (!this.user) return
93 switch (this.user.nsfwPolicy) {
94 case 'do_not_list':
95 return this.i18n('hide')
96 case 'blur':
97 return this.i18n('blur')
98 case 'display':
99 return this.i18n('display')
100 }
101 }
102
103 isRegistrationAllowed () {
104 return this.serverConfig.signup.allowed &&
105 this.serverConfig.signup.allowedForCurrentIP
106 }
107
108 getFirstAdminRightAvailable () {
109 const user = this.authService.getUser()
110 if (!user) return undefined
111
112 const adminRights = [
113 UserRight.MANAGE_USERS,
114 UserRight.MANAGE_SERVER_FOLLOW,
115 UserRight.MANAGE_VIDEO_ABUSES,
116 UserRight.MANAGE_VIDEO_BLACKLIST,
117 UserRight.MANAGE_JOBS,
118 UserRight.MANAGE_CONFIGURATION
119 ]
120
121 for (const adminRight of adminRights) {
122 if (user.hasRight(adminRight)) {
123 return adminRight
124 }
125 }
126
127 return undefined
128 }
129
130 getFirstAdminRouteAvailable () {
131 const right = this.getFirstAdminRightAvailable()
132
133 return this.routesPerRight[right]
134 }
135
136 logout (event: Event) {
137 event.preventDefault()
138
139 this.authService.logout()
140 // Redirect to home page
141 this.redirectService.redirectToHomepage()
142 }
143
144 openLanguageChooser () {
145 this.languageChooserModal.show()
146 }
147
148 openHotkeysCheatSheet () {
149 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
150 }
151
152 openQuickSettings () {
153 this.quickSettingsModal.show()
154 }
155
156 toggleUseP2P () {
157 if (!this.user) return
158 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
159 this.userService.updateMyProfile({
160 webTorrentEnabled: this.user.webTorrentEnabled
161 }).subscribe(() => this.authService.refreshUserInformation())
162 }
163
164 langForLocale(localeId: string) {
165 return this.languages.find(lang => lang.id = localeId).label
166 }
167
168 private computeIsUserHasAdminAccess () {
169 const right = this.getFirstAdminRightAvailable()
170
171 this.userHasAdminAccess = right !== undefined
172 }
173 }