followers-list.component.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { SortMeta } from 'primeng/api'
  2. import { Component, OnInit } from '@angular/core'
  3. import { ConfirmService, Notifier, RestPagination, RestTable } from '@app/core'
  4. import { formatICU } from '@app/helpers'
  5. import { AdvancedInputFilter } from '@app/shared/shared-forms'
  6. import { InstanceFollowService } from '@app/shared/shared-instance'
  7. import { DropdownAction } from '@app/shared/shared-main'
  8. import { ActorFollow } from '@peertube/peertube-models'
  9. @Component({
  10. selector: 'my-followers-list',
  11. templateUrl: './followers-list.component.html',
  12. styleUrls: [ './followers-list.component.scss' ]
  13. })
  14. export class FollowersListComponent extends RestTable <ActorFollow> implements OnInit {
  15. followers: ActorFollow[] = []
  16. totalRecords = 0
  17. sort: SortMeta = { field: 'createdAt', order: -1 }
  18. pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
  19. searchFilters: AdvancedInputFilter[] = []
  20. bulkActions: DropdownAction<ActorFollow[]>[] = []
  21. constructor (
  22. private confirmService: ConfirmService,
  23. private notifier: Notifier,
  24. private followService: InstanceFollowService
  25. ) {
  26. super()
  27. }
  28. ngOnInit () {
  29. this.initialize()
  30. this.searchFilters = this.followService.buildFollowsListFilters()
  31. this.bulkActions = [
  32. {
  33. label: $localize`Reject`,
  34. handler: follows => this.rejectFollower(follows),
  35. isDisplayed: follows => follows.every(f => f.state !== 'rejected')
  36. },
  37. {
  38. label: $localize`Accept`,
  39. handler: follows => this.acceptFollower(follows),
  40. isDisplayed: follows => follows.every(f => f.state !== 'accepted')
  41. },
  42. {
  43. label: $localize`Delete`,
  44. handler: follows => this.deleteFollowers(follows),
  45. isDisplayed: follows => follows.every(f => f.state === 'rejected')
  46. }
  47. ]
  48. }
  49. getIdentifier () {
  50. return 'FollowersListComponent'
  51. }
  52. acceptFollower (follows: ActorFollow[]) {
  53. this.followService.acceptFollower(follows)
  54. .subscribe({
  55. next: () => {
  56. // eslint-disable-next-line max-len
  57. const message = formatICU(
  58. $localize`Accepted {count, plural, =1 {{followerName} follow request} other {{count} follow requests}}`,
  59. { count: follows.length, followerName: this.buildFollowerName(follows[0]) }
  60. )
  61. this.notifier.success(message)
  62. this.reloadData()
  63. },
  64. error: err => this.notifier.error(err.message)
  65. })
  66. }
  67. async rejectFollower (follows: ActorFollow[]) {
  68. // eslint-disable-next-line max-len
  69. const message = formatICU(
  70. $localize`Do you really want to reject {count, plural, =1 {{followerName} follow request?} other {{count} follow requests?}}`,
  71. { count: follows.length, followerName: this.buildFollowerName(follows[0]) }
  72. )
  73. const res = await this.confirmService.confirm(message, $localize`Reject`)
  74. if (res === false) return
  75. this.followService.rejectFollower(follows)
  76. .subscribe({
  77. next: () => {
  78. // eslint-disable-next-line max-len
  79. const message = formatICU(
  80. $localize`Rejected {count, plural, =1 {{followerName} follow request} other {{count} follow requests}}`,
  81. { count: follows.length, followerName: this.buildFollowerName(follows[0]) }
  82. )
  83. this.notifier.success(message)
  84. this.reloadData()
  85. },
  86. error: err => this.notifier.error(err.message)
  87. })
  88. }
  89. async deleteFollowers (follows: ActorFollow[]) {
  90. const icuParams = { count: follows.length, followerName: this.buildFollowerName(follows[0]) }
  91. let message = $localize`Deleted followers will be able to send again a follow request.`
  92. message += '<br /><br />'
  93. // eslint-disable-next-line max-len
  94. message += formatICU(
  95. $localize`Do you really want to delete {count, plural, =1 {{followerName} follow request?} other {{count} follow requests?}}`,
  96. icuParams
  97. )
  98. const res = await this.confirmService.confirm(message, $localize`Delete`)
  99. if (res === false) return
  100. this.followService.removeFollower(follows)
  101. .subscribe({
  102. next: () => {
  103. // eslint-disable-next-line max-len
  104. const message = formatICU(
  105. $localize`Removed {count, plural, =1 {{followerName} follow request} other {{count} follow requests}}`,
  106. icuParams
  107. )
  108. this.notifier.success(message)
  109. this.reloadData()
  110. },
  111. error: err => this.notifier.error(err.message)
  112. })
  113. }
  114. buildFollowerName (follow: ActorFollow) {
  115. return follow.follower.name + '@' + follow.follower.host
  116. }
  117. protected reloadDataInternal () {
  118. this.followService.getFollowers({ pagination: this.pagination, sort: this.sort, search: this.search })
  119. .subscribe({
  120. next: resultList => {
  121. this.followers = resultList.data
  122. this.totalRecords = resultList.total
  123. },
  124. error: err => this.notifier.error(err.message)
  125. })
  126. }
  127. }