1 import { HttpClient, HttpParams } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import { SortMeta } from 'primeng/primeng'
4 import 'rxjs/add/operator/catch'
5 import 'rxjs/add/operator/map'
6 import { Observable } from 'rxjs/Observable'
7 import { AccountFollow, ResultList } from '../../../../../../shared'
8 import { environment } from '../../../../environments/environment'
9 import { RestExtractor, RestPagination, RestService } from '../../../shared'
12 export class FollowService {
13 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server'
16 private authHttp: HttpClient,
17 private restService: RestService,
18 private restExtractor: RestExtractor
21 getFollowing (pagination: RestPagination, sort: SortMeta): Observable<ResultList<AccountFollow>> {
22 let params = new HttpParams()
23 params = this.restService.addRestGetParams(params, pagination, sort)
25 return this.authHttp.get<ResultList<Account>>(FollowService.BASE_APPLICATION_URL + '/following', { params })
26 .map(res => this.restExtractor.convertResultListDateToHuman(res))
27 .catch(res => this.restExtractor.handleError(res))
30 getFollowers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<AccountFollow>> {
31 let params = new HttpParams()
32 params = this.restService.addRestGetParams(params, pagination, sort)
34 return this.authHttp.get<ResultList<Account>>(FollowService.BASE_APPLICATION_URL + '/followers', { params })
35 .map(res => this.restExtractor.convertResultListDateToHuman(res))
36 .catch(res => this.restExtractor.handleError(res))
39 follow (notEmptyHosts: string[]) {
44 return this.authHttp.post(FollowService.BASE_APPLICATION_URL + '/following', body)
45 .map(this.restExtractor.extractDataBool)
46 .catch(res => this.restExtractor.handleError(res))
49 unfollow (follow: AccountFollow) {
50 return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host)
51 .map(this.restExtractor.extractDataBool)
52 .catch(res => this.restExtractor.handleError(res))