]>
Commit | Line | Data |
---|---|---|
1 | import { SortMeta } from 'primeng/api' | |
2 | import { from, Observable } from 'rxjs' | |
3 | import { catchError, concatMap, toArray } from 'rxjs/operators' | |
4 | import { HttpClient, HttpParams } from '@angular/common/http' | |
5 | import { Injectable } from '@angular/core' | |
6 | import { RestExtractor, RestPagination, RestService } from '@app/core' | |
7 | import { arrayify } from '@shared/core-utils' | |
8 | import { ActivityPubActorType, ActorFollow, FollowState, ResultList, ServerFollowCreate } from '@shared/models' | |
9 | import { environment } from '../../../environments/environment' | |
10 | import { AdvancedInputFilter } from '../shared-forms' | |
11 | ||
12 | @Injectable() | |
13 | export class InstanceFollowService { | |
14 | private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server' | |
15 | ||
16 | constructor ( | |
17 | private authHttp: HttpClient, | |
18 | private restService: RestService, | |
19 | private restExtractor: RestExtractor | |
20 | ) { | |
21 | } | |
22 | ||
23 | getFollowing (options: { | |
24 | pagination: RestPagination | |
25 | sort: SortMeta | |
26 | search?: string | |
27 | actorType?: ActivityPubActorType | |
28 | state?: FollowState | |
29 | }): Observable<ResultList<ActorFollow>> { | |
30 | const { pagination, sort, search, state, actorType } = options | |
31 | ||
32 | let params = new HttpParams() | |
33 | params = this.restService.addRestGetParams(params, pagination, sort) | |
34 | ||
35 | if (search) { | |
36 | params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search)) | |
37 | } | |
38 | ||
39 | if (state) params = params.append('state', state) | |
40 | if (actorType) params = params.append('actorType', actorType) | |
41 | ||
42 | return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/following', { params }) | |
43 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
44 | } | |
45 | ||
46 | getFollowers (options: { | |
47 | pagination: RestPagination | |
48 | sort: SortMeta | |
49 | search?: string | |
50 | actorType?: ActivityPubActorType | |
51 | state?: FollowState | |
52 | }): Observable<ResultList<ActorFollow>> { | |
53 | const { pagination, sort, search, state, actorType } = options | |
54 | ||
55 | let params = new HttpParams() | |
56 | params = this.restService.addRestGetParams(params, pagination, sort) | |
57 | ||
58 | if (search) { | |
59 | params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search)) | |
60 | } | |
61 | ||
62 | if (state) params = params.append('state', state) | |
63 | if (actorType) params = params.append('actorType', actorType) | |
64 | ||
65 | return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params }) | |
66 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
67 | } | |
68 | ||
69 | follow (hostsOrHandles: string[]) { | |
70 | const body: ServerFollowCreate = { | |
71 | handles: hostsOrHandles.filter(v => v.includes('@')), | |
72 | hosts: hostsOrHandles.filter(v => !v.includes('@')) | |
73 | } | |
74 | ||
75 | return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body) | |
76 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
77 | } | |
78 | ||
79 | unfollow (followsArg: ActorFollow[] | ActorFollow) { | |
80 | const follows = arrayify(followsArg) | |
81 | ||
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 | ) | |
92 | } | |
93 | ||
94 | acceptFollower (followsArg: ActorFollow[] | ActorFollow) { | |
95 | const follows = arrayify(followsArg) | |
96 | ||
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 | ) | |
107 | } | |
108 | ||
109 | rejectFollower (followsArg: ActorFollow[] | ActorFollow) { | |
110 | const follows = arrayify(followsArg) | |
111 | ||
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 | ) | |
122 | } | |
123 | ||
124 | removeFollower (followsArg: ActorFollow[] | ActorFollow) { | |
125 | const follows = arrayify(followsArg) | |
126 | ||
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 | ) | |
137 | } | |
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 | } | |
168 | } |