]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-instance/instance-follow.service.ts
Upgrade to angular 10
[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'
583eb04b 7import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models'
a6dbbf03 8import { environment } from '../../../environments/environment'
e2f555ca
C
9
10@Injectable()
67ed6552 11export class InstanceFollowService {
ac84064a 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
C
21 getFollowing (options: {
22 pagination: RestPagination,
23 sort: SortMeta,
24 search?: string,
97ecddae 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
C
44 getFollowers (options: {
45 pagination: RestPagination,
46 sort: SortMeta,
47 search?: string,
97ecddae 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
7e9334c3 67 follow (notEmptyHosts: string[]) {
e105c19c 68 const body = {
49abbbbe 69 hosts: notEmptyHosts
df98563e 70 }
e105c19c 71
67ed6552 72 return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body)
db400f44
C
73 .pipe(
74 map(this.restExtractor.extractDataBool),
75 catchError(res => this.restExtractor.handleError(res))
76 )
d5f5a670 77 }
7e9334c3 78
c48e82b5 79 unfollow (follow: ActorFollow) {
67ed6552 80 return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host)
db400f44
C
81 .pipe(
82 map(this.restExtractor.extractDataBool),
83 catchError(res => this.restExtractor.handleError(res))
84 )
7e9334c3 85 }
0dc64777
C
86
87 acceptFollower (follow: ActorFollow) {
88 const handle = follow.follower.name + '@' + follow.follower.host
89
67ed6552 90 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
0dc64777
C
91 .pipe(
92 map(this.restExtractor.extractDataBool),
93 catchError(res => this.restExtractor.handleError(res))
94 )
95 }
96
97 rejectFollower (follow: ActorFollow) {
98 const handle = follow.follower.name + '@' + follow.follower.host
99
67ed6552 100 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
0dc64777
C
101 .pipe(
102 map(this.restExtractor.extractDataBool),
103 catchError(res => this.restExtractor.handleError(res))
104 )
105 }
106
107 removeFollower (follow: ActorFollow) {
108 const handle = follow.follower.name + '@' + follow.follower.host
109
67ed6552 110 return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`)
0dc64777
C
111 .pipe(
112 map(this.restExtractor.extractDataBool),
113 catchError(res => this.restExtractor.handleError(res))
114 )
115 }
e2f555ca 116}