2019-11-05 13:02:23 +03:00
|
|
|
const url = require('url');
|
2019-11-06 11:12:45 +03:00
|
|
|
const moment = require('moment');
|
2019-11-04 13:53:42 +03:00
|
|
|
const common = require('../../lib/common');
|
|
|
|
const api = require('../../api');
|
|
|
|
const membersService = require('../members');
|
|
|
|
const bulkEmailService = require('../bulk-email');
|
|
|
|
const models = require('../../models');
|
2019-11-05 08:14:54 +03:00
|
|
|
const postEmailSerializer = require('./post-email-serializer');
|
2019-11-06 13:46:30 +03:00
|
|
|
const urlUtils = require('../../lib/url-utils');
|
2019-11-04 13:53:42 +03:00
|
|
|
|
2019-11-06 11:12:45 +03:00
|
|
|
const sendEmail = async (post, members) => {
|
2019-11-05 08:14:54 +03:00
|
|
|
const emailTmpl = postEmailSerializer.serialize(post);
|
2019-11-04 13:53:42 +03:00
|
|
|
|
2019-11-06 13:52:45 +03:00
|
|
|
const membersToSendTo = members.filter((member) => {
|
2019-11-05 07:20:07 +03:00
|
|
|
return membersService.contentGating.checkPostAccess(post, member);
|
2019-11-06 13:52:45 +03:00
|
|
|
});
|
|
|
|
const emails = membersToSendTo.map(member => member.email);
|
|
|
|
const emailData = membersToSendTo.reduce((emailData, member) => {
|
|
|
|
return Object.assign({
|
|
|
|
[member.email]: {
|
|
|
|
unsubscribe_url: createUnsubscribeUrl(member)
|
|
|
|
}
|
|
|
|
}, emailData);
|
|
|
|
}, {});
|
2019-11-04 13:53:42 +03:00
|
|
|
|
2019-11-06 13:52:45 +03:00
|
|
|
return bulkEmailService.send(emailTmpl, emails, emailData)
|
2019-11-06 11:12:45 +03:00
|
|
|
.then(() => ({emailTmpl, emails}));
|
2019-11-04 13:53:42 +03:00
|
|
|
};
|
|
|
|
|
2019-11-05 12:09:07 +03:00
|
|
|
const sendTestEmail = async (post, emails) => {
|
|
|
|
const emailTmpl = postEmailSerializer.serialize(post);
|
2019-11-06 15:20:12 +03:00
|
|
|
emailTmpl.subject = `${emailTmpl.subject} [Test]`;
|
2019-11-05 12:09:07 +03:00
|
|
|
return bulkEmailService.send(emailTmpl, emails);
|
|
|
|
};
|
|
|
|
|
2019-11-04 13:53:42 +03:00
|
|
|
// NOTE: serialization is needed to make sure we are using current API and do post transformations
|
|
|
|
// such as image URL transformation from relative to absolute
|
|
|
|
const serialize = async (model) => {
|
2019-11-05 08:30:30 +03:00
|
|
|
const frame = {options: {context: {user: true}}};
|
2019-11-04 13:53:42 +03:00
|
|
|
const apiVersion = model.get('api_version') || 'v3';
|
|
|
|
const docName = 'posts';
|
|
|
|
|
|
|
|
await api.shared
|
|
|
|
.serializers
|
|
|
|
.handle
|
|
|
|
.output(model, {docName: docName, method: 'read'}, api[apiVersion].serializers.output, frame);
|
|
|
|
|
|
|
|
return frame.response[docName][0];
|
|
|
|
};
|
|
|
|
|
2019-11-06 13:46:30 +03:00
|
|
|
/**
|
|
|
|
* createUnsubscribeUrl
|
|
|
|
*
|
|
|
|
* Takes a member and returns the url that should be used to unsubscribe
|
|
|
|
*
|
|
|
|
* @param {object} member
|
|
|
|
* @param {string} member.uuid
|
|
|
|
*/
|
|
|
|
function createUnsubscribeUrl(member) {
|
|
|
|
const siteUrl = urlUtils.getSiteUrl();
|
|
|
|
const unsubscribeUrl = new URL(siteUrl);
|
|
|
|
unsubscribeUrl.searchParams.set('action', 'unsubscribe');
|
|
|
|
unsubscribeUrl.searchParams.set('unsubscribe', member.uuid);
|
|
|
|
|
|
|
|
return unsubscribeUrl.href;
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:02:23 +03:00
|
|
|
/**
|
|
|
|
* handleUnsubscribeRequest
|
|
|
|
*
|
|
|
|
* Takes a request/response pair and reads the `unsubscribe` query parameter,
|
|
|
|
* using the content to update the members service to set the `subscribed` flag
|
|
|
|
* to false on the member
|
|
|
|
*
|
|
|
|
* If any operation fails, or the request is invalid the function will error - so using
|
|
|
|
* as middleware should consider wrapping with `try/catch`
|
|
|
|
*
|
|
|
|
* @param {Request} req
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async function handleUnsubscribeRequest(req) {
|
|
|
|
if (!req.url) {
|
|
|
|
throw new common.errors.BadRequestError({
|
|
|
|
message: 'Expected unsubscribe param containing token'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const {query} = url.parse(req.url, true);
|
|
|
|
if (!query || !query.unsubscribe) {
|
|
|
|
throw new common.errors.BadRequestError({
|
|
|
|
message: 'Expected unsubscribe param containing token'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const member = await membersService.api.members.get({
|
|
|
|
uuid: query.unsubscribe
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!member) {
|
|
|
|
throw new common.errors.BadRequestError({
|
|
|
|
message: 'Expected valid subscribe param - could not find member'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await membersService.api.members.update({subscribed: false}, {id: member.id});
|
|
|
|
} catch (err) {
|
|
|
|
throw new common.errors.InternalServerError({
|
|
|
|
message: 'Failed to unsubscribe member'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 13:53:42 +03:00
|
|
|
async function listener(model, options) {
|
|
|
|
// CASE: do not send email if we import a database
|
|
|
|
// TODO: refactor post.published events to never fire on importing
|
|
|
|
if (options && options.importing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 07:28:16 +03:00
|
|
|
if (!model.get('send_email_when_published')) {
|
2019-11-04 13:53:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 07:20:03 +03:00
|
|
|
const post = await serialize(model);
|
|
|
|
|
2019-11-04 13:53:42 +03:00
|
|
|
const deliveredEvents = await models.Action.findAll({
|
|
|
|
filter: `event:delivered+resource_id:${model.id}`
|
|
|
|
});
|
|
|
|
|
|
|
|
if (deliveredEvents && deliveredEvents.toJSON().length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-06 11:12:45 +03:00
|
|
|
const {members} = await membersService.api.members.list(Object.assign({filter: 'subscribed:true'}, {limit: 'all'}));
|
|
|
|
|
|
|
|
if (!members.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendEmail(post, members)
|
|
|
|
.then(async ({emailTmpl, emails}) => {
|
|
|
|
return models.Email.add({
|
|
|
|
post_id: post.id,
|
|
|
|
status: 'sent',
|
|
|
|
email_count: emails.length,
|
|
|
|
subject: emailTmpl.subject,
|
|
|
|
html: emailTmpl.html,
|
|
|
|
plaintext: emailTmpl.plaintext,
|
|
|
|
submitted_at: moment().toDate()
|
|
|
|
}, {context: {internal: true}});
|
|
|
|
})
|
|
|
|
.then(async () => {
|
|
|
|
let actor = {id: null, type: null};
|
|
|
|
if (options.context && options.context.user) {
|
|
|
|
actor = {
|
|
|
|
id: options.context.user,
|
|
|
|
type: 'user'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const action = {
|
|
|
|
event: 'delivered',
|
|
|
|
resource_id: model.id,
|
|
|
|
resource_type: 'post',
|
|
|
|
actor_id: actor.id,
|
|
|
|
actor_type: actor.type
|
2019-11-04 13:53:42 +03:00
|
|
|
};
|
2019-11-06 11:12:45 +03:00
|
|
|
return models.Action.add(action, {context: {internal: true}});
|
|
|
|
});
|
2019-11-04 13:53:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function listen() {
|
|
|
|
common.events.on('post.published', listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public API
|
|
|
|
module.exports = {
|
2019-11-05 12:09:07 +03:00
|
|
|
listen,
|
2019-11-05 13:02:23 +03:00
|
|
|
sendTestEmail,
|
2019-11-06 13:46:30 +03:00
|
|
|
handleUnsubscribeRequest,
|
|
|
|
createUnsubscribeUrl
|
2019-11-04 13:53:42 +03:00
|
|
|
};
|