From 901637bb87f5eb0518fb7ca69d98b53ed918339e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 1 Mar 2018 13:57:29 +0100 Subject: Add ability to change the homepage --- .../edit-custom-config.component.html | 10 +++++ .../edit-custom-config.component.ts | 6 ++- client/src/app/app-routing.module.ts | 10 ++--- client/src/app/app.component.ts | 9 +++- client/src/app/core/core.module.ts | 5 ++- client/src/app/core/routing/index.ts | 1 + client/src/app/core/routing/redirect.service.ts | 48 ++++++++++++++++++++++ client/src/app/core/server/server.service.ts | 1 + .../videos/+video-watch/video-watch.component.ts | 13 +++--- config/default.yaml | 1 + config/production.yaml.example | 1 + server/controllers/api/config.ts | 4 ++ server/initializers/constants.ts | 1 + server/middlewares/validators/config.ts | 6 +++ server/tests/api/check-params/config.ts | 1 + server/tests/api/server/config.ts | 5 +++ shared/models/server/custom-config.model.ts | 1 + shared/models/server/server-config.model.ts | 3 +- 18 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 client/src/app/core/routing/redirect.service.ts diff --git a/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.html b/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.html index 18ba7ba06..c7ddaaf01 100644 --- a/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.html +++ b/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.html @@ -37,6 +37,16 @@ +
+ +
+ +
+
+
Cache
diff --git a/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts b/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts index cf93b4060..c38bc326a 100644 --- a/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts +++ b/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts @@ -46,6 +46,7 @@ export class EditCustomConfigComponent extends FormReactive implements OnInit { instanceName: '', instanceDescription: '', instanceTerms: '', + instanceDefaultClientRoute: '', cachePreviewsSize: '', signupLimit: '', adminEmail: '', @@ -85,6 +86,7 @@ export class EditCustomConfigComponent extends FormReactive implements OnInit { instanceName: [ '', INSTANCE_NAME.VALIDATORS ], instanceDescription: [ '' ], instanceTerms: [ '' ], + instanceDefaultClientRoute: [ '' ], cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ], signupEnabled: [ ], signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ], @@ -153,11 +155,12 @@ export class EditCustomConfigComponent extends FormReactive implements OnInit { if (confirmRes === false) return } - const data = { + const data: CustomConfig = { instance: { name: this.form.value['instanceName'], description: this.form.value['instanceDescription'], terms: this.form.value['instanceTerms'], + defaultClientRoute: this.form.value['instanceDefaultClientRoute'], customizations: { javascript: this.form.value['customizationJavascript'], css: this.form.value['customizationCSS'] @@ -213,6 +216,7 @@ export class EditCustomConfigComponent extends FormReactive implements OnInit { instanceName: this.customConfig.instance.name, instanceDescription: this.customConfig.instance.description, instanceTerms: this.customConfig.instance.terms, + instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute, cachePreviewsSize: this.customConfig.cache.previews.size, signupEnabled: this.customConfig.signup.enabled, signupLimit: this.customConfig.signup.limit, diff --git a/client/src/app/app-routing.module.ts b/client/src/app/app-routing.module.ts index f31b51e23..c8a6b3924 100644 --- a/client/src/app/app-routing.module.ts +++ b/client/src/app/app-routing.module.ts @@ -1,14 +1,10 @@ import { NgModule } from '@angular/core' import { Routes, RouterModule } from '@angular/router' +import { RedirectService } from '@app/core/routing/redirect.service' import { PreloadSelectedModulesList } from './core' const routes: Routes = [ - { - path: '', - redirectTo: '/videos/trending', - pathMatch: 'full' - }, { path: 'admin', loadChildren: './+admin/admin.module#AdminModule' @@ -22,7 +18,9 @@ const routes: Routes = [ preloadingStrategy: PreloadSelectedModulesList }) ], - providers: [ PreloadSelectedModulesList ], + providers: [ + PreloadSelectedModulesList + ], exports: [ RouterModule ] }) export class AppRoutingModule {} diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index 25936146c..346e966e5 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts @@ -1,7 +1,7 @@ import { Component, OnInit } from '@angular/core' import { DomSanitizer, SafeHtml } from '@angular/platform-browser' import { GuardsCheckStart, Router } from '@angular/router' -import { AuthService, ServerService } from '@app/core' +import { AuthService, RedirectService, ServerService } from '@app/core' import { isInSmallView } from '@app/shared/misc/utils' @Component({ @@ -31,7 +31,8 @@ export class AppComponent implements OnInit { private router: Router, private authService: AuthService, private serverService: ServerService, - private domSanitizer: DomSanitizer + private domSanitizer: DomSanitizer, + private redirectService: RedirectService ) {} get serverVersion () { @@ -43,6 +44,10 @@ export class AppComponent implements OnInit { } ngOnInit () { + if (this.router.url === '/') { + this.redirectService.redirectToHomepage() + } + this.authService.loadClientCredentials() if (this.authService.isLoggedIn()) { diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.ts index 708831965..c2de2084e 100644 --- a/client/src/app/core/core.module.ts +++ b/client/src/app/core/core.module.ts @@ -13,7 +13,7 @@ import { ModalModule } from 'ngx-bootstrap/modal' import { AuthService } from './auth' import { ConfirmComponent, ConfirmService } from './confirm' import { throwIfAlreadyLoaded } from './module-import-guard' -import { LoginGuard, UserRightGuard } from './routing' +import { LoginGuard, RedirectService, UserRightGuard } from './routing' import { ServerService } from './server' @NgModule({ @@ -48,7 +48,8 @@ import { ServerService } from './server' ConfirmService, ServerService, LoginGuard, - UserRightGuard + UserRightGuard, + RedirectService ] }) export class CoreModule { diff --git a/client/src/app/core/routing/index.ts b/client/src/app/core/routing/index.ts index d1b982834..9f0b4eac5 100644 --- a/client/src/app/core/routing/index.ts +++ b/client/src/app/core/routing/index.ts @@ -1,3 +1,4 @@ export * from './login-guard.service' export * from './user-right-guard.service' export * from './preload-selected-modules-list' +export * from './redirect.service' diff --git a/client/src/app/core/routing/redirect.service.ts b/client/src/app/core/routing/redirect.service.ts new file mode 100644 index 000000000..a0125e0ae --- /dev/null +++ b/client/src/app/core/routing/redirect.service.ts @@ -0,0 +1,48 @@ +import { Injectable } from '@angular/core' +import { Router } from '@angular/router' +import { ServerService } from '../server' + +@Injectable() +export class RedirectService { + // Default route could change according to the instance configuration + static INIT_DEFAULT_ROUTE = '/videos/trending' + static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE + + constructor ( + private router: Router, + private serverService: ServerService + ) { + // The config is first loaded from the cache so try to get the default route + const config = this.serverService.getConfig() + if (config && config.instance && config.instance.defaultClientRoute) { + RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute + } + + this.serverService.configLoaded + .subscribe(() => { + const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute + + if (defaultRouteConfig) { + RedirectService.DEFAULT_ROUTE = defaultRouteConfig + } + }) + } + + redirectToHomepage () { + console.log('Redirecting to %s...', RedirectService.DEFAULT_ROUTE) + + this.router.navigate([ RedirectService.DEFAULT_ROUTE ]) + .catch(() => { + console.error( + 'Cannot navigate to %s, resetting default route to %s.', + RedirectService.DEFAULT_ROUTE, + RedirectService.INIT_DEFAULT_ROUTE + ) + + RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE + return this.router.navigate([ RedirectService.DEFAULT_ROUTE ]) + }) + + } + +} diff --git a/client/src/app/core/server/server.service.ts b/client/src/app/core/server/server.service.ts index 984738948..2135c3268 100644 --- a/client/src/app/core/server/server.service.ts +++ b/client/src/app/core/server/server.service.ts @@ -21,6 +21,7 @@ export class ServerService { private config: ServerConfig = { instance: { name: 'PeerTube', + defaultClientRoute: '', customizations: { javascript: '', css: '' diff --git a/client/src/app/videos/+video-watch/video-watch.component.ts b/client/src/app/videos/+video-watch/video-watch.component.ts index 585ab2e00..66ef0399a 100644 --- a/client/src/app/videos/+video-watch/video-watch.component.ts +++ b/client/src/app/videos/+video-watch/video-watch.component.ts @@ -1,9 +1,9 @@ import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' import { ActivatedRoute, Router } from '@angular/router' +import { RedirectService } from '@app/core/routing/redirect.service' import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component' import { MetaService } from '@ngx-meta/core' import { NotificationsService } from 'angular2-notifications' -import { Observable } from 'rxjs/Observable' import { Subscription } from 'rxjs/Subscription' import * as videojs from 'video.js' import 'videojs-hotkeys' @@ -64,7 +64,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy { private authService: AuthService, private notificationsService: NotificationsService, private markdownService: MarkdownService, - private zone: NgZone + private zone: NgZone, + private redirectService: RedirectService ) {} get user () { @@ -142,7 +143,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { .subscribe( status => { this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`) - this.router.navigate(['/videos/list']) + this.redirectService.redirectToHomepage() }, error => this.notificationsService.error('Error', error.message) @@ -247,7 +248,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { this.notificationsService.success('Success', `Video ${this.video.name} deleted.`) // Go back to the video-list. - this.router.navigate([ '/videos/list' ]) + this.redirectService.redirectToHomepage() }, error => this.notificationsService.error('Error', error.message) @@ -313,7 +314,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { 'This video contains mature or explicit content. Are you sure you want to watch it?', 'Mature or explicit content' ) - if (res === false) return this.router.navigate([ '/videos/list' ]) + if (res === false) return this.redirectService.redirectToHomepage() } if (!this.hasAlreadyAcceptedPrivacyConcern()) { @@ -323,7 +324,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { 'Privacy concern', 'I accept!' ) - if (res === false) return this.router.navigate([ '/videos/list' ]) + if (res === false) return this.redirectService.redirectToHomepage() } this.acceptedPrivacyConcern() diff --git a/config/default.yaml b/config/default.yaml index a634be61c..5389f1164 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -74,6 +74,7 @@ instance: name: 'PeerTube' description: 'Welcome to this PeerTube instance!' # Support markdown terms: 'No terms for now.' # Support markdown + default_client_route: '/videos/trending' customizations: javascript: '' # Directly your JavaScript code (without