]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { SortMeta } from 'primeng/api' |
2 | import { Observable } from 'rxjs' | |
db400f44 | 3 | import { catchError, map } from 'rxjs/operators' |
8a02bd04 | 4 | import { HttpClient, HttpParams } from '@angular/common/http' |
63c4db6d | 5 | import { Injectable } from '@angular/core' |
67ed6552 | 6 | import { RestExtractor, RestPagination, RestService } from '@app/core' |
4d029ef8 | 7 | import { ActivityPubActorType, ActorFollow, FollowState, ResultList, ServerFollowCreate } from '@shared/models' |
a6dbbf03 | 8 | import { environment } from '../../../environments/environment' |
073deef8 | 9 | import { AdvancedInputFilter } from '../shared-forms' |
e2f555ca C |
10 | |
11 | @Injectable() | |
67ed6552 | 12 | export class InstanceFollowService { |
c5ac9fe7 | 13 | private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server' |
e2f555ca C |
14 | |
15 | constructor ( | |
d592e0a9 | 16 | private authHttp: HttpClient, |
8a02bd04 | 17 | private restService: RestService, |
de59c48f | 18 | private restExtractor: RestExtractor |
db400f44 C |
19 | ) { |
20 | } | |
e2f555ca | 21 | |
b8f4167f | 22 | getFollowing (options: { |
9df52d66 C |
23 | pagination: RestPagination |
24 | sort: SortMeta | |
25 | search?: string | |
26 | actorType?: ActivityPubActorType | |
b8f4167f C |
27 | state?: FollowState |
28 | }): Observable<ResultList<ActorFollow>> { | |
97ecddae | 29 | const { pagination, sort, search, state, actorType } = options |
b8f4167f | 30 | |
8a02bd04 C |
31 | let params = new HttpParams() |
32 | params = this.restService.addRestGetParams(params, pagination, sort) | |
33 | ||
073deef8 C |
34 | if (search) { |
35 | params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search)) | |
36 | } | |
37 | ||
b8f4167f | 38 | if (state) params = params.append('state', state) |
97ecddae | 39 | if (actorType) params = params.append('actorType', actorType) |
b014b6b9 | 40 | |
67ed6552 | 41 | return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/following', { params }) |
db400f44 C |
42 | .pipe( |
43 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
44 | catchError(res => this.restExtractor.handleError(res)) | |
45 | ) | |
e2f555ca C |
46 | } |
47 | ||
b8f4167f | 48 | getFollowers (options: { |
9df52d66 C |
49 | pagination: RestPagination |
50 | sort: SortMeta | |
51 | search?: string | |
52 | actorType?: ActivityPubActorType | |
b8f4167f C |
53 | state?: FollowState |
54 | }): Observable<ResultList<ActorFollow>> { | |
97ecddae | 55 | const { pagination, sort, search, state, actorType } = options |
b8f4167f | 56 | |
51548b31 C |
57 | let params = new HttpParams() |
58 | params = this.restService.addRestGetParams(params, pagination, sort) | |
59 | ||
073deef8 C |
60 | if (search) { |
61 | params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search)) | |
62 | } | |
63 | ||
b8f4167f | 64 | if (state) params = params.append('state', state) |
97ecddae | 65 | if (actorType) params = params.append('actorType', actorType) |
b014b6b9 | 66 | |
67ed6552 | 67 | return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params }) |
db400f44 C |
68 | .pipe( |
69 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
70 | catchError(res => this.restExtractor.handleError(res)) | |
71 | ) | |
51548b31 C |
72 | } |
73 | ||
4d029ef8 C |
74 | follow (hostsOrHandles: string[]) { |
75 | const body: ServerFollowCreate = { | |
76 | handles: hostsOrHandles.filter(v => v.includes('@')), | |
77 | hosts: hostsOrHandles.filter(v => !v.includes('@')) | |
df98563e | 78 | } |
e105c19c | 79 | |
67ed6552 | 80 | return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body) |
e8bffe96 | 81 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
d5f5a670 | 82 | } |
7e9334c3 | 83 | |
c48e82b5 | 84 | unfollow (follow: ActorFollow) { |
4d029ef8 C |
85 | const handle = follow.following.name + '@' + follow.following.host |
86 | ||
87 | return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + handle) | |
e8bffe96 | 88 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
7e9334c3 | 89 | } |
0dc64777 C |
90 | |
91 | acceptFollower (follow: ActorFollow) { | |
92 | const handle = follow.follower.name + '@' + follow.follower.host | |
93 | ||
67ed6552 | 94 | return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {}) |
e8bffe96 | 95 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
0dc64777 C |
96 | } |
97 | ||
98 | rejectFollower (follow: ActorFollow) { | |
99 | const handle = follow.follower.name + '@' + follow.follower.host | |
100 | ||
67ed6552 | 101 | return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {}) |
e8bffe96 | 102 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
0dc64777 C |
103 | } |
104 | ||
105 | removeFollower (follow: ActorFollow) { | |
106 | const handle = follow.follower.name + '@' + follow.follower.host | |
107 | ||
67ed6552 | 108 | return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`) |
e8bffe96 | 109 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
0dc64777 | 110 | } |
073deef8 C |
111 | |
112 | buildFollowsListFilters (): AdvancedInputFilter[] { | |
113 | return [ | |
114 | { | |
115 | title: $localize`Advanced filters`, | |
116 | children: [ | |
117 | { | |
118 | value: 'state:accepted', | |
119 | label: $localize`Accepted follows` | |
120 | }, | |
121 | { | |
122 | value: 'state:rejected', | |
123 | label: $localize`Rejected follows` | |
124 | }, | |
125 | { | |
126 | value: 'state:pending', | |
127 | label: $localize`Pending follows` | |
128 | } | |
129 | ] | |
130 | } | |
131 | ] | |
132 | } | |
133 | ||
134 | private parseFollowsListFilters (search: string) { | |
135 | return this.restService.parseQueryStringFilter(search, { | |
136 | state: { | |
137 | prefix: 'state:' | |
138 | } | |
139 | }) | |
140 | } | |
e2f555ca | 141 | } |