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 { ServerConfig, UserRight, VideoConstant } from '@shared/models'
10 const logger = debug('peertube:menu:MenuComponent')
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_ABUSES]: '/admin/moderation/abuses',
35 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
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,
46 private screenService: ScreenService
49 get isInMobileView () {
50 return this.screenService.isInMobileView()
54 if (this.isInMobileView) {
55 return 'left-top auto'
57 return 'right-top auto'
62 this.serverConfig = this.serverService.getTmpConfig()
63 this.serverService.getConfig()
64 .subscribe(config => this.serverConfig = config)
66 this.isLoggedIn = this.authService.isLoggedIn()
67 if (this.isLoggedIn === true) {
68 this.user = this.authService.getUser()
69 this.computeVideosLink()
72 this.computeAdminAccess()
74 this.authService.loginChangedSource.subscribe(
76 if (status === AuthStatus.LoggedIn) {
77 this.isLoggedIn = true
78 this.user = this.authService.getUser()
80 this.computeAdminAccess()
81 this.computeVideosLink()
84 } else if (status === AuthStatus.LoggedOut) {
85 this.isLoggedIn = false
88 this.computeAdminAccess()
92 console.error('Unknown auth status: ' + status)
97 this.hotkeysService.cheatSheetToggle
98 .subscribe(isOpen => this.helpVisible = isOpen)
100 this.serverService.getVideoLanguages()
101 .subscribe(languages => {
102 this.languages = languages
104 this.authService.userInformationLoaded
105 .subscribe(() => this.buildUserLanguages())
110 return this.languageChooserModal.getCurrentLanguage()
114 if (!this.user) return
116 switch (this.user.nsfwPolicy) {
118 return $localize`hide`
121 return $localize`blur`
124 return $localize`display`
128 isRegistrationAllowed () {
129 return this.serverConfig.signup.allowed &&
130 this.serverConfig.signup.allowedForCurrentIP
133 getFirstAdminRightAvailable () {
134 const user = this.authService.getUser()
135 if (!user) return undefined
137 const adminRights = [
138 UserRight.MANAGE_USERS,
139 UserRight.MANAGE_SERVER_FOLLOW,
140 UserRight.MANAGE_ABUSES,
141 UserRight.MANAGE_VIDEO_BLACKLIST,
142 UserRight.MANAGE_JOBS,
143 UserRight.MANAGE_CONFIGURATION
146 for (const adminRight of adminRights) {
147 if (user.hasRight(adminRight)) {
155 getFirstAdminRouteAvailable () {
156 const right = this.getFirstAdminRightAvailable()
158 return this.routesPerRight[right]
161 logout (event: Event) {
162 event.preventDefault()
164 this.authService.logout()
165 // Redirect to home page
166 this.redirectService.redirectToHomepage()
169 openLanguageChooser () {
170 this.languageChooserModal.show()
173 openHotkeysCheatSheet () {
174 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
177 openQuickSettings () {
178 this.quickSettingsModal.show()
182 if (!this.user) return
183 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
185 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
186 .subscribe(() => this.authService.refreshUserInformation())
189 langForLocale (localeId: string) {
190 if (localeId === '_unknown') return $localize`Unknown`
192 return this.languages.find(lang => lang.id === localeId).label
195 private buildUserLanguages () {
197 this.videoLanguages = []
201 if (!this.user.videoLanguages) {
202 this.videoLanguages = [$localize`any language`]
206 this.videoLanguages = this.user.videoLanguages
207 .map(locale => this.langForLocale(locale))
208 .map(value => value === undefined ? '?' : value)
211 private computeAdminAccess () {
212 const right = this.getFirstAdminRightAvailable()
214 this.userHasAdminAccess = right !== undefined
217 private computeVideosLink () {
218 this.authService.userInformationLoaded
220 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
222 if (res === true) logger('User can see videos link.')
223 else logger('User cannot see videos link.')