Some fixes in RSS feed processing.

It is now possible to have hierarchical merge tags (separated by "."). The merge tags are now case sensitive.
Mailtrain allows passing element "mt:entries-json" in the RSS item feed. If this is detected, it parses the json structure and makes it available through RSS_ENTRY_CUSTOM_TAGS. Then it can be used as [RSS_ENTRY_CUSTOM_TAGS.field_quote_text.rendered]
This commit is contained in:
Tomas Bures 2018-12-29 11:21:25 +01:00
parent 6eead89fef
commit e786964411
5 changed files with 36 additions and 21 deletions

View file

@ -262,7 +262,8 @@ class CampaignSender {
tags['RSS_ENTRY_LINK'] = rssEntry.link;
tags['RSS_ENTRY_CONTENT'] = rssEntry.content;
tags['RSS_ENTRY_SUMMARY'] = rssEntry.summary;
tags['RSS_ENTRY_IMAGE_URL'] = rssEntry.image_url;
tags['RSS_ENTRY_IMAGE_URL'] = rssEntry.imageUrl;
tags['RSS_ENTRY_CUSTOM_TAGS'] = rssEntry.customTags;
}
return tags;
@ -311,6 +312,7 @@ class CampaignSender {
const sendConfiguration = this.sendConfiguration;
const {html, text, attachments} = await this._getMessage(campaign, list, subscriptionGrouped, mergeTags, true);
console.log(html);
const campaignAddress = [campaign.cid, list.cid, subscriptionGrouped.cid].join('.');

View file

@ -150,23 +150,31 @@ function validateEmailGetMessage(result, address, language) {
function formatMessage(campaign, list, subscription, mergeTags, message, isHTML) {
const links = getMessageLinks(campaign, list, subscription);
const getValue = key => {
key = (key || '').toString().toUpperCase().trim();
if (links.hasOwnProperty(key)) {
return links[key];
const getValue = fullKey => {
const keys = (fullKey || '').split('.');
if (links.hasOwnProperty(keys[0])) {
return links[keys[0]];
}
if (mergeTags.hasOwnProperty(key)) {
const value = (mergeTags[key] || '').toString();
const containsHTML = /<[a-z][\s\S]*>/.test(value);
return isHTML ? he.encode((containsHTML ? value : value.replace(/(?:\r\n|\r|\n)/g, '<br/>')), {
useNamedReferences: true,
allowUnsafeSymbols: true
}) : (containsHTML ? htmlToText.fromString(value) : value);
let value = mergeTags;
while (keys.length > 0) {
let key = keys.shift();
if (value.hasOwnProperty(key)) {
value = value[key];
} else {
return false;
}
}
return false;
const containsHTML = /<[a-z][\s\S]*>/.test(value);
return isHTML ? he.encode((containsHTML ? value : value.replace(/(?:\r\n|\r|\n)/g, '<br/>')), {
useNamedReferences: true,
allowUnsafeSymbols: true
}) : (containsHTML ? htmlToText.fromString(value) : value);
};
return message.replace(/\[([a-z0-9_]+)(?:\/([^\]]+))?\]/ig, (match, identifier, fallback) => {
return message.replace(/\[([a-z0-9_.]+)(?:\/([^\]]+))?\]/ig, (match, identifier, fallback) => {
let value = getValue(identifier);
if (value === false) {
return match;

View file

@ -186,7 +186,7 @@ fieldTypes['radio-enum'] = {
cardinality: Cardinality.SINGLE,
getHbsType: field => 'typeRadioEnum',
render: (field, value) => {
const fld = field.groupedOptions[value];
const fld = field.settings.options[value];
return fld ? fld.name : '';
}
};
@ -203,7 +203,7 @@ fieldTypes['dropdown-enum'] = {
cardinality: Cardinality.SINGLE,
getHbsType: field => 'typeDropdownEnum',
render: (field, value) => {
const fld = field.groupedOptions[value];
const fld = field.settings.options[value];
return fld ? fld.name : '';
}
};

View file

@ -863,7 +863,7 @@
"dependencies": {
"iconv-lite": {
"version": "0.4.15",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.15.tgz",
"resolved": "http://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.15.tgz",
"integrity": "sha1-/iZaIYrGpXz+hUkn6dBMGYJe3es="
},
"libmime": {

View file

@ -19,7 +19,7 @@ let running = false;
async function fetch(url) {
const httpOptions = {
uri: 'http://feeds.feedwrench.com/JavaScriptJabber.rss',
uri: url,
headers: {
'user-agent': 'Mailtrain',
'accept': 'text/html,application/xhtml+xml'
@ -37,8 +37,13 @@ async function fetch(url) {
link: item.link,
content: item.description || item.summary,
summary: item.summary || item.description,
image_url: item.image.url
imageUrl: item.image.url,
};
if ('mt:entries-json' in item) {
entry.customTags = JSON.parse(item['mt:entries-json']['#'])
}
entries.push(entry);
}
@ -140,7 +145,7 @@ async function run() {
rssCampaign.data.checkStatus = checkStatus;
await knex('campaigns').where('id', rssCampaign.id).update({
last_check: Date.now(),
last_check: new Date(),
data: JSON.stringify(rssCampaign.data)
});
@ -148,7 +153,7 @@ async function run() {
log.error('Feed', err.message);
rssCampaign.data.checkStatus = err.message;
await knex('campaigns').where('id', rssCampaign.id).update({
last_check: Date.now(),
last_check: new Date(),
data: JSON.stringify(rssCampaign.data)
});
}