instance-follow.service.ts 4.3 KB

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