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