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/helpers/core-utils.ts | 20 ++++++++++++++++++++ server/helpers/custom-validators/servers.ts | 11 +++++++++++ server/helpers/utils.ts | 6 ++---- 3 files changed, 33 insertions(+), 4 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 84e33c0e9..3fb824e36 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -11,6 +11,25 @@ import * as pem from 'pem' import { URL } from 'url' import { truncate } from 'lodash' import { exec } from 'child_process' +import { isArray } from './custom-validators/misc' + +const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { + if (!oldObject || typeof oldObject !== 'object') { + return valueConverter(oldObject) + } + + if (isArray(oldObject)) { + return oldObject.map(e => objectConverter(e, keyConverter, valueConverter)) + } + + const newObject = {} + Object.keys(oldObject).forEach(oldKey => { + const newKey = keyConverter(oldKey) + newObject[ newKey ] = objectConverter(oldObject[ oldKey ], keyConverter, valueConverter) + }) + + return newObject +} const timeTable = { ms: 1, @@ -235,6 +254,7 @@ export { isTestInstance, isProdInstance, + objectConverter, root, escapeHTML, pageToStartAndCount, diff --git a/server/helpers/custom-validators/servers.ts b/server/helpers/custom-validators/servers.ts index d5021bf38..18c80ec8f 100644 --- a/server/helpers/custom-validators/servers.ts +++ b/server/helpers/custom-validators/servers.ts @@ -3,6 +3,7 @@ import 'express-validator' import { isArray, exists } from './misc' import { isTestInstance } from '../core-utils' +import { CONSTRAINTS_FIELDS } from '../../initializers' function isHostValid (host: string) { const isURLOptions = { @@ -26,9 +27,19 @@ function isEachUniqueHostValid (hosts: string[]) { }) } +function isValidContactBody (value: any) { + return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.BODY) +} + +function isValidContactFromName (value: any) { + return exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.CONTACT_FORM.FROM_NAME) +} + // --------------------------------------------------------------------------- export { + isValidContactBody, + isValidContactFromName, isEachUniqueHostValid, isHostValid } diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index 9b89e3e61..3c3406e38 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts @@ -7,6 +7,7 @@ import { join } from 'path' import { Instance as ParseTorrent } from 'parse-torrent' import { remove } from 'fs-extra' import * as memoizee from 'memoizee' +import { isArray } from './custom-validators/misc' function deleteFileAsync (path: string) { remove(path) @@ -19,10 +20,7 @@ async function generateRandomString (size: number) { return raw.toString('hex') } -interface FormattableToJSON { - toFormattedJSON (args?: any) -} - +interface FormattableToJSON { toFormattedJSON (args?: any) } function getFormattedObjects (objects: T[], objectsTotal: number, formattedArg?: any) { const formattedObjects: U[] = [] -- cgit v1.2.3