]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/instance/follow.service.ts
Only display accepted followers/followings in about page
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / instance / follow.service.ts
1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { Observable } from 'rxjs'
5 import { ActorFollow, FollowState, ResultList } from '@shared/index'
6 import { environment } from '../../../environments/environment'
7 import { RestExtractor, RestPagination, RestService } from '../rest'
8 import { SortMeta } from 'primeng/api'
9
10 @Injectable()
11 export class FollowService {
12 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server'
13
14 constructor (
15 private authHttp: HttpClient,
16 private restService: RestService,
17 private restExtractor: RestExtractor
18 ) {
19 }
20
21 getFollowing (options: {
22 pagination: RestPagination,
23 sort: SortMeta,
24 search?: string,
25 state?: FollowState
26 }): Observable<ResultList<ActorFollow>> {
27 const { pagination, sort, search, state } = options
28
29 let params = new HttpParams()
30 params = this.restService.addRestGetParams(params, pagination, sort)
31
32 if (search) params = params.append('search', search)
33 if (state) params = params.append('state', state)
34
35 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/following', { params })
36 .pipe(
37 map(res => this.restExtractor.convertResultListDateToHuman(res)),
38 catchError(res => this.restExtractor.handleError(res))
39 )
40 }
41
42 getFollowers (options: {
43 pagination: RestPagination,
44 sort: SortMeta,
45 search?: string,
46 state?: FollowState
47 }): Observable<ResultList<ActorFollow>> {
48 const { pagination, sort, search, state } = options
49
50 let params = new HttpParams()
51 params = this.restService.addRestGetParams(params, pagination, sort)
52
53 if (search) params = params.append('search', search)
54 if (state) params = params.append('state', state)
55
56 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/followers', { params })
57 .pipe(
58 map(res => this.restExtractor.convertResultListDateToHuman(res)),
59 catchError(res => this.restExtractor.handleError(res))
60 )
61 }
62
63 follow (notEmptyHosts: string[]) {
64 const body = {
65 hosts: notEmptyHosts
66 }
67
68 return this.authHttp.post(FollowService.BASE_APPLICATION_URL + '/following', body)
69 .pipe(
70 map(this.restExtractor.extractDataBool),
71 catchError(res => this.restExtractor.handleError(res))
72 )
73 }
74
75 unfollow (follow: ActorFollow) {
76 return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host)
77 .pipe(
78 map(this.restExtractor.extractDataBool),
79 catchError(res => this.restExtractor.handleError(res))
80 )
81 }
82
83 acceptFollower (follow: ActorFollow) {
84 const handle = follow.follower.name + '@' + follow.follower.host
85
86 return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
87 .pipe(
88 map(this.restExtractor.extractDataBool),
89 catchError(res => this.restExtractor.handleError(res))
90 )
91 }
92
93 rejectFollower (follow: ActorFollow) {
94 const handle = follow.follower.name + '@' + follow.follower.host
95
96 return this.authHttp.post(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
97 .pipe(
98 map(this.restExtractor.extractDataBool),
99 catchError(res => this.restExtractor.handleError(res))
100 )
101 }
102
103 removeFollower (follow: ActorFollow) {
104 const handle = follow.follower.name + '@' + follow.follower.host
105
106 return this.authHttp.delete(`${FollowService.BASE_APPLICATION_URL}/followers/${handle}`)
107 .pipe(
108 map(this.restExtractor.extractDataBool),
109 catchError(res => this.restExtractor.handleError(res))
110 )
111 }
112 }