]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Feature/subject in contact form (#1926)
authorNassim Bounouas <NassimBounouas@users.noreply.github.com>
Fri, 21 Jun 2019 06:49:35 +0000 (08:49 +0200)
committerChocobozzz <me@florianbigard.com>
Fri, 21 Jun 2019 06:49:35 +0000 (08:49 +0200)
* #1915 Add a subject to contact form and add it to email sent

* #1915 Add subject to contact form tests

* #1915 Contact form test corrected && tslint correction

client/src/app/+about/about-instance/contact-admin-modal.component.html
client/src/app/+about/about-instance/contact-admin-modal.component.ts
client/src/app/shared/forms/form-validators/instance-validators.service.ts
client/src/app/shared/instance/instance.service.ts
server/controllers/api/server/contact.ts
server/lib/emailer.ts
server/tests/api/check-params/contact-form.ts
server/tests/api/server/contact-form.ts
shared/extra-utils/server/contact-form.ts
shared/models/server/contact-form.model.ts

index b2cbd087383be90f588eb134fe5b5759bba3644f..c3c71bdeeba17796bd9e05c542b9175486d3a7c9 100644 (file)
         <div *ngIf="formErrors.fromEmail" class="form-error">{{ formErrors.fromEmail }}</div>
       </div>
 
+      <div class="form-group">
+        <label i18n for="subject">Subject</label>
+        <input
+          type="text" id="subject"
+          formControlName="subject" [ngClass]="{ 'input-error': formErrors['subject'] }"
+        >
+        <div *ngIf="formErrors.subject" class="form-error">{{ formErrors.subject }}</div>
+      </div>
+
       <div class="form-group">
         <label i18n for="body">Your message</label>
         <textarea id="body" formControlName="body" [ngClass]="{ 'input-error': formErrors['body'] }">
index 7d79c2215eb3197dcef5a8b335c5a85741879cbe..c609c98c3a754a1423940ab1114791d1ae5342d2 100644 (file)
@@ -39,6 +39,7 @@ export class ContactAdminModalComponent extends FormReactive implements OnInit {
     this.buildForm({
       fromName: this.instanceValidatorsService.FROM_NAME,
       fromEmail: this.instanceValidatorsService.FROM_EMAIL,
+      subject: this.instanceValidatorsService.SUBJECT,
       body: this.instanceValidatorsService.BODY
     })
   }
@@ -58,9 +59,10 @@ export class ContactAdminModalComponent extends FormReactive implements OnInit {
   sendForm () {
     const fromName = this.form.value['fromName']
     const fromEmail = this.form.value[ 'fromEmail' ]
+    const subject = this.form.value[ 'subject' ]
     const body = this.form.value[ 'body' ]
 
-    this.instanceService.contactAdministrator(fromEmail, fromName, body)
+    this.instanceService.contactAdministrator(fromEmail, fromName, subject, body)
         .subscribe(
           () => {
             this.notifier.success(this.i18n('Your message has been sent.'))
index 5bb852858f930d7a1459ce931fcd30cc31c16d93..cc5f3c5a12e1ac1411735f485a4eb461bb55dae9 100644 (file)
@@ -7,6 +7,7 @@ import { Injectable } from '@angular/core'
 export class InstanceValidatorsService {
   readonly FROM_EMAIL: BuildFormValidator
   readonly FROM_NAME: BuildFormValidator
+  readonly SUBJECT: BuildFormValidator
   readonly BODY: BuildFormValidator
 
   constructor (private i18n: I18n) {
@@ -32,6 +33,19 @@ export class InstanceValidatorsService {
       }
     }
 
+    this.SUBJECT = {
+      VALIDATORS: [
+        Validators.required,
+        Validators.minLength(1),
+        Validators.maxLength(120)
+      ],
+      MESSAGES: {
+        'required': this.i18n('A subject is required.'),
+        'minlength': this.i18n('The subject must be at least 1 character long.'),
+        'maxlength': this.i18n('The subject cannot be more than 120 characters long.')
+      }
+    }
+
     this.BODY = {
       VALIDATORS: [
         Validators.required,
index 61321ecce496c1f3efa92fd6ddec79df472e2362..d0c96941d168a2dbf85cfb6759d708889fbf36a2 100644 (file)
@@ -22,10 +22,11 @@ export class InstanceService {
                .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
-  contactAdministrator (fromEmail: string, fromName: string, message: string) {
+  contactAdministrator (fromEmail: string, fromName: string, subject: string, message: string) {
     const body = {
       fromEmail,
       fromName,
+      subject,
       body: message
     }
 
index b1144c94e34b863749df9a57c23a305cefd243b3..de02ffc0ba31181908c843ec55bd0073093eba6b 100644 (file)
@@ -14,7 +14,7 @@ contactRouter.post('/contact',
 async function contactAdministrator (req: express.Request, res: express.Response) {
   const data = req.body as ContactForm
 
-  await Emailer.Instance.addContactFormJob(data.fromEmail, data.fromName, data.body)
+  await Emailer.Instance.addContactFormJob(data.fromEmail, data.fromName, data.subject, data.body)
 
   await Redis.Instance.setContactFormIp(req.ip)
 
index c4a5a5853f2a0cea4153a4641d55f2675e959deb..73c2bcb1b520bb0cf022b6100ad96c0fd1c037d4 100644 (file)
@@ -402,7 +402,7 @@ class Emailer {
     return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
   }
 
-  addContactFormJob (fromEmail: string, fromName: string, body: string) {
+  addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) {
     const text = 'Hello dear admin,\n\n' +
       fromName + ' sent you a message' +
       '\n\n---------------------------------------\n\n' +
@@ -415,7 +415,7 @@ class Emailer {
       fromDisplayName: fromEmail,
       replyTo: fromEmail,
       to: [ CONFIG.ADMIN.EMAIL ],
-      subject: CONFIG.EMAIL.OBJECT.PREFIX + 'Contact form submitted',
+      subject: CONFIG.EMAIL.OBJECT.PREFIX + subject,
       text
     }
 
index dbdd3a8a6830731e456ec80f4b43d69f6c822862..b3051945edc67ecfb96894325d207fd11d00d527 100644 (file)
@@ -26,6 +26,7 @@ describe('Test contact form API validators', function () {
   const defaultBody = {
     fromName: 'super name',
     fromEmail: 'toto@example.com',
+    subject: 'my subject',
     body: 'Hello, how are you?'
   }
   let emailPort: number
index 87e55060ca050da199331a44650d4e05b3926d31..e4e895acb3f6b050f8d449bc005a02ff0570fae5 100644 (file)
@@ -43,6 +43,7 @@ describe('Test contact form', function () {
       url: server.url,
       fromEmail: 'toto@example.com',
       body: 'my super message',
+      subject: 'my subject',
       fromName: 'Super toto'
     })
 
@@ -55,7 +56,7 @@ describe('Test contact form', function () {
     expect(email['from'][0]['address']).equal('test-admin@localhost')
     expect(email['from'][0]['name']).equal('toto@example.com')
     expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
-    expect(email['subject']).contains('Contact form')
+    expect(email['subject']).contains('my subject')
     expect(email['text']).contains('my super message')
   })
 
@@ -64,6 +65,7 @@ describe('Test contact form', function () {
       url: server.url,
       fromEmail: 'toto@example.com',
       body: 'my super message',
+      subject: 'my subject',
       fromName: 'Super toto'
     })
 
@@ -72,6 +74,7 @@ describe('Test contact form', function () {
       fromEmail: 'toto@example.com',
       body: 'my super message',
       fromName: 'Super toto',
+      subject: 'my subject',
       expectedStatus: 403
     })
   })
@@ -82,8 +85,9 @@ describe('Test contact form', function () {
     await sendContactForm({
       url: server.url,
       fromEmail: 'toto@example.com',
-      body: 'my super message',
-      fromName: 'Super toto'
+      fromName: 'Super toto',
+      subject: 'my subject',
+      body: 'my super message'
     })
   })
 
index 80394cf994849792ed10780f3c1f62d3f495e19a..e002e03dd76a9eab35d456d01fe56929cfd3a5e8 100644 (file)
@@ -5,6 +5,7 @@ function sendContactForm (options: {
   url: string,
   fromEmail: string,
   fromName: string,
+  subject: string,
   body: string,
   expectedStatus?: number
 }) {
@@ -13,6 +14,7 @@ function sendContactForm (options: {
   const body: ContactForm = {
     fromEmail: options.fromEmail,
     fromName: options.fromName,
+    subject: options.subject,
     body: options.body
   }
   return request(options.url)
index 0696be8b4f58365b15d850288680f1e3fd7e2023..c23e6d1bac3188fbbc45ab2ab3e469e62dbd7438 100644 (file)
@@ -1,5 +1,6 @@
 export interface ContactForm {
   fromEmail: string
   fromName: string
+  subject: string
   body: string
 }