]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/moderation/user-moderation-dropdown.component.ts
Remove inferred type
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / moderation / user-moderation-dropdown.component.ts
CommitLineData
af5767ff 1import { Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
e724fa93
C
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'
fc2ec87a 7import { AuthService, ConfirmService, ServerService } from '@app/core'
79bd2632 8import { User, UserRight } from '../../../../../shared/models/users'
af5767ff
C
9import { Account } from '@app/shared/account/account.model'
10import { BlocklistService } from '@app/shared/blocklist'
e724fa93
C
11
12@Component({
13 selector: 'my-user-moderation-dropdown',
14 templateUrl: './user-moderation-dropdown.component.html',
15 styleUrls: [ './user-moderation-dropdown.component.scss' ]
16})
af5767ff 17export class UserModerationDropdownComponent implements OnChanges {
e724fa93
C
18 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
19
20 @Input() user: User
af5767ff
C
21 @Input() account: Account
22
79bd2632 23 @Input() buttonSize: 'normal' | 'small' = 'normal'
24b9417c 24 @Input() placement = 'left'
79bd2632 25
e724fa93 26 @Output() userChanged = new EventEmitter()
79bd2632 27 @Output() userDeleted = new EventEmitter()
e724fa93 28
f97c91f7 29 userActions: DropdownAction<{ user: User, account: Account }>[][] = []
e724fa93 30
e724fa93
C
31 constructor (
32 private authService: AuthService,
33 private notificationsService: NotificationsService,
34 private confirmService: ConfirmService,
fc2ec87a 35 private serverService: ServerService,
e724fa93 36 private userService: UserService,
af5767ff 37 private blocklistService: BlocklistService,
e724fa93
C
38 private i18n: I18n
39 ) { }
40
fc2ec87a
JM
41 get requiresEmailVerification () {
42 return this.serverService.getConfig().signup.requiresEmailVerification
43 }
44
af5767ff 45 ngOnChanges () {
79bd2632 46 this.buildActions()
e724fa93
C
47 }
48
e724fa93
C
49 openBanUserModal (user: User) {
50 if (user.username === 'root') {
51 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.'))
52 return
53 }
54
55 this.userBanModal.openModal(user)
56 }
57
58 onUserBanned () {
59 this.userChanged.emit()
60 }
61
62 async unbanUser (user: User) {
63 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
64 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
65 if (res === false) return
66
791645e6 67 this.userService.unbanUsers(user)
e724fa93
C
68 .subscribe(
69 () => {
70 this.notificationsService.success(
71 this.i18n('Success'),
72 this.i18n('User {{username}} unbanned.', { username: user.username })
73 )
74
75 this.userChanged.emit()
76 },
77
78 err => this.notificationsService.error(this.i18n('Error'), err.message)
79 )
80 }
81
82 async removeUser (user: User) {
83 if (user.username === 'root') {
84 this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.'))
85 return
86 }
87
88 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
89 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
90 if (res === false) return
91
92 this.userService.removeUser(user).subscribe(
93 () => {
94 this.notificationsService.success(
95 this.i18n('Success'),
96 this.i18n('User {{username}} deleted.', { username: user.username })
97 )
79bd2632 98 this.userDeleted.emit()
e724fa93
C
99 },
100
101 err => this.notificationsService.error(this.i18n('Error'), err.message)
102 )
103 }
104
fc2ec87a
JM
105 setEmailAsVerified (user: User) {
106 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
107 () => {
108 this.notificationsService.success(
109 this.i18n('Success'),
110 this.i18n('User {{username}} email set as verified', { username: user.username })
111 )
a99e2d94
C
112
113 this.userChanged.emit()
fc2ec87a
JM
114 },
115
116 err => this.notificationsService.error(this.i18n('Error'), err.message)
117 )
118 }
119
af5767ff
C
120 blockAccountByUser (account: Account) {
121 this.blocklistService.blockAccountByUser(account)
122 .subscribe(
123 () => {
124 this.notificationsService.success(
125 this.i18n('Success'),
126 this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost })
127 )
128
65b21c96 129 this.account.mutedByUser = true
af5767ff
C
130 this.userChanged.emit()
131 },
132
133 err => this.notificationsService.error(this.i18n('Error'), err.message)
134 )
135 }
136
137 unblockAccountByUser (account: Account) {
138 this.blocklistService.unblockAccountByUser(account)
139 .subscribe(
140 () => {
141 this.notificationsService.success(
142 this.i18n('Success'),
143 this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost })
144 )
145
65b21c96 146 this.account.mutedByUser = false
af5767ff
C
147 this.userChanged.emit()
148 },
149
150 err => this.notificationsService.error(this.i18n('Error'), err.message)
151 )
152 }
153
154 blockServerByUser (host: string) {
155 this.blocklistService.blockServerByUser(host)
156 .subscribe(
157 () => {
158 this.notificationsService.success(
159 this.i18n('Success'),
160 this.i18n('Instance {{host}} muted.', { host })
161 )
162
65b21c96 163 this.account.mutedServerByUser = true
af5767ff
C
164 this.userChanged.emit()
165 },
166
167 err => this.notificationsService.error(this.i18n('Error'), err.message)
168 )
169 }
170
171 unblockServerByUser (host: string) {
172 this.blocklistService.unblockServerByUser(host)
173 .subscribe(
174 () => {
175 this.notificationsService.success(
176 this.i18n('Success'),
177 this.i18n('Instance {{host}} unmuted.', { host })
178 )
179
65b21c96
C
180 this.account.mutedServerByUser = false
181 this.userChanged.emit()
182 },
183
184 err => this.notificationsService.error(this.i18n('Error'), err.message)
185 )
186 }
187
188 blockAccountByInstance (account: Account) {
189 this.blocklistService.blockAccountByInstance(account)
190 .subscribe(
191 () => {
192 this.notificationsService.success(
193 this.i18n('Success'),
194 this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })
195 )
196
197 this.account.mutedByInstance = true
198 this.userChanged.emit()
199 },
200
201 err => this.notificationsService.error(this.i18n('Error'), err.message)
202 )
203 }
204
205 unblockAccountByInstance (account: Account) {
206 this.blocklistService.unblockAccountByInstance(account)
207 .subscribe(
208 () => {
209 this.notificationsService.success(
210 this.i18n('Success'),
211 this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost })
212 )
213
214 this.account.mutedByInstance = false
215 this.userChanged.emit()
216 },
217
218 err => this.notificationsService.error(this.i18n('Error'), err.message)
219 )
220 }
221
222 blockServerByInstance (host: string) {
223 this.blocklistService.blockServerByInstance(host)
224 .subscribe(
225 () => {
226 this.notificationsService.success(
227 this.i18n('Success'),
228 this.i18n('Instance {{host}} muted by the instance.', { host })
229 )
230
231 this.account.mutedServerByInstance = true
232 this.userChanged.emit()
233 },
234
235 err => this.notificationsService.error(this.i18n('Error'), err.message)
236 )
237 }
238
239 unblockServerByInstance (host: string) {
240 this.blocklistService.unblockServerByInstance(host)
241 .subscribe(
242 () => {
243 this.notificationsService.success(
244 this.i18n('Success'),
245 this.i18n('Instance {{host}} unmuted by the instance.', { host })
246 )
247
248 this.account.mutedServerByInstance = false
af5767ff
C
249 this.userChanged.emit()
250 },
251
252 err => this.notificationsService.error(this.i18n('Error'), err.message)
253 )
254 }
255
e724fa93
C
256 getRouterUserEditLink (user: User) {
257 return [ '/admin', 'users', 'update', user.id ]
258 }
79bd2632
C
259
260 private buildActions () {
261 this.userActions = []
262
263 if (this.authService.isLoggedIn()) {
264 const authUser = this.authService.getUser()
265
af5767ff
C
266 if (this.user && authUser.id === this.user.id) return
267
268 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS)) {
f97c91f7 269 this.userActions.push([
79bd2632
C
270 {
271 label: this.i18n('Edit'),
af5767ff 272 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
79bd2632
C
273 },
274 {
275 label: this.i18n('Delete'),
af5767ff 276 handler: ({ user }) => this.removeUser(user)
79bd2632
C
277 },
278 {
279 label: this.i18n('Ban'),
4e74e803
C
280 handler: ({ user }) => this.openBanUserModal(user),
281 isDisplayed: ({ user }) => !user.blocked
79bd2632
C
282 },
283 {
284 label: this.i18n('Unban'),
4e74e803
C
285 handler: ({ user }) => this.unbanUser(user),
286 isDisplayed: ({ user }) => user.blocked
fc2ec87a
JM
287 },
288 {
289 label: this.i18n('Set Email as Verified'),
4e74e803
C
290 handler: ({ user }) => this.setEmailAsVerified(user),
291 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
af5767ff
C
292 }
293 ])
294 }
295
65b21c96 296 // Actions on accounts/servers
af5767ff 297 if (this.account) {
65b21c96 298 // User actions
f97c91f7 299 this.userActions.push([
af5767ff
C
300 {
301 label: this.i18n('Mute this account'),
4e74e803
C
302 isDisplayed: ({ account }) => account.mutedByUser === false,
303 handler: ({ account }) => this.blockAccountByUser(account)
af5767ff
C
304 },
305 {
306 label: this.i18n('Unmute this account'),
4e74e803
C
307 isDisplayed: ({ account }) => account.mutedByUser === true,
308 handler: ({ account }) => this.unblockAccountByUser(account)
af5767ff
C
309 },
310 {
311 label: this.i18n('Mute the instance'),
4e74e803
C
312 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
313 handler: ({ account }) => this.blockServerByUser(account.host)
af5767ff
C
314 },
315 {
316 label: this.i18n('Unmute the instance'),
4e74e803
C
317 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
318 handler: ({ account }) => this.unblockServerByUser(account.host)
79bd2632
C
319 }
320 ])
65b21c96 321
f97c91f7
C
322 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
323
65b21c96
C
324 // Instance actions
325 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
f97c91f7 326 instanceActions = instanceActions.concat([
65b21c96
C
327 {
328 label: this.i18n('Mute this account by your instance'),
4e74e803
C
329 isDisplayed: ({ account }) => account.mutedByInstance === false,
330 handler: ({ account }) => this.blockAccountByInstance(account)
65b21c96
C
331 },
332 {
333 label: this.i18n('Unmute this account by your instance'),
4e74e803
C
334 isDisplayed: ({ account }) => account.mutedByInstance === true,
335 handler: ({ account }) => this.unblockAccountByInstance(account)
65b21c96
C
336 }
337 ])
338 }
339
340 // Instance actions
341 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
f97c91f7 342 instanceActions = instanceActions.concat([
65b21c96
C
343 {
344 label: this.i18n('Mute the instance by your instance'),
4e74e803
C
345 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
346 handler: ({ account }) => this.blockServerByInstance(account.host)
65b21c96
C
347 },
348 {
349 label: this.i18n('Unmute the instance by your instance'),
4e74e803
C
350 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
351 handler: ({ account }) => this.unblockServerByInstance(account.host)
65b21c96
C
352 }
353 ])
354 }
f97c91f7
C
355
356 if (instanceActions.length !== 0) {
357 this.userActions.push(instanceActions)
358 }
79bd2632
C
359 }
360 }
361 }
e724fa93 362}