]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/moderation/registration-list/registration-list.component.ts
Support bulk registration request removal
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / moderation / registration-list / registration-list.component.ts
1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { ConfirmService, MarkdownService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
5 import { prepareIcu } from '@app/helpers'
6 import { AdvancedInputFilter } from '@app/shared/shared-forms'
7 import { DropdownAction } from '@app/shared/shared-main'
8 import { UserRegistration, UserRegistrationState } from '@shared/models'
9 import { AdminRegistrationService } from './admin-registration.service'
10 import { ProcessRegistrationModalComponent } from './process-registration-modal.component'
11
12 @Component({
13 selector: 'my-registration-list',
14 templateUrl: './registration-list.component.html',
15 styleUrls: [ '../../../shared/shared-moderation/moderation.scss', './registration-list.component.scss' ]
16 })
17 export class RegistrationListComponent extends RestTable <UserRegistration> implements OnInit {
18 @ViewChild('processRegistrationModal', { static: true }) processRegistrationModal: ProcessRegistrationModalComponent
19
20 registrations: (UserRegistration & { registrationReasonHTML?: string, moderationResponseHTML?: string })[] = []
21 totalRecords = 0
22 sort: SortMeta = { field: 'createdAt', order: -1 }
23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
24
25 registrationActions: DropdownAction<UserRegistration>[][] = []
26 bulkActions: DropdownAction<UserRegistration[]>[] = []
27
28 inputFilters: AdvancedInputFilter[] = []
29
30 requiresEmailVerification: boolean
31
32 constructor (
33 protected route: ActivatedRoute,
34 protected router: Router,
35 private server: ServerService,
36 private notifier: Notifier,
37 private markdownRenderer: MarkdownService,
38 private confirmService: ConfirmService,
39 private adminRegistrationService: AdminRegistrationService
40 ) {
41 super()
42
43 this.registrationActions = [
44 [
45 {
46 label: $localize`Accept this request`,
47 handler: registration => this.openRegistrationRequestProcessModal(registration, 'accept'),
48 isDisplayed: registration => registration.state.id === UserRegistrationState.PENDING
49 },
50 {
51 label: $localize`Reject this request`,
52 handler: registration => this.openRegistrationRequestProcessModal(registration, 'reject'),
53 isDisplayed: registration => registration.state.id === UserRegistrationState.PENDING
54 },
55 {
56 label: $localize`Remove this request`,
57 handler: registration => this.removeRegistrations([ registration ])
58 }
59 ]
60 ]
61
62 this.bulkActions = [
63 {
64 label: $localize`Delete`,
65 handler: registrations => this.removeRegistrations(registrations)
66 }
67 ]
68 }
69
70 ngOnInit () {
71 this.initialize()
72
73 this.server.getConfig()
74 .subscribe(config => {
75 this.requiresEmailVerification = config.signup.requiresEmailVerification
76 })
77 }
78
79 getIdentifier () {
80 return 'RegistrationListComponent'
81 }
82
83 isRegistrationAccepted (registration: UserRegistration) {
84 return registration.state.id === UserRegistrationState.ACCEPTED
85 }
86
87 isRegistrationRejected (registration: UserRegistration) {
88 return registration.state.id === UserRegistrationState.REJECTED
89 }
90
91 onRegistrationProcessed () {
92 this.reloadData()
93 }
94
95 protected reloadData () {
96 this.adminRegistrationService.listRegistrations({
97 pagination: this.pagination,
98 sort: this.sort,
99 search: this.search
100 }).subscribe({
101 next: async resultList => {
102 this.totalRecords = resultList.total
103 this.registrations = resultList.data
104
105 for (const registration of this.registrations) {
106 registration.registrationReasonHTML = await this.toHtml(registration.registrationReason)
107 registration.moderationResponseHTML = await this.toHtml(registration.moderationResponse)
108 }
109 },
110
111 error: err => this.notifier.error(err.message)
112 })
113 }
114
115 private openRegistrationRequestProcessModal (registration: UserRegistration, mode: 'accept' | 'reject') {
116 this.processRegistrationModal.openModal(registration, mode)
117 }
118
119 private async removeRegistrations (registrations: UserRegistration[]) {
120 const icuParams = { count: registrations.length, username: registrations[0].username }
121
122 // eslint-disable-next-line max-len
123 const message = prepareIcu($localize`Do you really want to delete {count, plural, =1 {{username} registration request?} other {{count} registration requests?}}`)(
124 icuParams,
125 $localize`Do you really want to delete these registration requests?`
126 )
127
128 const res = await this.confirmService.confirm(message, $localize`Delete`)
129 if (res === false) return
130
131 this.adminRegistrationService.removeRegistrations(registrations)
132 .subscribe({
133 next: () => {
134 // eslint-disable-next-line max-len
135 const message = prepareIcu($localize`Removed {count, plural, =1 {{username} registration request} other {{count} registration requests}}`)(
136 icuParams,
137 $localize`Registration requests removed`
138 )
139
140 this.notifier.success(message)
141 this.reloadData()
142 },
143
144 error: err => this.notifier.error(err.message)
145 })
146 }
147
148 private toHtml (text: string) {
149 return this.markdownRenderer.textMarkdownToHTML({ markdown: text })
150 }
151 }