]>
Commit | Line | Data |
---|---|---|
1 | import { SortMeta } from 'primeng/api' | |
2 | import { Observable } from 'rxjs' | |
3 | import { catchError, map } 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 { ActivityPubActorType, ActorFollow, FollowState, ResultList, ServerFollowCreate } from '@shared/models' | |
8 | import { environment } from '../../../environments/environment' | |
9 | ||
10 | @Injectable() | |
11 | export class InstanceFollowService { | |
12 | private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server' | |
13 | ||
14 | constructor ( | |
15 | private authHttp: HttpClient, | |
16 | private restService: RestService, | |
17 | private restExtractor: RestExtractor | |
18 | ) { | |
19 | } | |
20 | ||
21 | getFollowing (options: { | |
22 | pagination: RestPagination | |
23 | sort: SortMeta | |
24 | search?: string | |
25 | actorType?: ActivityPubActorType | |
26 | state?: FollowState | |
27 | }): Observable<ResultList<ActorFollow>> { | |
28 | const { pagination, sort, search, state, actorType } = options | |
29 | ||
30 | let params = new HttpParams() | |
31 | params = this.restService.addRestGetParams(params, pagination, sort) | |
32 | ||
33 | if (search) params = params.append('search', search) | |
34 | if (state) params = params.append('state', state) | |
35 | if (actorType) params = params.append('actorType', actorType) | |
36 | ||
37 | return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/following', { params }) | |
38 | .pipe( | |
39 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
40 | catchError(res => this.restExtractor.handleError(res)) | |
41 | ) | |
42 | } | |
43 | ||
44 | getFollowers (options: { | |
45 | pagination: RestPagination | |
46 | sort: SortMeta | |
47 | search?: string | |
48 | actorType?: ActivityPubActorType | |
49 | state?: FollowState | |
50 | }): Observable<ResultList<ActorFollow>> { | |
51 | const { pagination, sort, search, state, actorType } = options | |
52 | ||
53 | let params = new HttpParams() | |
54 | params = this.restService.addRestGetParams(params, pagination, sort) | |
55 | ||
56 | if (search) params = params.append('search', search) | |
57 | if (state) params = params.append('state', state) | |
58 | if (actorType) params = params.append('actorType', actorType) | |
59 | ||
60 | return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params }) | |
61 | .pipe( | |
62 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
63 | catchError(res => this.restExtractor.handleError(res)) | |
64 | ) | |
65 | } | |
66 | ||
67 | follow (hostsOrHandles: string[]) { | |
68 | const body: ServerFollowCreate = { | |
69 | handles: hostsOrHandles.filter(v => v.includes('@')), | |
70 | hosts: hostsOrHandles.filter(v => !v.includes('@')) | |
71 | } | |
72 | ||
73 | return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body) | |
74 | .pipe( | |
75 | map(this.restExtractor.extractDataBool), | |
76 | catchError(res => this.restExtractor.handleError(res)) | |
77 | ) | |
78 | } | |
79 | ||
80 | unfollow (follow: ActorFollow) { | |
81 | const handle = follow.following.name + '@' + follow.following.host | |
82 | ||
83 | return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + handle) | |
84 | .pipe( | |
85 | map(this.restExtractor.extractDataBool), | |
86 | catchError(res => this.restExtractor.handleError(res)) | |
87 | ) | |
88 | } | |
89 | ||
90 | acceptFollower (follow: ActorFollow) { | |
91 | const handle = follow.follower.name + '@' + follow.follower.host | |
92 | ||
93 | return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {}) | |
94 | .pipe( | |
95 | map(this.restExtractor.extractDataBool), | |
96 | catchError(res => this.restExtractor.handleError(res)) | |
97 | ) | |
98 | } | |
99 | ||
100 | rejectFollower (follow: ActorFollow) { | |
101 | const handle = follow.follower.name + '@' + follow.follower.host | |
102 | ||
103 | return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {}) | |
104 | .pipe( | |
105 | map(this.restExtractor.extractDataBool), | |
106 | catchError(res => this.restExtractor.handleError(res)) | |
107 | ) | |
108 | } | |
109 | ||
110 | removeFollower (follow: ActorFollow) { | |
111 | const handle = follow.follower.name + '@' + follow.follower.host | |
112 | ||
113 | return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`) | |
114 | .pipe( | |
115 | map(this.restExtractor.extractDataBool), | |
116 | catchError(res => this.restExtractor.handleError(res)) | |
117 | ) | |
118 | } | |
119 | } |