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 import { PluginsManager } from '@root-helpers/plugins-manager'
8 import { environment } from 'src/environments/environment'
10 const debugLogger = debug('peertube:router:RedirectService')
13 export class RedirectService {
14 private static SESSION_STORAGE_LATEST_SESSION_URL_KEY = 'redirect-latest-session-url'
16 // Default route could change according to the instance configuration
17 static INIT_DEFAULT_ROUTE = '/videos/trending'
18 static INIT_DEFAULT_TRENDING_ALGORITHM = 'most-viewed'
20 private previousUrl: string
21 private currentUrl: string
23 private latestSessionUrl: string
25 private redirectingToHomepage = false
26 private defaultTrendingAlgorithm = RedirectService.INIT_DEFAULT_TRENDING_ALGORITHM
27 private defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
30 private router: Router,
31 private serverService: ServerService,
32 private storage: SessionStorageService
38 const config = this.serverService.getHTMLConfig()
39 if (config.instance.defaultClientRoute) {
40 this.defaultRoute = config.instance.defaultClientRoute
42 if (config.trending.videos.algorithms.default) {
43 this.defaultTrendingAlgorithm = config.trending.videos.algorithms.default
46 this.latestSessionUrl = this.storage.getItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY)
47 this.storage.removeItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY)
49 debugLogger('Loaded latest session URL %s', this.latestSessionUrl)
52 this.currentUrl = this.router.url
53 this.router.events.subscribe(event => {
54 if (event instanceof NavigationEnd || event instanceof NavigationCancel) {
55 if ([ '/401', '/404' ].includes(event.url)) return
57 this.previousUrl = this.currentUrl
58 this.currentUrl = event.url
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)
63 this.storage.setItem(RedirectService.SESSION_STORAGE_LATEST_SESSION_URL_KEY, this.currentUrl)
69 return this.defaultRoute
72 getDefaultTrendingAlgorithm () {
73 return this.defaultTrendingAlgorithm
76 redirectToLatestSessionRoute () {
77 return this.doRedirect(this.latestSessionUrl)
80 redirectToPreviousRoute (fallbackRoute?: string) {
81 return this.doRedirect(this.previousUrl, fallbackRoute)
85 return this.previousUrl
88 redirectToHomepage (skipLocationChange = false) {
89 if (this.redirectingToHomepage) return
91 this.redirectingToHomepage = true
93 logger.info(`Redirecting to ${this.defaultRoute}...`)
95 this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
96 .then(() => this.redirectingToHomepage = false)
98 this.redirectingToHomepage = false
100 logger.error(`Cannot navigate to ${this.defaultRoute}, resetting default route to ${RedirectService.INIT_DEFAULT_ROUTE}`, err)
102 this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
103 return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
109 const externalLoginUrl = PluginsManager.getDefaultLoginHref(environment.apiUrl, this.serverService.getHTMLConfig())
111 if (externalLoginUrl) window.location.href = externalLoginUrl
112 else this.router.navigate([ '/login' ])
115 private doRedirect (redirectUrl: string, fallbackRoute?: string) {
116 debugLogger('Redirecting on %s', redirectUrl)
118 if (this.isValidRedirection(redirectUrl)) {
119 return this.router.navigateByUrl(redirectUrl)
122 debugLogger('%s is not a valid redirection, try fallback route %s', redirectUrl, fallbackRoute)
124 return this.router.navigateByUrl(fallbackRoute)
127 debugLogger('There was no fallback route, redirecting to homepage')
128 return this.redirectToHomepage()
131 private isValidRedirection (redirectUrl: string) {
138 if (!redirectUrl || redirectUrl === '/') return false
140 return exceptions.every(e => !redirectUrl.startsWith(e))