Release candidate of the selectable unsubscription
Implemented the resubscription process - i.e. pre-filling in the form when the subscription link is clicked in the unsubscription notice.
This commit is contained in:
parent
bd4961366f
commit
a6d25e668b
12 changed files with 238 additions and 194 deletions
|
@ -98,7 +98,7 @@ function filterCustomFields(customFieldsIn = [], fieldIds = [], method = 'includ
|
|||
id: 'email',
|
||||
name: 'Email Address',
|
||||
type: 'Email',
|
||||
typeSubsciptionEmail: true
|
||||
typeSubscriptionEmail: true
|
||||
}, {
|
||||
id: 'firstname',
|
||||
name: 'First Name',
|
||||
|
|
|
@ -1054,13 +1054,13 @@ module.exports.updateMessage = (message, status, updateSubscription, callback) =
|
|||
|
||||
let statusCode;
|
||||
if (status === 'unsubscribed') {
|
||||
statusCode = 2;
|
||||
}
|
||||
if (status === 'bounced') {
|
||||
statusCode = 3;
|
||||
}
|
||||
if (status === 'complained') {
|
||||
statusCode = 4;
|
||||
statusCode = subscriptions.Status.UNSUBSCRIBED;
|
||||
} else if (status === 'bounced') {
|
||||
statusCode = subscriptions.Status.BOUNCED;
|
||||
} else if (status === 'complained') {
|
||||
statusCode = subscriptions.Status.COMPLAINED;
|
||||
} else {
|
||||
return callback(new Error(_('Unrecognized message status')));
|
||||
}
|
||||
|
||||
let query = 'UPDATE `campaigns` SET `' + status + '`=`' + status + '`+1 WHERE id=? LIMIT 1';
|
||||
|
@ -1074,7 +1074,7 @@ module.exports.updateMessage = (message, status, updateSubscription, callback) =
|
|||
}
|
||||
|
||||
if (updateSubscription) {
|
||||
subscriptions.changeStatus(message.list, message.subscription, statusCode === 2 ? message.campaign : false, statusCode, callback);
|
||||
subscriptions.changeStatus(message.list, message.subscription, statusCode === subscriptions.Status.UNSUBSCRIBED ? message.campaign : false, statusCode, callback);
|
||||
} else {
|
||||
return callback(null, true);
|
||||
}
|
||||
|
|
|
@ -16,14 +16,14 @@ module.exports.addConfirmation = (listId, action, ip, data, callback) => {
|
|||
}
|
||||
|
||||
let query = 'INSERT INTO confirmations (cid, list, action, ip, data) VALUES (?,?,?,?,?)';
|
||||
connection.query(query, [cid, list, action, ip, JSON.stringify(data || {})], (err, result) => {
|
||||
connection.query(query, [cid, listId, action, ip, JSON.stringify(data || {})], (err, result) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!result || !result.affectedRows) {
|
||||
return callback(null, false);
|
||||
return callback(new Error(_('Could not store confirmation data')));
|
||||
}
|
||||
|
||||
return callback(null, cid);
|
||||
|
|
|
@ -13,7 +13,8 @@ const ReportState = {
|
|||
SCHEDULED: 0,
|
||||
PROCESSING: 1,
|
||||
FINISHED: 2,
|
||||
FAILED: 3
|
||||
FAILED: 3,
|
||||
MAX: 4
|
||||
};
|
||||
|
||||
module.exports.ReportState = ReportState;
|
||||
|
|
|
@ -15,6 +15,8 @@ function listValues(filter, callback) {
|
|||
filter = false;
|
||||
}
|
||||
|
||||
// TODO: It would be good to cache the settings. It feels awkward to always go to DB to retrieve something what is essentially a constant
|
||||
|
||||
filter = [].concat(filter || []).map(key => tools.toDbKey(key));
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
|
|
|
@ -12,6 +12,16 @@ let _ = require('../translate')._;
|
|||
let util = require('util');
|
||||
let tableHelpers = require('../table-helpers');
|
||||
|
||||
const Status = {
|
||||
SUBSCRIBED: 1,
|
||||
UNSUBSCRIBED: 2,
|
||||
BOUNCED: 3,
|
||||
COMPLAINED: 4,
|
||||
MAX: 5
|
||||
};
|
||||
|
||||
module.exports.Status = Status;
|
||||
|
||||
module.exports.list = (listId, start, limit, callback) => {
|
||||
listId = Number(listId) || 0;
|
||||
if (!listId) {
|
||||
|
@ -152,19 +162,19 @@ module.exports.insert = (listId, meta, subscriptionData, callback) => {
|
|||
let entryId = existing ? existing.id : false;
|
||||
|
||||
meta.cid = existing ? rows[0].cid : meta.cid;
|
||||
meta.status = meta.status || (existing ? existing.status : 1);
|
||||
meta.status = meta.status || (existing ? existing.status : Status.SUBSCRIBED);
|
||||
|
||||
let statusChange = !existing || existing.status !== meta.status;
|
||||
let statusDirection;
|
||||
|
||||
if (existing && !meta.partial) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, new Error(_('Email address already registered')), callback);
|
||||
if (existing && existing.status === Status.SUBSCRIBED && !meta.partial) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(new Error(_('Email address already registered'))));
|
||||
}
|
||||
|
||||
if (statusChange) {
|
||||
keys.push('status', 'status_change');
|
||||
values.push(meta.status, new Date());
|
||||
statusDirection = !existing ? (meta.status === 1 ? '+' : false) : (existing.status === 1 ? '-' : '+');
|
||||
statusDirection = !existing ? (meta.status === Status.SUBSCRIBED ? '+' : false) : (existing.status === Status.SUBSCRIBED ? '-' : '+');
|
||||
}
|
||||
|
||||
if (!keys.length) {
|
||||
|
@ -458,8 +468,8 @@ module.exports.changeStatus = (listId, id, campaignId, status, callback) => {
|
|||
return helpers.rollbackAndReleaseConnection(connection, () => callback(null, true));
|
||||
}
|
||||
|
||||
if (statusChange && oldStatus === 1 || status === 1) {
|
||||
statusDirection = status === 1 ? '+' : '-';
|
||||
if (statusChange && oldStatus === Status.SUBSCRIBED || status === Status.SUBSCRIBED) {
|
||||
statusDirection = status === Status.SUBSCRIBED ? '+' : '-';
|
||||
}
|
||||
|
||||
connection.query('UPDATE `subscription__' + listId + '` SET `status`=?, `status_change`=NOW() WHERE id=? LIMIT 1', [status, id], err => {
|
||||
|
@ -483,7 +493,7 @@ module.exports.changeStatus = (listId, id, campaignId, status, callback) => {
|
|||
}
|
||||
|
||||
// status change is not related to a campaign or it marks message as bounced etc.
|
||||
if (!campaignId || status > 2) {
|
||||
if (!campaignId || status !== Status.SUBSCRIBED) {
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
|
@ -512,7 +522,7 @@ module.exports.changeStatus = (listId, id, campaignId, status, callback) => {
|
|||
}
|
||||
|
||||
// we should see only unsubscribe events here but you never know
|
||||
connection.query('UPDATE `campaigns` SET `unsubscribed`=`unsubscribed`' + (status === 2 ? '+' : '-') + '1 WHERE `cid`=? LIMIT 1', [campaignId], err => {
|
||||
connection.query('UPDATE `campaigns` SET `unsubscribed`=`unsubscribed`' + (status === Status.UNSUBSCRIBED ? '+' : '-') + '1 WHERE `cid`=? LIMIT 1', [campaignId], err => {
|
||||
if (err) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
@ -583,7 +593,7 @@ module.exports.delete = (listId, cid, callback) => {
|
|||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
if (subscription.status !== 1) {
|
||||
if (subscription.status !== Status.SUBSCRIBED) {
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
|
@ -679,11 +689,10 @@ module.exports.updateImport = (listId, importId, data, callback) => {
|
|||
connection.release();
|
||||
return callback(null, affected);
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
connection.release();
|
||||
return callback(null, affected);
|
||||
}
|
||||
|
||||
connection.release();
|
||||
return callback(null, affected);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
@ -816,7 +825,7 @@ module.exports.updateAddressCheck = (list, cid, emailNew, ip, callback) => {
|
|||
return callback(err);
|
||||
}
|
||||
|
||||
let query = 'SELECT * FROM `subscription__' + list.id + '` WHERE `cid`=? LIMIT 1';
|
||||
let query = 'SELECT * FROM `subscription__' + list.id + '` WHERE `cid`=? AND `status`=' + Status.SUBSCRIBED + ' LIMIT 1';
|
||||
let args = [cid];
|
||||
connection.query(query, args, (err, rows) => {
|
||||
if (err) {
|
||||
|
@ -835,7 +844,7 @@ module.exports.updateAddressCheck = (list, cid, emailNew, ip, callback) => {
|
|||
|
||||
let old = rows[0];
|
||||
|
||||
let query = 'SELECT `id` FROM `subscription__' + list.id + '` WHERE `email`=? AND `cid`<>? AND `status`=1 LIMIT 1';
|
||||
let query = 'SELECT `id` FROM `subscription__' + list.id + '` WHERE `email`=? AND `cid`<>? AND `status`=' + Status.SUBSCRIBED + ' LIMIT 1';
|
||||
let args = [emailNew, cid];
|
||||
connection.query(query, args, (err, rows) => {
|
||||
connection.release();
|
||||
|
@ -870,7 +879,7 @@ module.exports.updateAddress = (listId, subscriptionId, emailNew, callback) => {
|
|||
return callback(err);
|
||||
}
|
||||
|
||||
let query = 'SELECT `id` FROM `subscription__' + listId + '` WHERE `email`=? AND `id`<>? AND `status`=1 LIMIT 1';
|
||||
let query = 'SELECT `id` FROM `subscription__' + listId + '` WHERE `email`=? AND `id`<>? AND `status`=' + Status.SUBSCRIBED + ' LIMIT 1';
|
||||
let args = [emailNew, subscriptionId];
|
||||
connection.query(query, args, (err, rows) => {
|
||||
if (err) {
|
||||
|
@ -878,7 +887,7 @@ module.exports.updateAddress = (listId, subscriptionId, emailNew, callback) => {
|
|||
}
|
||||
|
||||
if (rows && rows.length > 0) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, new Error(_('Email address already registered')), callback);
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(new Error(_('Email address already registered'))));
|
||||
}
|
||||
|
||||
let query = 'DELETE FROM `subscription__' + listId + '` WHERE `email`=? AND `id`<>?';
|
||||
|
@ -888,13 +897,17 @@ module.exports.updateAddress = (listId, subscriptionId, emailNew, callback) => {
|
|||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
let query = 'UPDATE `subscription__' + listId + '` SET `email`=? WHERE `id`=? LIMIT 1';
|
||||
let query = 'UPDATE `subscription__' + listId + '` SET `email`=? WHERE `id`=? AND `status`=' + Status.SUBSCRIBED + ' LIMIT 1';
|
||||
let args = [emailNew, subscriptionId];
|
||||
connection.query(query, args, err => {
|
||||
connection.query(query, args, (err, result) => {
|
||||
if (err) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
}
|
||||
|
||||
if (!result || !result.affectedRows) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(new Error(_('Subscription not found in this list'))));
|
||||
}
|
||||
|
||||
return connection.commit(err => {
|
||||
if (err) {
|
||||
return helpers.rollbackAndReleaseConnection(connection, () => callback(err));
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
const log = require('npmlog');
|
||||
const config = require('config');
|
||||
let db = require('./db');
|
||||
let fields = require('./models/fields');
|
||||
let settings = require('./models/settings');
|
||||
let mailer = require('./mailer');
|
||||
|
@ -28,7 +27,7 @@ function sendSubscriptionConfirmed(list, email, subscription, callback) {
|
|||
unsubscribeUrl: '/subscription/' + list.cid + '/unsubscribe/' + subscription.cid
|
||||
};
|
||||
|
||||
subscriptions.sendMail(list, email, 'subscription-confirmed', _('%s: Subscription Confirmed'), relativeUrls, {}, data.subscriptionData, callback);
|
||||
sendMail(list, email, 'subscription-confirmed', _('%s: Subscription Confirmed'), relativeUrls, {}, subscription, callback);
|
||||
}
|
||||
|
||||
function sendAlreadySubscribed(list, email, subscription, callback) {
|
||||
|
@ -39,7 +38,7 @@ function sendAlreadySubscribed(list, email, subscription, callback) {
|
|||
preferencesUrl: '/subscription/' + list.cid + '/manage/' + subscription.cid,
|
||||
unsubscribeUrl: '/subscription/' + list.cid + '/unsubscribe/' + subscription.cid
|
||||
};
|
||||
module.exports.sendMail(list, email, 'already-subscribed', _('%s: Email Address Already Registered'), relativeUrls, mailOpts, subscription, callback);
|
||||
sendMail(list, email, 'already-subscribed', _('%s: Email Address Already Registered'), relativeUrls, mailOpts, subscription, callback);
|
||||
}
|
||||
|
||||
function sendConfirmAddressChange(list, email, cid, subscription, callback) {
|
||||
|
@ -49,7 +48,7 @@ function sendConfirmAddressChange(list, email, cid, subscription, callback) {
|
|||
const relativeUrls = {
|
||||
confirmUrl: '/subscription/confirm/' + cid
|
||||
};
|
||||
module.exports.sendMail(list, email, 'confirm-address-change', _('%s: Please Confirm Email Change in Subscription'), relativeUrls, mailOpts, subscription, callback);
|
||||
sendMail(list, email, 'confirm-address-change', _('%s: Please Confirm Email Change in Subscription'), relativeUrls, mailOpts, subscription, callback);
|
||||
}
|
||||
|
||||
function sendConfirmSubscription(list, email, cid, subscription, callback) {
|
||||
|
@ -59,7 +58,7 @@ function sendConfirmSubscription(list, email, cid, subscription, callback) {
|
|||
const relativeUrls = {
|
||||
confirmUrl: '/subscription/confirm/' + cid
|
||||
};
|
||||
module.exports.sendMail(list, email, 'confirm-subscription', _('%s: Please Confirm Subscription'), relativeUrls, mailOpts, subscription, callback);
|
||||
sendMail(list, email, 'confirm-subscription', _('%s: Please Confirm Subscription'), relativeUrls, mailOpts, subscription, callback);
|
||||
}
|
||||
|
||||
function sendConfirmUnsubscription(list, email, cid, subscription, callback) {
|
||||
|
@ -69,98 +68,92 @@ function sendConfirmUnsubscription(list, email, cid, subscription, callback) {
|
|||
const relativeUrls = {
|
||||
confirmUrl: '/subscription/confirm/' + cid
|
||||
};
|
||||
module.exports.sendMail(list, email, 'confirm-unsubscription', _('%s: Please Confirm Unsubscription'), relativeUrls, mailOpts, subscription, callback);
|
||||
sendMail(list, email, 'confirm-unsubscription', _('%s: Please Confirm Unsubscription'), relativeUrls, mailOpts, subscription, callback);
|
||||
}
|
||||
|
||||
function sendUnsubscriptionConfirmed(list, email, subscription, callback) {
|
||||
const relativeUrls = {
|
||||
subscribeUrl: '/subscription/' + list.cid + '?cid=' + subscription.cid
|
||||
};
|
||||
subscriptions.sendMail(list, email, 'unsubscription-confirmed', _('%s: Unsubscribe Confirmed'), relativeUrls, {}, subscription, callback);
|
||||
sendMail(list, email, 'unsubscription-confirmed', _('%s: Unsubscription Confirmed'), relativeUrls, {}, subscription, callback);
|
||||
}
|
||||
|
||||
|
||||
function sendMail(list, email, template, subject, relativeUrls, mailOpts, subscription, callback) {
|
||||
db.getConnection((err, connection) => {
|
||||
fields.list(list.id, (err, fieldList) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
fields.list(list.id, (err, fieldList) => {
|
||||
let encryptionKeys = [];
|
||||
fields.getRow(fieldList, subscription).forEach(field => {
|
||||
if (field.type === 'gpg' && field.value) {
|
||||
encryptionKeys.push(field.value.trim());
|
||||
}
|
||||
});
|
||||
|
||||
settings.list(['defaultHomepage', 'defaultFrom', 'defaultAddress', 'defaultPostaddress', 'serviceUrl', 'disableConfirmations'], (err, configItems) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
let encryptionKeys = [];
|
||||
fields.getRow(fieldList, subscription).forEach(field => {
|
||||
if (field.type === 'gpg' && field.value) {
|
||||
encryptionKeys.push(field.value.trim());
|
||||
}
|
||||
});
|
||||
if (!mailOpts.ignoreDisableConfirmations && configItems.disableConfirmations) {
|
||||
return;
|
||||
}
|
||||
|
||||
settings.list(['defaultHomepage', 'defaultFrom', 'defaultAddress', 'defaultPostaddress', 'serviceUrl', 'disableConfirmations'], (err, configItems) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
const data = {
|
||||
title: list.name,
|
||||
homepage: configItems.defaultHomepage || configItems.serviceUrl,
|
||||
contactAddress: configItems.defaultAddress,
|
||||
defaultPostaddress: configItems.defaultPostaddress,
|
||||
};
|
||||
|
||||
if (!mailOpts.ignoreDisableConfirmations && configItems.disableConfirmations) {
|
||||
return;
|
||||
}
|
||||
for (let relativeUrlKey in relativeUrls) {
|
||||
data[relativeUrlKey] = urllib.resolve(configItems.serviceUrl, relativeUrls[relativeUrlKey]);
|
||||
}
|
||||
|
||||
const data = {
|
||||
title: list.name,
|
||||
homepage: configItems.defaultHomepage || configItems.serviceUrl,
|
||||
contactAddress: configItems.defaultAddress,
|
||||
defaultPostaddress: configItems.defaultPostaddress,
|
||||
};
|
||||
|
||||
for (let relativeUrlKey in relativeUrls) {
|
||||
data[relativeUrlKey] = urllib.resolve(configItems.serviceUrl, relativeUrls[relativeUrlKey]);
|
||||
}
|
||||
|
||||
function sendMail(html, text) {
|
||||
mailer.sendMail({
|
||||
from: {
|
||||
name: configItems.defaultFrom,
|
||||
address: configItems.defaultAddress
|
||||
},
|
||||
to: {
|
||||
name: [].concat(subscription.firstName || []).concat(subscription.lastName || []).join(' '),
|
||||
address: email
|
||||
},
|
||||
subject: util.format(subject, list.name),
|
||||
encryptionKeys
|
||||
}, {
|
||||
html,
|
||||
text,
|
||||
data
|
||||
}, err => {
|
||||
if (err) {
|
||||
log.error('Subscription', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let text = {
|
||||
template: 'subscription/mail-' + template + '-text.hbs'
|
||||
};
|
||||
|
||||
let html = {
|
||||
template: 'subscription/mail-' + template + '-html.mjml.hbs',
|
||||
layout: 'subscription/layout.mjml.hbs',
|
||||
type: 'mjml'
|
||||
};
|
||||
|
||||
helpers.injectCustomFormTemplates(list.defaultForm, { text, html }, (err, tmpl) => {
|
||||
function sendMail(html, text) {
|
||||
mailer.sendMail({
|
||||
from: {
|
||||
name: configItems.defaultFrom,
|
||||
address: configItems.defaultAddress
|
||||
},
|
||||
to: {
|
||||
name: [].concat(subscription.firstName || []).concat(subscription.lastName || []).join(' '),
|
||||
address: email
|
||||
},
|
||||
subject: util.format(subject, list.name),
|
||||
encryptionKeys
|
||||
}, {
|
||||
html,
|
||||
text,
|
||||
data
|
||||
}, err => {
|
||||
if (err) {
|
||||
return sendMail(html, text);
|
||||
log.error('Subscription', err);
|
||||
}
|
||||
|
||||
sendMail(tmpl.html, tmpl.text);
|
||||
});
|
||||
}
|
||||
|
||||
return callback();
|
||||
let text = {
|
||||
template: 'subscription/mail-' + template + '-text.hbs'
|
||||
};
|
||||
|
||||
let html = {
|
||||
template: 'subscription/mail-' + template + '-html.mjml.hbs',
|
||||
layout: 'subscription/layout.mjml.hbs',
|
||||
type: 'mjml'
|
||||
};
|
||||
|
||||
helpers.injectCustomFormTemplates(list.defaultForm, { text, html }, (err, tmpl) => {
|
||||
if (err) {
|
||||
return sendMail(html, text);
|
||||
}
|
||||
|
||||
sendMail(tmpl.html, tmpl.text);
|
||||
});
|
||||
|
||||
return callback();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue