]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Use userId key to distinct Account or VideoChannel actor 4009/head
authorKimsible <kimsible@users.noreply.github.com>
Mon, 3 May 2021 17:03:08 +0000 (19:03 +0200)
committerKimsible <kimsible@users.noreply.github.com>
Wed, 5 May 2021 09:48:25 +0000 (11:48 +0200)
client/src/app/root.component.ts
client/src/app/shared/shared-main/account/actor.service.ts

index ae999e0587de4489004599fc3d57ccd4349e2424..5a09e50d1d6cf4870b1d007e06242d9fc08c84f4 100644 (file)
@@ -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 })
           }
         })
   }
index a789b6f5b363f0eb81115814c0ab3d128e191a49..464ed45195507e412e207426a6a761d2bfd31133 100644 (file)
@@ -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> = 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))