From f8007fec0d3f68b649f7df1ae7f79897375a71dc Mon Sep 17 00:00:00 2001 From: samad-yar-khan Date: Sat, 5 Feb 2022 06:09:57 +0530 Subject: [PATCH 1/2] Fix Discourse module to store top N results --- cms/config/functions/cron.js | 2 +- cms/config/functions/fetchTopPosts.js | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cms/config/functions/cron.js b/cms/config/functions/cron.js index 5bea899c..98ccc152 100644 --- a/cms/config/functions/cron.js +++ b/cms/config/functions/cron.js @@ -20,7 +20,7 @@ module.exports = { // // } '*/5 * * * *': () => { - getLatestCommunityActivity(); + getLatestCommunityActivity(20); }, '*/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, From 2fdd3d9eb7853217ec481c9bdfcc307db6f449db Mon Sep 17 00:00:00 2001 From: samad-yar-khan Date: Sat, 5 Feb 2022 06:15:17 +0530 Subject: [PATCH 2/2] Fix Discourse module to store top N results --- cms/config/functions/cron.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cms/config/functions/cron.js b/cms/config/functions/cron.js index 98ccc152..55ffc8db 100644 --- a/cms/config/functions/cron.js +++ b/cms/config/functions/cron.js @@ -20,7 +20,7 @@ module.exports = { // // } '*/5 * * * *': () => { - getLatestCommunityActivity(20); + getLatestCommunityActivity(15); }, '*/60 * * * * *': () => { getCommunityContributors('https://gsoc.rocket.chat/api/data','rocketChat','Rocket.Chat');