mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Refactored unsubsribe url and getemailData methods
no issue
This commit is contained in:
parent
a3802c495d
commit
9a53177544
2 changed files with 15 additions and 16 deletions
|
@ -7,16 +7,16 @@ const bulkEmailService = require('../bulk-email');
|
|||
const models = require('../../models');
|
||||
const postEmailSerializer = require('./post-email-serializer');
|
||||
|
||||
const getEmailData = async (postModel, members = []) => {
|
||||
const getEmailData = async (postModel, recipients = []) => {
|
||||
const emailTmpl = await postEmailSerializer.serialize(postModel);
|
||||
emailTmpl.from = membersService.config.getEmailFromAddress();
|
||||
|
||||
const emails = members.map(member => member.email);
|
||||
const emailData = members.reduce((emailData, member) => {
|
||||
const emails = recipients.map(recipient => recipient.email);
|
||||
const emailData = recipients.reduce((emailData, recipient) => {
|
||||
return Object.assign({
|
||||
[member.email]: {
|
||||
unique_id: member.uuid,
|
||||
unsubscribe_url: postEmailSerializer.createUnsubscribeUrl(member)
|
||||
[recipient.email]: {
|
||||
unique_id: recipient.uuid,
|
||||
unsubscribe_url: postEmailSerializer.createUnsubscribeUrl(recipient.uuid)
|
||||
}
|
||||
}, emailData);
|
||||
}, {});
|
||||
|
@ -35,10 +35,10 @@ const sendEmail = async (postModel, members) => {
|
|||
};
|
||||
|
||||
const sendTestEmail = async (postModel, toEmails) => {
|
||||
const emailList = toEmails.map((email) => {
|
||||
const recipients = toEmails.map((email) => {
|
||||
return {email};
|
||||
});
|
||||
const {emailTmpl, emails, emailData} = await getEmailData(postModel, emailList);
|
||||
const {emailTmpl, emails, emailData} = await getEmailData(postModel, recipients);
|
||||
emailTmpl.subject = `${emailTmpl.subject} [Test]`;
|
||||
return bulkEmailService.send(emailTmpl, emails, emailData);
|
||||
};
|
||||
|
|
|
@ -15,18 +15,17 @@ const getSite = () => {
|
|||
/**
|
||||
* createUnsubscribeUrl
|
||||
*
|
||||
* Takes a member and returns the url that should be used to unsubscribe
|
||||
* In case of no member, generates the preview unsubscribe url - `?preview=1`
|
||||
* 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`
|
||||
*
|
||||
* @param {object} member
|
||||
* @param {string} member.uuid
|
||||
* @param {string} uuid
|
||||
*/
|
||||
const createUnsubscribeUrl = (member) => {
|
||||
const createUnsubscribeUrl = (uuid) => {
|
||||
const siteUrl = urlUtils.getSiteUrl();
|
||||
const unsubscribeUrl = new URL(siteUrl);
|
||||
unsubscribeUrl.pathname = `${unsubscribeUrl.pathname}/unsubscribe/`.replace('//', '/');
|
||||
if (member.uuid) {
|
||||
unsubscribeUrl.searchParams.set('uuid', member.uuid);
|
||||
if (uuid) {
|
||||
unsubscribeUrl.searchParams.set('uuid', uuid);
|
||||
} else {
|
||||
unsubscribeUrl.searchParams.set('preview', '1');
|
||||
}
|
||||
|
@ -59,7 +58,7 @@ const serialize = async (postModel, options = {isBrowserPreview: false}) => {
|
|||
}
|
||||
let htmlTemplate = template({post, site: getSite()});
|
||||
if (options.isBrowserPreview) {
|
||||
const previewUnsubscribeUrl = createUnsubscribeUrl({});
|
||||
const previewUnsubscribeUrl = createUnsubscribeUrl();
|
||||
htmlTemplate = htmlTemplate.replace('%recipient.unsubscribe_url%', previewUnsubscribeUrl);
|
||||
}
|
||||
let juicedHtml = juice(htmlTemplate);
|
||||
|
|
Loading…
Add table
Reference in a new issue