1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core'
2 import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
3 import { Account, DropdownAction } from '@app/shared/shared-main'
4 import { BulkRemoveCommentsOfBody, User, UserRight } from '@shared/models'
5 import { UserAdminService } from '../shared-users'
6 import { BlocklistService } from './blocklist.service'
7 import { BulkService } from './bulk.service'
8 import { UserBanModalComponent } from './user-ban-modal.component'
10 export type AccountMutedStatus =
11 Pick<Account, 'id' | 'nameWithHost' | 'host' | 'userId' |
12 'mutedByInstance' | 'mutedByUser' | 'mutedServerByInstance' | 'mutedServerByUser'>
14 export type UserModerationDisplayType = {
16 instanceAccount?: boolean
17 instanceUser?: boolean
21 selector: 'my-user-moderation-dropdown',
22 templateUrl: './user-moderation-dropdown.component.html'
24 export class UserModerationDropdownComponent implements OnInit, OnChanges {
25 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
28 @Input() account: AccountMutedStatus
29 @Input() prependActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[]
31 @Input() buttonSize: 'normal' | 'small' = 'normal'
32 @Input() buttonStyled = true
33 @Input() placement = 'right-top right-bottom auto'
34 @Input() label: string
35 @Input() container: 'body' | undefined = undefined
37 @Input() displayOptions: UserModerationDisplayType = {
39 instanceAccount: true,
43 @Output() userChanged = new EventEmitter()
44 @Output() userDeleted = new EventEmitter()
46 userActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[][] = []
48 requiresEmailVerification = false
51 private authService: AuthService,
52 private notifier: Notifier,
53 private confirmService: ConfirmService,
54 private serverService: ServerService,
55 private userAdminService: UserAdminService,
56 private blocklistService: BlocklistService,
57 private bulkService: BulkService
61 this.serverService.getConfig()
62 .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
69 openBanUserModal (user: User) {
70 if (user.username === 'root') {
71 this.notifier.error($localize`You cannot ban root.`)
75 this.userBanModal.openModal(user)
79 this.userChanged.emit()
82 async unbanUser (user: User) {
83 const res = await this.confirmService.confirm($localize`Do you really want to unban ${user.username}?`, $localize`Unban`)
84 if (res === false) return
86 this.userAdminService.unbanUsers(user)
89 this.notifier.success($localize`User ${user.username} unbanned.`)
90 this.userChanged.emit()
93 error: err => this.notifier.error(err.message)
97 async removeUser (user: User) {
98 if (user.username === 'root') {
99 this.notifier.error($localize`You cannot delete root.`)
103 // eslint-disable-next-line max-len
104 const message = $localize`If you remove this user, you won't be able to create another user or channel with <strong>${user.username}</strong> username!`
105 const res = await this.confirmService.confirm(message, $localize`Delete ${user.username}`)
106 if (res === false) return
108 this.userAdminService.removeUser(user)
111 this.notifier.success($localize`User ${user.username} deleted.`)
112 this.userDeleted.emit()
115 error: err => this.notifier.error(err.message)
119 setEmailAsVerified (user: User) {
120 this.userAdminService.updateUser(user.id, { emailVerified: true })
123 this.notifier.success($localize`User ${user.username} email set as verified`)
124 this.userChanged.emit()
127 error: err => this.notifier.error(err.message)
131 blockAccountByUser (account: AccountMutedStatus) {
132 this.blocklistService.blockAccountByUser(account)
135 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
137 this.account.mutedByUser = true
138 this.userChanged.emit()
141 error: err => this.notifier.error(err.message)
145 unblockAccountByUser (account: AccountMutedStatus) {
146 this.blocklistService.unblockAccountByUser(account)
149 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
151 this.account.mutedByUser = false
152 this.userChanged.emit()
155 error: err => this.notifier.error(err.message)
159 blockServerByUser (host: string) {
160 this.blocklistService.blockServerByUser(host)
163 this.notifier.success($localize`Instance ${host} muted.`)
165 this.account.mutedServerByUser = true
166 this.userChanged.emit()
169 error: err => this.notifier.error(err.message)
173 unblockServerByUser (host: string) {
174 this.blocklistService.unblockServerByUser(host)
177 this.notifier.success($localize`Instance ${host} unmuted.`)
179 this.account.mutedServerByUser = false
180 this.userChanged.emit()
183 error: err => this.notifier.error(err.message)
187 blockAccountByInstance (account: AccountMutedStatus) {
188 this.blocklistService.blockAccountByInstance(account)
191 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
193 this.account.mutedByInstance = true
194 this.userChanged.emit()
197 error: err => this.notifier.error(err.message)
201 unblockAccountByInstance (account: AccountMutedStatus) {
202 this.blocklistService.unblockAccountByInstance(account)
205 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
207 this.account.mutedByInstance = false
208 this.userChanged.emit()
211 error: err => this.notifier.error(err.message)
215 blockServerByInstance (host: string) {
216 this.blocklistService.blockServerByInstance(host)
219 this.notifier.success($localize`Instance ${host} muted by the instance.`)
221 this.account.mutedServerByInstance = true
222 this.userChanged.emit()
225 error: err => this.notifier.error(err.message)
229 unblockServerByInstance (host: string) {
230 this.blocklistService.unblockServerByInstance(host)
233 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
235 this.account.mutedServerByInstance = false
236 this.userChanged.emit()
239 error: err => this.notifier.error(err.message)
243 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
244 const message = $localize`Are you sure you want to remove all the comments of this account?`
245 const res = await this.confirmService.confirm(message, $localize`Delete account comments`)
246 if (res === false) return
248 this.bulkService.removeCommentsOf(body)
251 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
254 error: err => this.notifier.error(err.message)
258 getRouterUserEditLink (user: User) {
259 return [ '/admin', 'users', 'update', user.id ]
262 private isMyUser (user: User) {
263 return user && this.authService.getUser().id === user.id
266 private isMyAccount (account: AccountMutedStatus) {
267 return account && this.authService.getUser().account.id === account.id
270 private buildActions () {
271 this.userActions = []
273 if (this.prependActions && this.prependActions.length !== 0) {
279 const myAccountModerationActions = this.buildMyAccountModerationActions()
280 const instanceModerationActions = this.buildInstanceModerationActions()
282 if (myAccountModerationActions.length !== 0) this.userActions.push(myAccountModerationActions)
283 if (instanceModerationActions.length !== 0) this.userActions.push(instanceModerationActions)
286 private buildMyAccountModerationActions () {
287 if (!this.account || !this.displayOptions.myAccount || !this.authService.isLoggedIn()) return []
289 const myAccountActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[] = [
291 label: $localize`My account moderation`,
296 label: $localize`Mute this account`,
297 description: $localize`Hide any content from that user from you.`,
298 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === false,
299 handler: ({ account }) => this.blockAccountByUser(account)
302 label: $localize`Unmute this account`,
303 description: $localize`Show back content from that user for you.`,
304 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === true,
305 handler: ({ account }) => this.unblockAccountByUser(account)
308 label: $localize`Mute the instance`,
309 description: $localize`Hide any content from that instance for you.`,
310 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === false,
311 handler: ({ account }) => this.blockServerByUser(account.host)
314 label: $localize`Unmute the instance`,
315 description: $localize`Show back content from that instance for you.`,
316 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === true,
317 handler: ({ account }) => this.unblockServerByUser(account.host)
320 label: $localize`Remove comments from your videos`,
321 description: $localize`Remove comments made by this account on your videos.`,
322 isDisplayed: ({ account }) => !this.isMyAccount(account),
323 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
327 return myAccountActions
330 private buildInstanceModerationActions () {
331 if (!this.authService.isLoggedIn()) return []
333 const authUser = this.authService.getUser()
335 let instanceActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[] = []
337 if (this.user && this.displayOptions.instanceUser && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
338 instanceActions = instanceActions.concat([
340 label: $localize`Edit user`,
341 description: $localize`Change quota, role, and more.`,
342 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
345 label: $localize`Delete user`,
346 description: $localize`Videos will be deleted, comments will be tombstoned.`,
347 isDisplayed: ({ user }) => !this.isMyUser(user),
348 handler: ({ user }) => this.removeUser(user)
351 label: $localize`Ban`,
352 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
353 handler: ({ user }) => this.openBanUserModal(user),
354 isDisplayed: ({ user }) => !this.isMyUser(user) && !user.blocked
357 label: $localize`Unban user`,
358 description: $localize`Allow the user to login and create videos/comments again`,
359 handler: ({ user }) => this.unbanUser(user),
360 isDisplayed: ({ user }) => !this.isMyUser(user) && user.blocked
363 label: $localize`Set Email as Verified`,
364 handler: ({ user }) => this.setEmailAsVerified(user),
365 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
370 // Instance actions on account blocklists
371 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
372 instanceActions = instanceActions.concat([
374 label: $localize`Mute this account`,
375 description: $localize`Hide any content from that user from you, your instance and its users.`,
376 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === false,
377 handler: ({ account }) => this.blockAccountByInstance(account)
380 label: $localize`Unmute this account`,
381 description: $localize`Show this user's content to the users of this instance again.`,
382 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === true,
383 handler: ({ account }) => this.unblockAccountByInstance(account)
388 // Instance actions on server blocklists
389 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
390 instanceActions = instanceActions.concat([
392 label: $localize`Mute the instance`,
393 description: $localize`Hide any content from that instance from you, your instance and its users.`,
394 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
395 handler: ({ account }) => this.blockServerByInstance(account.host)
398 label: $localize`Unmute the instance by your instance`,
399 description: $localize`Show back content from that instance for you, your instance and its users.`,
400 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
401 handler: ({ account }) => this.unblockServerByInstance(account.host)
406 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
407 instanceActions = instanceActions.concat([
409 label: $localize`Remove comments from your instance`,
410 description: $localize`Remove comments made by this account from your instance.`,
411 isDisplayed: ({ account }) => !this.isMyAccount(account),
412 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
417 if (instanceActions.length === 0) return []
419 return [ { label: $localize`Instance moderation`, isHeader: true }, ...instanceActions ]