]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/routing/redirect.service.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / core / routing / redirect.service.ts
CommitLineData
13e7c3b0 1import * as debug from 'debug'
901637bb 2import { Injectable } from '@angular/core'
d43c6b1f 3import { NavigationCancel, NavigationEnd, Router } from '@angular/router'
42b40636 4import { logger } from '@root-helpers/logger'
901637bb 5import { ServerService } from '../server'
13e7c3b0 6import { SessionStorageService } from '../wrappers/storage.service'
98bd5e22
C
7import { PluginsManager } from '@root-helpers/plugins-manager'
8import { environment } from 'src/environments/environment'
13e7c3b0 9
42b40636 10const debugLogger = debug('peertube:router:RedirectService')
901637bb
C
11
12@Injectable()
13export class RedirectService {
13e7c3b0
C
14 private static SESSION_STORAGE_LATEST_SESSION_URL_KEY = 'redirect-latest-session-url'
15
901637bb
C
16 // Default route could change according to the instance configuration
17 static INIT_DEFAULT_ROUTE = '/videos/trending'
ba5d4a84 18 static INIT_DEFAULT_TRENDING_ALGORITHM = 'most-viewed'
901637bb 19
dae5ca24
C
20 private previousUrl: string
21 private currentUrl: string
22
13e7c3b0
C
23 private latestSessionUrl: string
24
68f6c87a 25 private redirectingToHomepage = false
aea0b0e7
C
26 private defaultTrendingAlgorithm = RedirectService.INIT_DEFAULT_TRENDING_ALGORITHM
27 private defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
68f6c87a 28
901637bb
C
29 constructor (
30 private router: Router,
13e7c3b0
C
31 private serverService: ServerService,
32 private storage: SessionStorageService
901637bb 33 ) {
15825ef1
C
34
35 }
36
37 init () {
2989628b 38 const config = this.serverService.getHTMLConfig()
15825ef1 39 if (config.instance.defaultClientRoute) {
2989628b 40 this.defaultRoute = config.instance.defaultClientRoute
ba5d4a84 41 }
15825ef1 42 if (config.trending.videos.algorithms.default) {
2989628b 43 this.defaultTrendingAlgorithm = config.trending.videos.algorithms.default
901637bb
C
44 }
45
13e7c3b0
C
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
42b40636 49 debugLogger('Loaded latest session URL %s', this.latestSessionUrl)
13e7c3b0 50
dae5ca24
C
51 // Track previous url
52 this.currentUrl = this.router.url
15825ef1 53 this.router.events.subscribe(event => {
d43c6b1f 54 if (event instanceof NavigationEnd || event instanceof NavigationCancel) {
c0e3d9ff 55 if ([ '/401', '/404' ].includes(event.url)) return
9469783d 56
dae5ca24
C
57 this.previousUrl = this.currentUrl
58 this.currentUrl = event.url
13e7c3b0 59
42b40636
C
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)
13e7c3b0
C
62
63 this.storage.setItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY, this.currentUrl)
dae5ca24
C
64 }
65 })
66 }
67
aea0b0e7
C
68 getDefaultRoute () {
69 return this.defaultRoute
70 }
71
72 getDefaultTrendingAlgorithm () {
73 return this.defaultTrendingAlgorithm
74 }
75
13e7c3b0
C
76 redirectToLatestSessionRoute () {
77 return this.doRedirect(this.latestSessionUrl)
cb28bb92
C
78 }
79
80 redirectToPreviousRoute (fallbackRoute?: string) {
13e7c3b0
C
81 return this.doRedirect(this.previousUrl, fallbackRoute)
82 }
a37e9e74 83
13e7c3b0
C
84 getPreviousUrl () {
85 return this.previousUrl
901637bb
C
86 }
87
7cf26f43 88 redirectToHomepage (skipLocationChange = false) {
68f6c87a
C
89 if (this.redirectingToHomepage) return
90
91 this.redirectingToHomepage = true
92
42b40636 93 logger.info(`Redirecting to ${this.defaultRoute}...`)
901637bb 94
aea0b0e7 95 this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
68f6c87a 96 .then(() => this.redirectingToHomepage = false)
ca0f0307 97 .catch(err => {
68f6c87a
C
98 this.redirectingToHomepage = false
99
ca0f0307 100 logger.error(`Cannot navigate to ${this.defaultRoute}, resetting default route to ${RedirectService.INIT_DEFAULT_ROUTE}`, err)
901637bb 101
aea0b0e7
C
102 this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
103 return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
a51bad1a 104 })
901637bb
C
105
106 }
13e7c3b0 107
98bd5e22
C
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
13e7c3b0 115 private doRedirect (redirectUrl: string, fallbackRoute?: string) {
42b40636 116 debugLogger('Redirecting on %s', redirectUrl)
13e7c3b0
C
117
118 if (this.isValidRedirection(redirectUrl)) {
119 return this.router.navigateByUrl(redirectUrl)
120 }
121
42b40636 122 debugLogger('%s is not a valid redirection, try fallback route %s', redirectUrl, fallbackRoute)
13e7c3b0
C
123 if (fallbackRoute) {
124 return this.router.navigateByUrl(fallbackRoute)
125 }
126
42b40636 127 debugLogger('There was no fallback route, redirecting to homepage')
13e7c3b0
C
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 }
901637bb 142}