]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/instance/follow.service.ts
Fix brackets truncation in video description
[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'
b8f4167f 5import { 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,
25 state?: FollowState
26 }): Observable<ResultList<ActorFollow>> {
27 const { pagination, sort, search, state } = options
28
8a02bd04
C
29 let params = new HttpParams()
30 params = this.restService.addRestGetParams(params, pagination, sort)
31
b014b6b9 32 if (search) params = params.append('search', search)
b8f4167f 33 if (state) params = params.append('state', state)
b014b6b9 34
c48e82b5 35 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/following', { params })
db400f44
C
36 .pipe(
37 map(res => this.restExtractor.convertResultListDateToHuman(res)),
38 catchError(res => this.restExtractor.handleError(res))
39 )
e2f555ca
C
40 }
41
b8f4167f
C
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
51548b31
C
50 let params = new HttpParams()
51 params = this.restService.addRestGetParams(params, pagination, sort)
52
b014b6b9 53 if (search) params = params.append('search', search)
b8f4167f 54 if (state) params = params.append('state', state)
b014b6b9 55
c48e82b5 56 return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/followers', { params })
db400f44
C
57 .pipe(
58 map(res => this.restExtractor.convertResultListDateToHuman(res)),
59 catchError(res => this.restExtractor.handleError(res))
60 )
51548b31
C
61 }
62
7e9334c3 63 follow (notEmptyHosts: string[]) {
e105c19c 64 const body = {
49abbbbe 65 hosts: notEmptyHosts
df98563e 66 }
e105c19c 67
9a27cdc2 68 return this.authHttp.post(FollowService.BASE_APPLICATION_URL + '/following', body)
db400f44
C
69 .pipe(
70 map(this.restExtractor.extractDataBool),
71 catchError(res => this.restExtractor.handleError(res))
72 )
d5f5a670 73 }
7e9334c3 74
c48e82b5 75 unfollow (follow: ActorFollow) {
39fdb3c0 76 return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host)
db400f44
C
77 .pipe(
78 map(this.restExtractor.extractDataBool),
79 catchError(res => this.restExtractor.handleError(res))
80 )
7e9334c3 81 }
0dc64777
C
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 }
e2f555ca 112}