]>
Commit | Line | Data |
---|---|---|
901637bb | 1 | import { Injectable } from '@angular/core' |
d43c6b1f | 2 | import { NavigationCancel, NavigationEnd, Router } from '@angular/router' |
901637bb C |
3 | import { ServerService } from '../server' |
4 | ||
5 | @Injectable() | |
6 | export 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 | |
2989628b C |
23 | const config = this.serverService.getHTMLConfig() |
24 | if (config?.instance?.defaultClientRoute) { | |
25 | this.defaultRoute = config.instance.defaultClientRoute | |
ba5d4a84 | 26 | } |
2989628b C |
27 | if (config?.trending?.videos?.algorithms?.default) { |
28 | this.defaultTrendingAlgorithm = config.trending.videos.algorithms.default | |
901637bb C |
29 | } |
30 | ||
dae5ca24 C |
31 | // Track previous url |
32 | this.currentUrl = this.router.url | |
33 | router.events.subscribe(event => { | |
d43c6b1f | 34 | if (event instanceof NavigationEnd || event instanceof NavigationCancel) { |
dae5ca24 C |
35 | this.previousUrl = this.currentUrl |
36 | this.currentUrl = event.url | |
37 | } | |
38 | }) | |
39 | } | |
40 | ||
aea0b0e7 C |
41 | getDefaultRoute () { |
42 | return this.defaultRoute | |
43 | } | |
44 | ||
45 | getDefaultTrendingAlgorithm () { | |
46 | return this.defaultTrendingAlgorithm | |
47 | } | |
48 | ||
dae5ca24 | 49 | redirectToPreviousRoute () { |
b247a132 | 50 | const exceptions = [ |
f003ef1b C |
51 | '/verify-account', |
52 | '/reset-password' | |
b247a132 C |
53 | ] |
54 | ||
55 | if (this.previousUrl) { | |
56 | const isException = exceptions.find(e => this.previousUrl.startsWith(e)) | |
57 | if (!isException) return this.router.navigateByUrl(this.previousUrl) | |
58 | } | |
dae5ca24 C |
59 | |
60 | return this.redirectToHomepage() | |
901637bb C |
61 | } |
62 | ||
7cf26f43 | 63 | redirectToHomepage (skipLocationChange = false) { |
68f6c87a C |
64 | if (this.redirectingToHomepage) return |
65 | ||
66 | this.redirectingToHomepage = true | |
67 | ||
aea0b0e7 | 68 | console.log('Redirecting to %s...', this.defaultRoute) |
901637bb | 69 | |
aea0b0e7 | 70 | this.router.navigateByUrl(this.defaultRoute, { skipLocationChange }) |
68f6c87a | 71 | .then(() => this.redirectingToHomepage = false) |
a51bad1a | 72 | .catch(() => { |
68f6c87a C |
73 | this.redirectingToHomepage = false |
74 | ||
a51bad1a C |
75 | console.error( |
76 | 'Cannot navigate to %s, resetting default route to %s.', | |
aea0b0e7 | 77 | this.defaultRoute, |
a51bad1a C |
78 | RedirectService.INIT_DEFAULT_ROUTE |
79 | ) | |
901637bb | 80 | |
aea0b0e7 C |
81 | this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE |
82 | return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange }) | |
a51bad1a | 83 | }) |
901637bb C |
84 | |
85 | } | |
901637bb | 86 | } |