Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature added: Ability to force email and name entry #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .env.SAMPLE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ STAGE=prod
# A Slack webhook to send notifications to (optional)
#SLACK=https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ

# Require author email address - string is error message to display.
#REQEMAIL='Email is required.'

# Require author name - string is error message to display.
#REQNAME='Name is required.'

# Size limit of post in bytes
#SIZELIMIT=4096

# Disallow empty content (spaces only, etc.) - string is error message to display.
#DISALLOW_EMPTY='You need to actually use words.'

7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ STAGE=prod

# A Slack webhook to send notifications to (optional)
#SLACK=https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ
```

# Require author email address - string is error message to display.
#REQEMAIL='Email is required.'

# Require author name - string is error message to display.
#REQNAME='Name is required.'

We use [dotenv](https://github.com/motdotla/dotenv) so it is also possible to
configure the project by setting environment variables.
Expand Down
16 changes: 16 additions & 0 deletions deploy/apex/webpack.config.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ if (process.env.SLACK) {
defines['process.env.SLACK'] = `'${process.env.SLACK}'`
}

if (process.env.REQEMAIL) {
defines['process.env.REQEMAIL'] = `'${process.env.REQEMAIL}'`
}

if (process.env.REQNAME) {
defines['process.env.REQNAME'] = `'${process.env.REQNAME}'`
}

if (process.env.SIZELIMIT) {
defines['process.env.SIZELIMIT'] = `'${process.env.SIZELIMIT}'`
}

if (process.env.DISALLOW_EMPTY) {
defines['process.env.DISALLOW_EMPTY'] = `'${process.env.DISALLOW_EMPTY}'`
}

export default {
entry: {
[lambdaDirNames['QueueComment']]: [
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"react-measure": "0.3.5",
"react-motion": "0.4.2",
"react-project": "0.0.30",
"react-redux": "4.4.5",
"react-router": "2.0.0",
"react-router-redux": "4.0.2",
"react-spinner": "0.2.6",
Expand Down
4 changes: 4 additions & 0 deletions packages/frontend/src/actions/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export const FORM_FIELDS = [
const websiteUrl = __CONFIG__.websiteUrl
const apiUrl = __CONFIG__.apiUrl
const apiKey = __CONFIG__.apiKey
const authorNameRequired = __CONFIG__.authorNameRequired
const authorEmailRequired = __CONFIG__.authorEmailRequired
const contentSizeLimit = __CONFIG__.contentSizeLimit
const disallowEmptyContent = __CONFIG__.disallowEmptyContent

class ValidationError extends Error {
constructor (data) {
Expand Down
7 changes: 7 additions & 0 deletions packages/frontend/src/ui/comments.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@
}
}

.commentCounter {
font-size: 13px;
color: gray;
text-decoration: none;
padding-left: 10px;
}

.previewWrapper {
overflow-y: hidden;
}
Expand Down
28 changes: 25 additions & 3 deletions packages/frontend/src/ui/postCommentForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
errorMessage,
postCommentFormHeader,
markdownNote,
commentCounter,
previewWrapper,
preview,
spinnerButton,
Expand All @@ -27,15 +28,34 @@ import {
} from './comments.css'
import { FORM_NAME, FORM_FIELDS } from '../actions/comments'

const authorNameRequired = __CONFIG__.authorNameRequired
const authorEmailRequired = __CONFIG__.authorEmailRequired
const contentSizeLimit = __CONFIG__.contentSizeLimit
const disallowEmptyContent = __CONFIG__.disallowEmptyContent

let contentRegex = /\w+/

function validate (values) {
const errors = {}
const { commentContent, authorEmail, authorUrl } = values
const { commentContent, authorName, authorEmail, authorUrl } = values
if (!commentContent) {
errors.commentContent = 'Required'
}
if (commentContent && commentContent.length < 3) {
errors.commentContent = 'Must be at least 3 characters'
}
if (commentContent && contentSizeLimit && (commentContent.length > parseInt(contentSizeLimit))) {
errors.commentContent = 'Comment has exceeded content length of: ' + parseInt(contentSizeLimit) + ' characters.'
}
if (commentContent && disallowEmptyContent && !commentContent.match(contentRegex)) {
errors.commentContent = disallowEmptyContent
}
if (authorNameRequired && !authorName) {
errors.authorName = authorNameRequired
}
if (authorEmailRequired && !authorEmail) {
errors.authorEmail = authorEmailRequired
}
if (authorEmail && !isEmail(authorEmail)) {
errors.authorEmail = 'Email format not valid'
}
Expand Down Expand Up @@ -138,13 +158,15 @@ export default class PostCommentForm extends Component {
date: new Date(),
commentContent: commentContent.value,
}
const length = commentContent.value ? commentContent.value.length : 0
return (
<form
className={postCommentForm}
onSubmit={handleSubmit}
>
<div className={postCommentFormHeader}>
<strong>Add your comment</strong>
<span className={commentCounter}>{length}</span>
<span className={markdownNote}>
<a
target="_blank"
Expand Down Expand Up @@ -175,7 +197,7 @@ export default class PostCommentForm extends Component {
}
<input
type="text"
placeholder="Name (optional)"
placeholder={authorNameRequired ? "Name (required)" : "Name"}
className={
authorName.touched && authorName.error && hasError
}
Expand All @@ -189,7 +211,7 @@ export default class PostCommentForm extends Component {
}
<input
type="email"
placeholder="Email (optional, not shown)"
placeholder={authorEmailRequired ? "Email (required - not shown)" : "Email (not shown)"}
className={
authorEmail.touched && authorEmail.error && hasError
}
Expand Down
9 changes: 8 additions & 1 deletion packages/frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ dotenv.config({ path: '../../.env' })

const apiUrl = getApiUrl()
const websiteUrl = getWebsiteUrl()
const { REQEMAIL: authorEmailRequired, REQNAME: authorNameRequired } = process.env
const { SIZELIMIT: contentSizeLimit, DISALLOW_EMPTY: disallowEmptyContent } = process.env


function modify (webpackConfig) {
webpackConfig.postcss = () => {
Expand Down Expand Up @@ -69,7 +72,11 @@ function modifyClient (webpackConfig) {
'__CONFIG__': JSON.stringify({
apiUrl,
websiteUrl,
apiKey
apiKey,
authorNameRequired,
authorEmailRequired,
contentSizeLimit,
disallowEmptyContent
})
}))
webpackConfig.plugins = plugins
Expand Down
15 changes: 15 additions & 0 deletions packages/lambda/src/queueComment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import { apiKey } from '../../../../deploy/state/apiKey.json'
dotenv.config({ silent: true })

const hmac = jwa('HS256')
const { REQEMAIL: authorEmailRequired, REQNAME: authorNameRequired } = process.env
const { SIZELIMIT: contentSizeLimit, DISALLOW_EMPTY: disallowEmptyContent } = process.env

let akismet = null
let contentRegex = /\w+/

class ValidationError extends Error {
constructor (data) {
Expand Down Expand Up @@ -56,6 +59,18 @@ function validate (payload) {
if (commentContent && commentContent.length < 3) {
errors.commentContent = 'Must be at least 3 characters'
}
if (commentContent && contentSizeLimit && (commentContent.length > parseInt(contentSizeLimit))) {
errors.commentContent = 'Comment has exceeded content length of: ' + parseInt(contentSizeLimit) + ' characters.'
}
if (commentContent && disallowEmptyContent && !commentContent.match(contentRegex)) {
errors.commentContent = disallowEmptyContent
}
if (authorNameRequired && !authorName) {
errors.authorName = authorNameRequired
}
if (authorEmailRequired && !authorEmail) {
errors.authorEmail = authorEmailRequired
}
if (authorEmail && !isEmail(authorEmail)) {
errors.authorEmail = 'Email format not valid'
}
Expand Down