]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-moderation/user-moderation-dropdown.component.ts
Add ability to mute user when banning them
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / user-moderation-dropdown.component.ts
CommitLineData
ba430d75 1import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core'
d92d070c 2import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
67ed6552 3import { Account, DropdownAction } from '@app/shared/shared-main'
2989628b 4import { BulkRemoveCommentsOfBody, User, UserRight } from '@shared/models'
d92d070c 5import { UserAdminService } from '../shared-users'
67ed6552
C
6import { BlocklistService } from './blocklist.service'
7import { BulkService } from './bulk.service'
8import { UserBanModalComponent } from './user-ban-modal.component'
e724fa93
C
9
10@Component({
11 selector: 'my-user-moderation-dropdown',
f421fa06 12 templateUrl: './user-moderation-dropdown.component.html'
e724fa93 13})
ba430d75 14export class UserModerationDropdownComponent implements OnInit, OnChanges {
2f5d2ec5 15 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
e724fa93
C
16
17 @Input() user: User
af5767ff 18 @Input() account: Account
8ca56654 19 @Input() prependActions: DropdownAction<{ user: User, account: Account }>[]
af5767ff 20
79bd2632 21 @Input() buttonSize: 'normal' | 'small' = 'normal'
fe88ca69 22 @Input() buttonStyled = true
30814423 23 @Input() placement = 'right-top right-bottom auto'
edf1a4e5 24 @Input() label: string
5ff52366 25 @Input() container: 'body' | undefined = undefined
79bd2632 26
e724fa93 27 @Output() userChanged = new EventEmitter()
79bd2632 28 @Output() userDeleted = new EventEmitter()
e724fa93 29
f97c91f7 30 userActions: DropdownAction<{ user: User, account: Account }>[][] = []
e724fa93 31
2989628b 32 requiresEmailVerification = false
ba430d75 33
e724fa93
C
34 constructor (
35 private authService: AuthService,
f8b2c1b4 36 private notifier: Notifier,
e724fa93 37 private confirmService: ConfirmService,
fc2ec87a 38 private serverService: ServerService,
d92d070c 39 private userAdminService: UserAdminService,
af5767ff 40 private blocklistService: BlocklistService,
66357162 41 private bulkService: BulkService
e724fa93
C
42 ) { }
43
2989628b 44 ngOnInit () {
ba430d75 45 this.serverService.getConfig()
2989628b 46 .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
fc2ec87a
JM
47 }
48
af5767ff 49 ngOnChanges () {
79bd2632 50 this.buildActions()
e724fa93
C
51 }
52
e724fa93
C
53 openBanUserModal (user: User) {
54 if (user.username === 'root') {
66357162 55 this.notifier.error($localize`You cannot ban root.`)
e724fa93
C
56 return
57 }
58
59 this.userBanModal.openModal(user)
60 }
61
62 onUserBanned () {
63 this.userChanged.emit()
64 }
65
66 async unbanUser (user: User) {
66357162 67 const res = await this.confirmService.confirm($localize`Do you really want to unban ${user.username}?`, $localize`Unban`)
e724fa93
C
68 if (res === false) return
69
d92d070c 70 this.userAdminService.unbanUsers(user)
1378c0d3
C
71 .subscribe({
72 next: () => {
66357162 73 this.notifier.success($localize`User ${user.username} unbanned.`)
e724fa93
C
74 this.userChanged.emit()
75 },
76
1378c0d3
C
77 error: err => this.notifier.error(err.message)
78 })
e724fa93
C
79 }
80
81 async removeUser (user: User) {
82 if (user.username === 'root') {
66357162 83 this.notifier.error($localize`You cannot delete root.`)
e724fa93
C
84 return
85 }
86
a7dbc608
C
87 const message = $localize`If you remove user ${user.username}, you won't be able to create another with the same username!`
88 const res = await this.confirmService.confirm(message, $localize`Delete ${user.username}`)
e724fa93
C
89 if (res === false) return
90
d92d070c 91 this.userAdminService.removeUser(user)
1378c0d3
C
92 .subscribe({
93 next: () => {
94 this.notifier.success($localize`User ${user.username} deleted.`)
95 this.userDeleted.emit()
96 },
e724fa93 97
1378c0d3
C
98 error: err => this.notifier.error(err.message)
99 })
e724fa93
C
100 }
101
fc2ec87a 102 setEmailAsVerified (user: User) {
d92d070c 103 this.userAdminService.updateUser(user.id, { emailVerified: true })
1378c0d3
C
104 .subscribe({
105 next: () => {
106 this.notifier.success($localize`User ${user.username} email set as verified`)
107 this.userChanged.emit()
108 },
109
110 error: err => this.notifier.error(err.message)
111 })
fc2ec87a
JM
112 }
113
af5767ff
C
114 blockAccountByUser (account: Account) {
115 this.blocklistService.blockAccountByUser(account)
1378c0d3
C
116 .subscribe({
117 next: () => {
66357162 118 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
af5767ff 119
65b21c96 120 this.account.mutedByUser = true
af5767ff
C
121 this.userChanged.emit()
122 },
123
1378c0d3
C
124 error: err => this.notifier.error(err.message)
125 })
af5767ff
C
126 }
127
128 unblockAccountByUser (account: Account) {
129 this.blocklistService.unblockAccountByUser(account)
1378c0d3
C
130 .subscribe({
131 next: () => {
66357162 132 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
af5767ff 133
65b21c96 134 this.account.mutedByUser = false
af5767ff
C
135 this.userChanged.emit()
136 },
137
1378c0d3
C
138 error: err => this.notifier.error(err.message)
139 })
af5767ff
C
140 }
141
142 blockServerByUser (host: string) {
143 this.blocklistService.blockServerByUser(host)
1378c0d3
C
144 .subscribe({
145 next: () => {
66357162 146 this.notifier.success($localize`Instance ${host} muted.`)
af5767ff 147
65b21c96 148 this.account.mutedServerByUser = true
af5767ff
C
149 this.userChanged.emit()
150 },
151
1378c0d3
C
152 error: err => this.notifier.error(err.message)
153 })
af5767ff
C
154 }
155
156 unblockServerByUser (host: string) {
157 this.blocklistService.unblockServerByUser(host)
1378c0d3
C
158 .subscribe({
159 next: () => {
66357162 160 this.notifier.success($localize`Instance ${host} unmuted.`)
af5767ff 161
65b21c96
C
162 this.account.mutedServerByUser = false
163 this.userChanged.emit()
164 },
165
1378c0d3
C
166 error: err => this.notifier.error(err.message)
167 })
65b21c96
C
168 }
169
170 blockAccountByInstance (account: Account) {
171 this.blocklistService.blockAccountByInstance(account)
1378c0d3
C
172 .subscribe({
173 next: () => {
66357162 174 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
65b21c96
C
175
176 this.account.mutedByInstance = true
177 this.userChanged.emit()
178 },
179
1378c0d3
C
180 error: err => this.notifier.error(err.message)
181 })
65b21c96
C
182 }
183
184 unblockAccountByInstance (account: Account) {
185 this.blocklistService.unblockAccountByInstance(account)
1378c0d3
C
186 .subscribe({
187 next: () => {
66357162 188 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
65b21c96
C
189
190 this.account.mutedByInstance = false
191 this.userChanged.emit()
192 },
193
1378c0d3
C
194 error: err => this.notifier.error(err.message)
195 })
65b21c96
C
196 }
197
198 blockServerByInstance (host: string) {
199 this.blocklistService.blockServerByInstance(host)
1378c0d3
C
200 .subscribe({
201 next: () => {
66357162 202 this.notifier.success($localize`Instance ${host} muted by the instance.`)
65b21c96
C
203
204 this.account.mutedServerByInstance = true
205 this.userChanged.emit()
206 },
207
1378c0d3
C
208 error: err => this.notifier.error(err.message)
209 })
65b21c96
C
210 }
211
212 unblockServerByInstance (host: string) {
213 this.blocklistService.unblockServerByInstance(host)
1378c0d3
C
214 .subscribe({
215 next: () => {
66357162 216 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
65b21c96
C
217
218 this.account.mutedServerByInstance = false
af5767ff
C
219 this.userChanged.emit()
220 },
221
1378c0d3
C
222 error: err => this.notifier.error(err.message)
223 })
af5767ff
C
224 }
225
923ff87d 226 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
66357162
C
227 const message = $localize`Are you sure you want to remove all the comments of this account?`
228 const res = await this.confirmService.confirm(message, $localize`Delete account comments`)
923ff87d
C
229 if (res === false) return
230
231 this.bulkService.removeCommentsOf(body)
1378c0d3
C
232 .subscribe({
233 next: () => {
66357162 234 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
923ff87d
C
235 },
236
1378c0d3
C
237 error: err => this.notifier.error(err.message)
238 })
923ff87d
C
239 }
240
e724fa93
C
241 getRouterUserEditLink (user: User) {
242 return [ '/admin', 'users', 'update', user.id ]
243 }
79bd2632 244
a2c3564a
C
245 private isMyUser (user: User) {
246 return user && this.authService.getUser().id === user.id
247 }
248
249 private isMyAccount (account: Account) {
250 return account && this.authService.getUser().account.id === account.id
251 }
252
79bd2632
C
253 private buildActions () {
254 this.userActions = []
255
a2c3564a 256 if (this.prependActions && this.prependActions.length !== 0) {
8ca56654
C
257 this.userActions = [
258 this.prependActions
259 ]
260 }
261
a2c3564a
C
262 const myAccountModerationActions = this.buildMyAccountModerationActions()
263 const instanceModerationActions = this.buildInstanceModerationActions()
79bd2632 264
a2c3564a
C
265 if (myAccountModerationActions.length !== 0) this.userActions.push(myAccountModerationActions)
266 if (instanceModerationActions.length !== 0) this.userActions.push(instanceModerationActions)
267 }
af5767ff 268
a2c3564a
C
269 private buildMyAccountModerationActions () {
270 if (!this.account || !this.authService.isLoggedIn()) return []
271
272 const myAccountActions: DropdownAction<{ user: User, account: Account }>[] = [
273 {
256fb92e 274 label: $localize`My account moderation`,
a2c3564a
C
275 class: [ 'red' ],
276 isHeader: true
277 },
278 {
279 label: $localize`Mute this account`,
280 description: $localize`Hide any content from that user from you.`,
281 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === false,
282 handler: ({ account }) => this.blockAccountByUser(account)
283 },
284 {
285 label: $localize`Unmute this account`,
286 description: $localize`Show back content from that user for you.`,
287 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === true,
288 handler: ({ account }) => this.unblockAccountByUser(account)
289 },
290 {
291 label: $localize`Mute the instance`,
292 description: $localize`Hide any content from that instance for you.`,
80badf49 293 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === false,
a2c3564a
C
294 handler: ({ account }) => this.blockServerByUser(account.host)
295 },
296 {
297 label: $localize`Unmute the instance`,
298 description: $localize`Show back content from that instance for you.`,
80badf49 299 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === true,
a2c3564a
C
300 handler: ({ account }) => this.unblockServerByUser(account.host)
301 },
302 {
303 label: $localize`Remove comments from your videos`,
304 description: $localize`Remove comments made by this account on your videos.`,
305 isDisplayed: ({ account }) => !this.isMyAccount(account),
306 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
af5767ff 307 }
a2c3564a 308 ]
af5767ff 309
a2c3564a
C
310 return myAccountActions
311 }
312
313 private buildInstanceModerationActions () {
314 if (!this.authService.isLoggedIn()) return []
315
316 const authUser = this.authService.getUser()
317
318 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
319
320 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
321 instanceActions = instanceActions.concat([
322 {
323 label: $localize`Edit user`,
324 description: $localize`Change quota, role, and more.`,
325 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
326 },
327 {
328 label: $localize`Delete user`,
329 description: $localize`Videos will be deleted, comments will be tombstoned.`,
330 isDisplayed: ({ user }) => !this.isMyUser(user),
331 handler: ({ user }) => this.removeUser(user)
332 },
333 {
334 label: $localize`Ban`,
335 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
336 handler: ({ user }) => this.openBanUserModal(user),
337 isDisplayed: ({ user }) => !this.isMyUser(user) && !user.blocked
338 },
339 {
340 label: $localize`Unban user`,
341 description: $localize`Allow the user to login and create videos/comments again`,
342 handler: ({ user }) => this.unbanUser(user),
343 isDisplayed: ({ user }) => !this.isMyUser(user) && user.blocked
344 },
345 {
346 label: $localize`Set Email as Verified`,
347 handler: ({ user }) => this.setEmailAsVerified(user),
348 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
65b21c96 349 }
a2c3564a
C
350 ])
351 }
65b21c96 352
a2c3564a
C
353 // Instance actions on account blocklists
354 if (this.account && authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
355 instanceActions = instanceActions.concat([
356 {
357 label: $localize`Mute this account`,
358 description: $localize`Hide any content from that user from you, your instance and its users.`,
359 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === false,
360 handler: ({ account }) => this.blockAccountByInstance(account)
361 },
362 {
363 label: $localize`Unmute this account`,
364 description: $localize`Show this user's content to the users of this instance again.`,
365 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === true,
366 handler: ({ account }) => this.unblockAccountByInstance(account)
65b21c96 367 }
a2c3564a
C
368 ])
369 }
f97c91f7 370
a2c3564a
C
371 // Instance actions on server blocklists
372 if (this.account && authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
373 instanceActions = instanceActions.concat([
374 {
375 label: $localize`Mute the instance`,
376 description: $localize`Hide any content from that instance from you, your instance and its users.`,
377 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
378 handler: ({ account }) => this.blockServerByInstance(account.host)
379 },
380 {
381 label: $localize`Unmute the instance by your instance`,
382 description: $localize`Show back content from that instance for you, your instance and its users.`,
383 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
384 handler: ({ account }) => this.unblockServerByInstance(account.host)
923ff87d 385 }
a2c3564a
C
386 ])
387 }
923ff87d 388
a2c3564a
C
389 if (this.account && authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
390 instanceActions = instanceActions.concat([
391 {
392 label: $localize`Remove comments from your instance`,
393 description: $localize`Remove comments made by this account from your instance.`,
394 isDisplayed: ({ account }) => !this.isMyAccount(account),
395 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
f97c91f7 396 }
a2c3564a 397 ])
79bd2632 398 }
a2c3564a
C
399
400 if (instanceActions.length === 0) return []
401
402 return [ { label: $localize`Instance moderation`, isHeader: true }, ...instanceActions ]
79bd2632 403 }
e724fa93 404}