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 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}`)
105 if (res === false) return
107 this.userAdminService.removeUser(user)
110 this.notifier.success($localize`User ${user.username} deleted.`)
111 this.userDeleted.emit()
114 error: err => this.notifier.error(err.message)
118 setEmailAsVerified (user: User) {
119 this.userAdminService.updateUser(user.id, { emailVerified: true })
122 this.notifier.success($localize`User ${user.username} email set as verified`)
123 this.userChanged.emit()
126 error: err => this.notifier.error(err.message)
130 blockAccountByUser (account: AccountMutedStatus) {
131 this.blocklistService.blockAccountByUser(account)
134 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
136 this.account.mutedByUser = true
137 this.userChanged.emit()
140 error: err => this.notifier.error(err.message)
144 unblockAccountByUser (account: AccountMutedStatus) {
145 this.blocklistService.unblockAccountByUser(account)
148 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
150 this.account.mutedByUser = false
151 this.userChanged.emit()
154 error: err => this.notifier.error(err.message)
158 blockServerByUser (host: string) {
159 this.blocklistService.blockServerByUser(host)
162 this.notifier.success($localize`Instance ${host} muted.`)
164 this.account.mutedServerByUser = true
165 this.userChanged.emit()
168 error: err => this.notifier.error(err.message)
172 unblockServerByUser (host: string) {
173 this.blocklistService.unblockServerByUser(host)
176 this.notifier.success($localize`Instance ${host} unmuted.`)
178 this.account.mutedServerByUser = false
179 this.userChanged.emit()
182 error: err => this.notifier.error(err.message)
186 blockAccountByInstance (account: AccountMutedStatus) {
187 this.blocklistService.blockAccountByInstance(account)
190 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
192 this.account.mutedByInstance = true
193 this.userChanged.emit()
196 error: err => this.notifier.error(err.message)
200 unblockAccountByInstance (account: AccountMutedStatus) {
201 this.blocklistService.unblockAccountByInstance(account)
204 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
206 this.account.mutedByInstance = false
207 this.userChanged.emit()
210 error: err => this.notifier.error(err.message)
214 blockServerByInstance (host: string) {
215 this.blocklistService.blockServerByInstance(host)
218 this.notifier.success($localize`Instance ${host} muted by the instance.`)
220 this.account.mutedServerByInstance = true
221 this.userChanged.emit()
224 error: err => this.notifier.error(err.message)
228 unblockServerByInstance (host: string) {
229 this.blocklistService.unblockServerByInstance(host)
232 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
234 this.account.mutedServerByInstance = false
235 this.userChanged.emit()
238 error: err => this.notifier.error(err.message)
242 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
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`)
245 if (res === false) return
247 this.bulkService.removeCommentsOf(body)
250 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
253 error: err => this.notifier.error(err.message)
257 getRouterUserEditLink (user: User) {
258 return [ '/admin', 'users', 'update', user.id ]
261 private isMyUser (user: User) {
262 return user && this.authService.getUser().id === user.id
265 private isMyAccount (account: AccountMutedStatus) {
266 return account && this.authService.getUser().account.id === account.id
269 private buildActions () {
270 this.userActions = []
272 if (this.prependActions && this.prependActions.length !== 0) {
278 const myAccountModerationActions = this.buildMyAccountModerationActions()
279 const instanceModerationActions = this.buildInstanceModerationActions()
281 if (myAccountModerationActions.length !== 0) this.userActions.push(myAccountModerationActions)
282 if (instanceModerationActions.length !== 0) this.userActions.push(instanceModerationActions)
285 private buildMyAccountModerationActions () {
286 if (!this.account || !this.displayOptions.myAccount || !this.authService.isLoggedIn()) return []
288 const myAccountActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[] = [
290 label: $localize`My account moderation`,
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)
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)
307 label: $localize`Mute the instance`,
308 description: $localize`Hide any content from that instance for you.`,
309 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === false,
310 handler: ({ account }) => this.blockServerByUser(account.host)
313 label: $localize`Unmute the instance`,
314 description: $localize`Show back content from that instance for you.`,
315 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === true,
316 handler: ({ account }) => this.unblockServerByUser(account.host)
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' })
326 return myAccountActions
329 private buildInstanceModerationActions () {
330 if (!this.authService.isLoggedIn()) return []
332 const authUser = this.authService.getUser()
334 let instanceActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[] = []
336 if (this.user && this.displayOptions.instanceUser && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
337 instanceActions = instanceActions.concat([
339 label: $localize`Edit user`,
340 description: $localize`Change quota, role, and more.`,
341 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
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)
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
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
362 label: $localize`Set Email as Verified`,
363 handler: ({ user }) => this.setEmailAsVerified(user),
364 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
369 // Instance actions on account blocklists
370 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
371 instanceActions = instanceActions.concat([
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)
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)
387 // Instance actions on server blocklists
388 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
389 instanceActions = instanceActions.concat([
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)
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)
405 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
406 instanceActions = instanceActions.concat([
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' })
416 if (instanceActions.length === 0) return []
418 return [ { label: $localize`Instance moderation`, isHeader: true }, ...instanceActions ]