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))