]>
Commit | Line | Data |
---|---|---|
13e7c3b0 | 1 | import * as debug from 'debug' |
901637bb | 2 | import { Injectable } from '@angular/core' |
d43c6b1f | 3 | import { NavigationCancel, NavigationEnd, Router } from '@angular/router' |
901637bb | 4 | import { ServerService } from '../server' |
13e7c3b0 C |
5 | import { SessionStorageService } from '../wrappers/storage.service' |
6 | ||
7 | const logger = debug('peertube:router:RedirectService') | |
901637bb C |
8 | |
9 | @Injectable() | |
10 | export class RedirectService { | |
13e7c3b0 C |
11 | private static SESSION_STORAGE_LATEST_SESSION_URL_KEY = 'redirect-latest-session-url' |
12 | ||
901637bb C |
13 | // Default route could change according to the instance configuration |
14 | static INIT_DEFAULT_ROUTE = '/videos/trending' | |
ba5d4a84 | 15 | static INIT_DEFAULT_TRENDING_ALGORITHM = 'most-viewed' |
901637bb | 16 | |
dae5ca24 C |
17 | private previousUrl: string |
18 | private currentUrl: string | |
19 | ||
13e7c3b0 C |
20 | private latestSessionUrl: string |
21 | ||
68f6c87a | 22 | private redirectingToHomepage = false |
aea0b0e7 C |
23 | private defaultTrendingAlgorithm = RedirectService.INIT_DEFAULT_TRENDING_ALGORITHM |
24 | private defaultRoute = RedirectService.INIT_DEFAULT_ROUTE | |
68f6c87a | 25 | |
901637bb C |
26 | constructor ( |
27 | private router: Router, | |
13e7c3b0 C |
28 | private serverService: ServerService, |
29 | private storage: SessionStorageService | |
901637bb C |
30 | ) { |
31 | // The config is first loaded from the cache so try to get the default route | |
2989628b C |
32 | const config = this.serverService.getHTMLConfig() |
33 | if (config?.instance?.defaultClientRoute) { | |
34 | this.defaultRoute = config.instance.defaultClientRoute | |
ba5d4a84 | 35 | } |
2989628b C |
36 | if (config?.trending?.videos?.algorithms?.default) { |
37 | this.defaultTrendingAlgorithm = config.trending.videos.algorithms.default | |
901637bb C |
38 | } |
39 | ||
13e7c3b0 C |
40 | this.latestSessionUrl = this.storage.getItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY) |
41 | this.storage.removeItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY) | |
42 | ||
43 | logger('Loaded latest session URL %s', this.latestSessionUrl) | |
44 | ||
dae5ca24 C |
45 | // Track previous url |
46 | this.currentUrl = this.router.url | |
47 | router.events.subscribe(event => { | |
d43c6b1f | 48 | if (event instanceof NavigationEnd || event instanceof NavigationCancel) { |
dae5ca24 C |
49 | this.previousUrl = this.currentUrl |
50 | this.currentUrl = event.url | |
13e7c3b0 C |
51 | |
52 | logger('Previous URL is %s, current URL is %s', this.previousUrl, this.currentUrl) | |
53 | logger('Setting %s as latest URL in session storage.', this.currentUrl) | |
54 | ||
55 | this.storage.setItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY, this.currentUrl) | |
dae5ca24 C |
56 | } |
57 | }) | |
58 | } | |
59 | ||
aea0b0e7 C |
60 | getDefaultRoute () { |
61 | return this.defaultRoute | |
62 | } | |
63 | ||
64 | getDefaultTrendingAlgorithm () { | |
65 | return this.defaultTrendingAlgorithm | |
66 | } | |
67 | ||
13e7c3b0 C |
68 | redirectToLatestSessionRoute () { |
69 | return this.doRedirect(this.latestSessionUrl) | |
cb28bb92 C |
70 | } |
71 | ||
72 | redirectToPreviousRoute (fallbackRoute?: string) { | |
13e7c3b0 C |
73 | return this.doRedirect(this.previousUrl, fallbackRoute) |
74 | } | |
a37e9e74 | 75 | |
13e7c3b0 C |
76 | getPreviousUrl () { |
77 | return this.previousUrl | |
901637bb C |
78 | } |
79 | ||
7cf26f43 | 80 | redirectToHomepage (skipLocationChange = false) { |
68f6c87a C |
81 | if (this.redirectingToHomepage) return |
82 | ||
83 | this.redirectingToHomepage = true | |
84 | ||
aea0b0e7 | 85 | console.log('Redirecting to %s...', this.defaultRoute) |
901637bb | 86 | |
aea0b0e7 | 87 | this.router.navigateByUrl(this.defaultRoute, { skipLocationChange }) |
68f6c87a | 88 | .then(() => this.redirectingToHomepage = false) |
a51bad1a | 89 | .catch(() => { |
68f6c87a C |
90 | this.redirectingToHomepage = false |
91 | ||
a51bad1a C |
92 | console.error( |
93 | 'Cannot navigate to %s, resetting default route to %s.', | |
aea0b0e7 | 94 | this.defaultRoute, |
a51bad1a C |
95 | RedirectService.INIT_DEFAULT_ROUTE |
96 | ) | |
901637bb | 97 | |
aea0b0e7 C |
98 | this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE |
99 | return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange }) | |
a51bad1a | 100 | }) |
901637bb C |
101 | |
102 | } | |
13e7c3b0 C |
103 | |
104 | private doRedirect (redirectUrl: string, fallbackRoute?: string) { | |
105 | logger('Redirecting on %s', redirectUrl) | |
106 | ||
107 | if (this.isValidRedirection(redirectUrl)) { | |
108 | return this.router.navigateByUrl(redirectUrl) | |
109 | } | |
110 | ||
111 | logger('%s is not a valid redirection, try fallback route %s', redirectUrl, fallbackRoute) | |
112 | if (fallbackRoute) { | |
113 | return this.router.navigateByUrl(fallbackRoute) | |
114 | } | |
115 | ||
116 | logger('There was no fallback route, redirecting to homepage') | |
117 | return this.redirectToHomepage() | |
118 | } | |
119 | ||
120 | private isValidRedirection (redirectUrl: string) { | |
121 | const exceptions = [ | |
122 | '/verify-account', | |
123 | '/reset-password', | |
124 | '/login' | |
125 | ] | |
126 | ||
127 | if (!redirectUrl || redirectUrl === '/') return false | |
128 | ||
129 | return exceptions.every(e => !redirectUrl.startsWith(e)) | |
130 | } | |
901637bb | 131 | } |