.pipe(
map(params => params[ 'actorName' ]),
distinctUntilChanged(),
- switchMap(actorName => this.actorService.getActor(actorName)),
+ switchMap(actorName => this.actorService.getActorType(actorName)),
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
HttpStatusCode.BAD_REQUEST_400,
HttpStatusCode.NOT_FOUND_404
]))
)
- .subscribe(actor => {
- if (/\/accounts\//.test(actor.url)) {
- this.router.navigate([ `/a/${actor.name}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true })
+ .subscribe(actorType => {
+ const actorName = this.route.snapshot.params[ 'actorName' ]
+
+ if (actorType === 'Account') {
+ this.router.navigate([ `/a/${actorName}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true })
}
- if (/\/video-channels\//.test(actor.url)) {
- this.router.navigate([ `/c/${actor.name}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true })
+ if (actorType === 'VideoChannel') {
+ this.router.navigate([ `/c/${actorName}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true })
}
})
}
import { RestExtractor } from '@app/core'
import { Account as ServerAccount, VideoChannel as ServerVideoChannel } from '@shared/models'
import { environment } from '../../../../environments/environment'
-import { Account } from './account.model'
-import { VideoChannel } from '../video-channel/video-channel.model'
+
+type KeysOfUnion<T> = T extends T ? keyof T: never
+type ServerActor = KeysOfUnion<ServerAccount | ServerVideoChannel>
@Injectable()
export class ActorService {
static BASE_ACTOR_API_URL = environment.apiUrl + '/api/v1/actors/'
- actorLoaded = new ReplaySubject<Account | VideoChannel>(1)
+ actorLoaded = new ReplaySubject<string>(1)
constructor (
private authHttp: HttpClient,
private restExtractor: RestExtractor
) {}
- getActor (actorName: string): Observable<Account | VideoChannel> {
- return this.authHttp.get<ServerAccount | ServerVideoChannel>(ActorService.BASE_ACTOR_API_URL + actorName)
+ getActorType (actorName: string): Observable<string> {
+ return this.authHttp.get<ServerActor>(ActorService.BASE_ACTOR_API_URL + actorName)
.pipe(
map(actorHash => {
- const isAccount = /\/accounts\/.+/.test(actorHash.url)
- const isVideoChannel = /\/video-channels\/.+/.test(actorHash.url)
-
- if (isAccount) {
- return new Account(actorHash)
+ if (actorHash[ 'userId' ]) {
+ return 'Account'
}
- if (isVideoChannel) {
- return new VideoChannel(actorHash)
- }
+ return 'VideoChannel'
}),
tap(actor => this.actorLoaded.next(actor)),
catchError(res => this.restExtractor.handleError(res))