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'
|
2017-10-19 09:43:01 +02:00
|
|
|
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'
|
2019-06-06 11:39:22 +02:00
|
|
|
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 (
|
2017-09-14 11:57:49 +02:00
|
|
|
private authHttp: HttpClient,
|
2017-10-19 09:43:01 +02:00
|
|
|
private restService: RestService,
|
2016-08-23 16:54:21 +02:00
|
|
|
private restExtractor: RestExtractor
|
2018-05-15 11:55:51 +02:00
|
|
|
) {
|
|
|
|
}
|
2016-08-12 18:22:58 +02:00
|
|
|
|
2019-11-28 11:37:32 +01:00
|
|
|
getFollowing (options: {
|
|
|
|
pagination: RestPagination,
|
|
|
|
sort: SortMeta,
|
|
|
|
search?: string,
|
2019-11-29 10:55:17 +01:00
|
|
|
actorType?: ActivityPubActorType,
|
2019-11-28 11:37:32 +01:00
|
|
|
state?: FollowState
|
|
|
|
}): Observable<ResultList<ActorFollow>> {
|
2019-11-29 10:55:17 +01:00
|
|
|
const { pagination, sort, search, state, actorType } = options
|
2019-11-28 11:37:32 +01:00
|
|
|
|
2017-10-19 09:43:01 +02:00
|
|
|
let params = new HttpParams()
|
|
|
|
params = this.restService.addRestGetParams(params, pagination, sort)
|
|
|
|
|
2018-10-10 09:43:53 +02:00
|
|
|
if (search) params = params.append('search', search)
|
2019-11-28 11:37:32 +01:00
|
|
|
if (state) params = params.append('state', state)
|
2019-11-29 10:55:17 +01:00
|
|
|
if (actorType) params = params.append('actorType', actorType)
|
2018-10-10 09:43:53 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-28 11:37:32 +01:00
|
|
|
getFollowers (options: {
|
|
|
|
pagination: RestPagination,
|
|
|
|
sort: SortMeta,
|
|
|
|
search?: string,
|
2019-11-29 10:55:17 +01:00
|
|
|
actorType?: ActivityPubActorType,
|
2019-11-28 11:37:32 +01:00
|
|
|
state?: FollowState
|
|
|
|
}): Observable<ResultList<ActorFollow>> {
|
2019-11-29 10:55:17 +01:00
|
|
|
const { pagination, sort, search, state, actorType } = options
|
2019-11-28 11:37:32 +01:00
|
|
|
|
2017-11-15 10:10:41 +01:00
|
|
|
let params = new HttpParams()
|
|
|
|
params = this.restService.addRestGetParams(params, pagination, sort)
|
|
|
|
|
2018-10-10 09:43:53 +02:00
|
|
|
if (search) params = params.append('search', search)
|
2019-11-28 11:37:32 +01:00
|
|
|
if (state) params = params.append('state', state)
|
2019-11-29 10:55:17 +01:00
|
|
|
if (actorType) params = params.append('actorType', actorType)
|
2018-10-10 09:43:53 +02:00
|
|
|
|
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-15 10:10:41 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 11:19:23 +01:00
|
|
|
follow (notEmptyHosts: string[]) {
|
2016-08-21 10:41:21 +02:00
|
|
|
const body = {
|
2016-11-14 20:03:04 +01:00
|
|
|
hosts: notEmptyHosts
|
2017-06-16 14:32:15 +02:00
|
|
|
}
|
2016-08-21 10:41:21 +02:00
|
|
|
|
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-08-02 21:50:42 +02:00
|
|
|
}
|
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
|
|
|
}
|
2019-04-08 15:47:44 +02: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`, {})
|
2019-04-08 15:47:44 +02:00
|
|
|
.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`, {})
|
2019-04-08 15:47:44 +02:00
|
|
|
.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}`)
|
2019-04-08 15:47:44 +02:00
|
|
|
.pipe(
|
|
|
|
map(this.restExtractor.extractDataBool),
|
|
|
|
catchError(res => this.restExtractor.handleError(res))
|
|
|
|
)
|
|
|
|
}
|
2016-08-12 18:22:58 +02:00
|
|
|
}
|