]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-instance/instance-follow.service.ts
Fix broken admin page
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-instance / instance-follow.service.ts
CommitLineData
67ed6552 1import { SortMeta } from 'primeng/api'
e3d6c643
C
2import { from, Observable } from 'rxjs'
3import { catchError, concatMap, map, toArray } from 'rxjs/operators'
8a02bd04 4import { HttpClient, HttpParams } from '@angular/common/http'
63c4db6d 5import { Injectable } from '@angular/core'
67ed6552 6import { RestExtractor, RestPagination, RestService } from '@app/core'
e3d6c643 7import { arrayify } from '@shared/core-utils'
4d029ef8 8import { ActivityPubActorType, ActorFollow, FollowState, ResultList, ServerFollowCreate } from '@shared/models'
a6dbbf03 9import { environment } from '../../../environments/environment'
073deef8 10import { AdvancedInputFilter } from '../shared-forms'
e2f555ca
C
11
12@Injectable()
67ed6552 13export class InstanceFollowService {
c5ac9fe7 14 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server'
e2f555ca
C
15
16 constructor (
d592e0a9 17 private authHttp: HttpClient,
8a02bd04 18 private restService: RestService,
de59c48f 19 private restExtractor: RestExtractor
db400f44
C
20 ) {
21 }
e2f555ca 22
b8f4167f 23 getFollowing (options: {
9df52d66
C
24 pagination: RestPagination
25 sort: SortMeta
26 search?: string
27 actorType?: ActivityPubActorType
b8f4167f
C
28 state?: FollowState
29 }): Observable<ResultList<ActorFollow>> {
97ecddae 30 const { pagination, sort, search, state, actorType } = options
b8f4167f 31
8a02bd04
C
32 let params = new HttpParams()
33 params = this.restService.addRestGetParams(params, pagination, sort)
34
073deef8
C
35 if (search) {
36 params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search))
37 }
38
b8f4167f 39 if (state) params = params.append('state', state)
97ecddae 40 if (actorType) params = params.append('actorType', actorType)
b014b6b9 41
67ed6552 42 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/following', { params })
db400f44
C
43 .pipe(
44 map(res => this.restExtractor.convertResultListDateToHuman(res)),
45 catchError(res => this.restExtractor.handleError(res))
46 )
e2f555ca
C
47 }
48
b8f4167f 49 getFollowers (options: {
9df52d66
C
50 pagination: RestPagination
51 sort: SortMeta
52 search?: string
53 actorType?: ActivityPubActorType
b8f4167f
C
54 state?: FollowState
55 }): Observable<ResultList<ActorFollow>> {
97ecddae 56 const { pagination, sort, search, state, actorType } = options
b8f4167f 57
51548b31
C
58 let params = new HttpParams()
59 params = this.restService.addRestGetParams(params, pagination, sort)
60
073deef8
C
61 if (search) {
62 params = this.restService.addObjectParams(params, this.parseFollowsListFilters(search))
63 }
64
b8f4167f 65 if (state) params = params.append('state', state)
97ecddae 66 if (actorType) params = params.append('actorType', actorType)
b014b6b9 67
67ed6552 68 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params })
db400f44
C
69 .pipe(
70 map(res => this.restExtractor.convertResultListDateToHuman(res)),
71 catchError(res => this.restExtractor.handleError(res))
72 )
51548b31
C
73 }
74
4d029ef8
C
75 follow (hostsOrHandles: string[]) {
76 const body: ServerFollowCreate = {
77 handles: hostsOrHandles.filter(v => v.includes('@')),
78 hosts: hostsOrHandles.filter(v => !v.includes('@'))
df98563e 79 }
e105c19c 80
67ed6552 81 return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body)
e8bffe96 82 .pipe(catchError(res => this.restExtractor.handleError(res)))
d5f5a670 83 }
7e9334c3 84
e3d6c643
C
85 unfollow (followsArg: ActorFollow[] | ActorFollow) {
86 const follows = arrayify(followsArg)
4d029ef8 87
e3d6c643
C
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 )
7e9334c3 98 }
0dc64777 99
e3d6c643
C
100 acceptFollower (followsArg: ActorFollow[] | ActorFollow) {
101 const follows = arrayify(followsArg)
0dc64777 102
e3d6c643
C
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 )
0dc64777
C
113 }
114
e3d6c643
C
115 rejectFollower (followsArg: ActorFollow[] | ActorFollow) {
116 const follows = arrayify(followsArg)
0dc64777 117
e3d6c643
C
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 )
0dc64777
C
128 }
129
e3d6c643
C
130 removeFollower (followsArg: ActorFollow[] | ActorFollow) {
131 const follows = arrayify(followsArg)
0dc64777 132
e3d6c643
C
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 )
0dc64777 143 }
073deef8
C
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 }
e2f555ca 174}