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