2020-06-23 07:10:17 -05:00
|
|
|
import { Hotkey, HotkeysService } from 'angular2-hotkeys'
|
2021-08-27 03:15:55 -05:00
|
|
|
import { forkJoin } from 'rxjs'
|
|
|
|
import { filter, first, map } from 'rxjs/operators'
|
2021-08-19 02:24:29 -05:00
|
|
|
import { DOCUMENT, getLocaleDirection, PlatformLocation } from '@angular/common'
|
2020-06-23 07:10:17 -05:00
|
|
|
import { AfterViewInit, Component, Inject, LOCALE_ID, OnInit, ViewChild } from '@angular/core'
|
2018-02-22 03:22:53 -06:00
|
|
|
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
|
2021-08-19 02:24:29 -05:00
|
|
|
import { Event, GuardsCheckStart, RouteConfigLoadEnd, RouteConfigLoadStart, Router } from '@angular/router'
|
|
|
|
import {
|
|
|
|
AuthService,
|
|
|
|
MarkdownService,
|
|
|
|
PeerTubeRouterService,
|
|
|
|
RedirectService,
|
|
|
|
ScreenService,
|
|
|
|
ScrollService,
|
|
|
|
ServerService,
|
|
|
|
ThemeService,
|
|
|
|
User
|
|
|
|
} from '@app/core'
|
2019-07-22 08:40:13 -05:00
|
|
|
import { HooksService } from '@app/core/plugins/hooks.service'
|
2020-06-23 07:10:17 -05:00
|
|
|
import { PluginService } from '@app/core/plugins/plugin.service'
|
2021-08-27 03:15:55 -05:00
|
|
|
import { AccountSetupWarningModalComponent } from '@app/modal/account-setup-warning-modal.component'
|
2020-04-15 08:35:41 -05:00
|
|
|
import { CustomModalComponent } from '@app/modal/custom-modal.component'
|
2020-06-23 07:10:17 -05:00
|
|
|
import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component'
|
2021-08-27 03:15:55 -05:00
|
|
|
import { AdminWelcomeModalComponent } from '@app/modal/admin-welcome-modal.component'
|
2020-11-19 04:12:01 -06:00
|
|
|
import { NgbConfig, NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
2021-01-13 04:29:55 -06:00
|
|
|
import { LoadingBarService } from '@ngx-loading-bar/core'
|
2020-08-12 03:40:04 -05:00
|
|
|
import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
|
2021-06-04 07:39:47 -05:00
|
|
|
import { getShortLocale } from '@shared/core-utils/i18n'
|
2021-08-27 03:15:55 -05:00
|
|
|
import { BroadcastMessageLevel, HTMLServerConfig, UserRole } from '@shared/models'
|
2020-03-07 06:50:26 -06:00
|
|
|
import { MenuService } from './core/menu/menu.service'
|
2020-08-03 11:06:49 -05:00
|
|
|
import { POP_STATE_MODAL_DISMISS } from './helpers'
|
2020-06-23 07:10:17 -05:00
|
|
|
import { InstanceService } from './shared/shared-instance'
|
2017-03-04 04:45:47 -06:00
|
|
|
|
2016-03-14 07:50:19 -05:00
|
|
|
@Component({
|
2016-11-04 10:23:18 -05:00
|
|
|
selector: 'my-app',
|
|
|
|
templateUrl: './app.component.html',
|
|
|
|
styleUrls: [ './app.component.scss' ]
|
2016-03-14 07:50:19 -05:00
|
|
|
})
|
2020-04-15 08:35:41 -05:00
|
|
|
export class AppComponent implements OnInit, AfterViewInit {
|
2020-05-28 04:15:38 -05:00
|
|
|
private static BROADCAST_MESSAGE_KEY = 'app-broadcast-message-dismissed'
|
|
|
|
|
2021-08-27 03:15:55 -05:00
|
|
|
@ViewChild('accountSetupWarningModal') accountSetupWarningModal: AccountSetupWarningModalComponent
|
|
|
|
@ViewChild('adminWelcomeModal') adminWelcomeModal: AdminWelcomeModalComponent
|
2020-02-07 03:00:34 -06:00
|
|
|
@ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent
|
2020-04-15 08:35:41 -05:00
|
|
|
@ViewChild('customModal') customModal: CustomModalComponent
|
2019-08-28 07:40:06 -05:00
|
|
|
|
2018-02-22 03:22:53 -06:00
|
|
|
customCSS: SafeHtml
|
2020-05-28 04:15:38 -05:00
|
|
|
broadcastMessage: { message: string, dismissable: boolean, class: string } | null = null
|
2018-02-22 03:22:53 -06:00
|
|
|
|
2021-06-04 06:31:41 -05:00
|
|
|
private serverConfig: HTMLServerConfig
|
2019-12-18 08:31:54 -06:00
|
|
|
|
2017-06-16 07:32:15 -05:00
|
|
|
constructor (
|
2020-06-02 08:21:36 -05:00
|
|
|
@Inject(DOCUMENT) private document: Document,
|
2020-06-02 10:53:32 -05:00
|
|
|
@Inject(LOCALE_ID) private localeId: string,
|
2016-11-04 10:23:18 -05:00
|
|
|
private router: Router,
|
2017-03-04 04:45:47 -06:00
|
|
|
private authService: AuthService,
|
2018-02-22 03:22:53 -06:00
|
|
|
private serverService: ServerService,
|
2021-08-19 02:24:29 -05:00
|
|
|
private peertubeRouter: PeerTubeRouterService,
|
2019-07-08 08:54:08 -05:00
|
|
|
private pluginService: PluginService,
|
2019-08-28 07:40:06 -05:00
|
|
|
private instanceService: InstanceService,
|
2018-03-01 06:57:29 -06:00
|
|
|
private domSanitizer: DomSanitizer,
|
2018-06-15 09:52:15 -05:00
|
|
|
private redirectService: RedirectService,
|
2018-08-28 02:32:03 -05:00
|
|
|
private screenService: ScreenService,
|
2018-09-06 05:00:53 -05:00
|
|
|
private hotkeysService: HotkeysService,
|
2019-07-22 08:40:13 -05:00
|
|
|
private themeService: ThemeService,
|
2019-08-22 09:13:26 -05:00
|
|
|
private hooks: HooksService,
|
|
|
|
private location: PlatformLocation,
|
2020-03-07 06:50:26 -06:00
|
|
|
private modalService: NgbModal,
|
2020-05-28 04:15:38 -05:00
|
|
|
private markdownService: MarkdownService,
|
2020-11-19 04:12:01 -06:00
|
|
|
private ngbConfig: NgbConfig,
|
2021-01-13 04:29:55 -06:00
|
|
|
private loadingBar: LoadingBarService,
|
2021-08-19 02:24:29 -05:00
|
|
|
private scrollService: ScrollService,
|
2020-03-07 06:50:26 -06:00
|
|
|
public menu: MenuService
|
2020-11-19 04:12:01 -06:00
|
|
|
) {
|
|
|
|
this.ngbConfig.animation = false
|
|
|
|
}
|
2016-05-24 16:00:58 -05:00
|
|
|
|
2018-01-31 10:47:36 -06:00
|
|
|
get instanceName () {
|
2019-12-18 08:31:54 -06:00
|
|
|
return this.serverConfig.instance.name
|
2018-01-31 10:47:36 -06:00
|
|
|
}
|
|
|
|
|
2021-01-27 10:15:21 -06:00
|
|
|
goToDefaultRoute () {
|
2021-05-14 05:04:44 -05:00
|
|
|
return this.router.navigateByUrl(this.redirectService.getDefaultRoute())
|
2018-03-21 03:40:57 -05:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:32:15 -05:00
|
|
|
ngOnInit () {
|
2018-03-08 05:04:10 -06:00
|
|
|
document.getElementById('incompatible-browser').className += ' browser-ok'
|
2018-03-08 05:01:55 -06:00
|
|
|
|
2021-06-04 06:31:41 -05:00
|
|
|
this.serverConfig = this.serverService.getHTMLConfig()
|
2019-12-18 08:31:54 -06:00
|
|
|
|
2021-06-04 07:39:47 -05:00
|
|
|
this.hooks.runAction('action:application.init', 'common')
|
2019-10-25 03:57:23 -05:00
|
|
|
this.themeService.initialize()
|
|
|
|
|
2017-09-14 04:57:49 -05:00
|
|
|
this.authService.loadClientCredentials()
|
|
|
|
|
2018-03-27 09:18:25 -05:00
|
|
|
if (this.isUserLoggedIn()) {
|
2017-03-04 04:45:47 -06:00
|
|
|
// The service will automatically redirect to the login page if the token is not valid anymore
|
2017-10-25 10:31:11 -05:00
|
|
|
this.authService.refreshUserInformation()
|
2017-03-04 04:45:47 -06:00
|
|
|
}
|
2017-03-22 15:15:55 -05:00
|
|
|
|
2019-03-21 10:49:46 -05:00
|
|
|
this.initRouteEvents()
|
2021-08-19 02:24:29 -05:00
|
|
|
this.scrollService.enableScrollRestoration()
|
2021-06-04 06:31:41 -05:00
|
|
|
|
2019-03-21 10:49:46 -05:00
|
|
|
this.injectJS()
|
|
|
|
this.injectCSS()
|
2020-05-28 04:15:38 -05:00
|
|
|
this.injectBroadcastMessage()
|
2019-03-21 10:49:46 -05:00
|
|
|
|
2021-06-04 06:31:41 -05:00
|
|
|
this.serverService.configReloaded
|
|
|
|
.subscribe(config => {
|
|
|
|
this.serverConfig = config
|
|
|
|
|
|
|
|
this.injectBroadcastMessage()
|
|
|
|
this.injectCSS()
|
|
|
|
|
|
|
|
// Don't reinject JS since it could conflict with existing one
|
|
|
|
})
|
|
|
|
|
2019-03-21 10:49:46 -05:00
|
|
|
this.initHotkeys()
|
|
|
|
|
2019-08-22 10:13:58 -05:00
|
|
|
this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS))
|
2019-08-28 07:40:06 -05:00
|
|
|
|
|
|
|
this.openModalsIfNeeded()
|
2020-06-02 10:53:32 -05:00
|
|
|
|
|
|
|
this.document.documentElement.lang = getShortLocale(this.localeId)
|
2021-06-07 10:38:31 -05:00
|
|
|
this.document.documentElement.dir = getLocaleDirection(this.localeId)
|
2019-03-21 10:49:46 -05:00
|
|
|
}
|
|
|
|
|
2020-04-15 08:35:41 -05:00
|
|
|
ngAfterViewInit () {
|
|
|
|
this.pluginService.initializeCustomModal(this.customModal)
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:44:34 -06:00
|
|
|
getToggleTitle () {
|
|
|
|
if (this.menu.isDisplayed()) return $localize`Close the left menu`
|
|
|
|
|
|
|
|
return $localize`Open the left menu`
|
|
|
|
}
|
|
|
|
|
2019-03-21 10:49:46 -05:00
|
|
|
isUserLoggedIn () {
|
|
|
|
return this.authService.isLoggedIn()
|
|
|
|
}
|
|
|
|
|
2020-05-28 04:15:38 -05:00
|
|
|
hideBroadcastMessage () {
|
|
|
|
peertubeLocalStorage.setItem(AppComponent.BROADCAST_MESSAGE_KEY, this.serverConfig.broadcastMessage.message)
|
|
|
|
|
|
|
|
this.broadcastMessage = null
|
2020-08-10 16:28:31 -05:00
|
|
|
this.screenService.isBroadcastMessageDisplayed = false
|
2020-05-28 04:15:38 -05:00
|
|
|
}
|
|
|
|
|
2019-03-21 10:49:46 -05:00
|
|
|
private initRouteEvents () {
|
|
|
|
const eventsObs = this.router.events
|
|
|
|
|
2021-01-13 04:29:55 -06:00
|
|
|
// Plugin hooks
|
2021-08-19 02:24:29 -05:00
|
|
|
this.peertubeRouter.getNavigationEndEvents().subscribe(e => {
|
2019-07-25 12:02:54 -05:00
|
|
|
this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url })
|
|
|
|
})
|
|
|
|
|
2021-01-13 04:29:55 -06:00
|
|
|
// Automatically hide/display the menu
|
2019-03-21 10:49:46 -05:00
|
|
|
eventsObs.pipe(
|
|
|
|
filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart),
|
2020-08-17 03:19:45 -05:00
|
|
|
filter(() => this.screenService.isInSmallView() || this.screenService.isInTouchScreen())
|
2020-08-17 03:17:54 -05:00
|
|
|
).subscribe(() => this.menu.setMenuDisplay(false)) // User clicked on a link in the menu, change the page
|
2021-01-13 04:29:55 -06:00
|
|
|
|
|
|
|
// Handle lazy loaded module
|
|
|
|
eventsObs.pipe(
|
|
|
|
filter((e: Event): e is RouteConfigLoadStart => e instanceof RouteConfigLoadStart)
|
|
|
|
).subscribe(() => this.loadingBar.useRef().start())
|
|
|
|
|
|
|
|
eventsObs.pipe(
|
|
|
|
filter((e: Event): e is RouteConfigLoadEnd => e instanceof RouteConfigLoadEnd)
|
|
|
|
).subscribe(() => this.loadingBar.useRef().complete())
|
2019-03-21 10:49:46 -05:00
|
|
|
}
|
|
|
|
|
2021-06-04 06:31:41 -05:00
|
|
|
private async injectBroadcastMessage () {
|
|
|
|
this.broadcastMessage = null
|
|
|
|
this.screenService.isBroadcastMessageDisplayed = false
|
2020-05-28 04:15:38 -05:00
|
|
|
|
2021-06-04 06:31:41 -05:00
|
|
|
const messageConfig = this.serverConfig.broadcastMessage
|
2020-05-28 04:15:38 -05:00
|
|
|
|
2021-06-04 06:31:41 -05:00
|
|
|
if (messageConfig.enabled) {
|
|
|
|
// Already dismissed this message?
|
|
|
|
if (messageConfig.dismissable && localStorage.getItem(AppComponent.BROADCAST_MESSAGE_KEY) === messageConfig.message) {
|
|
|
|
return
|
|
|
|
}
|
2020-05-28 04:15:38 -05:00
|
|
|
|
2021-06-04 06:31:41 -05:00
|
|
|
const classes: { [id in BroadcastMessageLevel]: string } = {
|
|
|
|
info: 'alert-info',
|
|
|
|
warning: 'alert-warning',
|
|
|
|
error: 'alert-danger'
|
|
|
|
}
|
2020-08-10 16:28:31 -05:00
|
|
|
|
2021-06-04 06:31:41 -05:00
|
|
|
this.broadcastMessage = {
|
|
|
|
message: await this.markdownService.unsafeMarkdownToHTML(messageConfig.message, true),
|
|
|
|
dismissable: messageConfig.dismissable,
|
|
|
|
class: classes[messageConfig.level]
|
2020-05-28 04:15:38 -05:00
|
|
|
}
|
2021-06-04 06:31:41 -05:00
|
|
|
|
|
|
|
this.screenService.isBroadcastMessageDisplayed = true
|
|
|
|
}
|
2020-05-28 04:15:38 -05:00
|
|
|
}
|
|
|
|
|
2019-03-21 10:49:46 -05:00
|
|
|
private injectJS () {
|
2018-07-18 02:52:46 -05:00
|
|
|
// Inject JS
|
2021-06-04 06:31:41 -05:00
|
|
|
if (this.serverConfig.instance.customizations.javascript) {
|
|
|
|
try {
|
2021-08-17 07:42:53 -05:00
|
|
|
/* eslint-disable no-eval */
|
2021-06-04 06:31:41 -05:00
|
|
|
eval(this.serverConfig.instance.customizations.javascript)
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Cannot eval custom JavaScript.', err)
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 10:49:46 -05:00
|
|
|
}
|
2018-02-22 03:22:53 -06:00
|
|
|
|
2019-03-21 10:49:46 -05:00
|
|
|
private injectCSS () {
|
2021-06-04 06:31:41 -05:00
|
|
|
const headStyle = document.querySelector('style.custom-css-style')
|
|
|
|
if (headStyle) headStyle.parentNode.removeChild(headStyle)
|
|
|
|
|
|
|
|
// We test customCSS if the admin removed the css
|
|
|
|
if (this.customCSS || this.serverConfig.instance.customizations.css) {
|
|
|
|
const styleTag = '<style>' + this.serverConfig.instance.customizations.css + '</style>'
|
|
|
|
this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
|
|
|
|
}
|
2019-03-21 10:49:46 -05:00
|
|
|
}
|
2018-08-28 02:32:03 -05:00
|
|
|
|
2021-08-25 09:14:11 -05:00
|
|
|
private openModalsIfNeeded () {
|
2021-08-27 03:15:55 -05:00
|
|
|
const userSub = this.authService.userInformationLoaded
|
|
|
|
.pipe(map(() => this.authService.getUser()))
|
|
|
|
|
|
|
|
// Admin modal
|
|
|
|
userSub.pipe(
|
|
|
|
filter(user => user.role === UserRole.ADMINISTRATOR)
|
|
|
|
).subscribe(user => this.openAdminModalsIfNeeded(user))
|
|
|
|
|
|
|
|
// Account modal
|
|
|
|
userSub.pipe(
|
|
|
|
filter(user => user.role !== UserRole.ADMINISTRATOR)
|
|
|
|
).subscribe(user => this.openAccountModalsIfNeeded(user))
|
2019-08-28 07:40:06 -05:00
|
|
|
}
|
|
|
|
|
2021-08-27 03:15:55 -05:00
|
|
|
private openAdminModalsIfNeeded (user: User) {
|
|
|
|
if (this.adminWelcomeModal.shouldOpen(user)) {
|
|
|
|
return this.adminWelcomeModal.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.instanceConfigWarningModal.shouldOpenByUser(user)) return
|
|
|
|
|
|
|
|
forkJoin([
|
|
|
|
this.serverService.getConfig().pipe(first()),
|
|
|
|
this.instanceService.getAbout().pipe(first())
|
|
|
|
]).subscribe(([ config, about ]) => {
|
|
|
|
if (this.instanceConfigWarningModal.shouldOpen(config, about)) {
|
|
|
|
this.instanceConfigWarningModal.show(about)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private openAccountModalsIfNeeded (user: User) {
|
|
|
|
if (this.accountSetupWarningModal.shouldOpen(user)) {
|
|
|
|
this.accountSetupWarningModal.show(user)
|
|
|
|
}
|
2019-08-28 07:40:06 -05:00
|
|
|
}
|
|
|
|
|
2019-03-21 10:49:46 -05:00
|
|
|
private initHotkeys () {
|
2018-08-28 02:32:03 -05:00
|
|
|
this.hotkeysService.add([
|
2021-08-17 07:42:53 -05:00
|
|
|
new Hotkey([ '/', 's' ], (event: KeyboardEvent): boolean => {
|
2018-08-28 02:32:03 -05:00
|
|
|
document.getElementById('search-video').focus()
|
2018-09-02 14:46:43 -05:00
|
|
|
return false
|
2020-08-12 03:40:04 -05:00
|
|
|
}, undefined, $localize`Focus the search bar`),
|
2019-08-28 07:40:06 -05:00
|
|
|
|
2018-09-02 14:46:43 -05:00
|
|
|
new Hotkey('b', (event: KeyboardEvent): boolean => {
|
2020-03-07 06:50:26 -06:00
|
|
|
this.menu.toggleMenu()
|
2018-09-02 14:46:43 -05:00
|
|
|
return false
|
2020-08-12 03:40:04 -05:00
|
|
|
}, undefined, $localize`Toggle the left menu`),
|
2019-08-28 07:40:06 -05:00
|
|
|
|
2018-09-02 13:54:23 -05:00
|
|
|
new Hotkey('g o', (event: KeyboardEvent): boolean => {
|
2018-09-02 12:22:34 -05:00
|
|
|
this.router.navigate([ '/videos/overview' ])
|
|
|
|
return false
|
2020-08-12 03:40:04 -05:00
|
|
|
}, undefined, $localize`Go to the discover videos page`),
|
2019-08-28 07:40:06 -05:00
|
|
|
|
2018-09-02 13:54:23 -05:00
|
|
|
new Hotkey('g t', (event: KeyboardEvent): boolean => {
|
2018-08-28 02:32:03 -05:00
|
|
|
this.router.navigate([ '/videos/trending' ])
|
|
|
|
return false
|
2020-08-12 03:40:04 -05:00
|
|
|
}, undefined, $localize`Go to the trending videos page`),
|
2019-08-28 07:40:06 -05:00
|
|
|
|
2018-09-02 13:54:23 -05:00
|
|
|
new Hotkey('g r', (event: KeyboardEvent): boolean => {
|
2018-08-28 02:32:03 -05:00
|
|
|
this.router.navigate([ '/videos/recently-added' ])
|
|
|
|
return false
|
2020-08-12 03:40:04 -05:00
|
|
|
}, undefined, $localize`Go to the recently added videos page`),
|
2019-08-28 07:40:06 -05:00
|
|
|
|
2018-09-02 13:54:23 -05:00
|
|
|
new Hotkey('g l', (event: KeyboardEvent): boolean => {
|
2018-08-28 02:32:03 -05:00
|
|
|
this.router.navigate([ '/videos/local' ])
|
|
|
|
return false
|
2020-08-12 03:40:04 -05:00
|
|
|
}, undefined, $localize`Go to the local videos page`),
|
2019-08-28 07:40:06 -05:00
|
|
|
|
2018-09-02 13:54:23 -05:00
|
|
|
new Hotkey('g u', (event: KeyboardEvent): boolean => {
|
2018-08-28 02:32:03 -05:00
|
|
|
this.router.navigate([ '/videos/upload' ])
|
|
|
|
return false
|
2020-08-12 03:40:04 -05:00
|
|
|
}, undefined, $localize`Go to the videos upload page`)
|
2018-08-28 02:32:03 -05:00
|
|
|
])
|
2017-04-26 14:22:00 -05:00
|
|
|
}
|
2016-03-14 07:50:19 -05:00
|
|
|
}
|