1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { Observable } from 'rxjs'
5 import { ActorFollow, ResultList } from '@shared/index'
6 import { environment } from '../../../environments/environment'
7 import { RestExtractor, RestPagination, RestService } from '../rest'
8 import { SortMeta } from 'primeng/api'
11 export class FollowService {
12 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server'
15 private authHttp: HttpClient,
16 private restService: RestService,
17 private restExtractor: RestExtractor
21 getFollowing (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<ActorFollow>> {
22 let params = new HttpParams()
23 params = this.restService.addRestGetParams(params, pagination, sort)
25 if (search) params = params.append('search', search)
27 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/following', { params })
29 map(res => this.restExtractor.convertResultListDateToHuman(res)),
30 catchError(res => this.restExtractor.handleError(res))
34 getFollowers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<ActorFollow>> {
35 let params = new HttpParams()
36 params = this.restService.addRestGetParams(params, pagination, sort)
38 if (search) params = params.append('search', search)
40 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/followers', { params })
42 map(res => this.restExtractor.convertResultListDateToHuman(res)),
43 catchError(res => this.restExtractor.handleError(res))
47 follow (notEmptyHosts: string[]) {
52 return this.authHttp.post(FollowService.BASE_APPLICATION_URL + '/following', body)
54 map(this.restExtractor.extractDataBool),
55 catchError(res => this.restExtractor.handleError(res))
59 unfollow (follow: ActorFollow) {
60 return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host)
62 map(this.restExtractor.extractDataBool),
63 catchError(res => this.restExtractor.handleError(res))
67 acceptFollower (follow: ActorFollow) {
68 const handle = follow.follower.name + '@' + follow.follower.host
70 return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
72 map(this.restExtractor.extractDataBool),
73 catchError(res => this.restExtractor.handleError(res))
77 rejectFollower (follow: ActorFollow) {
78 const handle = follow.follower.name + '@' + follow.follower.host
80 return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
82 map(this.restExtractor.extractDataBool),
83 catchError(res => this.restExtractor.handleError(res))
87 removeFollower (follow: ActorFollow) {
88 const handle = follow.follower.name + '@' + follow.follower.host
90 return this.authHttp.delete(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}`)
92 map(this.restExtractor.extractDataBool),
93 catchError(res => this.restExtractor.handleError(res))