diff options
author | Kimsible <kimsible@users.noreply.github.com> | 2021-04-28 22:17:02 +0200 |
---|---|---|
committer | Kimsible <kimsible@users.noreply.github.com> | 2021-05-05 11:47:03 +0200 |
commit | 69e076ddb0deda9e4120bab095d3369bb19fbd1e (patch) | |
tree | 6576a6293b9e76ce385813f62376e15a7e05001f /client/src/app | |
parent | 08ac081b37cd6115634e8951608116fe0f13032b (diff) | |
download | PeerTube-69e076ddb0deda9e4120bab095d3369bb19fbd1e.tar.gz PeerTube-69e076ddb0deda9e4120bab095d3369bb19fbd1e.tar.zst PeerTube-69e076ddb0deda9e4120bab095d3369bb19fbd1e.zip |
Refactor client @actorName matcher with new API route
Diffstat (limited to 'client/src/app')
-rw-r--r-- | client/src/app/+actors/actors.component.ts | 41 | ||||
-rw-r--r-- | client/src/app/app-routing.module.ts | 20 | ||||
-rw-r--r-- | client/src/app/root.component.ts | 42 | ||||
-rw-r--r-- | client/src/app/shared/shared-main/account/actor.service.ts | 41 | ||||
-rw-r--r-- | client/src/app/shared/shared-main/account/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/shared/shared-main/shared-main.module.ts | 3 |
6 files changed, 102 insertions, 46 deletions
diff --git a/client/src/app/+actors/actors.component.ts b/client/src/app/+actors/actors.component.ts deleted file mode 100644 index 74fbe7dea..000000000 --- a/client/src/app/+actors/actors.component.ts +++ /dev/null | |||
@@ -1,41 +0,0 @@ | |||
1 | import { Component, OnInit } from '@angular/core' | ||
2 | import { empty } from 'rxjs' | ||
3 | import { catchError } from 'rxjs/operators' | ||
4 | import { RestExtractor } from '@app/core' | ||
5 | |||
6 | import { ActivatedRoute, Router } from '@angular/router' | ||
7 | import { AccountService } from '@app/shared/shared-main/account' | ||
8 | |||
9 | @Component({ | ||
10 | selector: 'my-actor', | ||
11 | template: '' | ||
12 | }) | ||
13 | export class ActorsComponent implements OnInit { | ||
14 | constructor ( | ||
15 | private accountService: AccountService, | ||
16 | private route: ActivatedRoute, | ||
17 | private restExtractor: RestExtractor, | ||
18 | private router: Router | ||
19 | ) { | ||
20 | } | ||
21 | |||
22 | ngOnInit () { | ||
23 | const accountOrChannelName = this.route.snapshot.params['actorName'].replace('@', '') | ||
24 | |||
25 | this.accountService | ||
26 | .getAccount(accountOrChannelName) | ||
27 | .pipe( | ||
28 | catchError(res => { | ||
29 | if (res.status === 404 && res.message === 'Account not found') { | ||
30 | this.router.navigateByUrl(`/video-channels/${accountOrChannelName}`) | ||
31 | return empty() | ||
32 | } | ||
33 | |||
34 | return this.restExtractor.handleError(res) | ||
35 | }) | ||
36 | ) | ||
37 | .subscribe(() => { | ||
38 | this.router.navigateByUrl(`/accounts/${accountOrChannelName}`) | ||
39 | }) | ||
40 | } | ||
41 | } | ||
diff --git a/client/src/app/app-routing.module.ts b/client/src/app/app-routing.module.ts index c0a2d29e4..23fd52e8b 100644 --- a/client/src/app/app-routing.module.ts +++ b/client/src/app/app-routing.module.ts | |||
@@ -1,11 +1,11 @@ | |||
1 | import { NgModule } from '@angular/core' | 1 | import { NgModule } from '@angular/core' |
2 | import { RouteReuseStrategy, RouterModule, Routes } from '@angular/router' | 2 | import { RouteReuseStrategy, RouterModule, Routes, UrlMatchResult, UrlSegment } from '@angular/router' |
3 | import { CustomReuseStrategy } from '@app/core/routing/custom-reuse-strategy' | 3 | import { CustomReuseStrategy } from '@app/core/routing/custom-reuse-strategy' |
4 | import { MenuGuards } from '@app/core/routing/menu-guard.service' | 4 | import { MenuGuards } from '@app/core/routing/menu-guard.service' |
5 | import { POSSIBLE_LOCALES } from '@shared/core-utils/i18n' | 5 | import { POSSIBLE_LOCALES } from '@shared/core-utils/i18n' |
6 | import { PreloadSelectedModulesList } from './core' | 6 | import { PreloadSelectedModulesList } from './core' |
7 | import { EmptyComponent } from './empty.component' | 7 | import { EmptyComponent } from './empty.component' |
8 | import { ActorsComponent } from './+actors/actors.component' | 8 | import { RootComponent } from './root.component' |
9 | 9 | ||
10 | const routes: Routes = [ | 10 | const routes: Routes = [ |
11 | { | 11 | { |
@@ -75,8 +75,20 @@ const routes: Routes = [ | |||
75 | redirectTo: 'video-channels' | 75 | redirectTo: 'video-channels' |
76 | }, | 76 | }, |
77 | { | 77 | { |
78 | path: ':actorName', | 78 | matcher: (url): UrlMatchResult => { |
79 | component: ActorsComponent | 79 | // Matches /@:actorName |
80 | if (url.length === 1 && url[0].path.match(/^@[\w]+$/gm)) { | ||
81 | return { | ||
82 | consumed: url, | ||
83 | posParams: { | ||
84 | actorName: new UrlSegment(url[0].path.substr(1), {}) | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | |||
89 | return null | ||
90 | }, | ||
91 | component: RootComponent | ||
80 | }, | 92 | }, |
81 | { | 93 | { |
82 | path: '', | 94 | path: '', |
diff --git a/client/src/app/root.component.ts b/client/src/app/root.component.ts new file mode 100644 index 000000000..c65f59448 --- /dev/null +++ b/client/src/app/root.component.ts | |||
@@ -0,0 +1,42 @@ | |||
1 | import { Component, OnInit } from '@angular/core' | ||
2 | import { catchError, distinctUntilChanged, map, switchMap } from 'rxjs/operators' | ||
3 | import { ActivatedRoute, Router } from '@angular/router' | ||
4 | import { RestExtractor } from '@app/core' | ||
5 | import { ActorService } from '@app/shared/shared-main/account' | ||
6 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | ||
7 | |||
8 | @Component({ | ||
9 | selector: 'my-root', | ||
10 | template: '' | ||
11 | }) | ||
12 | export class RootComponent implements OnInit { | ||
13 | constructor ( | ||
14 | private actorService: ActorService, | ||
15 | private route: ActivatedRoute, | ||
16 | private restExtractor: RestExtractor, | ||
17 | private router: Router | ||
18 | ) { | ||
19 | } | ||
20 | |||
21 | ngOnInit () { | ||
22 | this.route.params | ||
23 | .pipe( | ||
24 | map(params => params[ 'actorName' ]), | ||
25 | distinctUntilChanged(), | ||
26 | switchMap(actorName => this.actorService.getActor(actorName)), | ||
27 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [ | ||
28 | HttpStatusCode.BAD_REQUEST_400, | ||
29 | HttpStatusCode.NOT_FOUND_404 | ||
30 | ])) | ||
31 | ) | ||
32 | .subscribe(actor => { | ||
33 | if (actor.constructor.name === 'Account') { | ||
34 | this.router.navigateByUrl(`/accounts/${actor.name}`) | ||
35 | } | ||
36 | |||
37 | if (actor.constructor.name === 'VideoChannel') { | ||
38 | this.router.navigateByUrl(`/video-channels/${actor.name}`) | ||
39 | } | ||
40 | }) | ||
41 | } | ||
42 | } | ||
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 @@ | |||
1 | import { Observable, ReplaySubject } from 'rxjs' | ||
2 | import { catchError, map, tap } from 'rxjs/operators' | ||
3 | import { HttpClient } from '@angular/common/http' | ||
4 | import { Injectable } from '@angular/core' | ||
5 | import { RestExtractor } from '@app/core' | ||
6 | import { Account as ServerAccount, VideoChannel as ServerVideoChannel } from '@shared/models' | ||
7 | import { environment } from '../../../../environments/environment' | ||
8 | import { Account } from './account.model' | ||
9 | import { VideoChannel } from '../video-channel/video-channel.model' | ||
10 | |||
11 | @Injectable() | ||
12 | export class ActorService { | ||
13 | static BASE_ACTOR_API_URL = environment.apiUrl + '/api/v1/actors/' | ||
14 | |||
15 | actorLoaded = new ReplaySubject<Account | VideoChannel>(1) | ||
16 | |||
17 | constructor ( | ||
18 | private authHttp: HttpClient, | ||
19 | private restExtractor: RestExtractor | ||
20 | ) {} | ||
21 | |||
22 | getActor (actorName: string): Observable<Account | VideoChannel> { | ||
23 | return this.authHttp.get<ServerAccount | ServerVideoChannel>(ActorService.BASE_ACTOR_API_URL + actorName) | ||
24 | .pipe( | ||
25 | map(actorHash => { | ||
26 | const isAccount = /\/accounts\/.+/.test(actorHash.url) | ||
27 | const isVideoChannel = /\/video-channels\/.+/.test(actorHash.url) | ||
28 | |||
29 | if (isAccount) { | ||
30 | return new Account(actorHash) | ||
31 | } | ||
32 | |||
33 | if (isVideoChannel) { | ||
34 | return new VideoChannel(actorHash) | ||
35 | } | ||
36 | }), | ||
37 | tap(actor => this.actorLoaded.next(actor)), | ||
38 | catchError(res => this.restExtractor.handleError(res)) | ||
39 | ) | ||
40 | } | ||
41 | } | ||
diff --git a/client/src/app/shared/shared-main/account/index.ts b/client/src/app/shared/shared-main/account/index.ts index b80ddb9f5..c6cdcd574 100644 --- a/client/src/app/shared/shared-main/account/index.ts +++ b/client/src/app/shared/shared-main/account/index.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | export * from './account.model' | 1 | export * from './account.model' |
2 | export * from './account.service' | 2 | export * from './account.service' |
3 | export * from './actor.model' | 3 | export * from './actor.model' |
4 | export * from './actor.service' | ||
diff --git a/client/src/app/shared/shared-main/shared-main.module.ts b/client/src/app/shared/shared-main/shared-main.module.ts index 772198cb2..05a5d77c7 100644 --- a/client/src/app/shared/shared-main/shared-main.module.ts +++ b/client/src/app/shared/shared-main/shared-main.module.ts | |||
@@ -17,7 +17,7 @@ import { | |||
17 | import { LoadingBarModule } from '@ngx-loading-bar/core' | 17 | import { LoadingBarModule } from '@ngx-loading-bar/core' |
18 | import { LoadingBarHttpClientModule } from '@ngx-loading-bar/http-client' | 18 | import { LoadingBarHttpClientModule } from '@ngx-loading-bar/http-client' |
19 | import { SharedGlobalIconModule } from '../shared-icons' | 19 | import { SharedGlobalIconModule } from '../shared-icons' |
20 | import { AccountService } from './account' | 20 | import { AccountService, ActorService } from './account' |
21 | import { | 21 | import { |
22 | AutofocusDirective, | 22 | AutofocusDirective, |
23 | BytesPipe, | 23 | BytesPipe, |
@@ -160,6 +160,7 @@ import { VideoChannelService } from './video-channel' | |||
160 | AUTH_INTERCEPTOR_PROVIDER, | 160 | AUTH_INTERCEPTOR_PROVIDER, |
161 | 161 | ||
162 | AccountService, | 162 | AccountService, |
163 | ActorService, | ||
163 | 164 | ||
164 | UserHistoryService, | 165 | UserHistoryService, |
165 | UserNotificationService, | 166 | UserNotificationService, |