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

210 lines
5.9 KiB
TypeScript
Raw Normal View History

2018-06-28 13:59:48 +02:00
import { Component, OnInit, ViewChild } from '@angular/core'
2017-12-01 09:20:19 +01:00
import { UserRight } from '../../../../shared/models/users/user-right.enum'
import { AuthService, AuthStatus, RedirectService, ServerService } from '../core'
import { User } from '@app/shared/users/user.model'
import { UserService } from '@app/shared/users/user.service'
2018-06-28 13:59:48 +02:00
import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
2018-09-26 14:23:10 +02:00
import { HotkeysService } from 'angular2-hotkeys'
import { ServerConfig, VideoConstant } from '@shared/models'
import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
import { I18n } from '@ngx-translate/i18n-polyfill'
import { ScreenService } from '@app/shared/misc/screen.service'
@Component({
selector: 'my-menu',
2017-04-21 11:06:33 +02:00
templateUrl: './menu.component.html',
2017-05-01 18:05:28 +02:00
styleUrls: [ './menu.component.scss' ]
})
export class MenuComponent implements OnInit {
2019-07-24 16:05:59 +02:00
@ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
@ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
2018-06-28 13:59:48 +02:00
2017-12-01 09:20:19 +01:00
user: User
isLoggedIn: boolean
userHasAdminAccess = false
2018-09-26 14:23:10 +02:00
helpVisible = false
2020-03-11 16:41:38 +01:00
videoLanguages: string[] = []
private languages: VideoConstant<string>[] = []
2019-12-18 15:31:54 +01:00
private serverConfig: ServerConfig
2018-10-18 14:35:31 +02:00
private routesPerRight: { [ role in UserRight ]?: string } = {
[UserRight.MANAGE_USERS]: '/admin/users',
2017-11-16 17:16:42 +01:00
[UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
2018-09-19 09:53:49 +02:00
[UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
[UserRight.MANAGE_VIDEO_BLOCKS]: '/admin/moderation/video-blocks',
2018-09-19 09:53:49 +02:00
[UserRight.MANAGE_JOBS]: '/admin/jobs',
[UserRight.MANAGE_CONFIGURATION]: '/admin/config'
}
constructor (
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 i18n: I18n
) { }
get isInMobileView () {
return this.screenService.isInMobileView()
}
get placement () {
if (this.isInMobileView) {
return 'left-top auto'
} else {
return 'right-top auto'
}
}
ngOnInit () {
2019-12-18 15:31:54 +01:00
this.serverConfig = this.serverService.getTmpConfig()
this.serverService.getConfig()
.subscribe(config => this.serverConfig = config)
this.isLoggedIn = this.authService.isLoggedIn()
2017-12-01 09:20:19 +01:00
if (this.isLoggedIn === true) this.user = this.authService.getUser()
this.computeIsUserHasAdminAccess()
this.authService.loginChangedSource.subscribe(
status => {
if (status === AuthStatus.LoggedIn) {
this.isLoggedIn = true
2017-12-01 09:20:19 +01:00
this.user = this.authService.getUser()
this.computeIsUserHasAdminAccess()
console.log('Logged in.')
} else if (status === AuthStatus.LoggedOut) {
this.isLoggedIn = false
2017-12-01 09:20:19 +01:00
this.user = undefined
this.computeIsUserHasAdminAccess()
console.log('Logged out.')
} else {
console.error('Unknown auth status: ' + status)
}
}
)
2018-09-26 14:23:10 +02:00
2020-03-11 16:41:38 +01:00
this.hotkeysService.cheatSheetToggle
.subscribe(isOpen => this.helpVisible = isOpen)
this.serverService.getVideoLanguages()
.subscribe(languages => {
this.languages = languages
2020-03-11 16:41:38 +01:00
this.authService.userInformationLoaded
.subscribe(() => this.buildUserLanguages())
})
}
get language () {
return this.languageChooserModal.getCurrentLanguage()
}
get nsfwPolicy () {
if (!this.user) return
2020-03-11 16:41:38 +01:00
switch (this.user.nsfwPolicy) {
case 'do_not_list':
return this.i18n('hide')
2020-03-11 16:41:38 +01:00
case 'blur':
return this.i18n('blur')
2020-03-11 16:41:38 +01:00
case 'display':
return this.i18n('display')
}
}
isRegistrationAllowed () {
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,
UserRight.MANAGE_VIDEO_ABUSES,
UserRight.MANAGE_VIDEO_BLOCKS,
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.webTorrentEnabled = !this.user.webTorrentEnabled
2020-03-11 16:41:38 +01:00
this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
.subscribe(() => this.authService.refreshUserInformation())
}
2020-02-28 13:54:31 +01:00
langForLocale (localeId: string) {
2020-04-16 17:04:02 +02:00
if (localeId === '_unknown') return this.i18n('Unknown')
2020-03-11 16:41:38 +01:00
return this.languages.find(lang => lang.id === localeId).label
}
private buildUserLanguages () {
if (!this.user) {
this.videoLanguages = []
return
}
if (!this.user.videoLanguages) {
this.videoLanguages = [ this.i18n('any language') ]
return
}
this.videoLanguages = this.user.videoLanguages
.map(locale => this.langForLocale(locale))
.map(value => value === undefined ? '?' : value)
}
private computeIsUserHasAdminAccess () {
const right = this.getFirstAdminRightAvailable()
this.userHasAdminAccess = right !== undefined
}
}