]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-instance/instance-follow.service.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-instance / instance-follow.service.ts
CommitLineData
67ed6552
C
1import { SortMeta } from 'primeng/api'
2import { Observable } from 'rxjs'
db400f44 3import { catchError, map } 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'
4d029ef8 7import { ActivityPubActorType, ActorFollow, FollowState, ResultList, ServerFollowCreate } from '@shared/models'
a6dbbf03 8import { environment } from '../../../environments/environment'
e2f555ca
C
9
10@Injectable()
67ed6552 11export class InstanceFollowService {
c5ac9fe7 12 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server'
e2f555ca
C
13
14 constructor (
d592e0a9 15 private authHttp: HttpClient,
8a02bd04 16 private restService: RestService,
de59c48f 17 private restExtractor: RestExtractor
db400f44
C
18 ) {
19 }
e2f555ca 20
b8f4167f 21 getFollowing (options: {
9df52d66
C
22 pagination: RestPagination
23 sort: SortMeta
24 search?: string
25 actorType?: ActivityPubActorType
b8f4167f
C
26 state?: FollowState
27 }): Observable<ResultList<ActorFollow>> {
97ecddae 28 const { pagination, sort, search, state, actorType } = options
b8f4167f 29
8a02bd04
C
30 let params = new HttpParams()
31 params = this.restService.addRestGetParams(params, pagination, sort)
32
b014b6b9 33 if (search) params = params.append('search', search)
b8f4167f 34 if (state) params = params.append('state', state)
97ecddae 35 if (actorType) params = params.append('actorType', actorType)
b014b6b9 36
67ed6552 37 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/following', { params })
db400f44
C
38 .pipe(
39 map(res => this.restExtractor.convertResultListDateToHuman(res)),
40 catchError(res => this.restExtractor.handleError(res))
41 )
e2f555ca
C
42 }
43
b8f4167f 44 getFollowers (options: {
9df52d66
C
45 pagination: RestPagination
46 sort: SortMeta
47 search?: string
48 actorType?: ActivityPubActorType
b8f4167f
C
49 state?: FollowState
50 }): Observable<ResultList<ActorFollow>> {
97ecddae 51 const { pagination, sort, search, state, actorType } = options
b8f4167f 52
51548b31
C
53 let params = new HttpParams()
54 params = this.restService.addRestGetParams(params, pagination, sort)
55
b014b6b9 56 if (search) params = params.append('search', search)
b8f4167f 57 if (state) params = params.append('state', state)
97ecddae 58 if (actorType) params = params.append('actorType', actorType)
b014b6b9 59
67ed6552 60 return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params })
db400f44
C
61 .pipe(
62 map(res => this.restExtractor.convertResultListDateToHuman(res)),
63 catchError(res => this.restExtractor.handleError(res))
64 )
51548b31
C
65 }
66
4d029ef8
C
67 follow (hostsOrHandles: string[]) {
68 const body: ServerFollowCreate = {
69 handles: hostsOrHandles.filter(v => v.includes('@')),
70 hosts: hostsOrHandles.filter(v => !v.includes('@'))
df98563e 71 }
e105c19c 72
67ed6552 73 return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body)
e8bffe96 74 .pipe(catchError(res => this.restExtractor.handleError(res)))
d5f5a670 75 }
7e9334c3 76
c48e82b5 77 unfollow (follow: ActorFollow) {
4d029ef8
C
78 const handle = follow.following.name + '@' + follow.following.host
79
80 return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + handle)
e8bffe96 81 .pipe(catchError(res => this.restExtractor.handleError(res)))
7e9334c3 82 }
0dc64777
C
83
84 acceptFollower (follow: ActorFollow) {
85 const handle = follow.follower.name + '@' + follow.follower.host
86
67ed6552 87 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
e8bffe96 88 .pipe(catchError(res => this.restExtractor.handleError(res)))
0dc64777
C
89 }
90
91 rejectFollower (follow: ActorFollow) {
92 const handle = follow.follower.name + '@' + follow.follower.host
93
67ed6552 94 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
e8bffe96 95 .pipe(catchError(res => this.restExtractor.handleError(res)))
0dc64777
C
96 }
97
98 removeFollower (follow: ActorFollow) {
99 const handle = follow.follower.name + '@' + follow.follower.host
100
67ed6552 101 return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`)
e8bffe96 102 .pipe(catchError(res => this.restExtractor.handleError(res)))
0dc64777 103 }
e2f555ca 104}