1 import { SortMeta } from 'primeng/api'
2 import { Observable } from 'rxjs'
3 import { catchError, map } 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 { ActivityPubActorType, ActorFollow, FollowState, ResultList, ServerFollowCreate } from '@shared/models'
8 import { environment } from '../../../environments/environment'
9 import { AdvancedInputFilter } from '../shared-forms'
12 export class InstanceFollowService {
13 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server'
16 private authHttp: HttpClient,
17 private restService: RestService,
18 private restExtractor: RestExtractor
22 getFollowing (options: {
23 pagination: RestPagination
26 actorType?: ActivityPubActorType
28 }): Observable<ResultList<ActorFollow>> {
29 const { pagination, sort, search, state, actorType } = options
31 let params = new HttpParams()
32 params = this.restService.addRestGetParams(params, pagination, sort)
35 params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search))
38 if (state) params = params.append('state', state)
39 if (actorType) params = params.append('actorType', actorType)
41 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/following', { params })
43 map(res => this.restExtractor.convertResultListDateToHuman(res)),
44 catchError(res => this.restExtractor.handleError(res))
48 getFollowers (options: {
49 pagination: RestPagination
52 actorType?: ActivityPubActorType
54 }): Observable<ResultList<ActorFollow>> {
55 const { pagination, sort, search, state, actorType } = options
57 let params = new HttpParams()
58 params = this.restService.addRestGetParams(params, pagination, sort)
61 params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search))
64 if (state) params = params.append('state', state)
65 if (actorType) params = params.append('actorType', actorType)
67 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params })
69 map(res => this.restExtractor.convertResultListDateToHuman(res)),
70 catchError(res => this.restExtractor.handleError(res))
74 follow (hostsOrHandles: string[]) {
75 const body: ServerFollowCreate = {
76 handles: hostsOrHandles.filter(v => v.includes('@')),
77 hosts: hostsOrHandles.filter(v => !v.includes('@'))
80 return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body)
81 .pipe(catchError(res => this.restExtractor.handleError(res)))
84 unfollow (follow: ActorFollow) {
85 const handle = follow.following.name + '@' + follow.following.host
87 return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + handle)
88 .pipe(catchError(res => this.restExtractor.handleError(res)))
91 acceptFollower (follow: ActorFollow) {
92 const handle = follow.follower.name + '@' + follow.follower.host
94 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
95 .pipe(catchError(res => this.restExtractor.handleError(res)))
98 rejectFollower (follow: ActorFollow) {
99 const handle = follow.follower.name + '@' + follow.follower.host
101 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
102 .pipe(catchError(res => this.restExtractor.handleError(res)))
105 removeFollower (follow: ActorFollow) {
106 const handle = follow.follower.name + '@' + follow.follower.host
108 return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`)
109 .pipe(catchError(res => this.restExtractor.handleError(res)))
112 buildFollowsListFilters (): AdvancedInputFilter[] {
115 title: $localize`Advanced filters`,
118 value: 'state:accepted',
119 label: $localize`Accepted follows`
122 value: 'state:rejected',
123 label: $localize`Rejected follows`
126 value: 'state:pending',
127 label: $localize`Pending follows`
134 private parseFollowsListFilters (search: string) {
135 return this.restService.parseQueryStringFilter(search, {