@sugarcube/plugin-mail
Version:
Send email notifications.
85 lines (67 loc) • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fp = require("lodash/fp");
var _dashp = require("dashp");
var _dot = _interopRequireDefault(require("dot"));
var _core = require("@sugarcube/core");
var _utils = require("../utils");
var _assertions = require("../assertions");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_dot.default.log = false;
const dots = _dot.default.process({
path: `${__dirname}/../../views`,
templateSettings: {
strip: false
}
});
const querySource = "mail_recipient";
const mailDiffStats = (envelope, {
cfg,
log,
stats
}) => {
const recipients = _core.envelope.queriesByType(querySource, envelope); // We only allow one sender.
const sender = cfg.mail.from;
const noEncrypt = (0, _fp.get)("mail.no_encrypt", cfg);
const {
added,
removed,
shared,
meta
} = stats.get("diff");
if (added.count === 0 && removed.count === 0) {
log.info("No new stats. Skipping mailing.");
return Promise.resolve(envelope);
}
const subject = "Message from SugarCube.";
const body = dots.diff_stats({
recipients,
added,
removed,
shared,
meta
});
const transporter = (0, _utils.createTransporter)(cfg.mail);
if (cfg.mail.debug) log.info(["Email text:", "", body].join("\n"));
return Promise.all(recipients.map(recipient => {
log.info(`Mailing diff stats to ${recipient}.`);
return (0, _dashp.flowP)([to => (0, _utils.mail)(transporter, sender, to, body, subject, !noEncrypt), (0, _dashp.tapP)(info => {
if (cfg.mail.debug) {
log.info(["Emailing the following:", "", info.message.toString()].join("\n"));
} else {
log.info(`Accepted mail for: ${info.accepted.join(", ")}`);
}
}), (0, _dashp.caughtP)(e => {
log.warn(`Failed to send to ${recipient}.`);
log.warn(e);
})], recipient);
})).then(() => envelope);
};
const plugin = _core.plugin.liftManyA2([_assertions.assertFrom, mailDiffStats]);
plugin.desc = "Mail diff stats to one or more recipient.";
plugin.argv = {};
var _default = plugin;
exports.default = _default;