1 import { Injectable } from '@angular/core'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Observable } from 'rxjs/Observable'
4 import 'rxjs/add/operator/catch'
5 import 'rxjs/add/operator/map'
7 import { SortMeta } from 'primeng/primeng'
9 import { RestExtractor, RestPagination, RestService } from '../../../shared'
10 import { AccountFollow, ResultList } from '../../../../../../shared'
13 export class FollowService {
14 private static BASE_APPLICATION_URL = API_URL + '/api/v1/server'
17 private authHttp: HttpClient,
18 private restService: RestService,
19 private restExtractor: RestExtractor
22 getFollowing (pagination: RestPagination, sort: SortMeta): Observable<ResultList<AccountFollow>> {
23 let params = new HttpParams()
24 params = this.restService.addRestGetParams(params, pagination, sort)
26 return this.authHttp.get<ResultList<Account>>(FollowService.BASE_APPLICATION_URL + '/following', { params })
27 .map(res => this.restExtractor.convertResultListDateToHuman(res))
28 .catch(res => this.restExtractor.handleError(res))
31 getFollowers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<AccountFollow>> {
32 let params = new HttpParams()
33 params = this.restService.addRestGetParams(params, pagination, sort)
35 return this.authHttp.get<ResultList<Account>>(FollowService.BASE_APPLICATION_URL + '/followers', { params })
36 .map(res => this.restExtractor.convertResultListDateToHuman(res))
37 .catch(res => this.restExtractor.handleError(res))
40 follow (notEmptyHosts: string[]) {
45 return this.authHttp.post(FollowService.BASE_APPLICATION_URL + '/following', body)
46 .map(this.restExtractor.extractDataBool)
47 .catch(res => this.restExtractor.handleError(res))
50 unfollow (follow: AccountFollow) {
51 return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.id)
52 .map(this.restExtractor.extractDataBool)
53 .catch(res => this.restExtractor.handleError(res))