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