1 import { HotkeysService } from 'angular2-hotkeys'
2 import * as debug from 'debug'
3 import { switchMap } from 'rxjs/operators'
4 import { Component, OnInit, ViewChild } from '@angular/core'
5 import { AuthService, AuthStatus, AuthUser, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
6 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
7 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { ServerConfig, UserRight, VideoConstant } from '@shared/models'
11 const logger = debug('peertube:menu:MenuComponent')
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_ABUSES]: '/admin/moderation/abuses',
36 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
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) {
70 this.user = this.authService.getUser()
71 this.computeVideosLink()
74 this.computeAdminAccess()
76 this.authService.loginChangedSource.subscribe(
78 if (status === AuthStatus.LoggedIn) {
79 this.isLoggedIn = true
80 this.user = this.authService.getUser()
82 this.computeAdminAccess()
83 this.computeVideosLink()
86 } else if (status === AuthStatus.LoggedOut) {
87 this.isLoggedIn = false
90 this.computeAdminAccess()
94 console.error('Unknown auth status: ' + status)
99 this.hotkeysService.cheatSheetToggle
100 .subscribe(isOpen => this.helpVisible = isOpen)
102 this.serverService.getVideoLanguages()
103 .subscribe(languages => {
104 this.languages = languages
106 this.authService.userInformationLoaded
107 .subscribe(() => this.buildUserLanguages())
112 return this.languageChooserModal.getCurrentLanguage()
116 if (!this.user) return
118 switch (this.user.nsfwPolicy) {
120 return this.i18n('hide')
123 return this.i18n('blur')
126 return this.i18n('display')
130 isRegistrationAllowed () {
131 return this.serverConfig.signup.allowed &&
132 this.serverConfig.signup.allowedForCurrentIP
135 getFirstAdminRightAvailable () {
136 const user = this.authService.getUser()
137 if (!user) return undefined
139 const adminRights = [
140 UserRight.MANAGE_USERS,
141 UserRight.MANAGE_SERVER_FOLLOW,
142 UserRight.MANAGE_ABUSES,
143 UserRight.MANAGE_VIDEO_BLACKLIST,
144 UserRight.MANAGE_JOBS,
145 UserRight.MANAGE_CONFIGURATION
148 for (const adminRight of adminRights) {
149 if (user.hasRight(adminRight)) {
157 getFirstAdminRouteAvailable () {
158 const right = this.getFirstAdminRightAvailable()
160 return this.routesPerRight[right]
163 logout (event: Event) {
164 event.preventDefault()
166 this.authService.logout()
167 // Redirect to home page
168 this.redirectService.redirectToHomepage()
171 openLanguageChooser () {
172 this.languageChooserModal.show()
175 openHotkeysCheatSheet () {
176 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
179 openQuickSettings () {
180 this.quickSettingsModal.show()
184 if (!this.user) return
185 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
187 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
188 .subscribe(() => this.authService.refreshUserInformation())
191 langForLocale (localeId: string) {
192 if (localeId === '_unknown') return this.i18n('Unknown')
194 return this.languages.find(lang => lang.id === localeId).label
197 private buildUserLanguages () {
199 this.videoLanguages = []
203 if (!this.user.videoLanguages) {
204 this.videoLanguages = [this.i18n('any language')]
208 this.videoLanguages = this.user.videoLanguages
209 .map(locale => this.langForLocale(locale))
210 .map(value => value === undefined ? '?' : value)
213 private computeAdminAccess () {
214 const right = this.getFirstAdminRightAvailable()
216 this.userHasAdminAccess = right !== undefined
219 private computeVideosLink () {
220 this.authService.userInformationLoaded
222 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
224 if (res === true) logger('User can see videos link.')
225 else logger('User cannot see videos link.')