From 030ccfce59a8cb8f2fee6ea8dd363ba635c5c5c2 Mon Sep 17 00:00:00 2001 From: Kimsible Date: Mon, 3 May 2021 19:03:08 +0200 Subject: Use userId key to distinct Account or VideoChannel actor --- client/src/app/root.component.ts | 14 ++++++++------ .../shared/shared-main/account/actor.service.ts | 22 +++++++++------------- 2 files changed, 17 insertions(+), 19 deletions(-) (limited to 'client') diff --git a/client/src/app/root.component.ts b/client/src/app/root.component.ts index ae999e058..5a09e50d1 100644 --- a/client/src/app/root.component.ts +++ b/client/src/app/root.component.ts @@ -23,19 +23,21 @@ export class RootComponent implements OnInit { .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 }) } }) } diff --git a/client/src/app/shared/shared-main/account/actor.service.ts b/client/src/app/shared/shared-main/account/actor.service.ts index a789b6f5b..464ed4519 100644 --- a/client/src/app/shared/shared-main/account/actor.service.ts +++ b/client/src/app/shared/shared-main/account/actor.service.ts @@ -5,34 +5,30 @@ import { Injectable } from '@angular/core' 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 extends T ? keyof T: never +type ServerActor = KeysOfUnion @Injectable() export class ActorService { static BASE_ACTOR_API_URL = environment.apiUrl + '/api/v1/actors/' - actorLoaded = new ReplaySubject(1) + actorLoaded = new ReplaySubject(1) constructor ( private authHttp: HttpClient, private restExtractor: RestExtractor ) {} - getActor (actorName: string): Observable { - return this.authHttp.get(ActorService.BASE_ACTOR_API_URL + actorName) + getActorType (actorName: string): Observable { + return this.authHttp.get(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)) -- cgit v1.2.3