]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/shared/shared-moderation/user-moderation-dropdown.component.ts
Fix abuse tests
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / user-moderation-dropdown.component.ts
... / ...
CommitLineData
1import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core'
2import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
3import { Account, DropdownAction } from '@app/shared/shared-main'
4import { BulkRemoveCommentsOfBody, User, UserRight } from '@shared/models'
5import { UserAdminService } from '../shared-users'
6import { BlocklistService } from './blocklist.service'
7import { BulkService } from './bulk.service'
8import { UserBanModalComponent } from './user-ban-modal.component'
9
10export type AccountMutedStatus =
11 Pick<Account, 'id' | 'nameWithHost' | 'host' | 'userId' |
12 'mutedByInstance' | 'mutedByUser' | 'mutedServerByInstance' | 'mutedServerByUser'>
13
14export 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})
24export 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 const message = $localize`If you remove user ${user.username}, you won't be able to create another with the same username!`
104 const res = await this.confirmService.confirm(message, $localize`Delete ${user.username}`)
105 if (res === false) return
106
107 this.userAdminService.removeUser(user)
108 .subscribe({
109 next: () => {
110 this.notifier.success($localize`User ${user.username} deleted.`)
111 this.userDeleted.emit()
112 },
113
114 error: err => this.notifier.error(err.message)
115 })
116 }
117
118 setEmailAsVerified (user: User) {
119 this.userAdminService.updateUser(user.id, { emailVerified: true })
120 .subscribe({
121 next: () => {
122 this.notifier.success($localize`User ${user.username} email set as verified`)
123 this.userChanged.emit()
124 },
125
126 error: err => this.notifier.error(err.message)
127 })
128 }
129
130 blockAccountByUser (account: AccountMutedStatus) {
131 this.blocklistService.blockAccountByUser(account)
132 .subscribe({
133 next: () => {
134 this.notifier.success($localize`Account ${account.nameWithHost} muted.`)
135
136 this.account.mutedByUser = true
137 this.userChanged.emit()
138 },
139
140 error: err => this.notifier.error(err.message)
141 })
142 }
143
144 unblockAccountByUser (account: AccountMutedStatus) {
145 this.blocklistService.unblockAccountByUser(account)
146 .subscribe({
147 next: () => {
148 this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`)
149
150 this.account.mutedByUser = false
151 this.userChanged.emit()
152 },
153
154 error: err => this.notifier.error(err.message)
155 })
156 }
157
158 blockServerByUser (host: string) {
159 this.blocklistService.blockServerByUser(host)
160 .subscribe({
161 next: () => {
162 this.notifier.success($localize`Instance ${host} muted.`)
163
164 this.account.mutedServerByUser = true
165 this.userChanged.emit()
166 },
167
168 error: err => this.notifier.error(err.message)
169 })
170 }
171
172 unblockServerByUser (host: string) {
173 this.blocklistService.unblockServerByUser(host)
174 .subscribe({
175 next: () => {
176 this.notifier.success($localize`Instance ${host} unmuted.`)
177
178 this.account.mutedServerByUser = false
179 this.userChanged.emit()
180 },
181
182 error: err => this.notifier.error(err.message)
183 })
184 }
185
186 blockAccountByInstance (account: AccountMutedStatus) {
187 this.blocklistService.blockAccountByInstance(account)
188 .subscribe({
189 next: () => {
190 this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`)
191
192 this.account.mutedByInstance = true
193 this.userChanged.emit()
194 },
195
196 error: err => this.notifier.error(err.message)
197 })
198 }
199
200 unblockAccountByInstance (account: AccountMutedStatus) {
201 this.blocklistService.unblockAccountByInstance(account)
202 .subscribe({
203 next: () => {
204 this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`)
205
206 this.account.mutedByInstance = false
207 this.userChanged.emit()
208 },
209
210 error: err => this.notifier.error(err.message)
211 })
212 }
213
214 blockServerByInstance (host: string) {
215 this.blocklistService.blockServerByInstance(host)
216 .subscribe({
217 next: () => {
218 this.notifier.success($localize`Instance ${host} muted by the instance.`)
219
220 this.account.mutedServerByInstance = true
221 this.userChanged.emit()
222 },
223
224 error: err => this.notifier.error(err.message)
225 })
226 }
227
228 unblockServerByInstance (host: string) {
229 this.blocklistService.unblockServerByInstance(host)
230 .subscribe({
231 next: () => {
232 this.notifier.success($localize`Instance ${host} unmuted by the instance.`)
233
234 this.account.mutedServerByInstance = false
235 this.userChanged.emit()
236 },
237
238 error: err => this.notifier.error(err.message)
239 })
240 }
241
242 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
243 const message = $localize`Are you sure you want to remove all the comments of this account?`
244 const res = await this.confirmService.confirm(message, $localize`Delete account comments`)
245 if (res === false) return
246
247 this.bulkService.removeCommentsOf(body)
248 .subscribe({
249 next: () => {
250 this.notifier.success($localize`Will remove comments of this account (may take several minutes).`)
251 },
252
253 error: err => this.notifier.error(err.message)
254 })
255 }
256
257 getRouterUserEditLink (user: User) {
258 return [ '/admin', 'users', 'update', user.id ]
259 }
260
261 private isMyUser (user: User) {
262 return user && this.authService.getUser().id === user.id
263 }
264
265 private isMyAccount (account: AccountMutedStatus) {
266 return account && this.authService.getUser().account.id === account.id
267 }
268
269 private buildActions () {
270 this.userActions = []
271
272 if (this.prependActions && this.prependActions.length !== 0) {
273 this.userActions = [
274 this.prependActions
275 ]
276 }
277
278 const myAccountModerationActions = this.buildMyAccountModerationActions()
279 const instanceModerationActions = this.buildInstanceModerationActions()
280
281 if (myAccountModerationActions.length !== 0) this.userActions.push(myAccountModerationActions)
282 if (instanceModerationActions.length !== 0) this.userActions.push(instanceModerationActions)
283 }
284
285 private buildMyAccountModerationActions () {
286 if (!this.account || !this.displayOptions.myAccount || !this.authService.isLoggedIn()) return []
287
288 const myAccountActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[] = [
289 {
290 label: $localize`My account moderation`,
291 class: [ 'red' ],
292 isHeader: true
293 },
294 {
295 label: $localize`Mute this account`,
296 description: $localize`Hide any content from that user from you.`,
297 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === false,
298 handler: ({ account }) => this.blockAccountByUser(account)
299 },
300 {
301 label: $localize`Unmute this account`,
302 description: $localize`Show back content from that user for you.`,
303 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === true,
304 handler: ({ account }) => this.unblockAccountByUser(account)
305 },
306 {
307 label: $localize`Mute the instance`,
308 description: $localize`Hide any content from that instance for you.`,
309 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === false,
310 handler: ({ account }) => this.blockServerByUser(account.host)
311 },
312 {
313 label: $localize`Unmute the instance`,
314 description: $localize`Show back content from that instance for you.`,
315 isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === true,
316 handler: ({ account }) => this.unblockServerByUser(account.host)
317 },
318 {
319 label: $localize`Remove comments from your videos`,
320 description: $localize`Remove comments made by this account on your videos.`,
321 isDisplayed: ({ account }) => !this.isMyAccount(account),
322 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
323 }
324 ]
325
326 return myAccountActions
327 }
328
329 private buildInstanceModerationActions () {
330 if (!this.authService.isLoggedIn()) return []
331
332 const authUser = this.authService.getUser()
333
334 let instanceActions: DropdownAction<{ user: User, account: AccountMutedStatus }>[] = []
335
336 if (this.user && this.displayOptions.instanceUser && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
337 instanceActions = instanceActions.concat([
338 {
339 label: $localize`Edit user`,
340 description: $localize`Change quota, role, and more.`,
341 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
342 },
343 {
344 label: $localize`Delete user`,
345 description: $localize`Videos will be deleted, comments will be tombstoned.`,
346 isDisplayed: ({ user }) => !this.isMyUser(user),
347 handler: ({ user }) => this.removeUser(user)
348 },
349 {
350 label: $localize`Ban`,
351 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
352 handler: ({ user }) => this.openBanUserModal(user),
353 isDisplayed: ({ user }) => !this.isMyUser(user) && !user.blocked
354 },
355 {
356 label: $localize`Unban user`,
357 description: $localize`Allow the user to login and create videos/comments again`,
358 handler: ({ user }) => this.unbanUser(user),
359 isDisplayed: ({ user }) => !this.isMyUser(user) && user.blocked
360 },
361 {
362 label: $localize`Set Email as Verified`,
363 handler: ({ user }) => this.setEmailAsVerified(user),
364 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
365 }
366 ])
367 }
368
369 // Instance actions on account blocklists
370 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
371 instanceActions = instanceActions.concat([
372 {
373 label: $localize`Mute this account`,
374 description: $localize`Hide any content from that user from you, your instance and its users.`,
375 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === false,
376 handler: ({ account }) => this.blockAccountByInstance(account)
377 },
378 {
379 label: $localize`Unmute this account`,
380 description: $localize`Show this user's content to the users of this instance again.`,
381 isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === true,
382 handler: ({ account }) => this.unblockAccountByInstance(account)
383 }
384 ])
385 }
386
387 // Instance actions on server blocklists
388 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
389 instanceActions = instanceActions.concat([
390 {
391 label: $localize`Mute the instance`,
392 description: $localize`Hide any content from that instance from you, your instance and its users.`,
393 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
394 handler: ({ account }) => this.blockServerByInstance(account.host)
395 },
396 {
397 label: $localize`Unmute the instance by your instance`,
398 description: $localize`Show back content from that instance for you, your instance and its users.`,
399 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
400 handler: ({ account }) => this.unblockServerByInstance(account.host)
401 }
402 ])
403 }
404
405 if (this.account && this.displayOptions.instanceAccount && authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
406 instanceActions = instanceActions.concat([
407 {
408 label: $localize`Remove comments from your instance`,
409 description: $localize`Remove comments made by this account from your instance.`,
410 isDisplayed: ({ account }) => !this.isMyAccount(account),
411 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
412 }
413 ])
414 }
415
416 if (instanceActions.length === 0) return []
417
418 return [ { label: $localize`Instance moderation`, isHeader: true }, ...instanceActions ]
419 }
420}