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 { I18n } from '@ngx-translate/i18n-polyfill'
5 import { BulkRemoveCommentsOfBody, ServerConfig, User, UserRight } from '@shared/models'
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() placement = 'left-top left-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 private serverConfig: ServerConfig
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 get requiresEmailVerification () {
45 return this.serverConfig.signup.requiresEmailVerification
49 this.serverConfig = this.serverService.getTmpConfig()
50 this.serverService.getConfig()
51 .subscribe(config => this.serverConfig = config)
58 openBanUserModal (user: User) {
59 if (user.username === 'root') {
60 this.notifier.error(this.i18n('You cannot ban root.'))
64 this.userBanModal.openModal(user)
68 this.userChanged.emit()
71 async unbanUser (user: User) {
72 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
73 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
74 if (res === false) return
76 this.userService.unbanUsers(user)
79 this.notifier.success(this.i18n('User {{username}} unbanned.', { username: user.username }))
81 this.userChanged.emit()
84 err => this.notifier.error(err.message)
88 async removeUser (user: User) {
89 if (user.username === 'root') {
90 this.notifier.error(this.i18n('You cannot delete root.'))
94 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
95 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
96 if (res === false) return
98 this.userService.removeUser(user).subscribe(
100 this.notifier.success(this.i18n('User {{username}} deleted.', { username: user.username }))
101 this.userDeleted.emit()
104 err => this.notifier.error(err.message)
108 setEmailAsVerified (user: User) {
109 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
111 this.notifier.success(this.i18n('User {{username}} email set as verified', { username: user.username }))
113 this.userChanged.emit()
116 err => this.notifier.error(err.message)
120 blockAccountByUser (account: Account) {
121 this.blocklistService.blockAccountByUser(account)
124 this.notifier.success(this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost }))
126 this.account.mutedByUser = true
127 this.userChanged.emit()
130 err => this.notifier.error(err.message)
134 unblockAccountByUser (account: Account) {
135 this.blocklistService.unblockAccountByUser(account)
138 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost }))
140 this.account.mutedByUser = false
141 this.userChanged.emit()
144 err => this.notifier.error(err.message)
148 blockServerByUser (host: string) {
149 this.blocklistService.blockServerByUser(host)
152 this.notifier.success(this.i18n('Instance {{host}} muted.', { host }))
154 this.account.mutedServerByUser = true
155 this.userChanged.emit()
158 err => this.notifier.error(err.message)
162 unblockServerByUser (host: string) {
163 this.blocklistService.unblockServerByUser(host)
166 this.notifier.success(this.i18n('Instance {{host}} unmuted.', { host }))
168 this.account.mutedServerByUser = false
169 this.userChanged.emit()
172 err => this.notifier.error(err.message)
176 blockAccountByInstance (account: Account) {
177 this.blocklistService.blockAccountByInstance(account)
180 this.notifier.success(this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost }))
182 this.account.mutedByInstance = true
183 this.userChanged.emit()
186 err => this.notifier.error(err.message)
190 unblockAccountByInstance (account: Account) {
191 this.blocklistService.unblockAccountByInstance(account)
194 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost }))
196 this.account.mutedByInstance = false
197 this.userChanged.emit()
200 err => this.notifier.error(err.message)
204 blockServerByInstance (host: string) {
205 this.blocklistService.blockServerByInstance(host)
208 this.notifier.success(this.i18n('Instance {{host}} muted by the instance.', { host }))
210 this.account.mutedServerByInstance = true
211 this.userChanged.emit()
214 err => this.notifier.error(err.message)
218 unblockServerByInstance (host: string) {
219 this.blocklistService.unblockServerByInstance(host)
222 this.notifier.success(this.i18n('Instance {{host}} unmuted by the instance.', { host }))
224 this.account.mutedServerByInstance = false
225 this.userChanged.emit()
228 err => this.notifier.error(err.message)
232 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
233 const message = this.i18n('Are you sure you want to remove all the comments of this account?')
234 const res = await this.confirmService.confirm(message, this.i18n('Delete account comments'))
235 if (res === false) return
237 this.bulkService.removeCommentsOf(body)
240 this.notifier.success(this.i18n('Will remove comments of this account (may take several minutes).'))
243 err => this.notifier.error(err.message)
247 getRouterUserEditLink (user: User) {
248 return [ '/admin', 'users', 'update', user.id ]
251 private buildActions () {
252 this.userActions = []
254 if (this.prependActions) {
260 if (this.authService.isLoggedIn()) {
261 const authUser = this.authService.getUser()
263 if (this.user && authUser.id === this.user.id) return
265 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
266 this.userActions.push([
268 label: this.i18n('Edit user'),
269 description: this.i18n('Change quota, role, and more.'),
270 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
273 label: this.i18n('Delete user'),
274 description: this.i18n('Videos will be deleted, comments will be tombstoned.'),
275 handler: ({ user }) => this.removeUser(user)
278 label: this.i18n('Ban'),
279 description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'),
280 handler: ({ user }) => this.openBanUserModal(user),
281 isDisplayed: ({ user }) => !user.blocked
284 label: this.i18n('Unban user'),
285 description: this.i18n('Allow the user to login and create videos/comments again'),
286 handler: ({ user }) => this.unbanUser(user),
287 isDisplayed: ({ user }) => user.blocked
290 label: this.i18n('Set Email as Verified'),
291 handler: ({ user }) => this.setEmailAsVerified(user),
292 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
297 // Actions on accounts/servers
300 this.userActions.push([
302 label: this.i18n('Mute this account'),
303 description: this.i18n('Hide any content from that user for you.'),
304 isDisplayed: ({ account }) => account.mutedByUser === false,
305 handler: ({ account }) => this.blockAccountByUser(account)
308 label: this.i18n('Unmute this account'),
309 description: this.i18n('Show back content from that user for you.'),
310 isDisplayed: ({ account }) => account.mutedByUser === true,
311 handler: ({ account }) => this.unblockAccountByUser(account)
314 label: this.i18n('Mute the instance'),
315 description: this.i18n('Hide any content from that instance for you.'),
316 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
317 handler: ({ account }) => this.blockServerByUser(account.host)
320 label: this.i18n('Unmute the instance'),
321 description: this.i18n('Show back content from that instance for you.'),
322 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
323 handler: ({ account }) => this.unblockServerByUser(account.host)
326 label: this.i18n('Remove comments from your videos'),
327 description: this.i18n('Remove comments of this account from your videos.'),
328 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
332 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
334 // Instance actions on account blocklists
335 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
336 instanceActions = instanceActions.concat([
338 label: this.i18n('Mute this account by your instance'),
339 description: this.i18n('Hide any content from that user for you, your instance and its users.'),
340 isDisplayed: ({ account }) => account.mutedByInstance === false,
341 handler: ({ account }) => this.blockAccountByInstance(account)
344 label: this.i18n('Unmute this account by your instance'),
345 description: this.i18n('Show back content from that user for you, your instance and its users.'),
346 isDisplayed: ({ account }) => account.mutedByInstance === true,
347 handler: ({ account }) => this.unblockAccountByInstance(account)
352 // Instance actions on server blocklists
353 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
354 instanceActions = instanceActions.concat([
356 label: this.i18n('Mute the instance by your instance'),
357 description: this.i18n('Hide any content from that instance for you, your instance and its users.'),
358 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
359 handler: ({ account }) => this.blockServerByInstance(account.host)
362 label: this.i18n('Unmute the instance by your instance'),
363 description: this.i18n('Show back content from that instance for you, your instance and its users.'),
364 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
365 handler: ({ account }) => this.unblockServerByInstance(account.host)
370 if (authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
371 instanceActions = instanceActions.concat([
373 label: this.i18n('Remove comments from your instance'),
374 description: this.i18n('Remove comments of this account from your instance.'),
375 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
380 if (instanceActions.length !== 0) {
381 this.userActions.push(instanceActions)