]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/moderation/user-moderation-dropdown.component.ts
Merge branch 'release/v1.0.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / moderation / user-moderation-dropdown.component.ts
CommitLineData
e724fa93
C
1import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2import { NotificationsService } from 'angular2-notifications'
3import { I18n } from '@ngx-translate/i18n-polyfill'
4import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
e724fa93 5import { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component'
79bd2632 6import { UserService } from '@app/shared/users'
e724fa93 7import { AuthService, ConfirmService } from '@app/core'
79bd2632 8import { User, UserRight } from '../../../../../shared/models/users'
e724fa93
C
9
10@Component({
11 selector: 'my-user-moderation-dropdown',
12 templateUrl: './user-moderation-dropdown.component.html',
13 styleUrls: [ './user-moderation-dropdown.component.scss' ]
14})
15export class UserModerationDropdownComponent implements OnInit {
16 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
17
18 @Input() user: User
79bd2632 19 @Input() buttonSize: 'normal' | 'small' = 'normal'
24b9417c 20 @Input() placement = 'left'
79bd2632 21
e724fa93 22 @Output() userChanged = new EventEmitter()
79bd2632 23 @Output() userDeleted = new EventEmitter()
e724fa93
C
24
25 userActions: DropdownAction<User>[] = []
26
e724fa93
C
27 constructor (
28 private authService: AuthService,
29 private notificationsService: NotificationsService,
30 private confirmService: ConfirmService,
31 private userService: UserService,
32 private i18n: I18n
33 ) { }
34
35 ngOnInit () {
79bd2632 36 this.buildActions()
e724fa93
C
37 }
38
e724fa93
C
39 openBanUserModal (user: User) {
40 if (user.username === 'root') {
41 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
42 return
43 }
44
45 this.userBanModal.openModal(user)
46 }
47
48 onUserBanned () {
49 this.userChanged.emit()
50 }
51
52 async unbanUser (user: User) {
53 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
54 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
55 if (res === false) return
56
791645e6 57 this.userService.unbanUsers(user)
e724fa93
C
58 .subscribe(
59 () => {
60 this.notificationsService.success(
61 this.i18n('Success'),
62 this.i18n('User {{username}} unbanned.', { username: user.username })
63 )
64
65 this.userChanged.emit()
66 },
67
68 err => this.notificationsService.error(this.i18n('Error'), err.message)
69 )
70 }
71
72 async removeUser (user: User) {
73 if (user.username === 'root') {
74 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
75 return
76 }
77
78 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
79 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
80 if (res === false) return
81
82 this.userService.removeUser(user).subscribe(
83 () => {
84 this.notificationsService.success(
85 this.i18n('Success'),
86 this.i18n('User {{username}} deleted.', { username: user.username })
87 )
79bd2632 88 this.userDeleted.emit()
e724fa93
C
89 },
90
91 err => this.notificationsService.error(this.i18n('Error'), err.message)
92 )
93 }
94
95 getRouterUserEditLink (user: User) {
96 return [ '/admin', 'users', 'update', user.id ]
97 }
79bd2632
C
98
99 private buildActions () {
100 this.userActions = []
101
102 if (this.authService.isLoggedIn()) {
103 const authUser = this.authService.getUser()
104
105 if (authUser.hasRight(UserRight.MANAGE_USERS)) {
106 this.userActions = this.userActions.concat([
107 {
108 label: this.i18n('Edit'),
109 linkBuilder: this.getRouterUserEditLink
110 },
111 {
112 label: this.i18n('Delete'),
113 handler: user => this.removeUser(user)
114 },
115 {
116 label: this.i18n('Ban'),
117 handler: user => this.openBanUserModal(user),
118 isDisplayed: user => !user.blocked
119 },
120 {
121 label: this.i18n('Unban'),
122 handler: user => this.unbanUser(user),
123 isDisplayed: user => user.blocked
124 }
125 ])
126 }
127 }
128 }
e724fa93 129}