PeerTube/client/src/app/core/routing/redirect.service.ts

91 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-03-01 13:57:29 +01:00
import { Injectable } from '@angular/core'
2021-01-14 14:13:23 +01:00
import { NavigationCancel, NavigationEnd, Router } from '@angular/router'
2018-03-01 13:57:29 +01:00
import { ServerService } from '../server'
@Injectable()
export class RedirectService {
// Default route could change according to the instance configuration
static INIT_DEFAULT_ROUTE = '/videos/trending'
2021-01-27 17:15:21 +01:00
static INIT_DEFAULT_TRENDING_ALGORITHM = 'most-viewed'
2018-03-01 13:57:29 +01:00
2018-12-11 15:27:46 +01:00
private previousUrl: string
private currentUrl: string
2020-11-27 15:31:09 +01:00
private redirectingToHomepage = false
2021-05-14 12:04:44 +02:00
private defaultTrendingAlgorithm = RedirectService.INIT_DEFAULT_TRENDING_ALGORITHM
private defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
2020-11-27 15:31:09 +01:00
2018-03-01 13:57:29 +01:00
constructor (
private router: Router,
private serverService: ServerService
) {
// The config is first loaded from the cache so try to get the default route
2021-06-04 13:31:41 +02:00
const config = this.serverService.getHTMLConfig()
if (config?.instance?.defaultClientRoute) {
this.defaultRoute = config.instance.defaultClientRoute
2021-01-27 17:15:21 +01:00
}
2021-06-04 13:31:41 +02:00
if (config?.trending?.videos?.algorithms?.default) {
this.defaultTrendingAlgorithm = config.trending.videos.algorithms.default
2018-03-01 13:57:29 +01:00
}
2018-12-11 15:27:46 +01:00
// Track previous url
this.currentUrl = this.router.url
router.events.subscribe(event => {
2021-01-14 14:13:23 +01:00
if (event instanceof NavigationEnd || event instanceof NavigationCancel) {
2018-12-11 15:27:46 +01:00
this.previousUrl = this.currentUrl
this.currentUrl = event.url
}
})
}
2021-05-14 12:04:44 +02:00
getDefaultRoute () {
return this.defaultRoute
}
getDefaultTrendingAlgorithm () {
return this.defaultTrendingAlgorithm
}
redirectToPreviousRoute (fallbackRoute: string[] = null) {
2019-05-29 14:39:49 +02:00
const exceptions = [
'/verify-account',
'/reset-password'
2019-05-29 14:39:49 +02:00
]
if (this.previousUrl) {
const isException = exceptions.find(e => this.previousUrl.startsWith(e))
if (!isException) return this.router.navigateByUrl(this.previousUrl)
}
2018-12-11 15:27:46 +01:00
if (fallbackRoute) {
return this.router.navigate(fallbackRoute)
}
2018-12-11 15:27:46 +01:00
return this.redirectToHomepage()
2018-03-01 13:57:29 +01:00
}
2018-06-08 11:25:12 +02:00
redirectToHomepage (skipLocationChange = false) {
2020-11-27 15:31:09 +01:00
if (this.redirectingToHomepage) return
this.redirectingToHomepage = true
2021-05-14 12:04:44 +02:00
console.log('Redirecting to %s...', this.defaultRoute)
2018-03-01 13:57:29 +01:00
2021-05-14 12:04:44 +02:00
this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
2020-11-27 15:31:09 +01:00
.then(() => this.redirectingToHomepage = false)
2018-05-31 11:35:01 +02:00
.catch(() => {
2020-11-27 15:31:09 +01:00
this.redirectingToHomepage = false
2018-05-31 11:35:01 +02:00
console.error(
'Cannot navigate to %s, resetting default route to %s.',
2021-05-14 12:04:44 +02:00
this.defaultRoute,
2018-05-31 11:35:01 +02:00
RedirectService.INIT_DEFAULT_ROUTE
)
2018-03-01 13:57:29 +01:00
2021-05-14 12:04:44 +02:00
this.defaultRoute = RedirectService.INIT_DEFAULT_ROUTE
return this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })
2018-05-31 11:35:01 +02:00
})
2018-03-01 13:57:29 +01:00
}
}