From a4101923e699e49ceb9ff36e971c75417fafc9f0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 9 Jan 2019 15:14:29 +0100 Subject: Implement contact form on server side --- server/lib/emailer.ts | 23 +++++++++++++++++++++-- server/lib/job-queue/handlers/email.ts | 3 ++- server/lib/redis.ts | 24 ++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) (limited to 'server/lib') diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index 3429498e7..9b1c5122f 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts @@ -354,13 +354,32 @@ class Emailer { return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) } - sendMail (to: string[], subject: string, text: string) { + addContactFormJob (fromEmail: string, fromName: string, body: string) { + const text = 'Hello dear admin,\n\n' + + fromName + ' sent you a message' + + '\n\n---------------------------------------\n\n' + + body + + '\n\n---------------------------------------\n\n' + + 'Cheers,\n' + + 'PeerTube.' + + const emailPayload: EmailPayload = { + from: fromEmail, + to: [ CONFIG.ADMIN.EMAIL ], + subject: '[PeerTube] Contact form submitted', + text + } + + return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) + } + + sendMail (to: string[], subject: string, text: string, from?: string) { if (!this.enabled) { throw new Error('Cannot send mail because SMTP is not configured.') } return this.transporter.sendMail({ - from: CONFIG.SMTP.FROM_ADDRESS, + from: from || CONFIG.SMTP.FROM_ADDRESS, to: to.join(','), subject, text diff --git a/server/lib/job-queue/handlers/email.ts b/server/lib/job-queue/handlers/email.ts index 73d98ae54..220d0af32 100644 --- a/server/lib/job-queue/handlers/email.ts +++ b/server/lib/job-queue/handlers/email.ts @@ -6,13 +6,14 @@ export type EmailPayload = { to: string[] subject: string text: string + from?: string } async function processEmail (job: Bull.Job) { const payload = job.data as EmailPayload logger.info('Processing email in job %d.', job.id) - return Emailer.Instance.sendMail(payload.to, payload.subject, payload.text) + return Emailer.Instance.sendMail(payload.to, payload.subject, payload.text, payload.from) } // --------------------------------------------------------------------------- diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 3e25e6a2c..3628c0583 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -2,7 +2,13 @@ import * as express from 'express' import { createClient, RedisClient } from 'redis' import { logger } from '../helpers/logger' import { generateRandomString } from '../helpers/utils' -import { CONFIG, USER_PASSWORD_RESET_LIFETIME, USER_EMAIL_VERIFY_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' +import { + CONFIG, + CONTACT_FORM_LIFETIME, + USER_EMAIL_VERIFY_LIFETIME, + USER_PASSWORD_RESET_LIFETIME, + VIDEO_VIEW_LIFETIME +} from '../initializers' type CachedRoute = { body: string, @@ -76,6 +82,16 @@ class Redis { return this.getValue(this.generateVerifyEmailKey(userId)) } + /************* Contact form per IP *************/ + + async setContactFormIp (ip: string) { + return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME) + } + + async isContactFormIpExists (ip: string) { + return this.exists(this.generateContactFormKey(ip)) + } + /************* Views per IP *************/ setIPVideoView (ip: string, videoUUID: string) { @@ -175,7 +191,11 @@ class Redis { } private generateViewKey (ip: string, videoUUID: string) { - return videoUUID + '-' + ip + return `views-${videoUUID}-${ip}` + } + + private generateContactFormKey (ip: string) { + return 'contact-form-' + ip } /************* Redis helpers *************/ -- cgit v1.2.3