]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/routing/redirect.service.ts
Fix anonymous user settings
[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'
9 static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
ba5d4a84
RK
10 static INIT_DEFAULT_TRENDING_ALGORITHM = 'most-viewed'
11 static DEFAULT_TRENDING_ALGORITHM = RedirectService.INIT_DEFAULT_TRENDING_ALGORITHM
901637bb 12
dae5ca24
C
13 private previousUrl: string
14 private currentUrl: string
15
68f6c87a
C
16 private redirectingToHomepage = false
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
RK
24 if (tmpConfig?.instance?.defaultClientRoute) {
25 RedirectService.DEFAULT_ROUTE = tmpConfig.instance.defaultClientRoute
26 }
27 if (tmpConfig?.trending?.videos?.algorithms?.default) {
28 RedirectService.DEFAULT_TRENDING_ALGORITHM = 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
a51bad1a
C
37 if (defaultRouteConfig) {
38 RedirectService.DEFAULT_ROUTE = defaultRouteConfig
39 }
3da68f0a
RK
40
41 if (defaultTrendingConfig) {
ba5d4a84 42 RedirectService.DEFAULT_TRENDING_ALGORITHM = defaultTrendingConfig
3da68f0a 43 }
a51bad1a 44 })
dae5ca24
C
45
46 // Track previous url
47 this.currentUrl = this.router.url
48 router.events.subscribe(event => {
d43c6b1f 49 if (event instanceof NavigationEnd || event instanceof NavigationCancel) {
dae5ca24
C
50 this.previousUrl = this.currentUrl
51 this.currentUrl = event.url
52 }
53 })
54 }
55
56 redirectToPreviousRoute () {
b247a132 57 const exceptions = [
f003ef1b
C
58 '/verify-account',
59 '/reset-password'
b247a132
C
60 ]
61
62 if (this.previousUrl) {
63 const isException = exceptions.find(e => this.previousUrl.startsWith(e))
64 if (!isException) return this.router.navigateByUrl(this.previousUrl)
65 }
dae5ca24
C
66
67 return this.redirectToHomepage()
901637bb
C
68 }
69
7cf26f43 70 redirectToHomepage (skipLocationChange = false) {
68f6c87a
C
71 if (this.redirectingToHomepage) return
72
73 this.redirectingToHomepage = true
74
901637bb
C
75 console.log('Redirecting to %s...', RedirectService.DEFAULT_ROUTE)
76
ba5d4a84 77 this.router.navigateByUrl(RedirectService.DEFAULT_ROUTE, { skipLocationChange })
68f6c87a 78 .then(() => this.redirectingToHomepage = false)
a51bad1a 79 .catch(() => {
68f6c87a
C
80 this.redirectingToHomepage = false
81
a51bad1a
C
82 console.error(
83 'Cannot navigate to %s, resetting default route to %s.',
84 RedirectService.DEFAULT_ROUTE,
85 RedirectService.INIT_DEFAULT_ROUTE
86 )
901637bb 87
a51bad1a 88 RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
ba5d4a84 89 return this.router.navigateByUrl(RedirectService.DEFAULT_ROUTE, { skipLocationChange })
a51bad1a 90 })
901637bb
C
91
92 }
901637bb 93}