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