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
27 videoLanguages: string[] = []
29 private languages: VideoConstant<string>[] = []
30 private serverConfig: ServerConfig
31 private routesPerRight: { [ role in UserRight ]?: string } = {
32 [UserRight.MANAGE_USERS]: '/admin/users',
33 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
34 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
35 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist',
36 [UserRight.MANAGE_JOBS]: '/admin/jobs',
37 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
41 private authService: AuthService,
42 private userService: UserService,
43 private serverService: ServerService,
44 private redirectService: RedirectService,
45 private hotkeysService: HotkeysService,
50 this.serverConfig = this.serverService.getTmpConfig()
51 this.serverService.getConfig()
52 .subscribe(config => this.serverConfig = config)
54 this.isLoggedIn = this.authService.isLoggedIn()
55 if (this.isLoggedIn === true) this.user = this.authService.getUser()
56 this.computeIsUserHasAdminAccess()
58 this.authService.loginChangedSource.subscribe(
60 if (status === AuthStatus.LoggedIn) {
61 this.isLoggedIn = true
62 this.user = this.authService.getUser()
63 this.computeIsUserHasAdminAccess()
64 console.log('Logged in.')
65 } else if (status === AuthStatus.LoggedOut) {
66 this.isLoggedIn = false
68 this.computeIsUserHasAdminAccess()
69 console.log('Logged out.')
71 console.error('Unknown auth status: ' + status)
76 this.hotkeysService.cheatSheetToggle
77 .subscribe(isOpen => this.helpVisible = isOpen)
79 this.serverService.getVideoLanguages()
80 .subscribe(languages => {
81 this.languages = languages
83 this.authService.userInformationLoaded
84 .subscribe(() => this.buildUserLanguages())
89 return this.languageChooserModal.getCurrentLanguage()
93 if (!this.user) return
95 switch (this.user.nsfwPolicy) {
97 return this.i18n('hide')
100 return this.i18n('blur')
103 return this.i18n('display')
107 isRegistrationAllowed () {
108 return this.serverConfig.signup.allowed &&
109 this.serverConfig.signup.allowedForCurrentIP
112 getFirstAdminRightAvailable () {
113 const user = this.authService.getUser()
114 if (!user) return undefined
116 const adminRights = [
117 UserRight.MANAGE_USERS,
118 UserRight.MANAGE_SERVER_FOLLOW,
119 UserRight.MANAGE_VIDEO_ABUSES,
120 UserRight.MANAGE_VIDEO_BLACKLIST,
121 UserRight.MANAGE_JOBS,
122 UserRight.MANAGE_CONFIGURATION
125 for (const adminRight of adminRights) {
126 if (user.hasRight(adminRight)) {
134 getFirstAdminRouteAvailable () {
135 const right = this.getFirstAdminRightAvailable()
137 return this.routesPerRight[right]
140 logout (event: Event) {
141 event.preventDefault()
143 this.authService.logout()
144 // Redirect to home page
145 this.redirectService.redirectToHomepage()
148 openLanguageChooser () {
149 this.languageChooserModal.show()
152 openHotkeysCheatSheet () {
153 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
156 openQuickSettings () {
157 this.quickSettingsModal.show()
161 if (!this.user) return
162 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
164 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
165 .subscribe(() => this.authService.refreshUserInformation())
168 langForLocale (localeId: string) {
169 return this.languages.find(lang => lang.id === localeId).label
172 private buildUserLanguages () {
174 this.videoLanguages = []
178 if (!this.user.videoLanguages) {
179 this.videoLanguages = [ this.i18n('any language') ]
183 this.videoLanguages = this.user.videoLanguages
184 .map(locale => this.langForLocale(locale))
185 .map(value => value === undefined ? '?' : value)
188 private computeIsUserHasAdminAccess () {
189 const right = this.getFirstAdminRightAvailable()
191 this.userHasAdminAccess = right !== undefined