Skip to content

Commit

Permalink
feat: #6594, add top/popular sections to digest email (#8709)
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 2, 2020
1 parent 55d5ccc commit e60357d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 23 deletions.
2 changes: 2 additions & 0 deletions public/language/en-GB/email.json
Expand Up @@ -29,6 +29,8 @@
"reset.notify.text2": "If you did not authorise this, please notify an administrator immediately.",

"digest.latest_topics": "Latest topics from %1",
"digest.top-topics": "Top topics from %1",
"digest.popular-topics": "Popular topics from %1",
"digest.cta": "Click here to visit %1",
"digest.unsub.info": "This digest was sent to you due to your subscription settings.",
"digest.day": "day",
Expand Down
50 changes: 27 additions & 23 deletions src/user/digest.js
Expand Up @@ -102,17 +102,19 @@ Digest.send = async function (data) {

await async.eachLimit(data.subscribers, 100, async function (uid) {
const userObj = await user.getUserFields(uid, ['uid', 'username', 'userslug', 'lastonline']);
let [notifications, topicsData] = await Promise.all([
const [notifications, topTopics, popularTopics, recentTopics] = await Promise.all([
user.notifications.getUnreadInterval(userObj.uid, data.interval),
getTermTopics(data.interval, userObj.uid, 0, 9),
getTermTopics(data.interval, userObj.uid, 0, 9, 'votes'),
getTermTopics(data.interval, userObj.uid, 0, 9, 'posts'),
getTermTopics(data.interval, userObj.uid, 0, 9, 'recent'),
]);
notifications = notifications.filter(Boolean);
const unreadNotifs = notifications.filter(Boolean);
// If there are no notifications and no new topics, don't bother sending a digest
if (!notifications.length && !topicsData.length) {
if (!unreadNotifs.length && !topTopics.length && !popularTopics.length && !recentTopics.length) {
return;
}

notifications.forEach(function (n) {
unreadNotifs.forEach(function (n) {
if (n.image && !n.image.startsWith('http')) {
n.image = nconf.get('base_url') + n.image;
}
Expand All @@ -121,23 +123,17 @@ Digest.send = async function (data) {
}
});

// Fix relative paths in topic data
topicsData = topicsData.map(function (topicObj) {
const user = topicObj.hasOwnProperty('teaser') && topicObj.teaser && topicObj.teaser.user ? topicObj.teaser.user : topicObj.user;
if (user && user.picture && utils.isRelativeUrl(user.picture)) {
user.picture = nconf.get('base_url') + user.picture;
}
return topicObj;
});
emailsSent += 1;
const now = new Date();
try {
await emailer.send('digest', userObj.uid, {
subject: '[[email:digest.subject, ' + (now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate()) + ']]',
username: userObj.username,
userslug: userObj.userslug,
notifications: notifications,
recent: topicsData,
notifications: unreadNotifs,
recent: recentTopics,
topTopics: topTopics,
popularTopics: popularTopics,
interval: data.interval,
showUnsubscribe: true,
});
Expand Down Expand Up @@ -179,22 +175,30 @@ Digest.getDeliveryTimes = async (start, stop) => {
};
};

async function getTermTopics(term, uid, start, stop) {
async function getTermTopics(term, uid, start, stop, sort) {
const options = {
uid: uid,
start: start,
stop: stop,
term: term,
sort: 'posts',
sort: sort,
teaserPost: 'last-post',
};
let data = await topics.getSortedTopics(options);
if (!data.topics.length) {
data = await topics.getLatestTopics(options);
}
const data = sort === 'recent' ?
await topics.getLatestTopics(options) :
await topics.getSortedTopics(options);

data.topics.forEach(function (topicObj) {
if (topicObj && topicObj.teaser && topicObj.teaser.content && topicObj.teaser.content.length > 255) {
topicObj.teaser.content = topicObj.teaser.content.slice(0, 255) + '...';
if (topicObj) {
if (topicObj.teaser && topicObj.teaser.content && topicObj.teaser.content.length > 255) {
topicObj.teaser.content = topicObj.teaser.content.slice(0, 255) + '...';
}
// Fix relative paths in topic data
const user = topicObj.hasOwnProperty('teaser') && topicObj.teaser && topicObj.teaser.user ?
topicObj.teaser.user : topicObj.user;
if (user && user.picture && utils.isRelativeUrl(user.picture)) {
user.picture = nconf.get('base_url') + user.picture;
}
}
});
return data.topics.filter(topic => topic && !topic.deleted);
Expand Down
70 changes: 70 additions & 0 deletions src/views/emails/digest.tpl
Expand Up @@ -43,6 +43,76 @@
</td>
</tr>
<!-- ENDIF notifications.length -->
<!-- IF topTopics.length -->
<tr>
<td style="padding: 0px 40px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; font-size: 15px; line-height: 20px; color: #555555;">
<h1 style="margin: 16px 0 24px 0; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; font-size: 18px; line-height: 21px; color: #aaaaaa; font-weight: normal;">[[email:digest.top-topics, {site_title}]]</h1>
<ul style="margin: 0; padding: 0;">
<!-- BEGIN topTopics -->
<li style="text-decoration: none; list-style-type: none; padding-bottom: 0.5em;">
<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td style="padding: 6px 16px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; width: 32px; vertical-align: middle;">{function.renderDigestAvatar}</td>
<td style="padding: 6px 16px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; line-height: 16px; color: #333333;">
<p style="margin: 0;"><a style="text-decoration:none !important; text-decoration:none; color: #333333;" href="{url}/topic/{topTopics.slug}"><strong>{topTopics.title}</strong></a></p>
<p style="margin: 0; font-size: 12px;"><a style="text-decoration:none !important; text-decoration:none; color: #aaaaaa; line-height: 16px;" href="{url}/uid/{topTopics.teaser.user.uid}"><strong>{topTopics.teaser.user.username}</strong></a></p>
</td>
</tr>
<tr>
<td colspan="2" style="padding: 8px 16px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; line-height: 16px; color: #333333;">
<p style="margin: 0; padding: 6px 0px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; font-size: 13px; line-height: 26px; color: #666666;">{topTopics.teaser.content}</p>
<p style="margin: 0; padding: 6px 0px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; line-height: 16px;">
<a style="text-decoration:none !important; text-decoration:none; text-transform: capitalize; color: #666666; line-height: 16px;" href="{url}/topic/{recent.slug}">
<small><strong><span style="color: #aaaaaa;">&rsaquo;</span> [[global:read_more]]</strong></small>
</a>
</p>
</td>
</tr>
</table>
</li>
<!-- IF !@last -->
<li style="text-decoration: none; list-style-type: none; margin: 0px 64px 16px 64px; border-bottom: 1px solid #dddddd"></li>
<!-- END -->
<!-- END -->
</ul>
</td>
</tr>
<!-- END -->
<!-- IF popularTopics.length -->
<tr>
<td style="padding: 0px 40px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; font-size: 15px; line-height: 20px; color: #555555;">
<h1 style="margin: 16px 0 24px 0; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; font-size: 18px; line-height: 21px; color: #aaaaaa; font-weight: normal;">[[email:digest.popular-topics, {site_title}]]</h1>
<ul style="margin: 0; padding: 0;">
<!-- BEGIN popularTopics -->
<li style="text-decoration: none; list-style-type: none; padding-bottom: 0.5em;">
<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td style="padding: 6px 16px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; width: 32px; vertical-align: middle;">{function.renderDigestAvatar}</td>
<td style="padding: 6px 16px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; line-height: 16px; color: #333333;">
<p style="margin: 0;"><a style="text-decoration:none !important; text-decoration:none; color: #333333;" href="{url}/topic/{popularTopics.slug}"><strong>{popularTopics.title}</strong></a></p>
<p style="margin: 0; font-size: 12px;"><a style="text-decoration:none !important; text-decoration:none; color: #aaaaaa; line-height: 16px;" href="{url}/uid/{popularTopics.teaser.user.uid}"><strong>{popularTopics.teaser.user.username}</strong></a></p>
</td>
</tr>
<tr>
<td colspan="2" style="padding: 8px 16px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; line-height: 16px; color: #333333;">
<p style="margin: 0; padding: 6px 0px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; font-size: 13px; line-height: 26px; color: #666666;">{popularTopics.teaser.content}</p>
<p style="margin: 0; padding: 6px 0px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; line-height: 16px;">
<a style="text-decoration:none !important; text-decoration:none; text-transform: capitalize; color: #666666; line-height: 16px;" href="{url}/topic/{recent.slug}">
<small><strong><span style="color: #aaaaaa;">&rsaquo;</span> [[global:read_more]]</strong></small>
</a>
</p>
</td>
</tr>
</table>
</li>
<!-- IF !@last -->
<li style="text-decoration: none; list-style-type: none; margin: 0px 64px 16px 64px; border-bottom: 1px solid #dddddd"></li>
<!-- END -->
<!-- END -->
</ul>
</td>
</tr>
<!-- END -->
<!-- IF recent.length -->
<tr>
<td style="padding: 0px 40px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; font-size: 15px; line-height: 20px; color: #555555;">
Expand Down

0 comments on commit e60357d

Please sign in to comment.