]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/routing/redirect.service.ts
Merge branch 'release/4.2.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 { ServerService } from '../server'
5 import { SessionStorageService } from '../wrappers/storage.service'
6
7 const logger = debug('peertube:router:RedirectService')
8
9 @Injectable()
10 export class RedirectService {
11 private static SESSION_STORAGE_LATEST_SESSION_URL_KEY = 'redirect-latest-session-url'
12
13 // Default route could change according to the instance configuration
14 static INIT_DEFAULT_ROUTE = '/videos/trending'
15 static INIT_DEFAULT_TRENDING_ALGORITHM = 'most-viewed'
16
17 private previousUrl: string
18 private currentUrl: string
19
20 private latestSessionUrl: string
21
22 private redirectingToHomepage = false
23 private defaultTrendingAlgorithm = RedirectService.INIT_DEFAULT_TRENDING_ALGORITHM
24 private defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
25
26 constructor (
27 private router: Router,
28 private serverService: ServerService,
29 private storage: SessionStorageService
30 ) {
31 // The config is first loaded from the cache so try to get the default route
32 const config = this.serverService.getHTMLConfig()
33 if (config?.instance?.defaultClientRoute) {
34 this.defaultRoute = config.instance.defaultClientRoute
35 }
36 if (config?.trending?.videos?.algorithms?.default) {
37 this.defaultTrendingAlgorithm = config.trending.videos.algorithms.default
38 }
39
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
45 // Track previous url
46 this.currentUrl = this.router.url
47 router.events.subscribe(event => {
48 if (event instanceof NavigationEnd || event instanceof NavigationCancel) {
49 this.previousUrl = this.currentUrl
50 this.currentUrl = event.url
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)
56 }
57 })
58 }
59
60 getDefaultRoute () {
61 return this.defaultRoute
62 }
63
64 getDefaultTrendingAlgorithm () {
65 return this.defaultTrendingAlgorithm
66 }
67
68 redirectToLatestSessionRoute () {
69 return this.doRedirect(this.latestSessionUrl)
70 }
71
72 redirectToPreviousRoute (fallbackRoute?: string) {
73 return this.doRedirect(this.previousUrl, fallbackRoute)
74 }
75
76 getPreviousUrl () {
77 return this.previousUrl
78 }
79
80 redirectToHomepage (skipLocationChange = false) {
81 if (this.redirectingToHomepage) return
82
83 this.redirectingToHomepage = true
84
85 console.log('Redirecting to %s...', this.defaultRoute)
86
87 this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
88 .then(() => this.redirectingToHomepage = false)
89 .catch(() => {
90 this.redirectingToHomepage = false
91
92 console.error(
93 'Cannot navigate to %s, resetting default route to %s.',
94 this.defaultRoute,
95 RedirectService.INIT_DEFAULT_ROUTE
96 )
97
98 this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
99 return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
100 })
101
102 }
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 }
131 }