PeerTube/client/src/app/shared/shared-instance/instance-follow.service.ts

117 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-06-23 14:10:17 +02:00
import { SortMeta } from 'primeng/api'
import { Observable } from 'rxjs'
2018-05-15 11:55:51 +02:00
import { catchError, map } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http'
2017-12-11 17:36:46 +01:00
import { Injectable } from '@angular/core'
2020-06-23 14:10:17 +02:00
import { RestExtractor, RestPagination, RestService } from '@app/core'
2020-06-26 08:37:26 +02:00
import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models'
import { environment } from '../../../environments/environment'
2016-08-12 18:22:58 +02:00
@Injectable()
2020-06-23 14:10:17 +02:00
export class InstanceFollowService {
2020-12-16 13:46:20 +01:00
private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server'
2016-08-12 18:22:58 +02:00
constructor (
private authHttp: HttpClient,
private restService: RestService,
private restExtractor: RestExtractor
2018-05-15 11:55:51 +02:00
) {
}
2016-08-12 18:22:58 +02:00
getFollowing (options: {
pagination: RestPagination,
sort: SortMeta,
search?: string,
actorType?: ActivityPubActorType,
state?: FollowState
}): Observable<ResultList<ActorFollow>> {
const { pagination, sort, search, state, actorType } = options
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
if (search) params = params.append('search', search)
if (state) params = params.append('state', state)
if (actorType) params = params.append('actorType', actorType)
2020-06-23 14:10:17 +02:00
return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/following', { params })
2018-05-15 11:55:51 +02:00
.pipe(
map(res => this.restExtractor.convertResultListDateToHuman(res)),
catchError(res => this.restExtractor.handleError(res))
)
2016-08-12 18:22:58 +02:00
}
getFollowers (options: {
pagination: RestPagination,
sort: SortMeta,
search?: string,
actorType?: ActivityPubActorType,
state?: FollowState
}): Observable<ResultList<ActorFollow>> {
const { pagination, sort, search, state, actorType } = options
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
if (search) params = params.append('search', search)
if (state) params = params.append('state', state)
if (actorType) params = params.append('actorType', actorType)
2020-06-23 14:10:17 +02:00
return this.authHttp.get<ResultList<ActorFollow>>(InstanceFollowService.BASE_APPLICATION_URL + '/followers', { params })
2018-05-15 11:55:51 +02:00
.pipe(
map(res => this.restExtractor.convertResultListDateToHuman(res)),
catchError(res => this.restExtractor.handleError(res))
)
}
2017-11-20 11:19:23 +01:00
follow (notEmptyHosts: string[]) {
const body = {
hosts: notEmptyHosts
}
2020-06-23 14:10:17 +02:00
return this.authHttp.post(InstanceFollowService.BASE_APPLICATION_URL + '/following', body)
2018-05-15 11:55:51 +02:00
.pipe(
map(this.restExtractor.extractDataBool),
catchError(res => this.restExtractor.handleError(res))
)
}
2017-11-20 11:19:23 +01:00
2018-09-11 16:27:07 +02:00
unfollow (follow: ActorFollow) {
2020-06-23 14:10:17 +02:00
return this.authHttp.delete(InstanceFollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host)
2018-05-15 11:55:51 +02:00
.pipe(
map(this.restExtractor.extractDataBool),
catchError(res => this.restExtractor.handleError(res))
)
2017-11-20 11:19:23 +01:00
}
acceptFollower (follow: ActorFollow) {
const handle = follow.follower.name + '@' + follow.follower.host
2020-06-23 14:10:17 +02:00
return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/accept`, {})
.pipe(
map(this.restExtractor.extractDataBool),
catchError(res => this.restExtractor.handleError(res))
)
}
rejectFollower (follow: ActorFollow) {
const handle = follow.follower.name + '@' + follow.follower.host
2020-06-23 14:10:17 +02:00
return this.authHttp.post(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}/reject`, {})
.pipe(
map(this.restExtractor.extractDataBool),
catchError(res => this.restExtractor.handleError(res))
)
}
removeFollower (follow: ActorFollow) {
const handle = follow.follower.name + '@' + follow.follower.host
2020-06-23 14:10:17 +02:00
return this.authHttp.delete(`${InstanceFollowService.BASE_APPLICATION_URL}/followers/${handle}`)
.pipe(
map(this.restExtractor.extractDataBool),
catchError(res => this.restExtractor.handleError(res))
)
}
2016-08-12 18:22:58 +02:00
}