diff --git a/public/language/en-GB/email.json b/public/language/en-GB/email.json index cb2fd70dfa16..78d6c5c00723 100644 --- a/public/language/en-GB/email.json +++ b/public/language/en-GB/email.json @@ -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", diff --git a/src/user/digest.js b/src/user/digest.js index 7f7044c901b2..f931a183de05 100644 --- a/src/user/digest.js +++ b/src/user/digest.js @@ -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; } @@ -121,14 +123,6 @@ 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 { @@ -136,8 +130,10 @@ Digest.send = async function (data) { 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, }); @@ -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); diff --git a/src/views/emails/digest.tpl b/src/views/emails/digest.tpl index 264d632c4911..0cfd293b46a4 100644 --- a/src/views/emails/digest.tpl +++ b/src/views/emails/digest.tpl @@ -43,6 +43,76 @@ + + + +

[[email:digest.top-topics, {site_title}]]

+ + + + + + + +

[[email:digest.popular-topics, {site_title}]]

+ + + +