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