1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core'
2 import { AuthService, ConfirmService, Notifier, ServerService, UserService } from '@app/core'
3 import { Account, DropdownAction } from '@app/shared/shared-main'
4 import { BulkRemoveCommentsOfBody, User, UserRight } from '@shared/models'
5 import { BlocklistService } from './blocklist.service'
6 import { BulkService } from './bulk.service'
7 import { UserBanModalComponent } from './user-ban-modal.component'
10 selector: 'my-user-moderation-dropdown',
11 templateUrl: './user-moderation-dropdown.component.html'
13 export class UserModerationDropdownComponent implements OnInit, OnChanges {
14 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
17 @Input() account: Account
18 @Input() prependActions: DropdownAction<{ user: User, account: Account }>[]
20 @Input() buttonSize: 'normal' | 'small' = 'normal'
21 @Input() buttonStyled = true
22 @Input() placement = 'right-top right-bottom auto'
23 @Input() label: string
24 @Input() container: 'body' | undefined = undefined
26 @Output() userChanged = new EventEmitter()
27 @Output() userDeleted = new EventEmitter()
29 userActions: DropdownAction<{ user: User, account: Account }>[][] = []
31 requiresEmailVerification = false
34 private authService: AuthService,
35 private notifier: Notifier,
36 private confirmService: ConfirmService,
37 private serverService: ServerService,
38 private userService: UserService,
39 private blocklistService: BlocklistService,
40 private bulkService: BulkService
44 this.serverService.getConfig()
45 .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
52 openBanUserModal (user: User) {
53 if (user.username === 'root') {
54 this.notifier.error($localize`You cannot ban root.`)
58 this.userBanModal.openModal(user)
62 this.userChanged.emit()
65 async unbanUser (user: User) {
66 const res = await this.confirmService.confirm($localize`Do you really want to unban ${user.username}?`, $localize`Unban`)
67 if (res === false) return
69 this.userService.unbanUsers(user)
72 this.notifier.success($localize`User ${user.username} unbanned.`)
73 this.userChanged.emit()
76 err => this.notifier.error(err.message)
80 async removeUser (user: User) {
81 if (user.username === 'root') {
82 this.notifier.error($localize`You cannot delete root.`)
86 const message = $localize`If you remove this user, you will not be able to create another with the same username!`
87 const res = await this.confirmService.confirm(message, $localize`Delete`)
88 if (res === false) return
90 this.userService.removeUser(user).subscribe(
92 this.notifier.success($localize`User ${user.username} deleted.`)
93 this.userDeleted.emit()
96 err => this.notifier.error(err.message)
100 setEmailAsVerified (user: User) {
101 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
103 this.notifier.success($localize`User ${user.username} email set as verified`)
104 this.userChanged.emit()
107 err => this.notifier.error(err.message)
111 blockAccountByUser (account: Account) {
112 this.blocklistService.blockAccountByUser(account)
115 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
117 this.account.mutedByUser = true
118 this.userChanged.emit()
121 err => this.notifier.error(err.message)
125 unblockAccountByUser (account: Account) {
126 this.blocklistService.unblockAccountByUser(account)
129 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
131 this.account.mutedByUser = false
132 this.userChanged.emit()
135 err => this.notifier.error(err.message)
139 blockServerByUser (host: string) {
140 this.blocklistService.blockServerByUser(host)
143 this.notifier.success($localize`Instance ${host} muted.`)
145 this.account.mutedServerByUser = true
146 this.userChanged.emit()
149 err => this.notifier.error(err.message)
153 unblockServerByUser (host: string) {
154 this.blocklistService.unblockServerByUser(host)
157 this.notifier.success($localize`Instance ${host} unmuted.`)
159 this.account.mutedServerByUser = false
160 this.userChanged.emit()
163 err => this.notifier.error(err.message)
167 blockAccountByInstance (account: Account) {
168 this.blocklistService.blockAccountByInstance(account)
171 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
173 this.account.mutedByInstance = true
174 this.userChanged.emit()
177 err => this.notifier.error(err.message)
181 unblockAccountByInstance (account: Account) {
182 this.blocklistService.unblockAccountByInstance(account)
185 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
187 this.account.mutedByInstance = false
188 this.userChanged.emit()
191 err => this.notifier.error(err.message)
195 blockServerByInstance (host: string) {
196 this.blocklistService.blockServerByInstance(host)
199 this.notifier.success($localize`Instance ${host} muted by the instance.`)
201 this.account.mutedServerByInstance = true
202 this.userChanged.emit()
205 err => this.notifier.error(err.message)
209 unblockServerByInstance (host: string) {
210 this.blocklistService.unblockServerByInstance(host)
213 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
215 this.account.mutedServerByInstance = false
216 this.userChanged.emit()
219 err => this.notifier.error(err.message)
223 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
224 const message = $localize`Are you sure you want to remove all the comments of this account?`
225 const res = await this.confirmService.confirm(message, $localize`Delete account comments`)
226 if (res === false) return
228 this.bulkService.removeCommentsOf(body)
231 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
234 err => this.notifier.error(err.message)
238 getRouterUserEditLink (user: User) {
239 return [ '/admin', 'users', 'update', user.id ]
242 private buildActions () {
243 this.userActions = []
245 if (this.prependActions) {
251 if (this.authService.isLoggedIn()) {
252 const authUser = this.authService.getUser()
254 if (this.user && authUser.id === this.user.id) return
256 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
257 this.userActions.push([
259 label: $localize`Edit user`,
260 description: $localize`Change quota, role, and more.`,
261 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
264 label: $localize`Delete user`,
265 description: $localize`Videos will be deleted, comments will be tombstoned.`,
266 handler: ({ user }) => this.removeUser(user)
269 label: $localize`Ban`,
270 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
271 handler: ({ user }) => this.openBanUserModal(user),
272 isDisplayed: ({ user }) => !user.blocked
275 label: $localize`Unban user`,
276 description: $localize`Allow the user to login and create videos/comments again`,
277 handler: ({ user }) => this.unbanUser(user),
278 isDisplayed: ({ user }) => user.blocked
281 label: $localize`Set Email as Verified`,
282 handler: ({ user }) => this.setEmailAsVerified(user),
283 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
288 // Actions on accounts/servers
291 this.userActions.push([
293 label: $localize`Mute this account`,
294 description: $localize`Hide any content from that user from you.`,
295 isDisplayed: ({ account }) => account.mutedByUser === false,
296 handler: ({ account }) => this.blockAccountByUser(account)
299 label: $localize`Unmute this account`,
300 description: $localize`Show back content from that user for you.`,
301 isDisplayed: ({ account }) => account.mutedByUser === true,
302 handler: ({ account }) => this.unblockAccountByUser(account)
305 label: $localize`Mute the instance`,
306 description: $localize`Hide any content from that instance for you.`,
307 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
308 handler: ({ account }) => this.blockServerByUser(account.host)
311 label: $localize`Unmute the instance`,
312 description: $localize`Show back content from that instance for you.`,
313 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
314 handler: ({ account }) => this.unblockServerByUser(account.host)
317 label: $localize`Remove comments from your videos`,
318 description: $localize`Remove comments made by this account on your videos.`,
319 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
323 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
325 // Instance actions on account blocklists
326 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
327 instanceActions = instanceActions.concat([
329 label: $localize`Mute this account by your instance`,
330 description: $localize`Hide any content from that user from you, your instance and its users.`,
331 isDisplayed: ({ account }) => account.mutedByInstance === false,
332 handler: ({ account }) => this.blockAccountByInstance(account)
335 label: $localize`Unmute this account by your instance`,
336 description: $localize`Show this user's content to the users of this instance again.`,
337 isDisplayed: ({ account }) => account.mutedByInstance === true,
338 handler: ({ account }) => this.unblockAccountByInstance(account)
343 // Instance actions on server blocklists
344 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
345 instanceActions = instanceActions.concat([
347 label: $localize`Mute the instance by your instance`,
348 description: $localize`Hide any content from that instance from you, your instance and its users.`,
349 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
350 handler: ({ account }) => this.blockServerByInstance(account.host)
353 label: $localize`Unmute the instance by your instance`,
354 description: $localize`Show back content from that instance for you, your instance and its users.`,
355 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
356 handler: ({ account }) => this.unblockServerByInstance(account.host)
361 if (authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
362 instanceActions = instanceActions.concat([
364 label: $localize`Remove comments from your instance`,
365 description: $localize`Remove comments made by this account from your instance.`,
366 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
371 if (instanceActions.length !== 0) {
372 this.userActions.push(instanceActions)