]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/instance/follow.service.ts
Filter on follows actor types in about page
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / instance / follow.service.ts
CommitLineData
db400f44 1import { catchError, map } from 'rxjs/operators'
8a02bd04 2import { HttpClient, HttpParams } from '@angular/common/http'
63c4db6d 3import { Injectable } from '@angular/core'
db400f44 4import { Observable } from 'rxjs'
97ecddae 5import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/index'
a6dbbf03
C
6import { environment } from '../../../environments/environment'
7import { RestExtractor, RestPagination, RestService } from '../rest'
41b15c89 8import { SortMeta } from 'primeng/api'
e2f555ca
C
9
10@Injectable()
51548b31 11export class FollowService {
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
c48e82b5 37 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.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
c48e82b5 60 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.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
9a27cdc2 72 return this.authHttp.post(FollowService.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) {
39fdb3c0 80 return this.authHttp.delete(FollowService.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
90 return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
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
100 return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
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
110 return this.authHttp.delete(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}`)
111 .pipe(
112 map(this.restExtractor.extractDataBool),
113 catchError(res => this.restExtractor.handleError(res))
114 )
115 }
e2f555ca 116}