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