diff --git a/cms/config/functions/cron.js b/cms/config/functions/cron.js index 5bea899c..55ffc8db 100644 --- a/cms/config/functions/cron.js +++ b/cms/config/functions/cron.js @@ -20,7 +20,7 @@ module.exports = { // // } '*/5 * * * *': () => { - getLatestCommunityActivity(); + getLatestCommunityActivity(15); }, '*/60 * * * * *': () => { getCommunityContributors('https://gsoc.rocket.chat/api/data','rocketChat','Rocket.Chat'); diff --git a/cms/config/functions/fetchTopPosts.js b/cms/config/functions/fetchTopPosts.js index b5edeb67..a17be7c4 100644 --- a/cms/config/functions/fetchTopPosts.js +++ b/cms/config/functions/fetchTopPosts.js @@ -1,8 +1,8 @@ const axios = require("axios"); -module.exports.getLatestCommunityActivity = async () => { +module.exports.getLatestCommunityActivity = async (maximumPostCount) => { // only run if env var is set, and don't break server - if ('DISCOURSE_DOMAIN' in Object.keys(process.env)) { + if ("DISCOURSE_DOMAIN" in Object.keys(process.env)) { const TopPost = await axios({ url: `${process.env.DISCOURSE_DOMAIN}/top.json?period=all`, method: "GET", @@ -11,14 +11,23 @@ module.exports.getLatestCommunityActivity = async () => { "Api-Key": process.env.DISCOURSE_API_KEY, }, }); - let currentTopPost = await strapi.query("discourse").find(); - if (currentTopPost.length !== 0) { - await strapi.query("discourse").update( - { id: currentTopPost[0].id }, - { - TopPost: TopPost.data, + let currentTopPosts = await strapi.query("discourse").find(); + if (currentTopPosts.length !== 0) { + let excessPostsCount = currentTopPost.length - maximumPostCount + 1; + if ( + JSON.stringify(currentTopPosts[currentTopPosts.length - 1].TopPost) !== + JSON.stringify(TopPost.data) + ) { + for (let post of currentTopPosts) { + if (excessPostsCount > 0) { + await strapi.query("discourse").delete({ id: post.id }); + excessPostsCount -= 1; + } } - ); + await strapi.query("discourse").create({ + TopPost: TopPost.data, + }); + } } else { await strapi.query("discourse").create({ TopPost: TopPost.data,