]>
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) { |
c0e3d9ff | 49 | if ([ '/401', '/404' ].includes(event.url)) return |
9469783d | 50 | |
dae5ca24 C |
51 | this.previousUrl = this.currentUrl |
52 | this.currentUrl = event.url | |
13e7c3b0 C |
53 | |
54 | logger('Previous URL is %s, current URL is %s', this.previousUrl, this.currentUrl) | |
55 | logger('Setting %s as latest URL in session storage.', this.currentUrl) | |
56 | ||
57 | this.storage.setItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY, this.currentUrl) | |
dae5ca24 C |
58 | } |
59 | }) | |
60 | } | |
61 | ||
aea0b0e7 C |
62 | getDefaultRoute () { |
63 | return this.defaultRoute | |
64 | } | |
65 | ||
66 | getDefaultTrendingAlgorithm () { | |
67 | return this.defaultTrendingAlgorithm | |
68 | } | |
69 | ||
13e7c3b0 C |
70 | redirectToLatestSessionRoute () { |
71 | return this.doRedirect(this.latestSessionUrl) | |
cb28bb92 C |
72 | } |
73 | ||
74 | redirectToPreviousRoute (fallbackRoute?: string) { | |
13e7c3b0 C |
75 | return this.doRedirect(this.previousUrl, fallbackRoute) |
76 | } | |
a37e9e74 | 77 | |
13e7c3b0 C |
78 | getPreviousUrl () { |
79 | return this.previousUrl | |
901637bb C |
80 | } |
81 | ||
7cf26f43 | 82 | redirectToHomepage (skipLocationChange = false) { |
68f6c87a C |
83 | if (this.redirectingToHomepage) return |
84 | ||
85 | this.redirectingToHomepage = true | |
86 | ||
aea0b0e7 | 87 | console.log('Redirecting to %s...', this.defaultRoute) |
901637bb | 88 | |
aea0b0e7 | 89 | this.router.navigateByUrl(this.defaultRoute, { skipLocationChange }) |
68f6c87a | 90 | .then(() => this.redirectingToHomepage = false) |
a51bad1a | 91 | .catch(() => { |
68f6c87a C |
92 | this.redirectingToHomepage = false |
93 | ||
a51bad1a C |
94 | console.error( |
95 | 'Cannot navigate to %s, resetting default route to %s.', | |
aea0b0e7 | 96 | this.defaultRoute, |
a51bad1a C |
97 | RedirectService.INIT_DEFAULT_ROUTE |
98 | ) | |
901637bb | 99 | |
aea0b0e7 C |
100 | this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE |
101 | return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange }) | |
a51bad1a | 102 | }) |
901637bb C |
103 | |
104 | } | |
13e7c3b0 C |
105 | |
106 | private doRedirect (redirectUrl: string, fallbackRoute?: string) { | |
107 | logger('Redirecting on %s', redirectUrl) | |
108 | ||
109 | if (this.isValidRedirection(redirectUrl)) { | |
110 | return this.router.navigateByUrl(redirectUrl) | |
111 | } | |
112 | ||
113 | logger('%s is not a valid redirection, try fallback route %s', redirectUrl, fallbackRoute) | |
114 | if (fallbackRoute) { | |
115 | return this.router.navigateByUrl(fallbackRoute) | |
116 | } | |
117 | ||
118 | logger('There was no fallback route, redirecting to homepage') | |
119 | return this.redirectToHomepage() | |
120 | } | |
121 | ||
122 | private isValidRedirection (redirectUrl: string) { | |
123 | const exceptions = [ | |
124 | '/verify-account', | |
125 | '/reset-password', | |
126 | '/login' | |
127 | ] | |
128 | ||
129 | if (!redirectUrl || redirectUrl === '/') return false | |
130 | ||
131 | return exceptions.every(e => !redirectUrl.startsWith(e)) | |
132 | } | |
901637bb | 133 | } |