]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { catchError, map } from 'rxjs/operators' |
8a02bd04 | 2 | import { HttpClient, HttpParams } from '@angular/common/http' |
63c4db6d C |
3 | import { Injectable } from '@angular/core' |
4 | import { SortMeta } from 'primeng/primeng' | |
db400f44 | 5 | import { Observable } from 'rxjs' |
a6dbbf03 C |
6 | import { ActorFollow, ResultList } from '@shared/index' |
7 | import { environment } from '../../../environments/environment' | |
8 | import { RestExtractor, RestPagination, RestService } from '../rest' | |
e2f555ca C |
9 | |
10 | @Injectable() | |
51548b31 | 11 | export class FollowService { |
ac84064a | 12 | private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server' |
e2f555ca C |
13 | |
14 | constructor ( | |
d592e0a9 | 15 | private authHttp: HttpClient, |
8a02bd04 | 16 | private restService: RestService, |
de59c48f | 17 | private restExtractor: RestExtractor |
db400f44 C |
18 | ) { |
19 | } | |
e2f555ca | 20 | |
b014b6b9 | 21 | getFollowing (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<ActorFollow>> { |
8a02bd04 C |
22 | let params = new HttpParams() |
23 | params = this.restService.addRestGetParams(params, pagination, sort) | |
24 | ||
b014b6b9 C |
25 | if (search) params = params.append('search', search) |
26 | ||
c48e82b5 | 27 | return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/following', { params }) |
db400f44 C |
28 | .pipe( |
29 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
30 | catchError(res => this.restExtractor.handleError(res)) | |
31 | ) | |
e2f555ca C |
32 | } |
33 | ||
b014b6b9 | 34 | getFollowers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<ActorFollow>> { |
51548b31 C |
35 | let params = new HttpParams() |
36 | params = this.restService.addRestGetParams(params, pagination, sort) | |
37 | ||
b014b6b9 C |
38 | if (search) params = params.append('search', search) |
39 | ||
c48e82b5 | 40 | return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/followers', { params }) |
db400f44 C |
41 | .pipe( |
42 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
43 | catchError(res => this.restExtractor.handleError(res)) | |
44 | ) | |
51548b31 C |
45 | } |
46 | ||
7e9334c3 | 47 | follow (notEmptyHosts: string[]) { |
e105c19c | 48 | const body = { |
49abbbbe | 49 | hosts: notEmptyHosts |
df98563e | 50 | } |
e105c19c | 51 | |
9a27cdc2 | 52 | return this.authHttp.post(FollowService.BASE_APPLICATION_URL + '/following', body) |
db400f44 C |
53 | .pipe( |
54 | map(this.restExtractor.extractDataBool), | |
55 | catchError(res => this.restExtractor.handleError(res)) | |
56 | ) | |
d5f5a670 | 57 | } |
7e9334c3 | 58 | |
c48e82b5 | 59 | unfollow (follow: ActorFollow) { |
39fdb3c0 | 60 | return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host) |
db400f44 C |
61 | .pipe( |
62 | map(this.restExtractor.extractDataBool), | |
63 | catchError(res => this.restExtractor.handleError(res)) | |
64 | ) | |
7e9334c3 | 65 | } |
0dc64777 C |
66 | |
67 | acceptFollower (follow: ActorFollow) { | |
68 | const handle = follow.follower.name + '@' + follow.follower.host | |
69 | ||
70 | return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {}) | |
71 | .pipe( | |
72 | map(this.restExtractor.extractDataBool), | |
73 | catchError(res => this.restExtractor.handleError(res)) | |
74 | ) | |
75 | } | |
76 | ||
77 | rejectFollower (follow: ActorFollow) { | |
78 | const handle = follow.follower.name + '@' + follow.follower.host | |
79 | ||
80 | return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {}) | |
81 | .pipe( | |
82 | map(this.restExtractor.extractDataBool), | |
83 | catchError(res => this.restExtractor.handleError(res)) | |
84 | ) | |
85 | } | |
86 | ||
87 | removeFollower (follow: ActorFollow) { | |
88 | const handle = follow.follower.name + '@' + follow.follower.host | |
89 | ||
90 | return this.authHttp.delete(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}`) | |
91 | .pipe( | |
92 | map(this.restExtractor.extractDataBool), | |
93 | catchError(res => this.restExtractor.handleError(res)) | |
94 | ) | |
95 | } | |
e2f555ca | 96 | } |