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