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 import { ScreenService } from '@app/shared/misc/screen.service'
15 templateUrl: './menu.component.html',
16 styleUrls: [ './menu.component.scss' ]
18 export class MenuComponent implements OnInit {
19 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
20 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
25 userHasAdminAccess = false
28 videoLanguages: string[] = []
30 private languages: VideoConstant<string>[] = []
31 private serverConfig: ServerConfig
32 private routesPerRight: { [ role in UserRight ]?: string } = {
33 [UserRight.MANAGE_USERS]: '/admin/users',
34 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
35 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
36 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist',
37 [UserRight.MANAGE_JOBS]: '/admin/jobs',
38 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
42 private authService: AuthService,
43 private userService: UserService,
44 private serverService: ServerService,
45 private redirectService: RedirectService,
46 private hotkeysService: HotkeysService,
47 private screenService: ScreenService,
51 get isInMobileView () {
52 return this.screenService.isInMobileView()
56 if (this.isInMobileView) {
57 return 'left-top auto'
59 return 'right-top auto'
64 this.serverConfig = this.serverService.getTmpConfig()
65 this.serverService.getConfig()
66 .subscribe(config => this.serverConfig = config)
68 this.isLoggedIn = this.authService.isLoggedIn()
69 if (this.isLoggedIn === true) this.user = this.authService.getUser()
70 this.computeIsUserHasAdminAccess()
72 this.authService.loginChangedSource.subscribe(
74 if (status === AuthStatus.LoggedIn) {
75 this.isLoggedIn = true
76 this.user = this.authService.getUser()
77 this.computeIsUserHasAdminAccess()
78 console.log('Logged in.')
79 } else if (status === AuthStatus.LoggedOut) {
80 this.isLoggedIn = false
82 this.computeIsUserHasAdminAccess()
83 console.log('Logged out.')
85 console.error('Unknown auth status: ' + status)
90 this.hotkeysService.cheatSheetToggle
91 .subscribe(isOpen => this.helpVisible = isOpen)
93 this.serverService.getVideoLanguages()
94 .subscribe(languages => {
95 this.languages = languages
97 this.authService.userInformationLoaded
98 .subscribe(() => this.buildUserLanguages())
103 return this.languageChooserModal.getCurrentLanguage()
107 if (!this.user) return
109 switch (this.user.nsfwPolicy) {
111 return this.i18n('hide')
114 return this.i18n('blur')
117 return this.i18n('display')
121 isRegistrationAllowed () {
122 return this.serverConfig.signup.allowed &&
123 this.serverConfig.signup.allowedForCurrentIP
126 getFirstAdminRightAvailable () {
127 const user = this.authService.getUser()
128 if (!user) return undefined
130 const adminRights = [
131 UserRight.MANAGE_USERS,
132 UserRight.MANAGE_SERVER_FOLLOW,
133 UserRight.MANAGE_VIDEO_ABUSES,
134 UserRight.MANAGE_VIDEO_BLACKLIST,
135 UserRight.MANAGE_JOBS,
136 UserRight.MANAGE_CONFIGURATION
139 for (const adminRight of adminRights) {
140 if (user.hasRight(adminRight)) {
148 getFirstAdminRouteAvailable () {
149 const right = this.getFirstAdminRightAvailable()
151 return this.routesPerRight[right]
154 logout (event: Event) {
155 event.preventDefault()
157 this.authService.logout()
158 // Redirect to home page
159 this.redirectService.redirectToHomepage()
162 openLanguageChooser () {
163 this.languageChooserModal.show()
166 openHotkeysCheatSheet () {
167 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
170 openQuickSettings () {
171 this.quickSettingsModal.show()
175 if (!this.user) return
176 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
178 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
179 .subscribe(() => this.authService.refreshUserInformation())
182 langForLocale (localeId: string) {
183 if (localeId === '_unknown') return this.i18n('Unknown')
185 return this.languages.find(lang => lang.id === localeId).label
188 private buildUserLanguages () {
190 this.videoLanguages = []
194 if (!this.user.videoLanguages) {
195 this.videoLanguages = [ this.i18n('any language') ]
199 this.videoLanguages = this.user.videoLanguages
200 .map(locale => this.langForLocale(locale))
201 .map(value => value === undefined ? '?' : value)
204 private computeIsUserHasAdminAccess () {
205 const right = this.getFirstAdminRightAvailable()
207 this.userHasAdminAccess = right !== undefined