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'
8 const debugLogger = debug('peertube:router:RedirectService')
11 export class RedirectService {
12 private static SESSION_STORAGE_LATEST_SESSION_URL_KEY = 'redirect-latest-session-url'
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'
18 private previousUrl: string
19 private currentUrl: string
21 private latestSessionUrl: string
23 private redirectingToHomepage = false
24 private defaultTrendingAlgorithm = RedirectService.INIT_DEFAULT_TRENDING_ALGORITHM
25 private defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
28 private router: Router,
29 private serverService: ServerService,
30 private storage: SessionStorageService
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
37 if (config?.trending?.videos?.algorithms?.default) {
38 this.defaultTrendingAlgorithm = config.trending.videos.algorithms.default
41 this.latestSessionUrl = this.storage.getItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY)
42 this.storage.removeItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY)
44 debugLogger('Loaded latest session URL %s', this.latestSessionUrl)
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
52 this.previousUrl = this.currentUrl
53 this.currentUrl = event.url
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)
58 this.storage.setItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY, this.currentUrl)
64 return this.defaultRoute
67 getDefaultTrendingAlgorithm () {
68 return this.defaultTrendingAlgorithm
71 redirectToLatestSessionRoute () {
72 return this.doRedirect(this.latestSessionUrl)
75 redirectToPreviousRoute (fallbackRoute?: string) {
76 return this.doRedirect(this.previousUrl, fallbackRoute)
80 return this.previousUrl
83 redirectToHomepage (skipLocationChange = false) {
84 if (this.redirectingToHomepage) return
86 this.redirectingToHomepage = true
88 logger.info(`Redirecting to ${this.defaultRoute}...`)
90 this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
91 .then(() => this.redirectingToHomepage = false)
93 this.redirectingToHomepage = false
95 logger.error(`Cannot navigate to ${this.defaultRoute}, resetting default route to ${RedirectService.INIT_DEFAULT_ROUTE}`, err)
97 this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
98 return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
103 private doRedirect (redirectUrl: string, fallbackRoute?: string) {
104 debugLogger('Redirecting on %s', redirectUrl)
106 if (this.isValidRedirection(redirectUrl)) {
107 return this.router.navigateByUrl(redirectUrl)
110 debugLogger('%s is not a valid redirection, try fallback route %s', redirectUrl, fallbackRoute)
112 return this.router.navigateByUrl(fallbackRoute)
115 debugLogger('There was no fallback route, redirecting to homepage')
116 return this.redirectToHomepage()
119 private isValidRedirection (redirectUrl: string) {
126 if (!redirectUrl || redirectUrl === '/') return false
128 return exceptions.every(e => !redirectUrl.startsWith(e))