2019-11-05 08:14:54 +03:00
|
|
|
const juice = require('juice');
|
|
|
|
const template = require('./template');
|
|
|
|
const settingsCache = require('../../services/settings/cache');
|
|
|
|
const urlUtils = require('../../lib/url-utils');
|
2019-11-06 08:06:24 +03:00
|
|
|
const moment = require('moment');
|
2019-11-15 08:55:46 +03:00
|
|
|
const cheerio = require('cheerio');
|
2019-11-26 19:07:04 +03:00
|
|
|
const api = require('../../api');
|
2019-11-27 10:48:44 +03:00
|
|
|
const {URL} = require('url');
|
2019-11-05 08:14:54 +03:00
|
|
|
|
|
|
|
const getSite = () => {
|
|
|
|
return Object.assign({}, settingsCache.getPublic(), {
|
|
|
|
url: urlUtils.urlFor('home', true)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-11-26 19:07:04 +03:00
|
|
|
/**
|
|
|
|
* createUnsubscribeUrl
|
|
|
|
*
|
2019-11-27 08:28:21 +03:00
|
|
|
* Takes a member uuid and returns the url that should be used to unsubscribe
|
|
|
|
* In case of no member uuid, generates the preview unsubscribe url - `?preview=1`
|
2019-11-26 19:07:04 +03:00
|
|
|
*
|
2019-11-27 08:28:21 +03:00
|
|
|
* @param {string} uuid
|
2019-11-26 19:07:04 +03:00
|
|
|
*/
|
2019-11-27 08:28:21 +03:00
|
|
|
const createUnsubscribeUrl = (uuid) => {
|
2019-11-26 19:07:04 +03:00
|
|
|
const siteUrl = urlUtils.getSiteUrl();
|
|
|
|
const unsubscribeUrl = new URL(siteUrl);
|
|
|
|
unsubscribeUrl.pathname = `${unsubscribeUrl.pathname}/unsubscribe/`.replace('//', '/');
|
2019-11-27 08:28:21 +03:00
|
|
|
if (uuid) {
|
|
|
|
unsubscribeUrl.searchParams.set('uuid', uuid);
|
2019-11-26 19:07:04 +03:00
|
|
|
} else {
|
|
|
|
unsubscribeUrl.searchParams.set('preview', '1');
|
|
|
|
}
|
|
|
|
|
|
|
|
return unsubscribeUrl.href;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 serializePostModel = async (model) => {
|
2019-11-27 08:27:48 +03:00
|
|
|
const frame = {options: {context: {user: true}, formats: 'html, plaintext'}};
|
2019-11-26 19:07:04 +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];
|
|
|
|
};
|
|
|
|
|
|
|
|
const serialize = async (postModel, options = {isBrowserPreview: false}) => {
|
|
|
|
const post = await serializePostModel(postModel);
|
2019-11-06 08:06:24 +03:00
|
|
|
post.published_at = post.published_at ? moment(post.published_at).format('DD MMM YYYY') : moment().format('DD MMM YYYY');
|
2019-11-06 14:03:28 +03:00
|
|
|
post.authors = post.authors && post.authors.map(author => author.name).join(',');
|
2019-11-07 07:15:16 +03:00
|
|
|
post.html = post.html || '';
|
2019-11-06 14:32:11 +03:00
|
|
|
if (post.posts_meta) {
|
|
|
|
post.email_subject = post.posts_meta.email_subject;
|
|
|
|
}
|
2019-11-26 19:07:04 +03:00
|
|
|
let htmlTemplate = template({post, site: getSite()});
|
|
|
|
if (options.isBrowserPreview) {
|
2019-11-27 08:28:21 +03:00
|
|
|
const previewUnsubscribeUrl = createUnsubscribeUrl();
|
2019-11-26 19:07:04 +03:00
|
|
|
htmlTemplate = htmlTemplate.replace('%recipient.unsubscribe_url%', previewUnsubscribeUrl);
|
|
|
|
}
|
|
|
|
let juicedHtml = juice(htmlTemplate);
|
2019-11-15 08:55:46 +03:00
|
|
|
// Force all links to open in new tab
|
|
|
|
let _cheerio = cheerio.load(juicedHtml);
|
|
|
|
_cheerio('a').attr('target','_blank');
|
|
|
|
juicedHtml = _cheerio.html();
|
2019-11-05 08:14:54 +03:00
|
|
|
return {
|
|
|
|
subject: post.email_subject || post.title,
|
2019-11-15 08:55:46 +03:00
|
|
|
html: juicedHtml,
|
2019-11-05 11:04:48 +03:00
|
|
|
plaintext: post.plaintext
|
2019-11-05 08:14:54 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2019-11-26 19:07:04 +03:00
|
|
|
serialize,
|
|
|
|
createUnsubscribeUrl
|
2019-11-05 08:14:54 +03:00
|
|
|
};
|