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 { Router } from '@angular/router'
6 import { AuthService, AuthStatus, AuthUser, MenuService, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
7 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
8 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
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,
48 private menuService: MenuService,
49 private router: Router
52 get isInMobileView () {
53 return this.screenService.isInMobileView()
57 if (this.isInMobileView) {
58 return 'left-top auto'
60 return 'right-top auto'
65 return this.languageChooserModal.getCurrentLanguage()
69 if (!this.user) return
71 switch (this.user.nsfwPolicy) {
73 return $localize`hide`
76 return $localize`blur`
79 return $localize`display`
84 return this.serverConfig.instance.name
88 this.serverConfig = this.serverService.getTmpConfig()
89 this.serverService.getConfig()
90 .subscribe(config => this.serverConfig = config)
92 this.isLoggedIn = this.authService.isLoggedIn()
93 if (this.isLoggedIn === true) {
94 this.user = this.authService.getUser()
95 this.computeVideosLink()
98 this.computeAdminAccess()
100 this.authService.loginChangedSource.subscribe(
102 if (status === AuthStatus.LoggedIn) {
103 this.isLoggedIn = true
104 this.user = this.authService.getUser()
106 this.computeAdminAccess()
107 this.computeVideosLink()
110 } else if (status === AuthStatus.LoggedOut) {
111 this.isLoggedIn = false
112 this.user = undefined
114 this.computeAdminAccess()
116 logger('Logged out.')
118 console.error('Unknown auth status: ' + status)
123 this.hotkeysService.cheatSheetToggle
124 .subscribe(isOpen => this.helpVisible = isOpen)
126 this.serverService.getVideoLanguages()
127 .subscribe(languages => {
128 this.languages = languages
130 this.authService.userInformationLoaded
131 .subscribe(() => this.buildUserLanguages())
135 isRegistrationAllowed () {
136 return this.serverConfig.signup.allowed &&
137 this.serverConfig.signup.allowedForCurrentIP
140 getFirstAdminRightAvailable () {
141 const user = this.authService.getUser()
142 if (!user) return undefined
144 const adminRights = [
145 UserRight.MANAGE_USERS,
146 UserRight.MANAGE_SERVER_FOLLOW,
147 UserRight.MANAGE_ABUSES,
148 UserRight.MANAGE_VIDEO_BLACKLIST,
149 UserRight.MANAGE_JOBS,
150 UserRight.MANAGE_CONFIGURATION
153 for (const adminRight of adminRights) {
154 if (user.hasRight(adminRight)) {
162 getFirstAdminRouteAvailable () {
163 const right = this.getFirstAdminRightAvailable()
165 return this.routesPerRight[right]
168 logout (event: Event) {
169 event.preventDefault()
171 this.authService.logout()
172 // Redirect to home page
173 this.redirectService.redirectToHomepage()
176 openLanguageChooser () {
177 this.languageChooserModal.show()
180 openHotkeysCheatSheet () {
181 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
184 openQuickSettings () {
185 this.quickSettingsModal.show()
189 if (!this.user) return
190 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
192 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
193 .subscribe(() => this.authService.refreshUserInformation())
196 langForLocale (localeId: string) {
197 if (localeId === '_unknown') return $localize`Unknown`
199 return this.languages.find(lang => lang.id === localeId).label
202 onSameUrlRestoreScrollPosition (link: HTMLAnchorElement) {
203 const linkURL = link.getAttribute('href')
204 const linkHash = link.getAttribute('fragment')
206 // On same url without fragment restore top scroll position
207 if (!linkHash && this.router.url.includes(linkURL)) {
215 // On same url with fragment restore anchor scroll position
216 if (linkHash && this.router.url === linkURL) {
217 const anchor = document.getElementById(link.getAttribute('fragment'))
218 anchor.scrollIntoView({ behavior: 'smooth', inline: 'nearest' })
221 if (this.screenService.isInSmallView()) {
222 this.menuService.toggleMenu()
226 private buildUserLanguages () {
228 this.videoLanguages = []
232 if (!this.user.videoLanguages) {
233 this.videoLanguages = [$localize`any language`]
237 this.videoLanguages = this.user.videoLanguages
238 .map(locale => this.langForLocale(locale))
239 .map(value => value === undefined ? '?' : value)
242 private computeAdminAccess () {
243 const right = this.getFirstAdminRightAvailable()
245 this.userHasAdminAccess = right !== undefined
248 private computeVideosLink () {
249 this.authService.userInformationLoaded
251 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
253 if (res === true) logger('User can see videos link.')
254 else logger('User cannot see videos link.')