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'
14 templateUrl: './menu.component.html',
15 styleUrls: [ './menu.component.scss' ]
17 export class MenuComponent implements OnInit {
18 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
19 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
24 userHasAdminAccess = false
26 languages: VideoConstant<string>[] = []
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'
39 private authService: AuthService,
40 private userService: UserService,
41 private serverService: ServerService,
42 private redirectService: RedirectService,
43 private hotkeysService: HotkeysService,
48 this.serverConfig = this.serverService.getTmpConfig()
49 this.serverService.getConfig()
50 .subscribe(config => this.serverConfig = config)
52 this.isLoggedIn = this.authService.isLoggedIn()
53 if (this.isLoggedIn === true) this.user = this.authService.getUser()
54 this.computeIsUserHasAdminAccess()
56 this.authService.loginChangedSource.subscribe(
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
66 this.computeIsUserHasAdminAccess()
67 console.log('Logged out.')
69 console.error('Unknown auth status: ' + status)
74 this.hotkeysService.cheatSheetToggle.subscribe(isOpen => this.helpVisible = isOpen)
76 this.serverService.getVideoLanguages().subscribe(languages => this.languages = languages)
80 return this.languageChooserModal.getCurrentLanguage()
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)
92 if (!this.user) return
93 switch (this.user.nsfwPolicy) {
95 return this.i18n('hide')
97 return this.i18n('blur')
99 return this.i18n('display')
103 isRegistrationAllowed () {
104 return this.serverConfig.signup.allowed &&
105 this.serverConfig.signup.allowedForCurrentIP
108 getFirstAdminRightAvailable () {
109 const user = this.authService.getUser()
110 if (!user) return undefined
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
121 for (const adminRight of adminRights) {
122 if (user.hasRight(adminRight)) {
130 getFirstAdminRouteAvailable () {
131 const right = this.getFirstAdminRightAvailable()
133 return this.routesPerRight[right]
136 logout (event: Event) {
137 event.preventDefault()
139 this.authService.logout()
140 // Redirect to home page
141 this.redirectService.redirectToHomepage()
144 openLanguageChooser () {
145 this.languageChooserModal.show()
148 openHotkeysCheatSheet () {
149 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
152 openQuickSettings () {
153 this.quickSettingsModal.show()
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())
164 langForLocale (localeId: string) {
165 return this.languages.find(lang => lang.id = localeId).label
168 private computeIsUserHasAdminAccess () {
169 const right = this.getFirstAdminRightAvailable()
171 this.userHasAdminAccess = right !== undefined