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'
11 selector: 'my-user-moderation-dropdown',
12 templateUrl: './user-moderation-dropdown.component.html'
14 export class UserModerationDropdownComponent implements OnInit, OnChanges {
15 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
18 @Input() account: Account
19 @Input() prependActions: DropdownAction<{ user: User, account: Account }>[]
21 @Input() buttonSize: 'normal' | 'small' = 'normal'
22 @Input() buttonStyled = true
23 @Input() placement = 'right-top right-bottom auto'
24 @Input() label: string
25 @Input() container: 'body' | undefined = undefined
27 @Output() userChanged = new EventEmitter()
28 @Output() userDeleted = new EventEmitter()
30 userActions: DropdownAction<{ user: User, account: Account }>[][] = []
32 requiresEmailVerification = false
35 private authService: AuthService,
36 private notifier: Notifier,
37 private confirmService: ConfirmService,
38 private serverService: ServerService,
39 private userAdminService: UserAdminService,
40 private blocklistService: BlocklistService,
41 private bulkService: BulkService
45 this.serverService.getConfig()
46 .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
53 openBanUserModal (user: User) {
54 if (user.username === 'root') {
55 this.notifier.error($localize`You cannot ban root.`)
59 this.userBanModal.openModal(user)
63 this.userChanged.emit()
66 async unbanUser (user: User) {
67 const res = await this.confirmService.confirm($localize`Do you really want to unban ${user.username}?`, $localize`Unban`)
68 if (res === false) return
70 this.userAdminService.unbanUsers(user)
73 this.notifier.success($localize`User ${user.username} unbanned.`)
74 this.userChanged.emit()
77 error: err => this.notifier.error(err.message)
81 async removeUser (user: User) {
82 if (user.username === 'root') {
83 this.notifier.error($localize`You cannot delete root.`)
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}`)
89 if (res === false) return
91 this.userAdminService.removeUser(user)
94 this.notifier.success($localize`User ${user.username} deleted.`)
95 this.userDeleted.emit()
98 error: err => this.notifier.error(err.message)
102 setEmailAsVerified (user: User) {
103 this.userAdminService.updateUser(user.id, { emailVerified: true })
106 this.notifier.success($localize`User ${user.username} email set as verified`)
107 this.userChanged.emit()
110 error: err => this.notifier.error(err.message)
114 blockAccountByUser (account: Account) {
115 this.blocklistService.blockAccountByUser(account)
118 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
120 this.account.mutedByUser = true
121 this.userChanged.emit()
124 error: err => this.notifier.error(err.message)
128 unblockAccountByUser (account: Account) {
129 this.blocklistService.unblockAccountByUser(account)
132 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
134 this.account.mutedByUser = false
135 this.userChanged.emit()
138 error: err => this.notifier.error(err.message)
142 blockServerByUser (host: string) {
143 this.blocklistService.blockServerByUser(host)
146 this.notifier.success($localize`Instance ${host} muted.`)
148 this.account.mutedServerByUser = true
149 this.userChanged.emit()
152 error: err => this.notifier.error(err.message)
156 unblockServerByUser (host: string) {
157 this.blocklistService.unblockServerByUser(host)
160 this.notifier.success($localize`Instance ${host} unmuted.`)
162 this.account.mutedServerByUser = false
163 this.userChanged.emit()
166 error: err => this.notifier.error(err.message)
170 blockAccountByInstance (account: Account) {
171 this.blocklistService.blockAccountByInstance(account)
174 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
176 this.account.mutedByInstance = true
177 this.userChanged.emit()
180 error: err => this.notifier.error(err.message)
184 unblockAccountByInstance (account: Account) {
185 this.blocklistService.unblockAccountByInstance(account)
188 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
190 this.account.mutedByInstance = false
191 this.userChanged.emit()
194 error: err => this.notifier.error(err.message)
198 blockServerByInstance (host: string) {
199 this.blocklistService.blockServerByInstance(host)
202 this.notifier.success($localize`Instance ${host} muted by the instance.`)
204 this.account.mutedServerByInstance = true
205 this.userChanged.emit()
208 error: err => this.notifier.error(err.message)
212 unblockServerByInstance (host: string) {
213 this.blocklistService.unblockServerByInstance(host)
216 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
218 this.account.mutedServerByInstance = false
219 this.userChanged.emit()
222 error: err => this.notifier.error(err.message)
226 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
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`)
229 if (res === false) return
231 this.bulkService.removeCommentsOf(body)
234 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
237 error: err => this.notifier.error(err.message)
241 getRouterUserEditLink (user: User) {
242 return [ '/admin', 'users', 'update', user.id ]
245 private isMyUser (user: User) {
246 return user && this.authService.getUser().id === user.id
249 private isMyAccount (account: Account) {
250 return account && this.authService.getUser().account.id === account.id
253 private buildActions () {
254 this.userActions = []
256 if (this.prependActions && this.prependActions.length !== 0) {
262 const myAccountModerationActions = this.buildMyAccountModerationActions()
263 const instanceModerationActions = this.buildInstanceModerationActions()
265 if (myAccountModerationActions.length !== 0) this.userActions.push(myAccountModerationActions)
266 if (instanceModerationActions.length !== 0) this.userActions.push(instanceModerationActions)
269 private buildMyAccountModerationActions () {
270 if (!this.account || !this.authService.isLoggedIn()) return []
272 const myAccountActions: DropdownAction<{ user: User, account: Account }>[] = [
274 label: $localize`My account moderation`,
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)
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)
291 label: $localize`Mute the instance`,
292 description: $localize`Hide any content from that instance for you.`,
293 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === false,
294 handler: ({ account }) => this.blockServerByUser(account.host)
297 label: $localize`Unmute the instance`,
298 description: $localize`Show back content from that instance for you.`,
299 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === true,
300 handler: ({ account }) => this.unblockServerByUser(account.host)
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' })
310 return myAccountActions
313 private buildInstanceModerationActions () {
314 if (!this.authService.isLoggedIn()) return []
316 const authUser = this.authService.getUser()
318 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
320 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
321 instanceActions = instanceActions.concat([
323 label: $localize`Edit user`,
324 description: $localize`Change quota, role, and more.`,
325 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
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)
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
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
346 label: $localize`Set Email as Verified`,
347 handler: ({ user }) => this.setEmailAsVerified(user),
348 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
353 // Instance actions on account blocklists
354 if (this.account && authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
355 instanceActions = instanceActions.concat([
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)
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)
371 // Instance actions on server blocklists
372 if (this.account && authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
373 instanceActions = instanceActions.concat([
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)
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)
389 if (this.account && authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
390 instanceActions = instanceActions.concat([
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' })
400 if (instanceActions.length === 0) return []
402 return [ { label: $localize`Instance moderation`, isHeader: true }, ...instanceActions ]