]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
af6766e3 | 2 | import { forkJoin, delay } from 'rxjs' |
8f581725 | 3 | import { filter, first, map } from 'rxjs/operators' |
dd24f1bb | 4 | import { DOCUMENT, getLocaleDirection, PlatformLocation } from '@angular/common' |
67ed6552 | 5 | import { AfterViewInit, Component, Inject, LOCALE_ID, OnInit, ViewChild } from '@angular/core' |
00b5556c | 6 | import { DomSanitizer, SafeHtml } from '@angular/platform-browser' |
dd24f1bb C |
7 | import { Event, GuardsCheckStart, RouteConfigLoadEnd, RouteConfigLoadStart, Router } from '@angular/router' |
8 | import { | |
9 | AuthService, | |
10 | MarkdownService, | |
11 | PeerTubeRouterService, | |
12 | RedirectService, | |
13 | ScreenService, | |
14 | ScrollService, | |
15 | ServerService, | |
16 | ThemeService, | |
a9bfa85d C |
17 | User, |
18 | UserLocalStorageService | |
dd24f1bb | 19 | } from '@app/core' |
93cae479 | 20 | import { HooksService } from '@app/core/plugins/hooks.service' |
67ed6552 | 21 | import { PluginService } from '@app/core/plugins/plugin.service' |
8f581725 | 22 | import { AccountSetupWarningModalComponent } from '@app/modal/account-setup-warning-modal.component' |
437e8e06 | 23 | import { CustomModalComponent } from '@app/modal/custom-modal.component' |
67ed6552 | 24 | import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component' |
8f581725 | 25 | import { AdminWelcomeModalComponent } from '@app/modal/admin-welcome-modal.component' |
4f926722 | 26 | import { NgbConfig, NgbModal } from '@ng-bootstrap/ng-bootstrap' |
6d0110ad | 27 | import { LoadingBarService } from '@ngx-loading-bar/core' |
66357162 | 28 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' |
fc21ef5c | 29 | import { getShortLocale } from '@shared/core-utils/i18n' |
8f581725 | 30 | import { BroadcastMessageLevel, HTMLServerConfig, UserRole } from '@shared/models' |
3b20bdd6 | 31 | import { MenuService } from './core/menu/menu.service' |
4504f09f | 32 | import { POP_STATE_MODAL_DISMISS } from './helpers' |
67ed6552 | 33 | import { InstanceService } from './shared/shared-instance' |
e2a2d6c8 | 34 | |
dc8bc31b | 35 | @Component({ |
3154f382 C |
36 | selector: 'my-app', |
37 | templateUrl: './app.component.html', | |
38 | styleUrls: [ './app.component.scss' ] | |
dc8bc31b | 39 | }) |
437e8e06 | 40 | export class AppComponent implements OnInit, AfterViewInit { |
72c33e71 C |
41 | private static BROADCAST_MESSAGE_KEY = 'app-broadcast-message-dismissed' |
42 | ||
8f581725 C |
43 | @ViewChild('accountSetupWarningModal') accountSetupWarningModal: AccountSetupWarningModalComponent |
44 | @ViewChild('adminWelcomeModal') adminWelcomeModal: AdminWelcomeModalComponent | |
2f5d2ec5 | 45 | @ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent |
437e8e06 | 46 | @ViewChild('customModal') customModal: CustomModalComponent |
43d0ea7f | 47 | |
00b5556c | 48 | customCSS: SafeHtml |
72c33e71 | 49 | broadcastMessage: { message: string, dismissable: boolean, class: string } | null = null |
00b5556c | 50 | |
2989628b | 51 | private serverConfig: HTMLServerConfig |
ba430d75 | 52 | |
df98563e | 53 | constructor ( |
140ea386 | 54 | @Inject(DOCUMENT) private document: Document, |
81fe3c67 | 55 | @Inject(LOCALE_ID) private localeId: string, |
3154f382 | 56 | private router: Router, |
e2a2d6c8 | 57 | private authService: AuthService, |
00b5556c | 58 | private serverService: ServerService, |
dd24f1bb | 59 | private peertubeRouter: PeerTubeRouterService, |
18a6f04c | 60 | private pluginService: PluginService, |
43d0ea7f | 61 | private instanceService: InstanceService, |
901637bb | 62 | private domSanitizer: DomSanitizer, |
bbe0f064 | 63 | private redirectService: RedirectService, |
ee1fc23a | 64 | private screenService: ScreenService, |
1a00c561 | 65 | private hotkeysService: HotkeysService, |
93cae479 | 66 | private themeService: ThemeService, |
4334445d C |
67 | private hooks: HooksService, |
68 | private location: PlatformLocation, | |
3b20bdd6 | 69 | private modalService: NgbModal, |
72c33e71 | 70 | private markdownService: MarkdownService, |
4f926722 | 71 | private ngbConfig: NgbConfig, |
6d0110ad | 72 | private loadingBar: LoadingBarService, |
dd24f1bb | 73 | private scrollService: ScrollService, |
a9bfa85d | 74 | private userLocalStorage: UserLocalStorageService, |
3b20bdd6 | 75 | public menu: MenuService |
4f926722 C |
76 | ) { |
77 | this.ngbConfig.animation = false | |
78 | } | |
a99593ed | 79 | |
36f9424f | 80 | get instanceName () { |
ba430d75 | 81 | return this.serverConfig.instance.name |
36f9424f C |
82 | } |
83 | ||
ba5d4a84 | 84 | goToDefaultRoute () { |
aea0b0e7 | 85 | return this.router.navigateByUrl(this.redirectService.getDefaultRoute()) |
29f9b562 C |
86 | } |
87 | ||
df98563e | 88 | ngOnInit () { |
b3eeb529 | 89 | document.getElementById('incompatible-browser').className += ' browser-ok' |
73e09f27 | 90 | |
a9bfa85d C |
91 | this.loadUser() |
92 | ||
2989628b | 93 | this.serverConfig = this.serverService.getHTMLConfig() |
ba430d75 | 94 | |
fc21ef5c | 95 | this.hooks.runAction('action:application.init', 'common') |
a2ffd046 C |
96 | this.themeService.initialize() |
97 | ||
d592e0a9 C |
98 | this.authService.loadClientCredentials() |
99 | ||
d414207f | 100 | if (this.isUserLoggedIn()) { |
e2a2d6c8 | 101 | // The service will automatically redirect to the login page if the token is not valid anymore |
bcd9f81e | 102 | this.authService.refreshUserInformation() |
e2a2d6c8 | 103 | } |
6e07c3de | 104 | |
489290b8 | 105 | this.initRouteEvents() |
dd24f1bb | 106 | this.scrollService.enableScrollRestoration() |
2989628b | 107 | |
489290b8 C |
108 | this.injectJS() |
109 | this.injectCSS() | |
72c33e71 | 110 | this.injectBroadcastMessage() |
489290b8 | 111 | |
2989628b C |
112 | this.serverService.configReloaded |
113 | .subscribe(config => { | |
114 | this.serverConfig = config | |
115 | ||
116 | this.injectBroadcastMessage() | |
117 | this.injectCSS() | |
118 | ||
119 | // Don't reinject JS since it could conflict with existing one | |
120 | }) | |
121 | ||
489290b8 C |
122 | this.initHotkeys() |
123 | ||
60c2bc80 | 124 | this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS)) |
43d0ea7f C |
125 | |
126 | this.openModalsIfNeeded() | |
81fe3c67 RK |
127 | |
128 | this.document.documentElement.lang = getShortLocale(this.localeId) | |
27bc9586 | 129 | this.document.documentElement.dir = getLocaleDirection(this.localeId) |
489290b8 C |
130 | } |
131 | ||
437e8e06 K |
132 | ngAfterViewInit () { |
133 | this.pluginService.initializeCustomModal(this.customModal) | |
134 | } | |
135 | ||
d95bc702 C |
136 | getToggleTitle () { |
137 | if (this.menu.isDisplayed()) return $localize`Close the left menu` | |
138 | ||
139 | return $localize`Open the left menu` | |
140 | } | |
141 | ||
489290b8 C |
142 | isUserLoggedIn () { |
143 | return this.authService.isLoggedIn() | |
144 | } | |
145 | ||
72c33e71 C |
146 | hideBroadcastMessage () { |
147 | peertubeLocalStorage.setItem(AppComponent.BROADCAST_MESSAGE_KEY, this.serverConfig.broadcastMessage.message) | |
148 | ||
149 | this.broadcastMessage = null | |
7034b3c9 | 150 | this.screenService.isBroadcastMessageDisplayed = false |
72c33e71 C |
151 | } |
152 | ||
489290b8 | 153 | private initRouteEvents () { |
489290b8 C |
154 | const eventsObs = this.router.events |
155 | ||
6d0110ad | 156 | // Plugin hooks |
dd24f1bb | 157 | this.peertubeRouter.getNavigationEndEvents().subscribe(e => { |
23bdacf8 C |
158 | this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url }) |
159 | }) | |
160 | ||
6d0110ad | 161 | // Automatically hide/display the menu |
489290b8 C |
162 | eventsObs.pipe( |
163 | filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart), | |
1bfc7b73 | 164 | filter(() => this.screenService.isInSmallView() || this.screenService.isInTouchScreen()) |
245b9d27 | 165 | ).subscribe(() => this.menu.setMenuDisplay(false)) // User clicked on a link in the menu, change the page |
6d0110ad C |
166 | |
167 | // Handle lazy loaded module | |
168 | eventsObs.pipe( | |
169 | filter((e: Event): e is RouteConfigLoadStart => e instanceof RouteConfigLoadStart) | |
170 | ).subscribe(() => this.loadingBar.useRef().start()) | |
171 | ||
172 | eventsObs.pipe( | |
173 | filter((e: Event): e is RouteConfigLoadEnd => e instanceof RouteConfigLoadEnd) | |
174 | ).subscribe(() => this.loadingBar.useRef().complete()) | |
489290b8 C |
175 | } |
176 | ||
2989628b C |
177 | private async injectBroadcastMessage () { |
178 | this.broadcastMessage = null | |
179 | this.screenService.isBroadcastMessageDisplayed = false | |
72c33e71 | 180 | |
2989628b | 181 | const messageConfig = this.serverConfig.broadcastMessage |
72c33e71 | 182 | |
2989628b C |
183 | if (messageConfig.enabled) { |
184 | // Already dismissed this message? | |
185 | if (messageConfig.dismissable && localStorage.getItem(AppComponent.BROADCAST_MESSAGE_KEY) === messageConfig.message) { | |
186 | return | |
187 | } | |
72c33e71 | 188 | |
2989628b C |
189 | const classes: { [id in BroadcastMessageLevel]: string } = { |
190 | info: 'alert-info', | |
191 | warning: 'alert-warning', | |
192 | error: 'alert-danger' | |
193 | } | |
7034b3c9 | 194 | |
2989628b C |
195 | this.broadcastMessage = { |
196 | message: await this.markdownService.unsafeMarkdownToHTML(messageConfig.message, true), | |
197 | dismissable: messageConfig.dismissable, | |
198 | class: classes[messageConfig.level] | |
72c33e71 | 199 | } |
2989628b C |
200 | |
201 | this.screenService.isBroadcastMessageDisplayed = true | |
202 | } | |
72c33e71 C |
203 | } |
204 | ||
489290b8 | 205 | private injectJS () { |
e032aec9 | 206 | // Inject JS |
2989628b C |
207 | if (this.serverConfig.instance.customizations.javascript) { |
208 | try { | |
9df52d66 | 209 | /* eslint-disable no-eval */ |
2989628b C |
210 | eval(this.serverConfig.instance.customizations.javascript) |
211 | } catch (err) { | |
212 | console.error('Cannot eval custom JavaScript.', err) | |
213 | } | |
214 | } | |
489290b8 | 215 | } |
00b5556c | 216 | |
489290b8 | 217 | private injectCSS () { |
2989628b C |
218 | const headStyle = document.querySelector('style.custom-css-style') |
219 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
220 | ||
221 | // We test customCSS if the admin removed the css | |
222 | if (this.customCSS || this.serverConfig.instance.customizations.css) { | |
223 | const styleTag = '<style>' + this.serverConfig.instance.customizations.css + '</style>' | |
224 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
225 | } | |
489290b8 | 226 | } |
ee1fc23a | 227 | |
98ab5dc8 | 228 | private openModalsIfNeeded () { |
8f581725 | 229 | const userSub = this.authService.userInformationLoaded |
af6766e3 C |
230 | .pipe( |
231 | delay(0), // Wait for modals creations | |
232 | map(() => this.authService.getUser()) | |
233 | ) | |
8f581725 C |
234 | |
235 | // Admin modal | |
236 | userSub.pipe( | |
237 | filter(user => user.role === UserRole.ADMINISTRATOR) | |
238 | ).subscribe(user => this.openAdminModalsIfNeeded(user)) | |
239 | ||
240 | // Account modal | |
241 | userSub.pipe( | |
242 | filter(user => user.role !== UserRole.ADMINISTRATOR) | |
243 | ).subscribe(user => this.openAccountModalsIfNeeded(user)) | |
43d0ea7f C |
244 | } |
245 | ||
8f581725 C |
246 | private openAdminModalsIfNeeded (user: User) { |
247 | if (this.adminWelcomeModal.shouldOpen(user)) { | |
248 | return this.adminWelcomeModal.show() | |
249 | } | |
250 | ||
251 | if (!this.instanceConfigWarningModal.shouldOpenByUser(user)) return | |
252 | ||
253 | forkJoin([ | |
254 | this.serverService.getConfig().pipe(first()), | |
255 | this.instanceService.getAbout().pipe(first()) | |
256 | ]).subscribe(([ config, about ]) => { | |
257 | if (this.instanceConfigWarningModal.shouldOpen(config, about)) { | |
258 | this.instanceConfigWarningModal.show(about) | |
259 | } | |
260 | }) | |
261 | } | |
262 | ||
263 | private openAccountModalsIfNeeded (user: User) { | |
264 | if (this.accountSetupWarningModal.shouldOpen(user)) { | |
265 | this.accountSetupWarningModal.show(user) | |
266 | } | |
43d0ea7f C |
267 | } |
268 | ||
489290b8 | 269 | private initHotkeys () { |
ee1fc23a | 270 | this.hotkeysService.add([ |
9df52d66 | 271 | new Hotkey([ '/', 's' ], (event: KeyboardEvent): boolean => { |
ee1fc23a | 272 | document.getElementById('search-video').focus() |
8542dc33 | 273 | return false |
66357162 | 274 | }, undefined, $localize`Focus the search bar`), |
43d0ea7f | 275 | |
8542dc33 | 276 | new Hotkey('b', (event: KeyboardEvent): boolean => { |
3b20bdd6 | 277 | this.menu.toggleMenu() |
8542dc33 | 278 | return false |
66357162 | 279 | }, undefined, $localize`Toggle the left menu`), |
43d0ea7f | 280 | |
20d21199 | 281 | new Hotkey('g o', (event: KeyboardEvent): boolean => { |
a54991da RK |
282 | this.router.navigate([ '/videos/overview' ]) |
283 | return false | |
66357162 | 284 | }, undefined, $localize`Go to the discover videos page`), |
43d0ea7f | 285 | |
20d21199 | 286 | new Hotkey('g t', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
287 | this.router.navigate([ '/videos/trending' ]) |
288 | return false | |
66357162 | 289 | }, undefined, $localize`Go to the trending videos page`), |
43d0ea7f | 290 | |
20d21199 | 291 | new Hotkey('g r', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
292 | this.router.navigate([ '/videos/recently-added' ]) |
293 | return false | |
66357162 | 294 | }, undefined, $localize`Go to the recently added videos page`), |
43d0ea7f | 295 | |
20d21199 | 296 | new Hotkey('g l', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
297 | this.router.navigate([ '/videos/local' ]) |
298 | return false | |
66357162 | 299 | }, undefined, $localize`Go to the local videos page`), |
43d0ea7f | 300 | |
20d21199 | 301 | new Hotkey('g u', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
302 | this.router.navigate([ '/videos/upload' ]) |
303 | return false | |
66357162 | 304 | }, undefined, $localize`Go to the videos upload page`) |
ee1fc23a | 305 | ]) |
67167390 | 306 | } |
a9bfa85d C |
307 | |
308 | private loadUser () { | |
309 | const tokens = this.userLocalStorage.getTokens() | |
310 | if (!tokens) return | |
311 | ||
312 | const user = this.userLocalStorage.getLoggedInUser() | |
313 | if (!user) return | |
314 | ||
315 | // Initialize user | |
316 | this.authService.buildAuthUser(user, tokens) | |
317 | } | |
dc8bc31b | 318 | } |