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 error: 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)
93 this.notifier.success($localize`User ${user.username} deleted.`)
94 this.userDeleted.emit()
97 error: err => this.notifier.error(err.message)
101 setEmailAsVerified (user: User) {
102 this.userService.updateUser(user.id, { emailVerified: true })
105 this.notifier.success($localize`User ${user.username} email set as verified`)
106 this.userChanged.emit()
109 error: err => this.notifier.error(err.message)
113 blockAccountByUser (account: Account) {
114 this.blocklistService.blockAccountByUser(account)
117 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
119 this.account.mutedByUser = true
120 this.userChanged.emit()
123 error: err => this.notifier.error(err.message)
127 unblockAccountByUser (account: Account) {
128 this.blocklistService.unblockAccountByUser(account)
131 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
133 this.account.mutedByUser = false
134 this.userChanged.emit()
137 error: err => this.notifier.error(err.message)
141 blockServerByUser (host: string) {
142 this.blocklistService.blockServerByUser(host)
145 this.notifier.success($localize`Instance ${host} muted.`)
147 this.account.mutedServerByUser = true
148 this.userChanged.emit()
151 error: err => this.notifier.error(err.message)
155 unblockServerByUser (host: string) {
156 this.blocklistService.unblockServerByUser(host)
159 this.notifier.success($localize`Instance ${host} unmuted.`)
161 this.account.mutedServerByUser = false
162 this.userChanged.emit()
165 error: err => this.notifier.error(err.message)
169 blockAccountByInstance (account: Account) {
170 this.blocklistService.blockAccountByInstance(account)
173 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
175 this.account.mutedByInstance = true
176 this.userChanged.emit()
179 error: err => this.notifier.error(err.message)
183 unblockAccountByInstance (account: Account) {
184 this.blocklistService.unblockAccountByInstance(account)
187 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
189 this.account.mutedByInstance = false
190 this.userChanged.emit()
193 error: err => this.notifier.error(err.message)
197 blockServerByInstance (host: string) {
198 this.blocklistService.blockServerByInstance(host)
201 this.notifier.success($localize`Instance ${host} muted by the instance.`)
203 this.account.mutedServerByInstance = true
204 this.userChanged.emit()
207 error: err => this.notifier.error(err.message)
211 unblockServerByInstance (host: string) {
212 this.blocklistService.unblockServerByInstance(host)
215 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
217 this.account.mutedServerByInstance = false
218 this.userChanged.emit()
221 error: err => this.notifier.error(err.message)
225 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
226 const message = $localize`Are you sure you want to remove all the comments of this account?`
227 const res = await this.confirmService.confirm(message, $localize`Delete account comments`)
228 if (res === false) return
230 this.bulkService.removeCommentsOf(body)
233 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
236 error: err => this.notifier.error(err.message)
240 getRouterUserEditLink (user: User) {
241 return [ '/admin', 'users', 'update', user.id ]
244 private buildActions () {
245 this.userActions = []
247 if (this.prependActions) {
253 if (this.authService.isLoggedIn()) {
254 const authUser = this.authService.getUser()
256 if (this.user && authUser.id === this.user.id) return
258 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
259 this.userActions.push([
261 label: $localize`Edit user`,
262 description: $localize`Change quota, role, and more.`,
263 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
266 label: $localize`Delete user`,
267 description: $localize`Videos will be deleted, comments will be tombstoned.`,
268 handler: ({ user }) => this.removeUser(user)
271 label: $localize`Ban`,
272 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
273 handler: ({ user }) => this.openBanUserModal(user),
274 isDisplayed: ({ user }) => !user.blocked
277 label: $localize`Unban user`,
278 description: $localize`Allow the user to login and create videos/comments again`,
279 handler: ({ user }) => this.unbanUser(user),
280 isDisplayed: ({ user }) => user.blocked
283 label: $localize`Set Email as Verified`,
284 handler: ({ user }) => this.setEmailAsVerified(user),
285 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
290 // Actions on accounts/servers
293 this.userActions.push([
295 label: $localize`Mute this account`,
296 description: $localize`Hide any content from that user from you.`,
297 isDisplayed: ({ 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 }) => 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.mutedServerByInstance === 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.mutedServerByInstance === 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 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
325 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
327 // Instance actions on account blocklists
328 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
329 instanceActions = instanceActions.concat([
331 label: $localize`Mute this account by your instance`,
332 description: $localize`Hide any content from that user from you, your instance and its users.`,
333 isDisplayed: ({ account }) => account.mutedByInstance === false,
334 handler: ({ account }) => this.blockAccountByInstance(account)
337 label: $localize`Unmute this account by your instance`,
338 description: $localize`Show this user's content to the users of this instance again.`,
339 isDisplayed: ({ account }) => account.mutedByInstance === true,
340 handler: ({ account }) => this.unblockAccountByInstance(account)
345 // Instance actions on server blocklists
346 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
347 instanceActions = instanceActions.concat([
349 label: $localize`Mute the instance by your instance`,
350 description: $localize`Hide any content from that instance from you, your instance and its users.`,
351 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
352 handler: ({ account }) => this.blockServerByInstance(account.host)
355 label: $localize`Unmute the instance by your instance`,
356 description: $localize`Show back content from that instance for you, your instance and its users.`,
357 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
358 handler: ({ account }) => this.unblockServerByInstance(account.host)
363 if (authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
364 instanceActions = instanceActions.concat([
366 label: $localize`Remove comments from your instance`,
367 description: $localize`Remove comments made by this account from your instance.`,
368 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
373 if (instanceActions.length !== 0) {
374 this.userActions.push(instanceActions)