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
|
2019-12-18 15:31:54 +01:00
|
|
|
const tmpConfig = this.serverService.getTmpConfig()
|
2021-01-27 17:15:21 +01:00
|
|
|
if (tmpConfig?.instance?.defaultClientRoute) {
|
2021-05-14 12:04:44 +02:00
|
|
|
this.defaultRoute = tmpConfig.instance.defaultClientRoute
|
2021-01-27 17:15:21 +01:00
|
|
|
}
|
|
|
|
if (tmpConfig?.trending?.videos?.algorithms?.default) {
|
2021-05-14 12:04:44 +02:00
|
|
|
this.defaultTrendingAlgorithm = tmpConfig.trending.videos.algorithms.default
|
2018-03-01 13:57:29 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 15:27:46 +01:00
|
|
|
// Load default route
|
2019-12-18 15:31:54 +01:00
|
|
|
this.serverService.getConfig()
|
|
|
|
.subscribe(config => {
|
|
|
|
const defaultRouteConfig = config.instance.defaultClientRoute
|
2021-01-27 17:15:21 +01:00
|
|
|
const defaultTrendingConfig = config.trending.videos.algorithms.default
|
2018-03-01 13:57:29 +01:00
|
|
|
|
2021-05-14 12:04:44 +02:00
|
|
|
if (defaultRouteConfig) this.defaultRoute = defaultRouteConfig
|
|
|
|
if (defaultTrendingConfig) this.defaultTrendingAlgorithm = defaultTrendingConfig
|
2018-05-31 11:35:01 +02: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
|
|
|
|
}
|
|
|
|
|
2018-12-11 15:27:46 +01:00
|
|
|
redirectToPreviousRoute () {
|
2019-05-29 14:39:49 +02:00
|
|
|
const exceptions = [
|
2019-10-28 08:47:17 +01:00
|
|
|
'/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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
}
|