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'
7 const logger = debug('peertube:router:RedirectService')
10 export class RedirectService {
11 private static SESSION_STORAGE_LATEST_SESSION_URL_KEY = 'redirect-latest-session-url'
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'
17 private previousUrl: string
18 private currentUrl: string
20 private latestSessionUrl: string
22 private redirectingToHomepage = false
23 private defaultTrendingAlgorithm = RedirectService.INIT_DEFAULT_TRENDING_ALGORITHM
24 private defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
27 private router: Router,
28 private serverService: ServerService,
29 private storage: SessionStorageService
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
36 if (config?.trending?.videos?.algorithms?.default) {
37 this.defaultTrendingAlgorithm = config.trending.videos.algorithms.default
40 this.latestSessionUrl = this.storage.getItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY)
41 this.storage.removeItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY)
43 logger('Loaded latest session URL %s', this.latestSessionUrl)
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
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)
55 this.storage.setItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY, this.currentUrl)
61 return this.defaultRoute
64 getDefaultTrendingAlgorithm () {
65 return this.defaultTrendingAlgorithm
68 redirectToLatestSessionRoute () {
69 return this.doRedirect(this.latestSessionUrl)
72 redirectToPreviousRoute (fallbackRoute?: string) {
73 return this.doRedirect(this.previousUrl, fallbackRoute)
77 return this.previousUrl
80 redirectToHomepage (skipLocationChange = false) {
81 if (this.redirectingToHomepage) return
83 this.redirectingToHomepage = true
85 console.log('Redirecting to %s...', this.defaultRoute)
87 this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
88 .then(() => this.redirectingToHomepage = false)
90 this.redirectingToHomepage = false
93 'Cannot navigate to %s, resetting default route to %s.',
95 RedirectService.INIT_DEFAULT_ROUTE
98 this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
99 return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
104 private doRedirect (redirectUrl: string, fallbackRoute?: string) {
105 logger('Redirecting on %s', redirectUrl)
107 if (this.isValidRedirection(redirectUrl)) {
108 return this.router.navigateByUrl(redirectUrl)
111 logger('%s is not a valid redirection, try fallback route %s', redirectUrl, fallbackRoute)
113 return this.router.navigateByUrl(fallbackRoute)
116 logger('There was no fallback route, redirecting to homepage')
117 return this.redirectToHomepage()
120 private isValidRedirection (redirectUrl: string) {
127 if (!redirectUrl || redirectUrl === '/') return false
129 return exceptions.every(e => !redirectUrl.startsWith(e))