]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/routing/redirect.service.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / core / routing / redirect.service.ts
CommitLineData
901637bb 1import { Injectable } from '@angular/core'
dae5ca24 2import { NavigationEnd, Router } from '@angular/router'
901637bb
C
3import { ServerService } from '../server'
4
5@Injectable()
6export class RedirectService {
7 // Default route could change according to the instance configuration
8 static INIT_DEFAULT_ROUTE = '/videos/trending'
9 static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
10
dae5ca24
C
11 private previousUrl: string
12 private currentUrl: string
13
901637bb
C
14 constructor (
15 private router: Router,
16 private serverService: ServerService
17 ) {
18 // The config is first loaded from the cache so try to get the default route
19 const config = this.serverService.getConfig()
20 if (config && config.instance && config.instance.defaultClientRoute) {
21 RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute
22 }
23
dae5ca24 24 // Load default route
901637bb 25 this.serverService.configLoaded
a51bad1a
C
26 .subscribe(() => {
27 const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute
901637bb 28
a51bad1a
C
29 if (defaultRouteConfig) {
30 RedirectService.DEFAULT_ROUTE = defaultRouteConfig
31 }
32 })
dae5ca24
C
33
34 // Track previous url
35 this.currentUrl = this.router.url
36 router.events.subscribe(event => {
37 if (event instanceof NavigationEnd) {
38 this.previousUrl = this.currentUrl
39 this.currentUrl = event.url
40 }
41 })
42 }
43
44 redirectToPreviousRoute () {
b247a132
C
45 const exceptions = [
46 '/verify-account'
47 ]
48
49 if (this.previousUrl) {
50 const isException = exceptions.find(e => this.previousUrl.startsWith(e))
51 if (!isException) return this.router.navigateByUrl(this.previousUrl)
52 }
dae5ca24
C
53
54 return this.redirectToHomepage()
901637bb
C
55 }
56
7cf26f43 57 redirectToHomepage (skipLocationChange = false) {
901637bb
C
58 console.log('Redirecting to %s...', RedirectService.DEFAULT_ROUTE)
59
7cf26f43 60 this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })
a51bad1a
C
61 .catch(() => {
62 console.error(
63 'Cannot navigate to %s, resetting default route to %s.',
64 RedirectService.DEFAULT_ROUTE,
65 RedirectService.INIT_DEFAULT_ROUTE
66 )
901637bb 67
a51bad1a 68 RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
7cf26f43 69 return this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })
a51bad1a 70 })
901637bb
C
71
72 }
901637bb 73}