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