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, ServerConfig, 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() placement = 'right-top right-bottom auto'
22 @Input() label: string
23 @Input() container: 'body' | undefined = undefined
25 @Output() userChanged = new EventEmitter()
26 @Output() userDeleted = new EventEmitter()
28 userActions: DropdownAction<{ user: User, account: Account }>[][] = []
30 private serverConfig: ServerConfig
33 private authService: AuthService,
34 private notifier: Notifier,
35 private confirmService: ConfirmService,
36 private serverService: ServerService,
37 private userService: UserService,
38 private blocklistService: BlocklistService,
39 private bulkService: BulkService
42 get requiresEmailVerification () {
43 return this.serverConfig.signup.requiresEmailVerification
47 this.serverConfig = this.serverService.getTmpConfig()
48 this.serverService.getConfig()
49 .subscribe(config => this.serverConfig = config)
56 openBanUserModal (user: User) {
57 if (user.username === 'root') {
58 this.notifier.error($localize`You cannot ban root.`)
62 this.userBanModal.openModal(user)
66 this.userChanged.emit()
69 async unbanUser (user: User) {
70 const res = await this.confirmService.confirm($localize`Do you really want to unban ${user.username}?`, $localize`Unban`)
71 if (res === false) return
73 this.userService.unbanUsers(user)
76 this.notifier.success($localize`User ${user.username} unbanned.`)
77 this.userChanged.emit()
80 err => this.notifier.error(err.message)
84 async removeUser (user: User) {
85 if (user.username === 'root') {
86 this.notifier.error($localize`You cannot delete root.`)
90 const message = $localize`If you remove this user, you will not be able to create another with the same username!`
91 const res = await this.confirmService.confirm(message, $localize`Delete`)
92 if (res === false) return
94 this.userService.removeUser(user).subscribe(
96 this.notifier.success($localize`User ${user.username} deleted.`)
97 this.userDeleted.emit()
100 err => this.notifier.error(err.message)
104 setEmailAsVerified (user: User) {
105 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
107 this.notifier.success($localize`User ${user.username} email set as verified`)
108 this.userChanged.emit()
111 err => this.notifier.error(err.message)
115 blockAccountByUser (account: Account) {
116 this.blocklistService.blockAccountByUser(account)
119 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
121 this.account.mutedByUser = true
122 this.userChanged.emit()
125 err => this.notifier.error(err.message)
129 unblockAccountByUser (account: Account) {
130 this.blocklistService.unblockAccountByUser(account)
133 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
135 this.account.mutedByUser = false
136 this.userChanged.emit()
139 err => this.notifier.error(err.message)
143 blockServerByUser (host: string) {
144 this.blocklistService.blockServerByUser(host)
147 this.notifier.success($localize`Instance ${host} muted.`)
149 this.account.mutedServerByUser = true
150 this.userChanged.emit()
153 err => this.notifier.error(err.message)
157 unblockServerByUser (host: string) {
158 this.blocklistService.unblockServerByUser(host)
161 this.notifier.success($localize`Instance ${host} unmuted.`)
163 this.account.mutedServerByUser = false
164 this.userChanged.emit()
167 err => this.notifier.error(err.message)
171 blockAccountByInstance (account: Account) {
172 this.blocklistService.blockAccountByInstance(account)
175 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
177 this.account.mutedByInstance = true
178 this.userChanged.emit()
181 err => this.notifier.error(err.message)
185 unblockAccountByInstance (account: Account) {
186 this.blocklistService.unblockAccountByInstance(account)
189 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
191 this.account.mutedByInstance = false
192 this.userChanged.emit()
195 err => this.notifier.error(err.message)
199 blockServerByInstance (host: string) {
200 this.blocklistService.blockServerByInstance(host)
203 this.notifier.success($localize`Instance ${host} muted by the instance.`)
205 this.account.mutedServerByInstance = true
206 this.userChanged.emit()
209 err => this.notifier.error(err.message)
213 unblockServerByInstance (host: string) {
214 this.blocklistService.unblockServerByInstance(host)
217 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
219 this.account.mutedServerByInstance = false
220 this.userChanged.emit()
223 err => this.notifier.error(err.message)
227 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
228 const message = $localize`Are you sure you want to remove all the comments of this account?`
229 const res = await this.confirmService.confirm(message, $localize`Delete account comments`)
230 if (res === false) return
232 this.bulkService.removeCommentsOf(body)
235 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
238 err => this.notifier.error(err.message)
242 getRouterUserEditLink (user: User) {
243 return [ '/admin', 'users', 'update', user.id ]
246 private buildActions () {
247 this.userActions = []
249 if (this.prependActions) {
255 if (this.authService.isLoggedIn()) {
256 const authUser = this.authService.getUser()
258 if (this.user && authUser.id === this.user.id) return
260 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
261 this.userActions.push([
263 label: $localize`Edit user`,
264 description: $localize`Change quota, role, and more.`,
265 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
268 label: $localize`Delete user`,
269 description: $localize`Videos will be deleted, comments will be tombstoned.`,
270 handler: ({ user }) => this.removeUser(user)
273 label: $localize`Ban`,
274 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
275 handler: ({ user }) => this.openBanUserModal(user),
276 isDisplayed: ({ user }) => !user.blocked
279 label: $localize`Unban user`,
280 description: $localize`Allow the user to login and create videos/comments again`,
281 handler: ({ user }) => this.unbanUser(user),
282 isDisplayed: ({ user }) => user.blocked
285 label: $localize`Set Email as Verified`,
286 handler: ({ user }) => this.setEmailAsVerified(user),
287 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
292 // Actions on accounts/servers
295 this.userActions.push([
297 label: $localize`Mute this account`,
298 description: $localize`Hide any content from that user from you.`,
299 isDisplayed: ({ account }) => account.mutedByUser === false,
300 handler: ({ account }) => this.blockAccountByUser(account)
303 label: $localize`Unmute this account`,
304 description: $localize`Show back content from that user for you.`,
305 isDisplayed: ({ account }) => account.mutedByUser === true,
306 handler: ({ account }) => this.unblockAccountByUser(account)
309 label: $localize`Mute the instance`,
310 description: $localize`Hide any content from that instance for you.`,
311 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
312 handler: ({ account }) => this.blockServerByUser(account.host)
315 label: $localize`Unmute the instance`,
316 description: $localize`Show back content from that instance for you.`,
317 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
318 handler: ({ account }) => this.unblockServerByUser(account.host)
321 label: $localize`Remove comments from your videos`,
322 description: $localize`Remove comments made by this account on your videos.`,
323 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
327 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
329 // Instance actions on account blocklists
330 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
331 instanceActions = instanceActions.concat([
333 label: $localize`Mute this account by your instance`,
334 description: $localize`Hide any content from that user from you, your instance and its users.`,
335 isDisplayed: ({ account }) => account.mutedByInstance === false,
336 handler: ({ account }) => this.blockAccountByInstance(account)
339 label: $localize`Unmute this account by your instance`,
340 description: $localize`Show this user's content to the users of this instance again.`,
341 isDisplayed: ({ account }) => account.mutedByInstance === true,
342 handler: ({ account }) => this.unblockAccountByInstance(account)
347 // Instance actions on server blocklists
348 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
349 instanceActions = instanceActions.concat([
351 label: $localize`Mute the instance by your instance`,
352 description: $localize`Hide any content from that instance from you, your instance and its users.`,
353 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
354 handler: ({ account }) => this.blockServerByInstance(account.host)
357 label: $localize`Unmute the instance by your instance`,
358 description: $localize`Show back content from that instance for you, your instance and its users.`,
359 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
360 handler: ({ account }) => this.unblockServerByInstance(account.host)
365 if (authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
366 instanceActions = instanceActions.concat([
368 label: $localize`Remove comments from your instance`,
369 description: $localize`Remove comments made by this account from your instance.`,
370 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
375 if (instanceActions.length !== 0) {
376 this.userActions.push(instanceActions)