]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-instance/instance-follow.service.ts
Handle rejected follows in client
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-instance / instance-follow.service.ts
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'
10
11 @Injectable()
12 export class InstanceFollowService {
13 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server'
14
15 constructor (
16 private authHttp: HttpClient,
17 private restService: RestService,
18 private restExtractor: RestExtractor
19 ) {
20 }
21
22 getFollowing (options: {
23 pagination: RestPagination
24 sort: SortMeta
25 search?: string
26 actorType?: ActivityPubActorType
27 state?: FollowState
28 }): Observable<ResultList<ActorFollow>> {
29 const { pagination, sort, search, state, actorType } = options
30
31 let params = new HttpParams()
32 params = this.restService.addRestGetParams(params, pagination, sort)
33
34 if (search) {
35 params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search))
36 }
37
38 if (state) params = params.append('state', state)
39 if (actorType) params = params.append('actorType', actorType)
40
41 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/following', { params })
42 .pipe(
43 map(res => this.restExtractor.convertResultListDateToHuman(res)),
44 catchError(res => this.restExtractor.handleError(res))
45 )
46 }
47
48 getFollowers (options: {
49 pagination: RestPagination
50 sort: SortMeta
51 search?: string
52 actorType?: ActivityPubActorType
53 state?: FollowState
54 }): Observable<ResultList<ActorFollow>> {
55 const { pagination, sort, search, state, actorType } = options
56
57 let params = new HttpParams()
58 params = this.restService.addRestGetParams(params, pagination, sort)
59
60 if (search) {
61 params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search))
62 }
63
64 if (state) params = params.append('state', state)
65 if (actorType) params = params.append('actorType', actorType)
66
67 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params })
68 .pipe(
69 map(res => this.restExtractor.convertResultListDateToHuman(res)),
70 catchError(res => this.restExtractor.handleError(res))
71 )
72 }
73
74 follow (hostsOrHandles: string[]) {
75 const body: ServerFollowCreate = {
76 handles: hostsOrHandles.filter(v => v.includes('@')),
77 hosts: hostsOrHandles.filter(v => !v.includes('@'))
78 }
79
80 return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body)
81 .pipe(catchError(res => this.restExtractor.handleError(res)))
82 }
83
84 unfollow (follow: ActorFollow) {
85 const handle = follow.following.name + '@' + follow.following.host
86
87 return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + handle)
88 .pipe(catchError(res => this.restExtractor.handleError(res)))
89 }
90
91 acceptFollower (follow: ActorFollow) {
92 const handle = follow.follower.name + '@' + follow.follower.host
93
94 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
95 .pipe(catchError(res => this.restExtractor.handleError(res)))
96 }
97
98 rejectFollower (follow: ActorFollow) {
99 const handle = follow.follower.name + '@' + follow.follower.host
100
101 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
102 .pipe(catchError(res => this.restExtractor.handleError(res)))
103 }
104
105 removeFollower (follow: ActorFollow) {
106 const handle = follow.follower.name + '@' + follow.follower.host
107
108 return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`)
109 .pipe(catchError(res => this.restExtractor.handleError(res)))
110 }
111
112 buildFollowsListFilters (): AdvancedInputFilter[] {
113 return [
114 {
115 title: $localize`Advanced filters`,
116 children: [
117 {
118 value: 'state:accepted',
119 label: $localize`Accepted follows`
120 },
121 {
122 value: 'state:rejected',
123 label: $localize`Rejected follows`
124 },
125 {
126 value: 'state:pending',
127 label: $localize`Pending follows`
128 }
129 ]
130 }
131 ]
132 }
133
134 private parseFollowsListFilters (search: string) {
135 return this.restService.parseQueryStringFilter(search, {
136 state: {
137 prefix: 'state:'
138 }
139 })
140 }
141 }