]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-instance/instance-follow.service.ts
Reduce history method names
[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)
db400f44
C
74 .pipe(
75 map(this.restExtractor.extractDataBool),
76 catchError(res => this.restExtractor.handleError(res))
77 )
d5f5a670 78 }
7e9334c3 79
c48e82b5 80 unfollow (follow: ActorFollow) {
4d029ef8
C
81 const handle = follow.following.name + '@' + follow.following.host
82
83 return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + handle)
db400f44
C
84 .pipe(
85 map(this.restExtractor.extractDataBool),
86 catchError(res => this.restExtractor.handleError(res))
87 )
7e9334c3 88 }
0dc64777
C
89
90 acceptFollower (follow: ActorFollow) {
91 const handle = follow.follower.name + '@' + follow.follower.host
92
67ed6552 93 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
0dc64777
C
94 .pipe(
95 map(this.restExtractor.extractDataBool),
96 catchError(res => this.restExtractor.handleError(res))
97 )
98 }
99
100 rejectFollower (follow: ActorFollow) {
101 const handle = follow.follower.name + '@' + follow.follower.host
102
67ed6552 103 return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
0dc64777
C
104 .pipe(
105 map(this.restExtractor.extractDataBool),
106 catchError(res => this.restExtractor.handleError(res))
107 )
108 }
109
110 removeFollower (follow: ActorFollow) {
111 const handle = follow.follower.name + '@' + follow.follower.host
112
67ed6552 113 return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`)
0dc64777
C
114 .pipe(
115 map(this.restExtractor.extractDataBool),
116 catchError(res => this.restExtractor.handleError(res))
117 )
118 }
e2f555ca 119}