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