2016-04-14 18:33:22 +01:00
|
|
|
var path = require('path'),
|
|
|
|
express = require('express'),
|
2016-05-08 16:49:12 +02:00
|
|
|
_ = require('lodash'),
|
2016-04-21 16:37:52 +01:00
|
|
|
subscribeRouter = express.Router(),
|
2016-10-11 09:36:00 +01:00
|
|
|
bodyParser = require('body-parser'),
|
2016-04-21 16:37:52 +01:00
|
|
|
|
|
|
|
// Dirty requires
|
2016-04-14 22:44:05 +02:00
|
|
|
api = require('../../../api'),
|
2016-05-07 10:33:04 +02:00
|
|
|
errors = require('../../../errors'),
|
2016-10-14 15:31:20 +01:00
|
|
|
validator = require('../../../data/validation').validator,
|
2017-09-12 16:31:14 +01:00
|
|
|
postLookup = require('../../../controllers/frontend/post-lookup'),
|
2017-11-10 12:44:29 +00:00
|
|
|
renderer = require('../../../controllers/frontend/renderer'),
|
2016-04-14 18:33:22 +01:00
|
|
|
|
2017-11-10 12:44:29 +00:00
|
|
|
templateName = 'subscribe';
|
2016-04-14 18:33:22 +01:00
|
|
|
|
2017-11-05 12:45:43 +00:00
|
|
|
function _renderer(req, res) {
|
2017-11-10 12:44:29 +00:00
|
|
|
// Note: this is super similar to the config middleware used in channels
|
|
|
|
// @TODO refactor into to something explicit & DRY this up
|
|
|
|
res._route = {
|
|
|
|
type: 'custom',
|
|
|
|
templateName: templateName,
|
|
|
|
defaultTemplate: path.resolve(__dirname, 'views', templateName + '.hbs')
|
|
|
|
};
|
|
|
|
|
2017-11-05 12:45:43 +00:00
|
|
|
// Renderer begin
|
|
|
|
// Format data
|
|
|
|
var data = req.body;
|
|
|
|
|
|
|
|
// Render Call
|
2017-11-10 12:44:29 +00:00
|
|
|
return renderer(req, res, data);
|
2016-04-14 18:33:22 +01:00
|
|
|
}
|
|
|
|
|
2017-04-05 23:02:16 +02:00
|
|
|
/**
|
|
|
|
* Takes care of sanitizing the email input.
|
|
|
|
* XSS prevention.
|
|
|
|
* For success cases, we don't have to worry, because then the input contained a valid email address.
|
|
|
|
*/
|
2016-04-21 16:37:52 +01:00
|
|
|
function errorHandler(error, req, res, next) {
|
2017-04-05 23:02:16 +02:00
|
|
|
req.body.email = '';
|
|
|
|
|
2016-04-21 16:37:52 +01:00
|
|
|
if (error.statusCode !== 404) {
|
|
|
|
res.locals.error = error;
|
2017-11-05 12:45:43 +00:00
|
|
|
return _renderer(req, res);
|
2016-04-21 16:37:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
function honeyPot(req, res, next) {
|
|
|
|
if (!req.body.hasOwnProperty('confirm') || req.body.confirm !== '') {
|
|
|
|
return next(new Error('Oops, something went wrong!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// we don't need this anymore
|
|
|
|
delete req.body.confirm;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2016-10-14 15:31:20 +01:00
|
|
|
function santizeUrl(url) {
|
2017-04-05 23:02:16 +02:00
|
|
|
return validator.isEmptyOrURL(url || '') ? url : '';
|
2016-10-14 15:31:20 +01:00
|
|
|
}
|
|
|
|
|
2016-04-21 16:37:52 +01:00
|
|
|
function handleSource(req, res, next) {
|
2016-10-14 15:31:20 +01:00
|
|
|
req.body.subscribed_url = santizeUrl(req.body.location);
|
|
|
|
req.body.subscribed_referrer = santizeUrl(req.body.referrer);
|
2016-04-21 16:37:52 +01:00
|
|
|
delete req.body.location;
|
|
|
|
delete req.body.referrer;
|
2016-05-07 10:33:04 +02:00
|
|
|
|
2017-09-12 16:31:14 +01:00
|
|
|
postLookup(req.body.subscribed_url)
|
2016-05-07 10:33:04 +02:00
|
|
|
.then(function (result) {
|
|
|
|
if (result && result.post) {
|
|
|
|
req.body.post_id = result.post.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (err instanceof errors.NotFoundError) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
});
|
2016-04-21 16:37:52 +01:00
|
|
|
}
|
2016-04-14 22:44:05 +02:00
|
|
|
|
|
|
|
function storeSubscriber(req, res, next) {
|
2016-04-21 16:37:52 +01:00
|
|
|
req.body.status = 'subscribed';
|
|
|
|
|
2016-05-08 16:49:12 +02:00
|
|
|
if (_.isEmpty(req.body.email)) {
|
2016-10-06 14:27:35 +02:00
|
|
|
return next(new errors.ValidationError({message: 'Email cannot be blank.'}));
|
2016-12-21 16:52:47 +07:00
|
|
|
} else if (!validator.isEmail(req.body.email)) {
|
|
|
|
return next(new errors.ValidationError({message: 'Invalid email.'}));
|
2016-05-08 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 16:37:52 +01:00
|
|
|
return api.subscribers.add({subscribers: [req.body]}, {context: {external: true}})
|
|
|
|
.then(function () {
|
|
|
|
res.locals.success = true;
|
|
|
|
next();
|
|
|
|
})
|
2016-05-08 16:49:12 +02:00
|
|
|
.catch(function () {
|
|
|
|
// we do not expose any information
|
|
|
|
res.locals.success = true;
|
|
|
|
next();
|
2016-04-21 16:37:52 +01:00
|
|
|
});
|
2016-04-14 22:44:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:33:22 +01:00
|
|
|
// subscribe frontend route
|
2017-11-10 12:44:29 +00:00
|
|
|
subscribeRouter
|
|
|
|
.route('/')
|
2016-04-14 18:33:22 +01:00
|
|
|
.get(
|
2017-11-05 12:45:43 +00:00
|
|
|
_renderer
|
2016-04-14 18:33:22 +01:00
|
|
|
)
|
|
|
|
.post(
|
2016-10-11 09:36:00 +01:00
|
|
|
bodyParser.urlencoded({extended: true}),
|
2016-04-21 16:37:52 +01:00
|
|
|
honeyPot,
|
|
|
|
handleSource,
|
2016-04-14 22:44:05 +02:00
|
|
|
storeSubscriber,
|
2017-11-05 12:45:43 +00:00
|
|
|
_renderer
|
2016-04-14 18:33:22 +01:00
|
|
|
);
|
|
|
|
|
2016-04-21 16:37:52 +01:00
|
|
|
// configure an error handler just for subscribe problems
|
|
|
|
subscribeRouter.use(errorHandler);
|
|
|
|
|
2016-04-14 18:33:22 +01:00
|
|
|
module.exports = subscribeRouter;
|
2017-03-01 13:02:53 +01:00
|
|
|
module.exports.storeSubscriber = storeSubscriber;
|