aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/app.component.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/app.component.ts')
-rw-r--r--client/src/app/app.component.ts121
1 files changed, 60 insertions, 61 deletions
diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts
index 863c3f3b5..6884068f6 100644
--- a/client/src/app/app.component.ts
+++ b/client/src/app/app.component.ts
@@ -1,6 +1,6 @@
1import { Hotkey, HotkeysService } from 'angular2-hotkeys' 1import { Hotkey, HotkeysService } from 'angular2-hotkeys'
2import { concat } from 'rxjs' 2import { concat } from 'rxjs'
3import { filter, first, map, pairwise } from 'rxjs/operators' 3import { filter, first, map, pairwise, switchMap } from 'rxjs/operators'
4import { DOCUMENT, PlatformLocation, ViewportScroller } from '@angular/common' 4import { DOCUMENT, PlatformLocation, ViewportScroller } from '@angular/common'
5import { AfterViewInit, Component, Inject, LOCALE_ID, OnInit, ViewChild } from '@angular/core' 5import { AfterViewInit, Component, Inject, LOCALE_ID, OnInit, ViewChild } from '@angular/core'
6import { DomSanitizer, SafeHtml } from '@angular/platform-browser' 6import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
@@ -15,7 +15,7 @@ import { NgbConfig, NgbModal } from '@ng-bootstrap/ng-bootstrap'
15import { LoadingBarService } from '@ngx-loading-bar/core' 15import { LoadingBarService } from '@ngx-loading-bar/core'
16import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' 16import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
17import { getShortLocale, is18nPath } from '@shared/core-utils/i18n' 17import { getShortLocale, is18nPath } from '@shared/core-utils/i18n'
18import { BroadcastMessageLevel, ServerConfig, UserRole } from '@shared/models' 18import { BroadcastMessageLevel, HTMLServerConfig, ServerConfig, UserRole } from '@shared/models'
19import { MenuService } from './core/menu/menu.service' 19import { MenuService } from './core/menu/menu.service'
20import { POP_STATE_MODAL_DISMISS } from './helpers' 20import { POP_STATE_MODAL_DISMISS } from './helpers'
21import { InstanceService } from './shared/shared-instance' 21import { InstanceService } from './shared/shared-instance'
@@ -35,7 +35,7 @@ export class AppComponent implements OnInit, AfterViewInit {
35 customCSS: SafeHtml 35 customCSS: SafeHtml
36 broadcastMessage: { message: string, dismissable: boolean, class: string } | null = null 36 broadcastMessage: { message: string, dismissable: boolean, class: string } | null = null
37 37
38 private serverConfig: ServerConfig 38 private serverConfig: HTMLServerConfig
39 39
40 constructor ( 40 constructor (
41 @Inject(DOCUMENT) private document: Document, 41 @Inject(DOCUMENT) private document: Document,
@@ -73,9 +73,7 @@ export class AppComponent implements OnInit, AfterViewInit {
73 ngOnInit () { 73 ngOnInit () {
74 document.getElementById('incompatible-browser').className += ' browser-ok' 74 document.getElementById('incompatible-browser').className += ' browser-ok'
75 75
76 this.serverConfig = this.serverService.getTmpConfig() 76 this.serverConfig = this.serverService.getHTMLConfig()
77 this.serverService.getConfig()
78 .subscribe(config => this.serverConfig = config)
79 77
80 this.loadPlugins() 78 this.loadPlugins()
81 this.themeService.initialize() 79 this.themeService.initialize()
@@ -88,10 +86,21 @@ export class AppComponent implements OnInit, AfterViewInit {
88 } 86 }
89 87
90 this.initRouteEvents() 88 this.initRouteEvents()
89
91 this.injectJS() 90 this.injectJS()
92 this.injectCSS() 91 this.injectCSS()
93 this.injectBroadcastMessage() 92 this.injectBroadcastMessage()
94 93
94 this.serverService.configReloaded
95 .subscribe(config => {
96 this.serverConfig = config
97
98 this.injectBroadcastMessage()
99 this.injectCSS()
100
101 // Don't reinject JS since it could conflict with existing one
102 })
103
95 this.initHotkeys() 104 this.initHotkeys()
96 105
97 this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS)) 106 this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS))
@@ -208,69 +217,55 @@ export class AppComponent implements OnInit, AfterViewInit {
208 ).subscribe(() => this.loadingBar.useRef().complete()) 217 ).subscribe(() => this.loadingBar.useRef().complete())
209 } 218 }
210 219
211 private injectBroadcastMessage () { 220 private async injectBroadcastMessage () {
212 concat( 221 this.broadcastMessage = null
213 this.serverService.getConfig().pipe(first()), 222 this.screenService.isBroadcastMessageDisplayed = false
214 this.serverService.configReloaded
215 ).subscribe(async config => {
216 this.broadcastMessage = null
217 this.screenService.isBroadcastMessageDisplayed = false
218
219 const messageConfig = config.broadcastMessage
220 223
221 if (messageConfig.enabled) { 224 const messageConfig = this.serverConfig.broadcastMessage
222 // Already dismissed this message?
223 if (messageConfig.dismissable && localStorage.getItem(AppComponent.BROADCAST_MESSAGE_KEY) === messageConfig.message) {
224 return
225 }
226 225
227 const classes: { [id in BroadcastMessageLevel]: string } = { 226 if (messageConfig.enabled) {
228 info: 'alert-info', 227 // Already dismissed this message?
229 warning: 'alert-warning', 228 if (messageConfig.dismissable && localStorage.getItem(AppComponent.BROADCAST_MESSAGE_KEY) === messageConfig.message) {
230 error: 'alert-danger' 229 return
231 } 230 }
232 231
233 this.broadcastMessage = { 232 const classes: { [id in BroadcastMessageLevel]: string } = {
234 message: await this.markdownService.unsafeMarkdownToHTML(messageConfig.message, true), 233 info: 'alert-info',
235 dismissable: messageConfig.dismissable, 234 warning: 'alert-warning',
236 class: classes[messageConfig.level] 235 error: 'alert-danger'
237 } 236 }
238 237
239 this.screenService.isBroadcastMessageDisplayed = true 238 this.broadcastMessage = {
239 message: await this.markdownService.unsafeMarkdownToHTML(messageConfig.message, true),
240 dismissable: messageConfig.dismissable,
241 class: classes[messageConfig.level]
240 } 242 }
241 }) 243
244 this.screenService.isBroadcastMessageDisplayed = true
245 }
242 } 246 }
243 247
244 private injectJS () { 248 private injectJS () {
245 // Inject JS 249 // Inject JS
246 this.serverService.getConfig() 250 if (this.serverConfig.instance.customizations.javascript) {
247 .subscribe(config => { 251 try {
248 if (config.instance.customizations.javascript) { 252 // tslint:disable:no-eval
249 try { 253 eval(this.serverConfig.instance.customizations.javascript)
250 // tslint:disable:no-eval 254 } catch (err) {
251 eval(config.instance.customizations.javascript) 255 console.error('Cannot eval custom JavaScript.', err)
252 } catch (err) { 256 }
253 console.error('Cannot eval custom JavaScript.', err) 257 }
254 }
255 }
256 })
257 } 258 }
258 259
259 private injectCSS () { 260 private injectCSS () {
260 // Inject CSS if modified (admin config settings) 261 const headStyle = document.querySelector('style.custom-css-style')
261 concat( 262 if (headStyle) headStyle.parentNode.removeChild(headStyle)
262 this.serverService.getConfig().pipe(first()), 263
263 this.serverService.configReloaded 264 // We test customCSS if the admin removed the css
264 ).subscribe(config => { 265 if (this.customCSS || this.serverConfig.instance.customizations.css) {
265 const headStyle = document.querySelector('style.custom-css-style') 266 const styleTag = '<style>' + this.serverConfig.instance.customizations.css + '</style>'
266 if (headStyle) headStyle.parentNode.removeChild(headStyle) 267 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
267 268 }
268 // We test customCSS if the admin removed the css
269 if (this.customCSS || config.instance.customizations.css) {
270 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
271 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
272 }
273 })
274 } 269 }
275 270
276 private async loadPlugins () { 271 private async loadPlugins () {
@@ -283,14 +278,18 @@ export class AppComponent implements OnInit, AfterViewInit {
283 this.authService.userInformationLoaded 278 this.authService.userInformationLoaded
284 .pipe( 279 .pipe(
285 map(() => this.authService.getUser()), 280 map(() => this.authService.getUser()),
286 filter(user => user.role === UserRole.ADMINISTRATOR) 281 filter(user => user.role === UserRole.ADMINISTRATOR),
287 ).subscribe(user => setTimeout(() => this._openAdminModalsIfNeeded(user))) // setTimeout because of ngIf in template 282 switchMap(user => {
283 return this.serverService.getConfig()
284 .pipe(map(serverConfig => ({ serverConfig, user })))
285 })
286 ).subscribe(({ serverConfig, user }) => this._openAdminModalsIfNeeded(serverConfig, user))
288 } 287 }
289 288
290 private async _openAdminModalsIfNeeded (user: User) { 289 private async _openAdminModalsIfNeeded (serverConfig: ServerConfig, user: User) {
291 if (user.noWelcomeModal !== true) return this.welcomeModal.show() 290 if (user.noWelcomeModal !== true) return this.welcomeModal.show()
292 291
293 if (user.noInstanceConfigWarningModal === true || !this.serverConfig.signup.allowed) return 292 if (user.noInstanceConfigWarningModal === true || !serverConfig.signup.allowed) return
294 293
295 this.instanceService.getAbout() 294 this.instanceService.getAbout()
296 .subscribe(about => { 295 .subscribe(about => {