0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00
ghost/core/server/services/mega.js
2019-11-04 16:07:33 +07:00

61 lines
1.7 KiB
JavaScript

const common = require('../lib/common');
const membersService = require('./members');
const bulkEmailService = require('./bulk-email');
const models = require('../models');
const sendEmail = async (post) => {
const emailTmpl = {
subject: post.posts_meta.email_subject || post.title,
html: post.html
};
const {members} = await membersService.api.members.list();
const emails = members.map(m => m.email);
return bulkEmailService.send(emailTmpl, emails);
};
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;
}
if (!model.get('send_email_when_published')) {
return;
}
sendEmail(model.toJSON()).then(async () => {
const deliveredEvents = await models.Action.findAll({
filter: `event:delivered+resource_id:${model.id}`
});
if (deliveredEvents && deliveredEvents.toJSON().length > 0) {
return;
}
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
};
return models.Action.add(action, {context: {internal: true}});
});
}
function listen() {
common.events.on('post.published', listener);
}
// Public API
module.exports = {
listen: listen
};