perf: single call to get digest topics, dont send duplicate topics

v1.18.x
Barış Soner Uşaklı 4 years ago
parent 86e911ba4b
commit 5ce2820799

@ -106,15 +106,13 @@ Digest.send = async function (data) {
return; return;
} }
await Promise.all(userData.map(async (userObj) => { await Promise.all(userData.map(async (userObj) => {
const [notifications, topTopics, popularTopics, recentTopics] = await Promise.all([ const [notifications, topics] = await Promise.all([
user.notifications.getUnreadInterval(userObj.uid, data.interval), user.notifications.getUnreadInterval(userObj.uid, data.interval),
getTermTopics(data.interval, userObj.uid, 0, 9, 'votes'), getTermTopics(data.interval, userObj.uid),
getTermTopics(data.interval, userObj.uid, 0, 9, 'posts'),
getTermTopics(data.interval, userObj.uid, 0, 9, 'recent'),
]); ]);
const unreadNotifs = notifications.filter(Boolean); const unreadNotifs = notifications.filter(Boolean);
// If there are no notifications and no new topics, don't bother sending a digest // If there are no notifications and no new topics, don't bother sending a digest
if (!unreadNotifs.length && !topTopics.length && !popularTopics.length && !recentTopics.length) { if (!unreadNotifs.length && !topics.top.length && !topics.popular.length && !topics.recent.length) {
return; return;
} }
@ -134,9 +132,9 @@ Digest.send = async function (data) {
username: userObj.username, username: userObj.username,
userslug: userObj.userslug, userslug: userObj.userslug,
notifications: unreadNotifs, notifications: unreadNotifs,
recent: recentTopics, recent: topics.recent,
topTopics: topTopics, topTopics: topics.top,
popularTopics: popularTopics, popularTopics: topics.popular,
interval: data.interval, interval: data.interval,
showUnsubscribe: true, showUnsubscribe: true,
}).catch(err => winston.error(`[user/jobs] Could not send digest email\n[emailer.send] ${err.stack}`)); }).catch(err => winston.error(`[user/jobs] Could not send digest email\n[emailer.send] ${err.stack}`));
@ -179,20 +177,32 @@ Digest.getDeliveryTimes = async (start, stop) => {
}; };
}; };
async function getTermTopics(term, uid, start, stop, sort) { async function getTermTopics(term, uid) {
const options = { const data = await topics.getSortedTopics({
uid: uid, uid: uid,
start: start, start: 0,
stop: stop, stop: 199,
term: term, term: term,
sort: sort, sort: 'votes',
teaserPost: 'last-post', teaserPost: 'first',
}; });
const data = sort === 'recent' ? data.topics = data.topics.filter(topic => topic && !topic.deleted);
await topics.getLatestTopics(options) :
await topics.getSortedTopics(options); const top = data.topics.filter(t => t.votes > 0).slice(0, 10);
const topTids = top.map(t => t.tid);
const popular = data.topics
.filter(t => t.postcount > 1 && !topTids.includes(t.tid))
.sort((a, b) => b.postcount - a.postcount)
.slice(0, 10);
const popularTids = popular.map(t => t.tid);
const recent = data.topics
.filter(t => !topTids.includes(t.tid) && !popularTids.includes(t.tid))
.sort((a, b) => b.lastposttime - a.lastposttime)
.slice(0, 10);
data.topics.forEach((topicObj) => { [...top, ...popular, ...recent].forEach((topicObj) => {
if (topicObj) { if (topicObj) {
if (topicObj.teaser && topicObj.teaser.content && topicObj.teaser.content.length > 255) { if (topicObj.teaser && topicObj.teaser.content && topicObj.teaser.content.length > 255) {
topicObj.teaser.content = `${topicObj.teaser.content.slice(0, 255)}...`; topicObj.teaser.content = `${topicObj.teaser.content.slice(0, 255)}...`;
@ -205,5 +215,5 @@ async function getTermTopics(term, uid, start, stop, sort) {
} }
} }
}); });
return data.topics.filter(topic => topic && !topic.deleted); return { top, popular, recent };
} }

Loading…
Cancel
Save