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