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()
89 get dropdownContainer () {
90 if (this.isInMobileView) return null
92 return 'body' as 'body'
96 return this.languageChooserModal.getCurrentLanguage()
99 get requiresApproval () {
100 return this.serverConfig.signup.requiresApproval
104 this.htmlServerConfig = this.serverService.getHTMLConfig()
105 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
107 this.isLoggedIn = this.authService.isLoggedIn()
108 this.updateUserState()
109 this.buildMenuSections()
111 this.authSub = this.authService.loginChangedSource.subscribe(status => {
112 if (status === AuthStatus.LoggedIn) {
113 this.isLoggedIn = true
114 } else if (status === AuthStatus.LoggedOut) {
115 this.isLoggedIn = false
118 this.updateUserState()
119 this.buildMenuSections()
122 this.hotkeysSub = this.hotkeysService.cheatSheetToggle
123 .subscribe(isOpen => this.helpVisible = isOpen)
125 this.languagesSub = forkJoin([
126 this.serverService.getVideoLanguages(),
127 this.authService.userInformationLoaded.pipe(first())
128 ]).subscribe(([ languages ]) => {
129 this.languages = languages
131 this.buildUserLanguages()
134 this.serverService.getConfig()
135 .subscribe(config => this.serverConfig = config)
137 this.modalSub = this.modalService.openQuickSettingsSubject
138 .subscribe(() => this.openQuickSettings())
142 if (this.modalSub) this.modalSub.unsubscribe()
143 if (this.languagesSub) this.languagesSub.unsubscribe()
144 if (this.hotkeysSub) this.hotkeysSub.unsubscribe()
145 if (this.authSub) this.authSub.unsubscribe()
148 isRegistrationAllowed () {
149 if (!this.serverConfig) return false
151 return this.serverConfig.signup.allowed &&
152 this.serverConfig.signup.allowedForCurrentIP
155 getFirstAdminRightAvailable () {
156 const user = this.authService.getUser()
157 if (!user) return undefined
159 const adminRights = [
160 UserRight.MANAGE_USERS,
161 UserRight.MANAGE_SERVER_FOLLOW,
162 UserRight.MANAGE_ABUSES,
163 UserRight.MANAGE_VIDEO_BLACKLIST,
164 UserRight.MANAGE_JOBS,
165 UserRight.MANAGE_CONFIGURATION
168 for (const adminRight of adminRights) {
169 if (user.hasRight(adminRight)) {
177 getFirstAdminRouteAvailable () {
178 const right = this.getFirstAdminRightAvailable()
180 return this.routesPerRight[right]
183 logout (event: Event) {
184 event.preventDefault()
186 this.authService.logout()
187 // Redirect to home page
188 this.redirectService.redirectToHomepage()
191 openLanguageChooser () {
192 this.languageChooserModal.show()
195 openHotkeysCheatSheet () {
196 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
199 openQuickSettings () {
200 this.quickSettingsModal.show()
204 if (!this.user) return
205 this.user.p2pEnabled = !this.user.p2pEnabled
207 this.userService.updateMyProfile({ p2pEnabled: this.user.p2pEnabled })
208 .subscribe(() => this.authService.refreshUserInformation())
211 langForLocale (localeId: string) {
212 if (localeId === '_unknown') return $localize`Unknown`
214 return this.languages.find(lang => lang.id === localeId).label
217 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
218 const linkURL = link.getAttribute('href')
219 const linkHash = link.getAttribute('fragment')
221 // On same url without fragment restore top scroll position
222 if (!linkHash && this.router.url.includes(linkURL)) {
223 scrollToTop('smooth')
226 // On same url with fragment restore anchor scroll position
227 if (linkHash && this.router.url === linkURL) {
228 this.viewportScroller.scrollToAnchor(linkHash)
231 if (this.screenService.isInSmallView()) {
232 this.menuService.toggleMenu()
236 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
237 onMenuScrollEvent () {
238 document.querySelector('menu').scrollTo(0, 0)
241 onDropdownOpenChange (opened: boolean) {
242 if (this.screenService.isInMobileView()) return
244 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
245 const onWindowScroll = () => {
246 this.dropdown?.close()
247 window.removeEventListener('scroll', onWindowScroll)
251 window.addEventListener('scroll', onWindowScroll)
252 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
253 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
255 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
259 private async buildMenuSections () {
260 const menuSections = []
262 if (this.isLoggedIn) {
264 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
269 this.menuService.buildCommonLinks(this.htmlServerConfig)
272 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
275 private buildUserLanguages () {
277 this.videoLanguages = []
281 if (!this.user.videoLanguages) {
282 this.videoLanguages = [ $localize`any language` ]
286 this.videoLanguages = this.user.videoLanguages
287 .map(locale => this.langForLocale(locale))
288 .map(value => value === undefined ? '?' : value)
291 private computeAdminAccess () {
292 const right = this.getFirstAdminRightAvailable()
294 this.userHasAdminAccess = right !== undefined
297 private computeVideosLink () {
298 if (!this.isLoggedIn) return
300 this.authService.userInformationLoaded
302 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
304 if (res === true) debugLogger('User can see videos link.')
305 else debugLogger('User cannot see videos link.')
309 private computeNSFWPolicy () {
311 this.nsfwPolicy = null
315 switch (this.user.nsfwPolicy) {
317 this.nsfwPolicy = $localize`hide`
321 this.nsfwPolicy = $localize`blur`
325 this.nsfwPolicy = $localize`display`
330 private updateUserState () {
331 this.user = this.isLoggedIn
332 ? this.authService.getUser()
335 this.computeAdminAccess()
336 this.computeNSFWPolicy()
337 this.computeVideosLink()