]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-moderation/user-moderation-dropdown.component.ts
Support ICU in TS components
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / user-moderation-dropdown.component.ts
1 import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core'
2 import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
3 import { Account, DropdownAction } from '@app/shared/shared-main'
4 import { BulkRemoveCommentsOfBody, User, UserRight } from '@shared/models'
5 import { UserAdminService } from '../shared-users'
6 import { BlocklistService } from './blocklist.service'
7 import { BulkService } from './bulk.service'
8 import { UserBanModalComponent } from './user-ban-modal.component'
9
10 export type AccountMutedStatus =
11 Pick<Account, 'id' | 'nameWithHost' | 'host' | 'userId' |
12 'mutedByInstance' | 'mutedByUser' | 'mutedServerByInstance' | 'mutedServerByUser'>
13
14 export type UserModerationDisplayType = {
15 myAccount?: boolean
16 instanceAccount?: boolean
17 instanceUser?: boolean
18 }
19
20 @Component({
21 selector: 'my-user-moderation-dropdown',
22 templateUrl: './user-moderation-dropdown.component.html'
23 })
24 export class UserModerationDropdownComponent implements OnInit, OnChanges {
25 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
26
27 @Input() user: User
28 @Input() account: AccountMutedStatus
29 @Input() prependActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[]
30
31 @Input() buttonSize: 'normal' | 'small' = 'normal'
32 @Input() buttonStyled = true
33 @Input() placement = 'right-top right-bottom auto'
34 @Input() label: string
35 @Input() container: 'body' | undefined = undefined
36
37 @Input() displayOptions: UserModerationDisplayType = {
38 myAccount: true,
39 instanceAccount: true,
40 instanceUser: true
41 }
42
43 @Output() userChanged = new EventEmitter()
44 @Output() userDeleted = new EventEmitter()
45
46 userActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[][] = []
47
48 requiresEmailVerification = false
49
50 constructor (
51 private authService: AuthService,
52 private notifier: Notifier,
53 private confirmService: ConfirmService,
54 private serverService: ServerService,
55 private userAdminService: UserAdminService,
56 private blocklistService: BlocklistService,
57 private bulkService: BulkService
58 ) { }
59
60 ngOnInit () {
61 this.serverService.getConfig()
62 .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
63 }
64
65 ngOnChanges () {
66 this.buildActions()
67 }
68
69 openBanUserModal (user: User) {
70 if (user.username === 'root') {
71 this.notifier.error($localize`You cannot ban root.`)
72 return
73 }
74
75 this.userBanModal.openModal(user)
76 }
77
78 onUserBanned () {
79 this.userChanged.emit()
80 }
81
82 async unbanUser (user: User) {
83 const res = await this.confirmService.confirm($localize`Do you really want to unban ${user.username}?`, $localize`Unban`)
84 if (res === false) return
85
86 this.userAdminService.unbanUsers(user)
87 .subscribe({
88 next: () => {
89 this.notifier.success($localize`User ${user.username} unbanned.`)
90 this.userChanged.emit()
91 },
92
93 error: err => this.notifier.error(err.message)
94 })
95 }
96
97 async removeUser (user: User) {
98 if (user.username === 'root') {
99 this.notifier.error($localize`You cannot delete root.`)
100 return
101 }
102
103 // eslint-disable-next-line max-len
104 const message = $localize`If you remove this user, you won't be able to create another user or channel with <strong>${user.username}</strong> username!`
105 const res = await this.confirmService.confirm(message, $localize`Delete ${user.username}`)
106 if (res === false) return
107
108 this.userAdminService.removeUser(user)
109 .subscribe({
110 next: () => {
111 this.notifier.success($localize`User ${user.username} deleted.`)
112 this.userDeleted.emit()
113 },
114
115 error: err => this.notifier.error(err.message)
116 })
117 }
118
119 setEmailAsVerified (user: User) {
120 this.userAdminService.updateUser(user.id, { emailVerified: true })
121 .subscribe({
122 next: () => {
123 this.notifier.success($localize`User ${user.username} email set as verified`)
124 this.userChanged.emit()
125 },
126
127 error: err => this.notifier.error(err.message)
128 })
129 }
130
131 blockAccountByUser (account: AccountMutedStatus) {
132 this.blocklistService.blockAccountByUser(account)
133 .subscribe({
134 next: () => {
135 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
136
137 this.account.mutedByUser = true
138 this.userChanged.emit()
139 },
140
141 error: err => this.notifier.error(err.message)
142 })
143 }
144
145 unblockAccountByUser (account: AccountMutedStatus) {
146 this.blocklistService.unblockAccountByUser(account)
147 .subscribe({
148 next: () => {
149 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
150
151 this.account.mutedByUser = false
152 this.userChanged.emit()
153 },
154
155 error: err => this.notifier.error(err.message)
156 })
157 }
158
159 blockServerByUser (host: string) {
160 this.blocklistService.blockServerByUser(host)
161 .subscribe({
162 next: () => {
163 this.notifier.success($localize`Instance ${host} muted.`)
164
165 this.account.mutedServerByUser = true
166 this.userChanged.emit()
167 },
168
169 error: err => this.notifier.error(err.message)
170 })
171 }
172
173 unblockServerByUser (host: string) {
174 this.blocklistService.unblockServerByUser(host)
175 .subscribe({
176 next: () => {
177 this.notifier.success($localize`Instance ${host} unmuted.`)
178
179 this.account.mutedServerByUser = false
180 this.userChanged.emit()
181 },
182
183 error: err => this.notifier.error(err.message)
184 })
185 }
186
187 blockAccountByInstance (account: AccountMutedStatus) {
188 this.blocklistService.blockAccountByInstance(account)
189 .subscribe({
190 next: () => {
191 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
192
193 this.account.mutedByInstance = true
194 this.userChanged.emit()
195 },
196
197 error: err => this.notifier.error(err.message)
198 })
199 }
200
201 unblockAccountByInstance (account: AccountMutedStatus) {
202 this.blocklistService.unblockAccountByInstance(account)
203 .subscribe({
204 next: () => {
205 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
206
207 this.account.mutedByInstance = false
208 this.userChanged.emit()
209 },
210
211 error: err => this.notifier.error(err.message)
212 })
213 }
214
215 blockServerByInstance (host: string) {
216 this.blocklistService.blockServerByInstance(host)
217 .subscribe({
218 next: () => {
219 this.notifier.success($localize`Instance ${host} muted by the instance.`)
220
221 this.account.mutedServerByInstance = true
222 this.userChanged.emit()
223 },
224
225 error: err => this.notifier.error(err.message)
226 })
227 }
228
229 unblockServerByInstance (host: string) {
230 this.blocklistService.unblockServerByInstance(host)
231 .subscribe({
232 next: () => {
233 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
234
235 this.account.mutedServerByInstance = false
236 this.userChanged.emit()
237 },
238
239 error: err => this.notifier.error(err.message)
240 })
241 }
242
243 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
244 const message = $localize`Are you sure you want to remove all the comments of this account?`
245 const res = await this.confirmService.confirm(message, $localize`Delete account comments`)
246 if (res === false) return
247
248 this.bulkService.removeCommentsOf(body)
249 .subscribe({
250 next: () => {
251 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
252 },
253
254 error: err => this.notifier.error(err.message)
255 })
256 }
257
258 getRouterUserEditLink (user: User) {
259 return [ '/admin', 'users', 'update', user.id ]
260 }
261
262 private isMyUser (user: User) {
263 return user && this.authService.getUser().id === user.id
264 }
265
266 private isMyAccount (account: AccountMutedStatus) {
267 return account && this.authService.getUser().account.id === account.id
268 }
269
270 private buildActions () {
271 this.userActions = []
272
273 if (this.prependActions && this.prependActions.length !== 0) {
274 this.userActions = [
275 this.prependActions
276 ]
277 }
278
279 const myAccountModerationActions = this.buildMyAccountModerationActions()
280 const instanceModerationActions = this.buildInstanceModerationActions()
281
282 if (myAccountModerationActions.length !== 0) this.userActions.push(myAccountModerationActions)
283 if (instanceModerationActions.length !== 0) this.userActions.push(instanceModerationActions)
284 }
285
286 private buildMyAccountModerationActions () {
287 if (!this.account || !this.displayOptions.myAccount || !this.authService.isLoggedIn()) return []
288
289 const myAccountActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[] = [
290 {
291 label: $localize`My account moderation`,
292 class: [ 'red' ],
293 isHeader: true
294 },
295 {
296 label: $localize`Mute this account`,
297 description: $localize`Hide any content from that user from you.`,
298 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === false,
299 handler: ({ account }) => this.blockAccountByUser(account)
300 },
301 {
302 label: $localize`Unmute this account`,
303 description: $localize`Show back content from that user for you.`,
304 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === true,
305 handler: ({ account }) => this.unblockAccountByUser(account)
306 },
307 {
308 label: $localize`Mute the instance`,
309 description: $localize`Hide any content from that instance for you.`,
310 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === false,
311 handler: ({ account }) => this.blockServerByUser(account.host)
312 },
313 {
314 label: $localize`Unmute the instance`,
315 description: $localize`Show back content from that instance for you.`,
316 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === true,
317 handler: ({ account }) => this.unblockServerByUser(account.host)
318 },
319 {
320 label: $localize`Remove comments from your videos`,
321 description: $localize`Remove comments made by this account on your videos.`,
322 isDisplayed: ({ account }) => !this.isMyAccount(account),
323 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
324 }
325 ]
326
327 return myAccountActions
328 }
329
330 private buildInstanceModerationActions () {
331 if (!this.authService.isLoggedIn()) return []
332
333 const authUser = this.authService.getUser()
334
335 let instanceActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[] = []
336
337 if (this.user && this.displayOptions.instanceUser && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
338 instanceActions = instanceActions.concat([
339 {
340 label: $localize`Edit user`,
341 description: $localize`Change quota, role, and more.`,
342 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
343 },
344 {
345 label: $localize`Delete user`,
346 description: $localize`Videos will be deleted, comments will be tombstoned.`,
347 isDisplayed: ({ user }) => !this.isMyUser(user),
348 handler: ({ user }) => this.removeUser(user)
349 },
350 {
351 label: $localize`Ban`,
352 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
353 handler: ({ user }) => this.openBanUserModal(user),
354 isDisplayed: ({ user }) => !this.isMyUser(user) && !user.blocked
355 },
356 {
357 label: $localize`Unban user`,
358 description: $localize`Allow the user to login and create videos/comments again`,
359 handler: ({ user }) => this.unbanUser(user),
360 isDisplayed: ({ user }) => !this.isMyUser(user) && user.blocked
361 },
362 {
363 label: $localize`Set Email as Verified`,
364 handler: ({ user }) => this.setEmailAsVerified(user),
365 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
366 }
367 ])
368 }
369
370 // Instance actions on account blocklists
371 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
372 instanceActions = instanceActions.concat([
373 {
374 label: $localize`Mute this account`,
375 description: $localize`Hide any content from that user from you, your instance and its users.`,
376 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === false,
377 handler: ({ account }) => this.blockAccountByInstance(account)
378 },
379 {
380 label: $localize`Unmute this account`,
381 description: $localize`Show this user's content to the users of this instance again.`,
382 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === true,
383 handler: ({ account }) => this.unblockAccountByInstance(account)
384 }
385 ])
386 }
387
388 // Instance actions on server blocklists
389 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
390 instanceActions = instanceActions.concat([
391 {
392 label: $localize`Mute the instance`,
393 description: $localize`Hide any content from that instance from you, your instance and its users.`,
394 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
395 handler: ({ account }) => this.blockServerByInstance(account.host)
396 },
397 {
398 label: $localize`Unmute the instance by your instance`,
399 description: $localize`Show back content from that instance for you, your instance and its users.`,
400 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
401 handler: ({ account }) => this.unblockServerByInstance(account.host)
402 }
403 ])
404 }
405
406 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
407 instanceActions = instanceActions.concat([
408 {
409 label: $localize`Remove comments from your instance`,
410 description: $localize`Remove comments made by this account from your instance.`,
411 isDisplayed: ({ account }) => !this.isMyAccount(account),
412 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
413 }
414 ])
415 }
416
417 if (instanceActions.length === 0) return []
418
419 return [ { label: $localize`Instance moderation`, isHeader: true }, ...instanceActions ]
420 }
421 }