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
20 @Input() buttonSize: 'normal' | 'small' = 'normal'
21 @Input() placement = 'left-top left-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,
43 get requiresEmailVerification () {
44 return this.serverConfig.signup.requiresEmailVerification
48 this.serverConfig = this.serverService.getTmpConfig()
49 this.serverService.getConfig()
50 .subscribe(config => this.serverConfig = config)
57 openBanUserModal (user: User) {
58 if (user.username === 'root') {
59 this.notifier.error(this.i18n('You cannot ban root.'))
63 this.userBanModal.openModal(user)
67 this.userChanged.emit()
70 async unbanUser (user: User) {
71 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
72 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
73 if (res === false) return
75 this.userService.unbanUsers(user)
78 this.notifier.success(this.i18n('User {{username}} unbanned.', { username: user.username }))
80 this.userChanged.emit()
83 err => this.notifier.error(err.message)
87 async removeUser (user: User) {
88 if (user.username === 'root') {
89 this.notifier.error(this.i18n('You cannot delete root.'))
93 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
94 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
95 if (res === false) return
97 this.userService.removeUser(user).subscribe(
99 this.notifier.success(this.i18n('User {{username}} deleted.', { username: user.username }))
100 this.userDeleted.emit()
103 err => this.notifier.error(err.message)
107 setEmailAsVerified (user: User) {
108 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
110 this.notifier.success(this.i18n('User {{username}} email set as verified', { username: user.username }))
112 this.userChanged.emit()
115 err => this.notifier.error(err.message)
119 blockAccountByUser (account: Account) {
120 this.blocklistService.blockAccountByUser(account)
123 this.notifier.success(this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost }))
125 this.account.mutedByUser = true
126 this.userChanged.emit()
129 err => this.notifier.error(err.message)
133 unblockAccountByUser (account: Account) {
134 this.blocklistService.unblockAccountByUser(account)
137 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost }))
139 this.account.mutedByUser = false
140 this.userChanged.emit()
143 err => this.notifier.error(err.message)
147 blockServerByUser (host: string) {
148 this.blocklistService.blockServerByUser(host)
151 this.notifier.success(this.i18n('Instance {{host}} muted.', { host }))
153 this.account.mutedServerByUser = true
154 this.userChanged.emit()
157 err => this.notifier.error(err.message)
161 unblockServerByUser (host: string) {
162 this.blocklistService.unblockServerByUser(host)
165 this.notifier.success(this.i18n('Instance {{host}} unmuted.', { host }))
167 this.account.mutedServerByUser = false
168 this.userChanged.emit()
171 err => this.notifier.error(err.message)
175 blockAccountByInstance (account: Account) {
176 this.blocklistService.blockAccountByInstance(account)
179 this.notifier.success(this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost }))
181 this.account.mutedByInstance = true
182 this.userChanged.emit()
185 err => this.notifier.error(err.message)
189 unblockAccountByInstance (account: Account) {
190 this.blocklistService.unblockAccountByInstance(account)
193 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost }))
195 this.account.mutedByInstance = false
196 this.userChanged.emit()
199 err => this.notifier.error(err.message)
203 blockServerByInstance (host: string) {
204 this.blocklistService.blockServerByInstance(host)
207 this.notifier.success(this.i18n('Instance {{host}} muted by the instance.', { host }))
209 this.account.mutedServerByInstance = true
210 this.userChanged.emit()
213 err => this.notifier.error(err.message)
217 unblockServerByInstance (host: string) {
218 this.blocklistService.unblockServerByInstance(host)
221 this.notifier.success(this.i18n('Instance {{host}} unmuted by the instance.', { host }))
223 this.account.mutedServerByInstance = false
224 this.userChanged.emit()
227 err => this.notifier.error(err.message)
231 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
232 const message = this.i18n('Are you sure you want to remove all the comments of this account?')
233 const res = await this.confirmService.confirm(message, this.i18n('Delete account comments'))
234 if (res === false) return
236 this.bulkService.removeCommentsOf(body)
239 this.notifier.success(this.i18n('Will remove comments of this account (may take several minutes).'))
242 err => this.notifier.error(err.message)
246 getRouterUserEditLink (user: User) {
247 return [ '/admin', 'users', 'update', user.id ]
250 private buildActions () {
251 this.userActions = []
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: this.i18n('Edit user'),
262 description: this.i18n('Change quota, role, and more.'),
263 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
266 label: this.i18n('Delete user'),
267 description: this.i18n('Videos will be deleted, comments will be tombstoned.'),
268 handler: ({ user }) => this.removeUser(user)
271 label: this.i18n('Ban'),
272 description: this.i18n('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: this.i18n('Unban user'),
278 description: this.i18n('Allow the user to login and create videos/comments again'),
279 handler: ({ user }) => this.unbanUser(user),
280 isDisplayed: ({ user }) => user.blocked
283 label: this.i18n('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: this.i18n('Mute this account'),
296 description: this.i18n('Hide any content from that user for you.'),
297 isDisplayed: ({ account }) => account.mutedByUser === false,
298 handler: ({ account }) => this.blockAccountByUser(account)
301 label: this.i18n('Unmute this account'),
302 description: this.i18n('Show back content from that user for you.'),
303 isDisplayed: ({ account }) => account.mutedByUser === true,
304 handler: ({ account }) => this.unblockAccountByUser(account)
307 label: this.i18n('Mute the instance'),
308 description: this.i18n('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: this.i18n('Unmute the instance'),
314 description: this.i18n('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: this.i18n('Remove comments from your videos'),
320 description: this.i18n('Remove comments of this account from 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: this.i18n('Mute this account by your instance'),
332 description: this.i18n('Hide any content from that user for you, your instance and its users.'),
333 isDisplayed: ({ account }) => account.mutedByInstance === false,
334 handler: ({ account }) => this.blockAccountByInstance(account)
337 label: this.i18n('Unmute this account by your instance'),
338 description: this.i18n('Show back content from that user for you, your instance and its users.'),
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: this.i18n('Mute the instance by your instance'),
350 description: this.i18n('Hide any content from that instance for you, your instance and its users.'),
351 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
352 handler: ({ account }) => this.blockServerByInstance(account.host)
355 label: this.i18n('Unmute the instance by your instance'),
356 description: this.i18n('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: this.i18n('Remove comments from your instance'),
367 description: this.i18n('Remove comments of 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)