]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/moderation/user-moderation-dropdown.component.ts
Add ability to mute a user/instance by server in server api
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / moderation / user-moderation-dropdown.component.ts
1 import { Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
5 import { UserBanModalComponent } from '@app/shared/moderation/user-ban-modal.component'
6 import { UserService } from '@app/shared/users'
7 import { AuthService, ConfirmService } from '@app/core'
8 import { User, UserRight } from '../../../../../shared/models/users'
9 import { Account } from '@app/shared/account/account.model'
10 import { BlocklistService } from '@app/shared/blocklist'
11
12 @Component({
13 selector: 'my-user-moderation-dropdown',
14 templateUrl: './user-moderation-dropdown.component.html',
15 styleUrls: [ './user-moderation-dropdown.component.scss' ]
16 })
17 export class UserModerationDropdownComponent implements OnChanges {
18 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
19
20 @Input() user: User
21 @Input() account: Account
22
23 @Input() buttonSize: 'normal' | 'small' = 'normal'
24 @Input() placement = 'left'
25
26 @Output() userChanged = new EventEmitter()
27 @Output() userDeleted = new EventEmitter()
28
29 userActions: DropdownAction<User>[] = []
30
31 constructor (
32 private authService: AuthService,
33 private notificationsService: NotificationsService,
34 private confirmService: ConfirmService,
35 private userService: UserService,
36 private blocklistService: BlocklistService,
37 private i18n: I18n
38 ) { }
39
40 ngOnChanges () {
41 this.buildActions()
42 }
43
44 openBanUserModal (user: User) {
45 if (user.username === 'root') {
46 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
47 return
48 }
49
50 this.userBanModal.openModal(user)
51 }
52
53 onUserBanned () {
54 this.userChanged.emit()
55 }
56
57 async unbanUser (user: User) {
58 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
59 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
60 if (res === false) return
61
62 this.userService.unbanUsers(user)
63 .subscribe(
64 () => {
65 this.notificationsService.success(
66 this.i18n('Success'),
67 this.i18n('User {{username}} unbanned.', { username: user.username })
68 )
69
70 this.userChanged.emit()
71 },
72
73 err => this.notificationsService.error(this.i18n('Error'), err.message)
74 )
75 }
76
77 async removeUser (user: User) {
78 if (user.username === 'root') {
79 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
80 return
81 }
82
83 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
84 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
85 if (res === false) return
86
87 this.userService.removeUser(user).subscribe(
88 () => {
89 this.notificationsService.success(
90 this.i18n('Success'),
91 this.i18n('User {{username}} deleted.', { username: user.username })
92 )
93 this.userDeleted.emit()
94 },
95
96 err => this.notificationsService.error(this.i18n('Error'), err.message)
97 )
98 }
99
100 blockAccountByUser (account: Account) {
101 this.blocklistService.blockAccountByUser(account)
102 .subscribe(
103 () => {
104 this.notificationsService.success(
105 this.i18n('Success'),
106 this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost })
107 )
108
109 this.account.muted = true
110 this.userChanged.emit()
111 },
112
113 err => this.notificationsService.error(this.i18n('Error'), err.message)
114 )
115 }
116
117 unblockAccountByUser (account: Account) {
118 this.blocklistService.unblockAccountByUser(account)
119 .subscribe(
120 () => {
121 this.notificationsService.success(
122 this.i18n('Success'),
123 this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost })
124 )
125
126 this.account.muted = false
127 this.userChanged.emit()
128 },
129
130 err => this.notificationsService.error(this.i18n('Error'), err.message)
131 )
132 }
133
134 blockServerByUser (host: string) {
135 this.blocklistService.blockServerByUser(host)
136 .subscribe(
137 () => {
138 this.notificationsService.success(
139 this.i18n('Success'),
140 this.i18n('Instance {{host}} muted.', { host })
141 )
142
143 this.account.mutedServer = true
144 this.userChanged.emit()
145 },
146
147 err => this.notificationsService.error(this.i18n('Error'), err.message)
148 )
149 }
150
151 unblockServerByUser (host: string) {
152 this.blocklistService.unblockServerByUser(host)
153 .subscribe(
154 () => {
155 this.notificationsService.success(
156 this.i18n('Success'),
157 this.i18n('Instance {{host}} unmuted.', { host })
158 )
159
160 this.account.mutedServer = false
161 this.userChanged.emit()
162 },
163
164 err => this.notificationsService.error(this.i18n('Error'), err.message)
165 )
166 }
167
168 getRouterUserEditLink (user: User) {
169 return [ '/admin', 'users', 'update', user.id ]
170 }
171
172 private buildActions () {
173 this.userActions = []
174
175 if (this.authService.isLoggedIn()) {
176 const authUser = this.authService.getUser()
177
178 if (this.user && authUser.id === this.user.id) return
179
180 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS)) {
181 this.userActions = this.userActions.concat([
182 {
183 label: this.i18n('Edit'),
184 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
185 },
186 {
187 label: this.i18n('Delete'),
188 handler: ({ user }) => this.removeUser(user)
189 },
190 {
191 label: this.i18n('Ban'),
192 handler: ({ user }) => this.openBanUserModal(user),
193 isDisplayed: ({ user }) => !user.muted
194 },
195 {
196 label: this.i18n('Unban'),
197 handler: ({ user }) => this.unbanUser(user),
198 isDisplayed: ({ user }) => user.muted
199 }
200 ])
201 }
202
203 // User actions on accounts/servers
204 if (this.account) {
205 this.userActions = this.userActions.concat([
206 {
207 label: this.i18n('Mute this account'),
208 isDisplayed: ({ account }) => account.muted === false,
209 handler: ({ account }) => this.blockAccountByUser(account)
210 },
211 {
212 label: this.i18n('Unmute this account'),
213 isDisplayed: ({ account }) => account.muted === true,
214 handler: ({ account }) => this.unblockAccountByUser(account)
215 },
216 {
217 label: this.i18n('Mute the instance'),
218 isDisplayed: ({ account }) => !account.userId && account.mutedServer === false,
219 handler: ({ account }) => this.blockServerByUser(account.host)
220 },
221 {
222 label: this.i18n('Unmute the instance'),
223 isDisplayed: ({ account }) => !account.userId && account.mutedServer === true,
224 handler: ({ account }) => this.unblockServerByUser(account.host)
225 }
226 ])
227 }
228 }
229 }
230 }