1 import { HotkeysService } from 'angular2-hotkeys'
2 import * as debug from 'debug'
3 import { forkJoin, Subscription } from 'rxjs'
4 import { first, switchMap } from 'rxjs/operators'
5 import { ViewportScroller } from '@angular/common'
6 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
7 import { Router } from '@angular/router'
20 import { scrollToTop } from '@app/helpers'
21 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
22 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
23 import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
24 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
25 import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models'
27 const debugLogger = debug('peertube:menu:MenuComponent')
31 templateUrl: './menu.component.html',
32 styleUrls: [ './menu.component.scss' ]
34 export class MenuComponent implements OnInit, OnDestroy {
35 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
36 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
37 @ViewChild('dropdown') dropdown: NgbDropdown
42 userHasAdminAccess = false
45 videoLanguages: string[] = []
48 currentInterfaceLanguage: string
50 menuSections: MenuSection[] = []
52 private languages: VideoConstant<string>[] = []
54 private htmlServerConfig: HTMLServerConfig
55 private serverConfig: ServerConfig
57 private routesPerRight: { [role in UserRight]?: string } = {
58 [UserRight.MANAGE_USERS]: '/admin/users',
59 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
60 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
61 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
62 [UserRight.MANAGE_JOBS]: '/admin/jobs',
63 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
66 private languagesSub: Subscription
67 private modalSub: Subscription
68 private hotkeysSub: Subscription
69 private authSub: Subscription
72 private viewportScroller: ViewportScroller,
73 private authService: AuthService,
74 private userService: UserService,
75 private serverService: ServerService,
76 private redirectService: RedirectService,
77 private hotkeysService: HotkeysService,
78 private screenService: ScreenService,
79 private menuService: MenuService,
80 private modalService: PeertubeModalService,
81 private router: Router,
82 private hooks: HooksService
85 get isInMobileView () {
86 return this.screenService.isInMobileView()
90 return this.languageChooserModal.getCurrentLanguage()
93 get requiresApproval () {
94 return this.serverConfig.signup.requiresApproval
98 this.htmlServerConfig = this.serverService.getHTMLConfig()
99 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
101 this.isLoggedIn = this.authService.isLoggedIn()
102 this.updateUserState()
103 this.buildMenuSections()
105 this.authSub = this.authService.loginChangedSource.subscribe(status => {
106 if (status === AuthStatus.LoggedIn) {
107 this.isLoggedIn = true
108 } else if (status === AuthStatus.LoggedOut) {
109 this.isLoggedIn = false
112 this.updateUserState()
113 this.buildMenuSections()
116 this.hotkeysSub = this.hotkeysService.cheatSheetToggle
117 .subscribe(isOpen => this.helpVisible = isOpen)
119 this.languagesSub = forkJoin([
120 this.serverService.getVideoLanguages(),
121 this.authService.userInformationLoaded.pipe(first())
122 ]).subscribe(([ languages ]) => {
123 this.languages = languages
125 this.buildUserLanguages()
128 this.serverService.getConfig()
129 .subscribe(config => this.serverConfig = config)
131 this.modalSub = this.modalService.openQuickSettingsSubject
132 .subscribe(() => this.openQuickSettings())
136 if (this.modalSub) this.modalSub.unsubscribe()
137 if (this.languagesSub) this.languagesSub.unsubscribe()
138 if (this.hotkeysSub) this.hotkeysSub.unsubscribe()
139 if (this.authSub) this.authSub.unsubscribe()
142 isRegistrationAllowed () {
143 if (!this.serverConfig) return false
145 return this.serverConfig.signup.allowed &&
146 this.serverConfig.signup.allowedForCurrentIP
149 getFirstAdminRightAvailable () {
150 const user = this.authService.getUser()
151 if (!user) return undefined
153 const adminRights = [
154 UserRight.MANAGE_USERS,
155 UserRight.MANAGE_SERVER_FOLLOW,
156 UserRight.MANAGE_ABUSES,
157 UserRight.MANAGE_VIDEO_BLACKLIST,
158 UserRight.MANAGE_JOBS,
159 UserRight.MANAGE_CONFIGURATION
162 for (const adminRight of adminRights) {
163 if (user.hasRight(adminRight)) {
171 getFirstAdminRouteAvailable () {
172 const right = this.getFirstAdminRightAvailable()
174 return this.routesPerRight[right]
177 logout (event: Event) {
178 event.preventDefault()
180 this.authService.logout()
181 // Redirect to home page
182 this.redirectService.redirectToHomepage()
185 openLanguageChooser () {
186 this.languageChooserModal.show()
189 openHotkeysCheatSheet () {
190 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
193 openQuickSettings () {
194 this.quickSettingsModal.show()
198 if (!this.user) return
199 this.user.p2pEnabled = !this.user.p2pEnabled
201 this.userService.updateMyProfile({ p2pEnabled: this.user.p2pEnabled })
202 .subscribe(() => this.authService.refreshUserInformation())
205 langForLocale (localeId: string) {
206 if (localeId === '_unknown') return $localize`Unknown`
208 return this.languages.find(lang => lang.id === localeId).label
211 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
212 const linkURL = link.getAttribute('href')
213 const linkHash = link.getAttribute('fragment')
215 // On same url without fragment restore top scroll position
216 if (!linkHash && this.router.url.includes(linkURL)) {
217 scrollToTop('smooth')
220 // On same url with fragment restore anchor scroll position
221 if (linkHash && this.router.url === linkURL) {
222 this.viewportScroller.scrollToAnchor(linkHash)
225 if (this.screenService.isInSmallView()) {
226 this.menuService.toggleMenu()
230 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
231 onMenuScrollEvent () {
232 document.querySelector('menu').scrollTo(0, 0)
235 onDropdownOpenChange (opened: boolean) {
236 if (this.screenService.isInMobileView()) return
238 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
239 const onWindowScroll = () => {
240 this.dropdown?.close()
241 window.removeEventListener('scroll', onWindowScroll)
245 window.addEventListener('scroll', onWindowScroll)
246 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
247 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
249 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
253 private async buildMenuSections () {
254 const menuSections = []
256 if (this.isLoggedIn) {
258 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
263 this.menuService.buildCommonLinks(this.htmlServerConfig)
266 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
269 private buildUserLanguages () {
271 this.videoLanguages = []
275 if (!this.user.videoLanguages) {
276 this.videoLanguages = [ $localize`any language` ]
280 this.videoLanguages = this.user.videoLanguages
281 .map(locale => this.langForLocale(locale))
282 .map(value => value === undefined ? '?' : value)
285 private computeAdminAccess () {
286 const right = this.getFirstAdminRightAvailable()
288 this.userHasAdminAccess = right !== undefined
291 private computeVideosLink () {
292 if (!this.isLoggedIn) return
294 this.authService.userInformationLoaded
296 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
298 if (res === true) debugLogger('User can see videos link.')
299 else debugLogger('User cannot see videos link.')
303 private computeNSFWPolicy () {
305 this.nsfwPolicy = null
309 switch (this.user.nsfwPolicy) {
311 this.nsfwPolicy = $localize`hide`
315 this.nsfwPolicy = $localize`blur`
319 this.nsfwPolicy = $localize`display`
324 private updateUserState () {
325 this.user = this.isLoggedIn
326 ? this.authService.getUser()
329 this.computeAdminAccess()
330 this.computeNSFWPolicy()
331 this.computeVideosLink()