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, FollowState, 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 (options: {
22 pagination: RestPagination,
26 }): Observable<ResultList<ActorFollow>> {
27 const { pagination, sort, search, state } = options
29 let params = new HttpParams()
30 params = this.restService.addRestGetParams(params, pagination, sort)
32 if (search) params = params.append('search', search)
33 if (state) params = params.append('state', state)
35 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/following', { params })
37 map(res => this.restExtractor.convertResultListDateToHuman(res)),
38 catchError(res => this.restExtractor.handleError(res))
42 getFollowers (options: {
43 pagination: RestPagination,
47 }): Observable<ResultList<ActorFollow>> {
48 const { pagination, sort, search, state } = options
50 let params = new HttpParams()
51 params = this.restService.addRestGetParams(params, pagination, sort)
53 if (search) params = params.append('search', search)
54 if (state) params = params.append('state', state)
56 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/followers', { params })
58 map(res => this.restExtractor.convertResultListDateToHuman(res)),
59 catchError(res => this.restExtractor.handleError(res))
63 follow (notEmptyHosts: string[]) {
68 return this.authHttp.post(FollowService.BASE_APPLICATION_URL + '/following', body)
70 map(this.restExtractor.extractDataBool),
71 catchError(res => this.restExtractor.handleError(res))
75 unfollow (follow: ActorFollow) {
76 return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host)
78 map(this.restExtractor.extractDataBool),
79 catchError(res => this.restExtractor.handleError(res))
83 acceptFollower (follow: ActorFollow) {
84 const handle = follow.follower.name + '@' + follow.follower.host
86 return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
88 map(this.restExtractor.extractDataBool),
89 catchError(res => this.restExtractor.handleError(res))
93 rejectFollower (follow: ActorFollow) {
94 const handle = follow.follower.name + '@' + follow.follower.host
96 return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
98 map(this.restExtractor.extractDataBool),
99 catchError(res => this.restExtractor.handleError(res))
103 removeFollower (follow: ActorFollow) {
104 const handle = follow.follower.name + '@' + follow.follower.host
106 return this.authHttp.delete(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}`)
108 map(this.restExtractor.extractDataBool),
109 catchError(res => this.restExtractor.handleError(res))