]>
Commit | Line | Data |
---|---|---|
012580d9 C |
1 | import { forkJoin, of } from 'rxjs' |
2 | import { catchError, map } from 'rxjs/operators' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router' | |
5 | import { AccountService } from '../account' | |
6 | import { VideoChannelService } from '../video-channel' | |
7 | ||
8 | @Injectable() | |
9 | export class ActorRedirectGuard implements CanActivate { | |
10 | ||
11 | constructor ( | |
12 | private router: Router, | |
13 | private accountService: AccountService, | |
14 | private channelService: VideoChannelService | |
15 | ) {} | |
16 | ||
17 | canActivate (route: ActivatedRouteSnapshot) { | |
18 | const actorName = route.params.actorName | |
19 | ||
20 | return forkJoin([ | |
21 | this.accountService.getAccount(actorName).pipe(this.orUndefined()), | |
22 | this.channelService.getVideoChannel(actorName).pipe(this.orUndefined()) | |
23 | ]).pipe( | |
24 | map(([ account, channel ]) => { | |
25 | if (!account && !channel) { | |
26 | this.router.navigate([ '/404' ]) | |
27 | return false | |
28 | } | |
29 | ||
30 | if (account) { | |
31 | this.router.navigate([ `/a/${actorName}` ], { skipLocationChange: true }) | |
32 | } | |
33 | ||
34 | if (channel) { | |
35 | this.router.navigate([ `/c/${actorName}` ], { skipLocationChange: true }) | |
36 | } | |
37 | ||
38 | return true | |
39 | }) | |
40 | ) | |
41 | } | |
42 | ||
43 | private orUndefined () { | |
44 | return catchError(() => of(undefined)) | |
45 | } | |
46 | } |