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