2021-05-28 10:21:39 +02:00
|
|
|
import { forkJoin, of } from 'rxjs'
|
|
|
|
import { catchError, map } from 'rxjs/operators'
|
|
|
|
import { Injectable } from '@angular/core'
|
2023-05-23 11:15:00 +02:00
|
|
|
import { ActivatedRouteSnapshot, Router } from '@angular/router'
|
2021-05-28 10:21:39 +02:00
|
|
|
import { AccountService } from '../account'
|
|
|
|
import { VideoChannelService } from '../video-channel'
|
|
|
|
|
|
|
|
@Injectable()
|
2023-05-23 11:15:00 +02:00
|
|
|
export class ActorRedirectGuard {
|
2021-05-28 10:21:39 +02:00
|
|
|
|
|
|
|
constructor (
|
|
|
|
private router: Router,
|
|
|
|
private accountService: AccountService,
|
|
|
|
private channelService: VideoChannelService
|
|
|
|
) {}
|
|
|
|
|
|
|
|
canActivate (route: ActivatedRouteSnapshot) {
|
|
|
|
const actorName = route.params.actorName
|
|
|
|
|
|
|
|
return forkJoin([
|
|
|
|
this.accountService.getAccount(actorName).pipe(this.orUndefined()),
|
|
|
|
this.channelService.getVideoChannel(actorName).pipe(this.orUndefined())
|
|
|
|
]).pipe(
|
|
|
|
map(([ account, channel ]) => {
|
|
|
|
if (account) {
|
2021-05-31 14:44:38 +02:00
|
|
|
return this.router.parseUrl(`/a/${actorName}`)
|
2021-05-28 10:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (channel) {
|
2021-05-31 14:44:38 +02:00
|
|
|
return this.router.parseUrl(`/c/${actorName}`)
|
2021-05-28 10:21:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:44:38 +02:00
|
|
|
return this.router.parseUrl('/404')
|
2021-05-28 10:21:39 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private orUndefined () {
|
|
|
|
return catchError(() => of(undefined))
|
|
|
|
}
|
|
|
|
}
|