Fixes in subscriptions. It now passes the tests.
API tests still don't work.
This commit is contained in:
parent
e9165838dc
commit
47b8d80c22
16 changed files with 2649 additions and 975 deletions
535
routes/api.js
535
routes/api.js
|
@ -1,340 +1,238 @@
|
|||
'use strict';
|
||||
|
||||
let lists = require('../lib/models/lists');
|
||||
let fields = require('../lib/models/fields');
|
||||
let blacklist = require('../models/blacklist');
|
||||
let subscriptions = require('../lib/models/subscriptions');
|
||||
let confirmations = require('../lib/models/confirmations');
|
||||
let tools = require('../lib/tools');
|
||||
let log = require('npmlog');
|
||||
const lists = require('../models/lists');
|
||||
const tools = require('../lib/tools-async');
|
||||
const blacklist = require('../models/blacklist');
|
||||
const fields = require('../models/fields');
|
||||
const { SubscriptionStatus } = require('../shared/lists');
|
||||
const subscriptions = require('../models/subscriptions');
|
||||
const confirmations = require('../models/confirmations');
|
||||
const log = require('npmlog');
|
||||
const router = require('../lib/router-async').create();
|
||||
let mailHelpers = require('../lib/subscription-mail-helpers');
|
||||
const mailHelpers = require('../lib/subscription-mail-helpers');
|
||||
const interoperableErrors = require('../shared/interoperable-errors');
|
||||
const contextHelpers = require('../lib/context-helpers');
|
||||
const shares = require('../models/shares');
|
||||
const slugify = require('slugify');
|
||||
|
||||
router.post('/subscribe/:listId', (req, res) => {
|
||||
let input = {};
|
||||
class APIError extends Error {
|
||||
constructor(msg, status) {
|
||||
super(msg);
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
router.postAsync('/subscribe/:listId', async (req, res) => {
|
||||
const listId = req.params.listId;
|
||||
|
||||
const input = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
input[(key || '').toString().trim().toUpperCase()] = (req.body[key] || '').toString().trim();
|
||||
});
|
||||
lists.getByCid(req.params.listId, (err, list) => {
|
||||
if (err) {
|
||||
log.error('API', err);
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!list) {
|
||||
res.status(404);
|
||||
return res.json({
|
||||
error: 'Selected listId not found',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!input.EMAIL) {
|
||||
res.status(400);
|
||||
return res.json({
|
||||
error: 'Missing EMAIL',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
tools.validateEmail(input.EMAIL, false, err => {
|
||||
if (err) {
|
||||
log.error('API', err);
|
||||
res.status(400);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
|
||||
if (!input.EMAIL) {
|
||||
throw new APIError('Missing EMAIL', 400);
|
||||
}
|
||||
|
||||
const emailErr = await tools.validateEmail(input.EMAIL, false);
|
||||
if (emailErr) {
|
||||
const errMsg = tools.validateEmailGetMessage(emailErr, email);
|
||||
log.error('API', errMsg);
|
||||
throw new APIError(errMsg, 400);
|
||||
}
|
||||
|
||||
const subscription = {
|
||||
email: input.EMAIL
|
||||
};
|
||||
|
||||
if (input.TIMEZONE) {
|
||||
subscription.tz = (input.TIMEZONE || '').toString().trim();
|
||||
}
|
||||
|
||||
const fieldList = await fields.fromPost(req.context, listId);
|
||||
|
||||
for (const field of fieldList) {
|
||||
if (field.key in input && field.column) {
|
||||
if (field.type === 'option') {
|
||||
subscription[field.column] = ['false', 'no', '0', ''].indexOf((input[field.key] || '').toString().trim().toLowerCase()) >= 0 ? '' : '1';
|
||||
} else {
|
||||
subscription[field.column] = input[field.key];
|
||||
}
|
||||
|
||||
let subscription = {
|
||||
email: input.EMAIL
|
||||
};
|
||||
|
||||
if (input.FIRST_NAME) {
|
||||
subscription.first_name = (input.FIRST_NAME || '').toString().trim();
|
||||
}
|
||||
|
||||
if (input.LAST_NAME) {
|
||||
subscription.last_name = (input.LAST_NAME || '').toString().trim();
|
||||
}
|
||||
|
||||
if (input.TIMEZONE) {
|
||||
subscription.tz = (input.TIMEZONE || '').toString().trim();
|
||||
}
|
||||
|
||||
fields.list(list.id, (err, fieldList) => {
|
||||
if (err && !fieldList) {
|
||||
fieldList = [];
|
||||
}
|
||||
|
||||
fieldList.forEach(field => {
|
||||
if (input.hasOwnProperty(field.key) && field.column) {
|
||||
subscription[field.column] = input[field.key];
|
||||
} else if (field.options) {
|
||||
for (let i = 0, len = field.options.length; i < len; i++) {
|
||||
if (input.hasOwnProperty(field.options[i].key) && field.options[i].column) {
|
||||
let value = input[field.options[i].key];
|
||||
if (field.options[i].type === 'option') {
|
||||
value = ['false', 'no', '0', ''].indexOf((value || '').toString().trim().toLowerCase()) >= 0 ? '' : '1';
|
||||
}
|
||||
subscription[field.options[i].column] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let meta = {
|
||||
partial: true
|
||||
};
|
||||
|
||||
if (/^(yes|true|1)$/i.test(input.FORCE_SUBSCRIBE)) {
|
||||
meta.status = 1;
|
||||
}
|
||||
|
||||
if (/^(yes|true|1)$/i.test(input.REQUIRE_CONFIRMATION)) {
|
||||
const data = {
|
||||
email: subscription.email,
|
||||
subscriptionData: subscription
|
||||
};
|
||||
|
||||
confirmations.addConfirmation(list.id, 'subscribe', req.ip, data, (err, confirmCid) => {
|
||||
if (err) {
|
||||
log.error('API', err);
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
|
||||
mailHelpers.sendConfirmSubscription(list, input.EMAIL, confirmCid, subscription, (err) => {
|
||||
if (err) {
|
||||
log.error('API', err);
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: confirmCid
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
subscriptions.insert(list.id, meta, subscription, (err, response) => {
|
||||
if (err) {
|
||||
log.error('API', err);
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: response.cid
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/unsubscribe/:listId', (req, res) => {
|
||||
let input = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
input[(key || '').toString().trim().toUpperCase()] = (req.body[key] || '').toString().trim();
|
||||
});
|
||||
lists.getByCid(req.params.listId, (err, list) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!list) {
|
||||
res.status(404);
|
||||
return res.json({
|
||||
error: 'Selected listId not found',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!input.EMAIL) {
|
||||
res.status(400);
|
||||
return res.json({
|
||||
error: 'Missing EMAIL',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
subscriptions.getByEmail(list.id, input.EMAIL, (err, subscription) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (/^(yes|true|1)$/i.test(input.FORCE_SUBSCRIBE)) {
|
||||
subscription.status = SubscriptionStatus.SUBSCRIBED;
|
||||
}
|
||||
|
||||
if (!subscription) {
|
||||
res.status(404);
|
||||
return res.json({
|
||||
error: 'Subscription with given email not found',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (/^(yes|true|1)$/i.test(input.REQUIRE_CONFIRMATION)) {
|
||||
const list = await lists.getByCid(contextHelpers.getAdminContext(), listId);
|
||||
await shares.enforceEntityPermission(req.context, 'list', listId, 'manageSubscriptions');
|
||||
|
||||
subscriptions.changeStatus(list.id, subscription.id, false, subscriptions.Status.UNSUBSCRIBED, (err, found) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: subscription.id,
|
||||
unsubscribed: true
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/delete/:listId', (req, res) => {
|
||||
let input = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
input[(key || '').toString().trim().toUpperCase()] = (req.body[key] || '').toString().trim();
|
||||
});
|
||||
lists.getByCid(req.params.listId, (err, list) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!list) {
|
||||
res.status(404);
|
||||
return res.json({
|
||||
error: 'Selected listId not found',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!input.EMAIL) {
|
||||
res.status(400);
|
||||
return res.json({
|
||||
error: 'Missing EMAIL',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
subscriptions.getByEmail(list.id, input.EMAIL, (err, subscription) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!subscription) {
|
||||
res.status(404);
|
||||
return res.json({
|
||||
error: 'Subscription not found',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
subscriptions.delete(list.id, subscription.cid, (err, subscription) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!subscription) {
|
||||
res.status(404);
|
||||
return res.json({
|
||||
error: 'Subscription not found',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: subscription.id,
|
||||
deleted: true
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/field/:listId', (req, res) => {
|
||||
let input = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
input[(key || '').toString().trim().toUpperCase()] = (req.body[key] || '').toString().trim();
|
||||
});
|
||||
lists.getByCid(req.params.listId, (err, list) => {
|
||||
if (err) {
|
||||
log.error('API', err);
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
}
|
||||
if (!list) {
|
||||
res.status(404);
|
||||
return res.json({
|
||||
error: 'Selected listId not found',
|
||||
data: []
|
||||
});
|
||||
}
|
||||
|
||||
let field = {
|
||||
name: (input.NAME || '').toString().trim(),
|
||||
defaultValue: (input.DEFAULT || '').toString().trim() || null,
|
||||
type: (input.TYPE || '').toString().toLowerCase().trim(),
|
||||
group: Number(input.GROUP) || null,
|
||||
groupTemplate: (input.GROUP_TEMPLATE || '').toString().toLowerCase().trim(),
|
||||
visible: ['false', 'no', '0', ''].indexOf((input.VISIBLE || '').toString().toLowerCase().trim()) < 0
|
||||
const data = {
|
||||
email,
|
||||
subscriptionData: subscription
|
||||
};
|
||||
|
||||
fields.create(list.id, field, (err, id, tag) => {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
return res.json({
|
||||
error: err.message || err,
|
||||
data: []
|
||||
});
|
||||
const confirmCid = await confirmations.addConfirmation(listId, 'subscribe', req.ip, data);
|
||||
await mailHelpers.sendConfirmSubscription(list, input.EMAIL, confirmCid, subscription);
|
||||
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: confirmCid
|
||||
}
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id,
|
||||
tag
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
const meta = {};
|
||||
await subscriptions.create(req.context, listId, subscription, meta);
|
||||
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: meta.cid
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
router.postAsync('/unsubscribe/:listId', async (req, res) => {
|
||||
const input = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
input[(key || '').toString().trim().toUpperCase()] = (req.body[key] || '').toString().trim();
|
||||
});
|
||||
|
||||
if (!input.EMAIL) {
|
||||
throw new APIError('Missing EMAIL', 400);
|
||||
}
|
||||
|
||||
const subscription = await subscriptions.unsubscribeByEmailAndGet(req.context, req.params.listId, input.EMAIL);
|
||||
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: subscription.id,
|
||||
unsubscribed: true
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
router.postAsync('/delete/:listId', async (req, res) => {
|
||||
const input = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
input[(key || '').toString().trim().toUpperCase()] = (req.body[key] || '').toString().trim();
|
||||
});
|
||||
|
||||
if (!input.EMAIL) {
|
||||
throw new APIError('Missing EMAIL', 400);
|
||||
}
|
||||
|
||||
const subscription = await subscriptions.removeByEmailAndGet(req.context, req.params.listId, input.EMAIL);
|
||||
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id: subscription.id,
|
||||
deleted: true
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
router.getAsync('/subscriptions/:listId', async (req, res) => {
|
||||
const start = parseInt(req.query.start || 0, 10);
|
||||
const limit = parseInt(req.query.limit || 10000, 10);
|
||||
|
||||
const { subscriptions, total } = await subscriptions.list(req.params.listId, false, start, limit);
|
||||
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
total: total,
|
||||
start: start,
|
||||
limit: limit,
|
||||
subscriptions
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
router.getAsync('/lists/:email', async (req, res) => {
|
||||
const lists = await subscriptions.getListsWithEmail(req.context, req.params.email);
|
||||
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: lists
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
router.postAsync('/field/:listId', async (req, res) => {
|
||||
const input = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
input[(key || '').toString().trim().toUpperCase()] = (req.body[key] || '').toString().trim();
|
||||
});
|
||||
|
||||
const key = (input.NAME || '').toString().trim() || slugify('merge ' + name, '_').toUpperCase();
|
||||
const visible = ['false', 'no', '0', ''].indexOf((input.VISIBLE || '').toString().toLowerCase().trim()) < 0;
|
||||
|
||||
const groupTemplate = (input.GROUP_TEMPLATE || '').toString().toLowerCase().trim();
|
||||
|
||||
let type = (input.TYPE || '').toString().toLowerCase().trim();
|
||||
const settings = {};
|
||||
|
||||
if (type === 'checkbox') {
|
||||
type = 'checkbox-grouped';
|
||||
settings.groupTemplate = groupTemplate;
|
||||
} else if (type === 'dropdown') {
|
||||
type = 'dropdown-grouped';
|
||||
settings.groupTemplate = groupTemplate;
|
||||
} else if (type === 'radio') {
|
||||
type = 'radio-grouped';
|
||||
settings.groupTemplate = groupTemplate;
|
||||
} else if (type === 'json') {
|
||||
settings.groupTemplate = groupTemplate;
|
||||
} else if (type === 'date-us') {
|
||||
type = 'date';
|
||||
settings.dateFormat = 'us';
|
||||
} else if (type === 'date-eur') {
|
||||
type = 'date';
|
||||
settings.dateFormat = 'eur';
|
||||
} else if (type === 'birthday-us') {
|
||||
type = 'birthday';
|
||||
settings.birthdayFormat = 'us';
|
||||
} else if (type === 'birthday-eur') {
|
||||
type = 'birthday';
|
||||
settings.birthdayFormat = 'eur';
|
||||
}
|
||||
|
||||
const field = {
|
||||
name: (input.NAME || '').toString().trim(),
|
||||
key,
|
||||
default_value: (input.DEFAULT || '').toString().trim() || null,
|
||||
type,
|
||||
settings,
|
||||
group: Number(input.GROUP) || null,
|
||||
orderListBefore: visible ? 'end' : 'none',
|
||||
orderSubscribeBefore: visible ? 'end' : 'none',
|
||||
orderManageBefore: visible ? 'end' : 'none'
|
||||
};
|
||||
|
||||
const id = await fields.create(req.context, req.params.listId, field);
|
||||
|
||||
res.status(200);
|
||||
res.json({
|
||||
data: {
|
||||
id,
|
||||
tag: key
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
router.postAsync('/blacklist/add', async (req, res) => {
|
||||
let input = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
|
@ -351,6 +249,7 @@ router.postAsync('/blacklist/add', async (req, res) => {
|
|||
});
|
||||
});
|
||||
|
||||
|
||||
router.postAsync('/blacklist/delete', async (req, res) => {
|
||||
let input = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
|
@ -367,6 +266,7 @@ router.postAsync('/blacklist/delete', async (req, res) => {
|
|||
});
|
||||
});
|
||||
|
||||
|
||||
router.getAsync('/blacklist/get', async (req, res) => {
|
||||
let start = parseInt(req.query.start || 0, 10);
|
||||
let limit = parseInt(req.query.limit || 10000, 10);
|
||||
|
@ -384,4 +284,5 @@ router.getAsync('/blacklist/get', async (req, res) => {
|
|||
});
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
|
|
@ -37,6 +37,7 @@ const objectHash = require('object-hash');
|
|||
const bluebird = require('bluebird');
|
||||
const fsReadFile = bluebird.promisify(require('fs').readFile);
|
||||
|
||||
const { cleanupFromPost } = require('../lib/helpers');
|
||||
|
||||
const originWhitelist = config.cors && config.cors.origins || [];
|
||||
|
||||
|
@ -158,8 +159,6 @@ router.getAsync('/confirm/subscribe/:cid', async (req, res) => {
|
|||
}
|
||||
}
|
||||
|
||||
console.log(subscription);
|
||||
|
||||
const list = await lists.getById(contextHelpers.getAdminContext(), confirmation.list);
|
||||
subscription.cid = meta.cid;
|
||||
await mailHelpers.sendSubscriptionConfirmed(list, subscription.email, subscription);
|
||||
|
@ -195,31 +194,15 @@ router.getAsync('/confirm/unsubscribe/:cid', async (req, res) => {
|
|||
});
|
||||
|
||||
|
||||
router.getAsync('/:cid', passport.csrfProtection, async (req, res) => {
|
||||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.cid);
|
||||
|
||||
if (!list.public_subscribe) {
|
||||
shares.throwPermissionDenied();
|
||||
}
|
||||
|
||||
const ucid = req.query.cid;
|
||||
|
||||
const data = req.query;
|
||||
async function _renderSubscribe(req, res, list, subscription) {
|
||||
const data = {};
|
||||
data.email = subscription && subscription.email;
|
||||
data.layout = 'subscription/layout';
|
||||
data.title = list.name;
|
||||
data.cid = list.cid;
|
||||
data.csrfToken = req.csrfToken();
|
||||
|
||||
let subscription;
|
||||
if (ucid) {
|
||||
subscription = await subscriptions.getByCid(contextHelpers.getAdminContext(), list.id, ucid, false);
|
||||
|
||||
if (subscription) {
|
||||
data.email = subscription.email;
|
||||
}
|
||||
}
|
||||
|
||||
data.customFields = await fields.getRow(contextHelpers.getAdminContext(), list.id, subscription);
|
||||
data.customFields = await fields.forHbs(contextHelpers.getAdminContext(), list.id, subscription);
|
||||
data.useEditor = true;
|
||||
|
||||
const configItems = await settings.get(['pgpPrivateKey', 'defaultAddress', 'defaultPostaddress']);
|
||||
|
@ -241,68 +224,63 @@ router.getAsync('/:cid', passport.csrfProtection, async (req, res) => {
|
|||
data.flashMessages = await captureFlashMessages(res);
|
||||
|
||||
const result = htmlRenderer(data);
|
||||
|
||||
res.send(result);
|
||||
});
|
||||
|
||||
|
||||
router.options('/:cid/widget', cors(corsOptions));
|
||||
|
||||
router.getAsync('/:cid/widget', cors(corsOptions), async (req, res) => {
|
||||
req.needsAPIJSONResponse = true;
|
||||
|
||||
const cached = cache.get(req.path);
|
||||
if (cached) {
|
||||
return res.status(200).json(cached);
|
||||
}
|
||||
}
|
||||
|
||||
router.getAsync('/:cid', passport.csrfProtection, async (req, res) => {
|
||||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.cid);
|
||||
|
||||
const configItems = await settings.get(['serviceUrl', 'pgpPrivateKey']);
|
||||
if (!list.public_subscribe) {
|
||||
shares.throwPermissionDenied();
|
||||
}
|
||||
|
||||
const data = {
|
||||
title: list.name,
|
||||
cid: list.cid,
|
||||
serviceUrl: configItems.serviceUrl,
|
||||
hasPubkey: !!configItems.pgpPrivateKey,
|
||||
customFields: await fields.getRow(contextHelpers.getAdminContext(), list.id),
|
||||
template: {},
|
||||
layout: null,
|
||||
};
|
||||
const ucid = req.query.cid;
|
||||
|
||||
await injectCustomFormData(req.query.fid || list.default_form, 'subscription/web-subscribe', data);
|
||||
let subscription;
|
||||
if (ucid) {
|
||||
try {
|
||||
subscription = await subscriptions.getByCid(contextHelpers.getAdminContext(), list.id, ucid);
|
||||
|
||||
const renderAsync = bluebird.promisify(res.render);
|
||||
const html = await renderAsync('subscription/widget-subscribe', data);
|
||||
|
||||
const response = {
|
||||
data: {
|
||||
title: data.title,
|
||||
cid: data.cid,
|
||||
html
|
||||
if (subscription.status === SubscriptionStatus.SUBSCRIBED) {
|
||||
subscription = null;
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof interoperableErrors.NotFoundError) {
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
cache.put(req.path, response, 30000); // ms
|
||||
res.status(200).json(response);
|
||||
await _renderSubscribe(req, res, list, subscription);
|
||||
});
|
||||
|
||||
|
||||
router.options('/:cid/subscribe', cors(corsOptions));
|
||||
|
||||
router.postAsync('/:cid/subscribe', passport.parseForm, corsOrCsrfProtection, async (req, res) => {
|
||||
const email = (req.body.email || '').toString().trim();
|
||||
|
||||
if (req.xhr) {
|
||||
req.needsAPIJSONResponse = true;
|
||||
}
|
||||
|
||||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.cid);
|
||||
|
||||
if (!list.public_subscribe) {
|
||||
shares.throwPermissionDenied();
|
||||
}
|
||||
|
||||
const subscriptionData = await fields.fromPost(contextHelpers.getAdminContext(), list.id, req.body);
|
||||
|
||||
const email = cleanupFromPost(req.body.EMAIL);
|
||||
|
||||
if (!email) {
|
||||
if (req.xhr) {
|
||||
throw new Error('Email address not set');
|
||||
}
|
||||
|
||||
req.flash('danger', _('Email address not set'));
|
||||
return res.redirect('/subscription/' + encodeURIComponent(req.params.cid) + '?' + tools.queryParams(req.body));
|
||||
return await _renderSubscribe(req, res, list, subscriptionData);
|
||||
}
|
||||
|
||||
const emailErr = await tools.validateEmail(email);
|
||||
|
@ -314,7 +292,8 @@ router.postAsync('/:cid/subscribe', passport.parseForm, corsOrCsrfProtection, as
|
|||
}
|
||||
|
||||
req.flash('danger', errMsg);
|
||||
return res.redirect('/subscription/' + encodeURIComponent(req.params.cid) + '?' + tools.queryParams(req.body));
|
||||
subscriptionData.email = email;
|
||||
return await _renderSubscribe(req, res, list, subscriptionData);
|
||||
}
|
||||
|
||||
// Check if the subscriber seems legit. This is a really simple check, the only requirement is that
|
||||
|
@ -326,23 +305,18 @@ router.postAsync('/:cid/subscribe', passport.parseForm, corsOrCsrfProtection, as
|
|||
let addressTest = !req.body.address;
|
||||
let testsPass = subTimeTest && addressTest;
|
||||
|
||||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.cid);
|
||||
|
||||
if (!list.public_subscribe) {
|
||||
shares.throwPermissionDenied();
|
||||
let existingSubscription;
|
||||
try {
|
||||
existingSubscription = await subscriptions.getByEmail(contextHelpers.getAdminContext(), list.id, email);
|
||||
} catch (err) {
|
||||
if (err instanceof interoperableErrors.NotFoundError) {
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
const subscriptionData = {};
|
||||
Object.keys(req.body).forEach(key => {
|
||||
if (key !== 'email' && key.charAt(0) !== '_') {
|
||||
subscriptionData[key] = (req.body[key] || '').toString().trim();
|
||||
}
|
||||
});
|
||||
|
||||
const subscription = await subscriptions.getByEmail(contextHelpers.getAdminContext(), list.id, email, false);
|
||||
|
||||
if (subscription && subscription.status === SubscriptionStatus.SUBSCRIBED) {
|
||||
await mailHelpers.sendAlreadySubscribed(list, email, subscription);
|
||||
if (existingSubscription && existingSubscription.status === SubscriptionStatus.SUBSCRIBED) {
|
||||
await mailHelpers.sendAlreadySubscribed(list, email, existingSubscription);
|
||||
res.redirect('/subscription/' + encodeURIComponent(req.params.cid) + '/confirm-subscription-notice');
|
||||
|
||||
} else {
|
||||
|
@ -368,11 +342,56 @@ router.postAsync('/:cid/subscribe', passport.parseForm, corsOrCsrfProtection, as
|
|||
}
|
||||
});
|
||||
|
||||
|
||||
router.options('/:cid/widget', cors(corsOptions));
|
||||
|
||||
router.getAsync('/:cid/widget', cors(corsOptions), async (req, res) => {
|
||||
req.needsAPIJSONResponse = true;
|
||||
|
||||
const cached = cache.get(req.path);
|
||||
if (cached) {
|
||||
return res.status(200).json(cached);
|
||||
}
|
||||
|
||||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.cid);
|
||||
|
||||
const configItems = await settings.get(['serviceUrl', 'pgpPrivateKey']);
|
||||
|
||||
const data = {
|
||||
title: list.name,
|
||||
cid: list.cid,
|
||||
serviceUrl: configItems.serviceUrl,
|
||||
hasPubkey: !!configItems.pgpPrivateKey,
|
||||
customFields: await fields.forHbs(contextHelpers.getAdminContext(), list.id),
|
||||
template: {},
|
||||
layout: null,
|
||||
};
|
||||
|
||||
await injectCustomFormData(req.query.fid || list.default_form, 'subscription/web-subscribe', data);
|
||||
|
||||
const renderAsync = bluebird.promisify(res.render);
|
||||
const html = await renderAsync('subscription/widget-subscribe', data);
|
||||
|
||||
const response = {
|
||||
data: {
|
||||
title: data.title,
|
||||
cid: data.cid,
|
||||
html
|
||||
}
|
||||
};
|
||||
|
||||
cache.put(req.path, response, 30000); // ms
|
||||
res.status(200).json(response);
|
||||
});
|
||||
|
||||
|
||||
|
||||
router.getAsync('/:lcid/manage/:ucid', passport.csrfProtection, async (req, res) => {
|
||||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.lcid);
|
||||
const subscription = await subscriptions.getByCid(contextHelpers.getAdminContext(), list.id, req.params.ucid, false);
|
||||
|
||||
if (!subscription || subscription.status !== SubscriptionStatus.SUBSCRIBED) {
|
||||
const subscription = await subscriptions.getByCid(contextHelpers.getAdminContext(), list.id, req.params.ucid);
|
||||
|
||||
if (subscription.status !== SubscriptionStatus.SUBSCRIBED) {
|
||||
throw new interoperableErrors.NotFoundError('Subscription not found in this list');
|
||||
}
|
||||
|
||||
|
@ -384,7 +403,7 @@ router.getAsync('/:lcid/manage/:ucid', passport.csrfProtection, async (req, res)
|
|||
data.csrfToken = req.csrfToken();
|
||||
data.layout = 'data/layout';
|
||||
|
||||
data.customFields = await fields.getRow(contextHelpers.getAdminContext(), list.id, subscription);
|
||||
data.customFields = await fields.forHbs(contextHelpers.getAdminContext(), list.id, subscription);
|
||||
|
||||
data.useEditor = true;
|
||||
|
||||
|
@ -414,7 +433,8 @@ router.postAsync('/:lcid/manage', passport.parseForm, passport.csrfProtection, a
|
|||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.lcid);
|
||||
|
||||
try {
|
||||
await subscriptions.updateManagedUngrouped(contextHelpers.getAdminContext(), list.id, req.body);
|
||||
const subscriptionData = await fields.fromPost(contextHelpers.getAdminContext(), list.id, req.body);
|
||||
await subscriptions.updateManaged(contextHelpers.getAdminContext(), list.id, req.body.cid, subscriptionData);
|
||||
} catch (err) {
|
||||
if (err instanceof interoperableErrors.NotFoundError) {
|
||||
throw new interoperableErrors.NotFoundError('Subscription not found in this list');
|
||||
|
@ -430,7 +450,7 @@ router.getAsync('/:lcid/manage-address/:ucid', passport.csrfProtection, async (r
|
|||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.lcid);
|
||||
const subscription = await subscriptions.getByCid(contextHelpers.getAdminContext(), list.id, req.params.ucid, false);
|
||||
|
||||
if (!subscription || subscription.status !== SubscriptionStatus.SUBSCRIBED) {
|
||||
if (subscription.status !== SubscriptionStatus.SUBSCRIBED) {
|
||||
throw new interoperableErrors.NotFoundError('Subscription not found in this list');
|
||||
}
|
||||
|
||||
|
@ -450,7 +470,7 @@ router.getAsync('/:lcid/manage-address/:ucid', passport.csrfProtection, async (r
|
|||
layout: 'subscription/layout.mjml.hbs'
|
||||
};
|
||||
|
||||
await injectCustomFormData(req.query.fid || list.default_form, 'data/web-manage-address', subscription);
|
||||
await injectCustomFormData(req.query.fid || list.default_form, 'data/web-manage-address', data);
|
||||
|
||||
const htmlRenderer = await getMjmlTemplate(data.template);
|
||||
|
||||
|
@ -466,7 +486,7 @@ router.getAsync('/:lcid/manage-address/:ucid', passport.csrfProtection, async (r
|
|||
router.postAsync('/:lcid/manage-address', passport.parseForm, passport.csrfProtection, async (req, res) => {
|
||||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.lcid);
|
||||
|
||||
const emailNew = (req.body['email-new'] || '').toString().trim();
|
||||
const emailNew = cleanupFromPost(req.body['EMAIL_NEW']);
|
||||
|
||||
const subscription = await subscriptions.getByCid(contextHelpers.getAdminContext(), list.id, req.body.cid, false);
|
||||
|
||||
|
@ -485,7 +505,15 @@ router.postAsync('/:lcid/manage-address', passport.parseForm, passport.csrfProte
|
|||
req.flash('danger', errMsg);
|
||||
|
||||
} else {
|
||||
const newSubscription = await subscriptions.getByEmail(contextHelpers.getAdminContext(), list.id, emailNew, false);
|
||||
let newSubscription;
|
||||
try {
|
||||
newSubscription = await subscriptions.getByEmail(contextHelpers.getAdminContext(), list.id, emailNew, false);
|
||||
} catch (err) {
|
||||
if (err instanceof interoperableErrors.NotFoundError) {
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
if (newSubscription && newSubscription.status === SubscriptionStatus.SUBSCRIBED) {
|
||||
await mailHelpers.sendAlreadySubscribed(list, emailNew, subscription);
|
||||
|
@ -523,7 +551,7 @@ router.getAsync('/:lcid/unsubscribe/:ucid', passport.csrfProtection, async (req,
|
|||
|
||||
const subscription = await subscriptions.getByCid(contextHelpers.getAdminContext(), list.id, req.params.ucid, false);
|
||||
|
||||
if (!subscription || subscription.status !== SubscriptionStatus.SUBSCRIBED) {
|
||||
if (subscription.status !== SubscriptionStatus.SUBSCRIBED) {
|
||||
throw new interoperableErrors.NotFoundError('Subscription not found in this list');
|
||||
}
|
||||
|
||||
|
@ -562,7 +590,7 @@ router.getAsync('/:lcid/unsubscribe/:ucid', passport.csrfProtection, async (req,
|
|||
router.postAsync('/:lcid/unsubscribe', passport.parseForm, passport.csrfProtection, async (req, res) => {
|
||||
const list = await lists.getByCid(contextHelpers.getAdminContext(), req.params.lcid);
|
||||
|
||||
const campaignCid = (req.body.campaign || '').toString().trim() || false;
|
||||
const campaignCid = cleanupFromPost(req.body.campaign);
|
||||
|
||||
await handleUnsubscribe(list, req.body.ucid, false, campaignCid, req.ip, res);
|
||||
});
|
||||
|
@ -588,7 +616,7 @@ async function handleUnsubscribe(list, subscriptionCid, autoUnsubscribe, campaig
|
|||
} else {
|
||||
const subscription = await subscriptions.getByCid(contextHelpers.getAdminContext(), list.id, subscriptionCid, false);
|
||||
|
||||
if (!subscription || subscription.status !== SubscriptionStatus.SUBSCRIBED) {
|
||||
if (subscription.status !== SubscriptionStatus.SUBSCRIBED) {
|
||||
throw new interoperableErrors.NotFoundError('Subscription not found in this list');
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue