social-butterfly
Version:
Incorporate federated social network protocols easily. Used with Hello, world federated blog.
61 lines (52 loc) • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.comment = comment;
exports.mention = mention;
exports.follow = follow;
var _url_factory = require("./util/url_factory");
var _nodemailer = _interopRequireDefault(require("nodemailer"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// TODO(mime): translate everything here.
async function send(req, subject, to, fromEmail, html) {
try {
// Setup to work with postfix currently. Tweak as necessary.
const transporter = _nodemailer.default.createTransport({
host: 'localhost',
port: 25,
tls: {
rejectUnauthorized: false
}
});
const me = `no-reply@${req.get('host')}`;
await transporter.sendMail({
from: `"Hello, world." <${me}>`,
to,
'reply-to': fromEmail?.indexOf('@') !== -1 ? fromEmail : undefined,
subject,
html
});
} catch (ex) {
console.log('efail', ex);
}
}
function comment(req, fromUsername, fromEmail, toEmail, contentUrl, comment) {
fromEmail = fromEmail.indexOf('@') !== -1 ? fromEmail : undefined;
send(req, `${fromUsername} made a comment on your post.`, toEmail, fromEmail, `<a href="${contentUrl}">View the post here.</a>`);
}
function mention(req, fromUsername, fromEmail, toEmail, remoteUrl) {
send(req, `${fromUsername} made a post mentioning you in it!`, toEmail, fromEmail, `<a href="${remoteUrl}">View the post here.</a>`);
}
function follow(req, fromUsername, toEmail, blogUrl) {
const followUrl = (0, _url_factory.buildUrl)({
req,
pathname: '/api/social/follow',
searchParams: {
resource: blogUrl
}
});
send(req, `${fromUsername} started following you!`, toEmail, undefined
/* fromEmail */
, `<a href="${blogUrl}">View their blog.</a><br/>` + `If you like their stuff you can follow them by <a href="${followUrl}">clicking here</a>.`);
}