]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/routing/redirect.service.ts
Add title to left menu toggle
[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
68f6c87a
C
14 private redirectingToHomepage = false
15
901637bb
C
16 constructor (
17 private router: Router,
18 private serverService: ServerService
19 ) {
20 // The config is first loaded from the cache so try to get the default route
ba430d75
C
21 const tmpConfig = this.serverService.getTmpConfig()
22 if (tmpConfig && tmpConfig.instance && tmpConfig.instance.defaultClientRoute) {
23 RedirectService.DEFAULT_ROUTE = tmpConfig.instance.defaultClientRoute
901637bb
C
24 }
25
dae5ca24 26 // Load default route
ba430d75
C
27 this.serverService.getConfig()
28 .subscribe(config => {
29 const defaultRouteConfig = config.instance.defaultClientRoute
901637bb 30
a51bad1a
C
31 if (defaultRouteConfig) {
32 RedirectService.DEFAULT_ROUTE = defaultRouteConfig
33 }
34 })
dae5ca24
C
35
36 // Track previous url
37 this.currentUrl = this.router.url
38 router.events.subscribe(event => {
39 if (event instanceof NavigationEnd) {
40 this.previousUrl = this.currentUrl
41 this.currentUrl = event.url
42 }
43 })
44 }
45
46 redirectToPreviousRoute () {
b247a132 47 const exceptions = [
f003ef1b
C
48 '/verify-account',
49 '/reset-password'
b247a132
C
50 ]
51
52 if (this.previousUrl) {
53 const isException = exceptions.find(e => this.previousUrl.startsWith(e))
54 if (!isException) return this.router.navigateByUrl(this.previousUrl)
55 }
dae5ca24
C
56
57 return this.redirectToHomepage()
901637bb
C
58 }
59
7cf26f43 60 redirectToHomepage (skipLocationChange = false) {
68f6c87a
C
61 if (this.redirectingToHomepage) return
62
63 this.redirectingToHomepage = true
64
901637bb
C
65 console.log('Redirecting to %s...', RedirectService.DEFAULT_ROUTE)
66
7cf26f43 67 this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })
68f6c87a 68 .then(() => this.redirectingToHomepage = false)
a51bad1a 69 .catch(() => {
68f6c87a
C
70 this.redirectingToHomepage = false
71
a51bad1a
C
72 console.error(
73 'Cannot navigate to %s, resetting default route to %s.',
74 RedirectService.DEFAULT_ROUTE,
75 RedirectService.INIT_DEFAULT_ROUTE
76 )
901637bb 77
a51bad1a 78 RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
7cf26f43 79 return this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })
a51bad1a 80 })
901637bb
C
81
82 }
901637bb 83}