]>
Commit | Line | Data |
---|---|---|
1 | import { catchError, map } from 'rxjs/operators' | |
2 | import { HttpClient, HttpParams } from '@angular/common/http' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { SortMeta } from 'primeng/primeng' | |
5 | import { Observable } from 'rxjs' | |
6 | import { ActorFollow, ResultList } from '../../../../../../shared' | |
7 | import { environment } from '../../../../environments/environment' | |
8 | import { RestExtractor, RestPagination, RestService } from '../../../shared' | |
9 | ||
10 | @Injectable() | |
11 | export class FollowService { | |
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 (pagination: RestPagination, sort: SortMeta): Observable<ResultList<ActorFollow>> { | |
22 | let params = new HttpParams() | |
23 | params = this.restService.addRestGetParams(params, pagination, sort) | |
24 | ||
25 | return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/following', { params }) | |
26 | .pipe( | |
27 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
28 | catchError(res => this.restExtractor.handleError(res)) | |
29 | ) | |
30 | } | |
31 | ||
32 | getFollowers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<ActorFollow>> { | |
33 | let params = new HttpParams() | |
34 | params = this.restService.addRestGetParams(params, pagination, sort) | |
35 | ||
36 | return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/followers', { params }) | |
37 | .pipe( | |
38 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
39 | catchError(res => this.restExtractor.handleError(res)) | |
40 | ) | |
41 | } | |
42 | ||
43 | follow (notEmptyHosts: string[]) { | |
44 | const body = { | |
45 | hosts: notEmptyHosts | |
46 | } | |
47 | ||
48 | return this.authHttp.post(FollowService.BASE_APPLICATION_URL + '/following', body) | |
49 | .pipe( | |
50 | map(this.restExtractor.extractDataBool), | |
51 | catchError(res => this.restExtractor.handleError(res)) | |
52 | ) | |
53 | } | |
54 | ||
55 | unfollow (follow: ActorFollow) { | |
56 | return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host) | |
57 | .pipe( | |
58 | map(this.restExtractor.extractDataBool), | |
59 | catchError(res => this.restExtractor.handleError(res)) | |
60 | ) | |
61 | } | |
62 | } |