Use userId key to distinct Account or VideoChannel actor

This commit is contained in:
Kimsible 2021-05-03 19:03:08 +02:00
parent 7188739644
commit 030ccfce59
2 changed files with 17 additions and 19 deletions

View File

@ -23,19 +23,21 @@ export class RootComponent implements OnInit {
.pipe( .pipe(
map(params => params[ 'actorName' ]), map(params => params[ 'actorName' ]),
distinctUntilChanged(), distinctUntilChanged(),
switchMap(actorName => this.actorService.getActor(actorName)), switchMap(actorName => this.actorService.getActorType(actorName)),
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [ catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
HttpStatusCode.BAD_REQUEST_400, HttpStatusCode.BAD_REQUEST_400,
HttpStatusCode.NOT_FOUND_404 HttpStatusCode.NOT_FOUND_404
])) ]))
) )
.subscribe(actor => { .subscribe(actorType => {
if (/\/accounts\//.test(actor.url)) { const actorName = this.route.snapshot.params[ 'actorName' ]
this.router.navigate([ `/a/${actor.name}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true })
if (actorType === 'Account') {
this.router.navigate([ `/a/${actorName}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true })
} }
if (/\/video-channels\//.test(actor.url)) { if (actorType === 'VideoChannel') {
this.router.navigate([ `/c/${actor.name}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true }) this.router.navigate([ `/c/${actorName}` ], { state: { type: 'others', obj: { status: 200 } }, skipLocationChange: true })
} }
}) })
} }

View File

@ -5,34 +5,30 @@ import { Injectable } from '@angular/core'
import { RestExtractor } from '@app/core' import { RestExtractor } from '@app/core'
import { Account as ServerAccount, VideoChannel as ServerVideoChannel } from '@shared/models' import { Account as ServerAccount, VideoChannel as ServerVideoChannel } from '@shared/models'
import { environment } from '../../../../environments/environment' 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() @Injectable()
export class ActorService { export class ActorService {
static BASE_ACTOR_API_URL = environment.apiUrl + '/api/v1/actors/' static BASE_ACTOR_API_URL = environment.apiUrl + '/api/v1/actors/'
actorLoaded = new ReplaySubject<Account | VideoChannel>(1) actorLoaded = new ReplaySubject<string>(1)
constructor ( constructor (
private authHttp: HttpClient, private authHttp: HttpClient,
private restExtractor: RestExtractor private restExtractor: RestExtractor
) {} ) {}
getActor (actorName: string): Observable<Account | VideoChannel> { getActorType (actorName: string): Observable<string> {
return this.authHttp.get<ServerAccount | ServerVideoChannel>(ActorService.BASE_ACTOR_API_URL + actorName) return this.authHttp.get<ServerActor>(ActorService.BASE_ACTOR_API_URL + actorName)
.pipe( .pipe(
map(actorHash => { map(actorHash => {
const isAccount = /\/accounts\/.+/.test(actorHash.url) if (actorHash[ 'userId' ]) {
const isVideoChannel = /\/video-channels\/.+/.test(actorHash.url) return 'Account'
if (isAccount) {
return new Account(actorHash)
} }
if (isVideoChannel) { return 'VideoChannel'
return new VideoChannel(actorHash)
}
}), }),
tap(actor => this.actorLoaded.next(actor)), tap(actor => this.actorLoaded.next(actor)),
catchError(res => this.restExtractor.handleError(res)) catchError(res => this.restExtractor.handleError(res))