Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cms/config/functions/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
//
// }
'*/5 * * * *': () => {
getLatestCommunityActivity();
getLatestCommunityActivity(15);
},
'*/60 * * * * *': () => {
getCommunityContributors('https://gsoc.rocket.chat/api/data','rocketChat','Rocket.Chat');
Expand Down
27 changes: 18 additions & 9 deletions cms/config/functions/fetchTopPosts.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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,
Expand Down