From 69e076ddb0deda9e4120bab095d3369bb19fbd1e Mon Sep 17 00:00:00 2001 From: Kimsible Date: Wed, 28 Apr 2021 22:17:02 +0200 Subject: Refactor client @actorName matcher with new API route --- .../shared/shared-main/account/actor.service.ts | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 client/src/app/shared/shared-main/account/actor.service.ts (limited to 'client/src/app/shared/shared-main/account/actor.service.ts') diff --git a/client/src/app/shared/shared-main/account/actor.service.ts b/client/src/app/shared/shared-main/account/actor.service.ts new file mode 100644 index 000000000..a789b6f5b --- /dev/null +++ b/client/src/app/shared/shared-main/account/actor.service.ts @@ -0,0 +1,41 @@ +import { Observable, ReplaySubject } from 'rxjs' +import { catchError, map, tap } from 'rxjs/operators' +import { HttpClient } from '@angular/common/http' +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' + +@Injectable() +export class ActorService { + static BASE_ACTOR_API_URL = environment.apiUrl + '/api/v1/actors/' + + 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) + .pipe( + map(actorHash => { + const isAccount = /\/accounts\/.+/.test(actorHash.url) + const isVideoChannel = /\/video-channels\/.+/.test(actorHash.url) + + if (isAccount) { + return new Account(actorHash) + } + + if (isVideoChannel) { + return new VideoChannel(actorHash) + } + }), + tap(actor => this.actorLoaded.next(actor)), + catchError(res => this.restExtractor.handleError(res)) + ) + } +} -- cgit v1.2.3