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