PeerTube/client/src/app/menu/menu.component.ts

336 lines
9.3 KiB
TypeScript
Raw Normal View History

import * as debug from 'debug'
2023-02-16 16:13:19 +01:00
import { forkJoin, Subscription } from 'rxjs'
import { first, switchMap } from 'rxjs/operators'
import { ViewportScroller } from '@angular/common'
2023-02-16 16:13:19 +01:00
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
import { Router } from '@angular/router'
import {
AuthService,
AuthStatus,
AuthUser,
2021-06-07 13:16:50 +02:00
HooksService,
HotkeysService,
2021-06-07 13:16:50 +02:00
MenuSection,
MenuService,
RedirectService,
ScreenService,
ServerService,
UserService
} from '@app/core'
import { scrollToTop } from '@app/helpers'
2018-06-28 13:59:48 +02:00
import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
import { HTMLServerConfig, ServerConfig, UserRight, UserRightType, VideoConstant } from '@peertube/peertube-models'
const debugLogger = debug('peertube:menu:MenuComponent')
@Component({
selector: 'my-menu',
2017-04-21 11:06:33 +02:00
templateUrl: './menu.component.html',
2021-08-17 14:42:53 +02:00
styleUrls: [ './menu.component.scss' ]
})
2023-02-16 16:13:19 +01:00
export class MenuComponent implements OnInit, OnDestroy {
2019-07-24 16:05:59 +02:00
@ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
@ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
@ViewChild('dropdown') dropdown: NgbDropdown
2018-06-28 13:59:48 +02:00
user: AuthUser
isLoggedIn: boolean
userHasAdminAccess = false
2018-09-26 14:23:10 +02:00
helpVisible = false
2020-03-11 16:41:38 +01:00
videoLanguages: string[] = []
2020-11-27 15:31:09 +01:00
nsfwPolicy: string
currentInterfaceLanguage: string
2020-03-11 16:41:38 +01:00
2021-06-07 13:16:50 +02:00
menuSections: MenuSection[] = []
2020-03-11 16:41:38 +01:00
private languages: VideoConstant<string>[] = []
2021-06-04 13:31:41 +02:00
private htmlServerConfig: HTMLServerConfig
2019-12-18 15:31:54 +01:00
private serverConfig: ServerConfig
2021-06-04 13:31:41 +02:00
private routesPerRight: { [role in UserRightType]?: string } = {
[UserRight.MANAGE_USERS]: '/admin/users',
2017-11-16 17:16:42 +01:00
[UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
2020-07-01 16:05:30 +02:00
[UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
[UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
2018-09-19 09:53:49 +02:00
[UserRight.MANAGE_JOBS]: '/admin/jobs',
[UserRight.MANAGE_CONFIGURATION]: '/admin/config'
}
2023-02-16 16:13:19 +01:00
private languagesSub: Subscription
private modalSub: Subscription
private hotkeysSub: Subscription
private authSub: Subscription
constructor (
private viewportScroller: ViewportScroller,
private authService: AuthService,
private userService: UserService,
private serverService: ServerService,
2018-09-04 23:14:31 +02:00
private redirectService: RedirectService,
private hotkeysService: HotkeysService,
private screenService: ScreenService,
private menuService: MenuService,
private modalService: PeertubeModalService,
2021-06-07 13:16:50 +02:00
private router: Router,
private hooks: HooksService
) { }
get isInMobileView () {
return this.screenService.isInMobileView()
}
get language () {
return this.languageChooserModal.getCurrentLanguage()
}
2023-01-19 09:29:47 +01:00
get requiresApproval () {
return this.serverConfig.signup.requiresApproval
}
ngOnInit () {
2021-06-04 13:31:41 +02:00
this.htmlServerConfig = this.serverService.getHTMLConfig()
2020-11-27 15:31:09 +01:00
this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
2021-06-07 17:38:31 +02:00
this.isLoggedIn = this.authService.isLoggedIn()
2021-06-07 13:16:50 +02:00
this.updateUserState()
this.buildMenuSections()
2023-02-16 16:13:19 +01:00
this.authSub = this.authService.loginChangedSource.subscribe(status => {
if (status === AuthStatus.LoggedIn) {
this.isLoggedIn = true
} else if (status === AuthStatus.LoggedOut) {
this.isLoggedIn = false
}
2018-09-26 14:23:10 +02:00
2023-02-16 16:13:19 +01:00
this.updateUserState()
this.buildMenuSections()
})
this.hotkeysSub = this.hotkeysService.cheatSheetToggle
.subscribe(isOpen => this.helpVisible = isOpen)
2020-03-11 16:41:38 +01:00
2023-02-16 16:13:19 +01:00
this.languagesSub = forkJoin([
this.serverService.getVideoLanguages(),
this.authService.userInformationLoaded.pipe(first())
]).subscribe(([ languages ]) => {
this.languages = languages
2023-02-16 16:13:19 +01:00
this.buildUserLanguages()
})
2021-07-20 13:49:46 +02:00
this.serverService.getConfig()
.subscribe(config => this.serverConfig = config)
2023-02-16 16:13:19 +01:00
this.modalSub = this.modalService.openQuickSettingsSubject
.subscribe(() => this.openQuickSettings())
}
2023-02-16 16:13:19 +01:00
ngOnDestroy () {
if (this.modalSub) this.modalSub.unsubscribe()
if (this.languagesSub) this.languagesSub.unsubscribe()
if (this.hotkeysSub) this.hotkeysSub.unsubscribe()
if (this.authSub) this.authSub.unsubscribe()
}
isRegistrationAllowed () {
2021-06-04 13:31:41 +02:00
if (!this.serverConfig) return false
2019-12-18 15:31:54 +01:00
return this.serverConfig.signup.allowed &&
this.serverConfig.signup.allowedForCurrentIP
2017-04-10 20:29:33 +02:00
}
getFirstAdminRightAvailable () {
const user = this.authService.getUser()
if (!user) return undefined
const adminRights = [
UserRight.MANAGE_USERS,
2017-11-16 17:16:42 +01:00
UserRight.MANAGE_SERVER_FOLLOW,
2020-07-01 16:05:30 +02:00
UserRight.MANAGE_ABUSES,
UserRight.MANAGE_VIDEO_BLACKLIST,
2018-09-19 09:53:49 +02:00
UserRight.MANAGE_JOBS,
UserRight.MANAGE_CONFIGURATION
]
for (const adminRight of adminRights) {
if (user.hasRight(adminRight)) {
return adminRight
}
}
return undefined
}
getFirstAdminRouteAvailable () {
const right = this.getFirstAdminRightAvailable()
return this.routesPerRight[right]
}
2017-12-01 09:20:19 +01:00
logout (event: Event) {
event.preventDefault()
this.authService.logout()
// Redirect to home page
2018-06-04 16:21:17 +02:00
this.redirectService.redirectToHomepage()
}
2018-06-28 13:59:48 +02:00
openLanguageChooser () {
this.languageChooserModal.show()
}
2018-09-26 14:23:10 +02:00
openHotkeysCheatSheet () {
this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
}
openQuickSettings () {
this.quickSettingsModal.show()
}
toggleUseP2P () {
if (!this.user) return
this.user.p2pEnabled = !this.user.p2pEnabled
2020-03-11 16:41:38 +01:00
this.userService.updateMyProfile({ p2pEnabled: this.user.p2pEnabled })
.subscribe(() => this.authService.refreshUserInformation())
}
2020-02-28 13:54:31 +01:00
langForLocale (localeId: string) {
if (localeId === '_unknown') return $localize`Unknown`
2020-04-16 17:04:02 +02:00
2020-03-11 16:41:38 +01:00
return this.languages.find(lang => lang.id === localeId).label
}
onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
const linkURL = link.getAttribute('href')
const linkHash = link.getAttribute('fragment')
// On same url without fragment restore top scroll position
if (!linkHash && this.router.url.includes(linkURL)) {
scrollToTop('smooth')
}
// On same url with fragment restore anchor scroll position
if (linkHash && this.router.url === linkURL) {
this.viewportScroller.scrollToAnchor(linkHash)
}
if (this.screenService.isInSmallView()) {
this.menuService.toggleMenu()
}
}
// Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
onMenuScrollEvent () {
2023-10-06 10:45:42 +02:00
document.querySelector('nav').scrollTo(0, 0)
}
onDropdownOpenChange (opened: boolean) {
if (this.screenService.isInMobileView()) return
// Close dropdown when window scroll to avoid dropdown quick jump for re-position
const onWindowScroll = () => {
2021-01-14 15:35:03 +01:00
this.dropdown?.close()
window.removeEventListener('scroll', onWindowScroll)
}
if (opened) {
window.addEventListener('scroll', onWindowScroll)
2023-10-06 10:45:42 +02:00
document.querySelector('nav').scrollTo(0, 0) // Reset menu scroll to easy lock
2024-02-22 10:12:04 +01:00
// eslint-disable-next-line @typescript-eslint/unbound-method
2023-10-06 10:45:42 +02:00
document.querySelector('nav').addEventListener('scroll', this.onMenuScrollEvent)
} else {
2024-02-22 10:12:04 +01:00
// eslint-disable-next-line @typescript-eslint/unbound-method
2023-10-06 10:45:42 +02:00
document.querySelector('nav').removeEventListener('scroll', this.onMenuScrollEvent)
}
}
2021-06-07 13:16:50 +02:00
private async buildMenuSections () {
const menuSections = []
if (this.isLoggedIn) {
menuSections.push(
this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
)
}
menuSections.push(
this.menuService.buildCommonLinks(this.htmlServerConfig)
)
this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
}
2020-03-11 16:41:38 +01:00
private buildUserLanguages () {
if (!this.user) {
this.videoLanguages = []
return
}
if (!this.user.videoLanguages) {
2021-06-07 13:16:50 +02:00
this.videoLanguages = [ $localize`any language` ]
2020-03-11 16:41:38 +01:00
return
}
this.videoLanguages = this.user.videoLanguages
.map(locale => this.langForLocale(locale))
.map(value => value === undefined ? '?' : value)
}
private computeAdminAccess () {
const right = this.getFirstAdminRightAvailable()
this.userHasAdminAccess = right !== undefined
}
private computeVideosLink () {
2021-06-07 13:16:50 +02:00
if (!this.isLoggedIn) return
this.authService.userInformationLoaded
.pipe(
switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
).subscribe(res => {
if (res === true) debugLogger('User can see videos link.')
else debugLogger('User cannot see videos link.')
})
}
2020-11-27 15:31:09 +01:00
private computeNSFWPolicy () {
if (!this.user) {
this.nsfwPolicy = null
return
}
switch (this.user.nsfwPolicy) {
case 'do_not_list':
this.nsfwPolicy = $localize`hide`
break
case 'blur':
this.nsfwPolicy = $localize`blur`
break
case 'display':
this.nsfwPolicy = $localize`display`
break
}
}
2021-06-07 13:16:50 +02:00
private updateUserState () {
this.user = this.isLoggedIn
? this.authService.getUser()
: undefined
this.computeAdminAccess()
this.computeNSFWPolicy()
this.computeVideosLink()
}
}