]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/routing/redirect.service.ts
Load server config on app init
[github/Chocobozzz/PeerTube.git] / client / src / app / core / routing / redirect.service.ts
CommitLineData
901637bb 1import { Injectable } from '@angular/core'
d43c6b1f 2import { NavigationCancel, 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'
ba5d4a84 9 static INIT_DEFAULT_TRENDING_ALGORITHM = 'most-viewed'
901637bb 10
dae5ca24
C
11 private previousUrl: string
12 private currentUrl: string
13
68f6c87a 14 private redirectingToHomepage = false
aea0b0e7
C
15 private defaultTrendingAlgorithm = RedirectService.INIT_DEFAULT_TRENDING_ALGORITHM
16 private defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
68f6c87a 17
901637bb
C
18 constructor (
19 private router: Router,
20 private serverService: ServerService
21 ) {
22 // The config is first loaded from the cache so try to get the default route
ba430d75 23 const tmpConfig = this.serverService.getTmpConfig()
ba5d4a84 24 if (tmpConfig?.instance?.defaultClientRoute) {
aea0b0e7 25 this.defaultRoute = tmpConfig.instance.defaultClientRoute
ba5d4a84
RK
26 }
27 if (tmpConfig?.trending?.videos?.algorithms?.default) {
aea0b0e7 28 this.defaultTrendingAlgorithm = tmpConfig.trending.videos.algorithms.default
901637bb
C
29 }
30
dae5ca24 31 // Load default route
ba430d75
C
32 this.serverService.getConfig()
33 .subscribe(config => {
34 const defaultRouteConfig = config.instance.defaultClientRoute
ba5d4a84 35 const defaultTrendingConfig = config.trending.videos.algorithms.default
901637bb 36
aea0b0e7
C
37 if (defaultRouteConfig) this.defaultRoute = defaultRouteConfig
38 if (defaultTrendingConfig) this.defaultTrendingAlgorithm = defaultTrendingConfig
a51bad1a 39 })
dae5ca24
C
40
41 // Track previous url
42 this.currentUrl = this.router.url
43 router.events.subscribe(event => {
d43c6b1f 44 if (event instanceof NavigationEnd || event instanceof NavigationCancel) {
dae5ca24
C
45 this.previousUrl = this.currentUrl
46 this.currentUrl = event.url
47 }
48 })
49 }
50
aea0b0e7
C
51 getDefaultRoute () {
52 return this.defaultRoute
53 }
54
55 getDefaultTrendingAlgorithm () {
56 return this.defaultTrendingAlgorithm
57 }
58
dae5ca24 59 redirectToPreviousRoute () {
b247a132 60 const exceptions = [
f003ef1b
C
61 '/verify-account',
62 '/reset-password'
b247a132
C
63 ]
64
65 if (this.previousUrl) {
66 const isException = exceptions.find(e => this.previousUrl.startsWith(e))
67 if (!isException) return this.router.navigateByUrl(this.previousUrl)
68 }
dae5ca24
C
69
70 return this.redirectToHomepage()
901637bb
C
71 }
72
7cf26f43 73 redirectToHomepage (skipLocationChange = false) {
68f6c87a
C
74 if (this.redirectingToHomepage) return
75
76 this.redirectingToHomepage = true
77
aea0b0e7 78 console.log('Redirecting to %s...', this.defaultRoute)
901637bb 79
aea0b0e7 80 this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
68f6c87a 81 .then(() => this.redirectingToHomepage = false)
a51bad1a 82 .catch(() => {
68f6c87a
C
83 this.redirectingToHomepage = false
84
a51bad1a
C
85 console.error(
86 'Cannot navigate to %s, resetting default route to %s.',
aea0b0e7 87 this.defaultRoute,
a51bad1a
C
88 RedirectService.INIT_DEFAULT_ROUTE
89 )
901637bb 90
aea0b0e7
C
91 this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
92 return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
a51bad1a 93 })
901637bb
C
94
95 }
901637bb 96}