1 import { SortMeta } from 'primeng/api'
2 import { from, Observable } from 'rxjs'
3 import { catchError, concatMap, 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 })
43 .pipe(catchError(res => this.restExtractor.handleError(res)))
46 getFollowers (options: {
47 pagination: RestPagination
50 actorType?: ActivityPubActorType
52 }): Observable<ResultList<ActorFollow>> {
53 const { pagination, sort, search, state, actorType } = options
55 let params = new HttpParams()
56 params = this.restService.addRestGetParams(params, pagination, sort)
59 params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search))
62 if (state) params = params.append('state', state)
63 if (actorType) params = params.append('actorType', actorType)
65 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params })
66 .pipe(catchError(res => this.restExtractor.handleError(res)))
69 follow (hostsOrHandles: string[]) {
70 const body: ServerFollowCreate = {
71 handles: hostsOrHandles.filter(v => v.includes('@')),
72 hosts: hostsOrHandles.filter(v => !v.includes('@'))
75 return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body)
76 .pipe(catchError(res => this.restExtractor.handleError(res)))
79 unfollow (followsArg: ActorFollow[] | ActorFollow) {
80 const follows = arrayify(followsArg)
85 const handle = follow.following.name + '@' + follow.following.host
87 return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + handle)
90 catchError(err => this.restExtractor.handleError(err))
94 acceptFollower (followsArg: ActorFollow[] | ActorFollow) {
95 const follows = arrayify(followsArg)
100 const handle = follow.follower.name + '@' + follow.follower.host
102 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
105 catchError(err => this.restExtractor.handleError(err))
109 rejectFollower (followsArg: ActorFollow[] | ActorFollow) {
110 const follows = arrayify(followsArg)
114 concatMap(follow => {
115 const handle = follow.follower.name + '@' + follow.follower.host
117 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
120 catchError(err => this.restExtractor.handleError(err))
124 removeFollower (followsArg: ActorFollow[] | ActorFollow) {
125 const follows = arrayify(followsArg)
129 concatMap(follow => {
130 const handle = follow.follower.name + '@' + follow.follower.host
132 return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`)
135 catchError(err => this.restExtractor.handleError(err))
139 buildFollowsListFilters (): AdvancedInputFilter[] {
142 title: $localize`Advanced filters`,
145 value: 'state:accepted',
146 label: $localize`Accepted follows`
149 value: 'state:rejected',
150 label: $localize`Rejected follows`
153 value: 'state:pending',
154 label: $localize`Pending follows`
161 private parseFollowsListFilters (search: string) {
162 return this.restService.parseQueryStringFilter(search, {