1 import { SortMeta } from 'primeng/api'
2 import { from, Observable } from 'rxjs'
3 import { catchError, concatMap, map, toArray } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService } from '@app/core'
7 import { arrayify } from '@shared/core-utils'
8 import { ActivityPubActorType, ActorFollow, FollowState, ResultList, ServerFollowCreate } from '@shared/models'
9 import { environment } from '../../../environments/environment'
10 import { AdvancedInputFilter } from '../shared-forms'
13 export class InstanceFollowService {
14 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server'
17 private authHttp: HttpClient,
18 private restService: RestService,
19 private restExtractor: RestExtractor
23 getFollowing (options: {
24 pagination: RestPagination
27 actorType?: ActivityPubActorType
29 }): Observable<ResultList<ActorFollow>> {
30 const { pagination, sort, search, state, actorType } = options
32 let params = new HttpParams()
33 params = this.restService.addRestGetParams(params, pagination, sort)
36 params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search))
39 if (state) params = params.append('state', state)
40 if (actorType) params = params.append('actorType', actorType)
42 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/following', { params })
44 map(res => this.restExtractor.convertResultListDateToHuman(res)),
45 catchError(res => this.restExtractor.handleError(res))
49 getFollowers (options: {
50 pagination: RestPagination
53 actorType?: ActivityPubActorType
55 }): Observable<ResultList<ActorFollow>> {
56 const { pagination, sort, search, state, actorType } = options
58 let params = new HttpParams()
59 params = this.restService.addRestGetParams(params, pagination, sort)
62 params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search))
65 if (state) params = params.append('state', state)
66 if (actorType) params = params.append('actorType', actorType)
68 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params })
70 map(res => this.restExtractor.convertResultListDateToHuman(res)),
71 catchError(res => this.restExtractor.handleError(res))
75 follow (hostsOrHandles: string[]) {
76 const body: ServerFollowCreate = {
77 handles: hostsOrHandles.filter(v => v.includes('@')),
78 hosts: hostsOrHandles.filter(v => !v.includes('@'))
81 return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body)
82 .pipe(catchError(res => this.restExtractor.handleError(res)))
85 unfollow (followsArg: ActorFollow[] | ActorFollow) {
86 const follows = arrayify(followsArg)
91 const handle = follow.following.name + '@' + follow.following.host
93 return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + handle)
96 catchError(err => this.restExtractor.handleError(err))
100 acceptFollower (followsArg: ActorFollow[] | ActorFollow) {
101 const follows = arrayify(followsArg)
105 concatMap(follow => {
106 const handle = follow.follower.name + '@' + follow.follower.host
108 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
111 catchError(err => this.restExtractor.handleError(err))
115 rejectFollower (followsArg: ActorFollow[] | ActorFollow) {
116 const follows = arrayify(followsArg)
120 concatMap(follow => {
121 const handle = follow.follower.name + '@' + follow.follower.host
123 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
126 catchError(err => this.restExtractor.handleError(err))
130 removeFollower (followsArg: ActorFollow[] | ActorFollow) {
131 const follows = arrayify(followsArg)
135 concatMap(follow => {
136 const handle = follow.follower.name + '@' + follow.follower.host
138 return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`)
141 catchError(err => this.restExtractor.handleError(err))
145 buildFollowsListFilters (): AdvancedInputFilter[] {
148 title: $localize`Advanced filters`,
151 value: 'state:accepted',
152 label: $localize`Accepted follows`
155 value: 'state:rejected',
156 label: $localize`Rejected follows`
159 value: 'state:pending',
160 label: $localize`Pending follows`
167 private parseFollowsListFilters (search: string) {
168 return this.restService.parseQueryStringFilter(search, {