Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Cron to populate discourse collection in strapi #11

Merged
merged 4 commits into from
Aug 16, 2021
Merged
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ Start strapi:
git clone https://github.com/rocketchat/RC4Community
cd cms
npm i
INITIALIZE_DATA=true npm run develop
INITIALIZE_DATA=true DISCOURSE_DOMAIN=domain DISCOURSE_API_USERNAME=username DISCOURSE_API_KEY=api_key npm run develop
```

Note the `INITIALIZE_DATA` environment variable is only needed the first time you startup the cms for development. It will seed the cms with a default set of components for you to start your own customization. (see [fetch data](https://github.com/RonLek/RC4Community/blob/master/cms/config/functions/fetchData.js) for the actual default initialization code)
Note:
1. `INITIALIZE_DATA` environment variable is only needed the first time you startup the cms for development. It will seed the cms with a default set of components for you to start your own customization. (see [fetch data](https://github.com/RonLek/RC4Community/blob/master/cms/config/functions/fetchData.js) for the actual default initialization code)
2. `DISCOURSE_DOMAIN`, `DISCOURSE_API_USERNAME`, `DISCOURSE_API_KEY` environment variables are required for the cron job to fetch the latest top activity on discourse with the time interval of 5 mins.


The application is written on nextjs and deployable on all nextjs compatible CDN + microservices and scaled deployment platforms. For build and design, start it in a shell:

Expand Down
94 changes: 45 additions & 49 deletions app/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useState } from "react";
import { fetchAPI } from "../lib/api";
import {
Button,
TextField,
Select,
MenuItem,
Expand Down Expand Up @@ -31,50 +30,50 @@ export default function Home({
guides,
releaseNotes,
topNavItems,
topPosts
}) {
const [searchCategory, setSearchCategory] = useState("");
const { t } = useTranslation();
let loginWindow = null;

const activityItems = [
{
title:
"I am setting up live chat and want to send an attachment ...how do I do that?",
author: "LigayaFernandez",
role: "LiveChat User",
community: "Question Forum",
time: "17 min ago",
upvotes: 0,
comments: 1,
},
{
title: "Stranger Introduction",
author: "Izzie ",
role: "GSoC Student",
community: "GSoC 2021",
time: "1 hour ago",
upvotes: 5,
comments: 10,
},
{
title: "Setting Up Rocket Chat",
author: "arary",
role: "Developer",
community: "Developer Discussions",
time: "2 hours ago",
upvotes: 0,
comments: 1,
},
{
title: "RC4Community Improvements",
author: "aumurad",
role: "Admin",
community: "Announcements",
time: "4 hours ago",
upvotes: 50,
comments: 3,
},
];

function timeSince(date) {
let seconds = Math.floor((new Date() - date) / 1000);
let interval = seconds / 31536000;
if (interval > 1) {
return Math.floor(interval) + " years";
}
interval = seconds / 2592000;
if (interval > 1) {
return Math.floor(interval) + " months ago";
}
interval = seconds / 86400;
if (interval > 1) {
return Math.floor(interval) + " days ago";
}
interval = seconds / 3600;
if (interval > 1) {
return Math.floor(interval) + " hours ago";
}
interval = seconds / 60;
if (interval > 1) {
return Math.floor(interval) + " minutes ago";
}
return Math.floor(seconds) + " seconds ago";
}
let activityItems = [];
topPosts[0]?.TopPost?.topic_list?.topics.map((topic) => {
let newTopic = {
title: topic.fancy_title,
time: timeSince(new Date(topic.created_at)),
upvotes: topic.like_count,
comments: topic.posts_count,
link: `https://forums.rocket.chat/t/${topic.slug}/${topic.id}`,
image_url: topic.image_url
}
activityItems.push(newTopic);
})


const Item = (props) => {
return (
Expand Down Expand Up @@ -283,20 +282,16 @@ export default function Home({
<div className="communities-wrapper">
<h1>{t("unsigned-home-demo.community-activity-heading")}</h1>
{activityItems.map((item) => (
<div className="community-activity-wrapper">
<a href={item.link} className="community-activity-wrapper">
<div className="community-activity-content">
<img
src={`https://open.rocket.chat/avatar/rocket.cat`}
src={item.image_url || `https://open.rocket.chat/avatar/rocket.cat`}
className="community-activity-author-image"
/>
<div className="community-activity-heading">
<h3>{item.title}</h3>
<p className="community-activity-info">
by{" "}
<strong>
{item.author}({item.role})
</strong>{" "}
in <strong>{item.community}</strong> <i>{item.time}</i>{" "}
<i>{item.time}</i>{" "}
</p>
</div>
</div>
Expand All @@ -320,7 +315,7 @@ export default function Home({
{item.comments}
</div>
</div>
</div>
</a>
))}
</div>
</div>
Expand All @@ -334,9 +329,10 @@ export async function getStaticProps({ params }) {
const guides = await fetchAPI("/guides");
const releaseNotes = await fetchAPI("/release-notes");
const topNavItems = await fetchAPI("/top-nav-item");
const topPosts = await fetchAPI("/discourses");

return {
props: { carousels, personas, guides, releaseNotes, topNavItems },
props: { carousels, personas, guides, releaseNotes, topNavItems, topPosts },
// Next.js will attempt to re-generate the page:
// - When a request comes in
// - At most once every 1 second
Expand Down
52 changes: 52 additions & 0 deletions cms/api/discourse/config/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"routes": [
{
"method": "GET",
"path": "/discourses",
"handler": "discourse.find",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/discourses/count",
"handler": "discourse.count",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/discourses/:id",
"handler": "discourse.findOne",
"config": {
"policies": []
}
},
{
"method": "POST",
"path": "/discourses",
"handler": "discourse.create",
"config": {
"policies": []
}
},
{
"method": "PUT",
"path": "/discourses/:id",
"handler": "discourse.update",
"config": {
"policies": []
}
},
{
"method": "DELETE",
"path": "/discourses/:id",
"handler": "discourse.delete",
"config": {
"policies": []
}
}
]
}
8 changes: 8 additions & 0 deletions cms/api/discourse/controllers/discourse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-controllers)
* to customize this controller
*/

module.exports = {};
8 changes: 8 additions & 0 deletions cms/api/discourse/models/discourse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#lifecycle-hooks)
* to customize this model
*/

module.exports = {};
21 changes: 21 additions & 0 deletions cms/api/discourse/models/discourse.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"kind": "collectionType",
"collectionName": "discourses",
"info": {
"name": "discourse",
"description": ""
},
"options": {
"increments": true,
"timestamps": true,
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
"TopPost": {
"type": "json",
"required": true,
"unique": false
}
}
}
8 changes: 8 additions & 0 deletions cms/api/discourse/services/discourse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services)
* to customize this service
*/

module.exports = {};
3 changes: 3 additions & 0 deletions cms/config/functions/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

const { getLatestCommunityActivity } = require("./fetchTopPosts");

/**
* An asynchronous bootstrap function that runs before
* your application gets started.
Expand Down Expand Up @@ -32,5 +34,6 @@ module.exports = async () => {
// Fetches data and populates CMS from remote on server restart
if (process.env.INITIALIZE_DATA) {
await strapi.config.functions.fetchData();
await getLatestCommunityActivity();
}
};
5 changes: 4 additions & 1 deletion cms/config/functions/cron.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

const { getLatestCommunityActivity } = require("./fetchTopPosts");
/**
* Cron config that gives you an opportunity
* to run scheduled jobs.
Expand All @@ -18,4 +18,7 @@ module.exports = {
// '0 1 * * 1': () => {
//
// }
'*/5 * * * *': () => {
getLatestCommunityActivity();
}
};
25 changes: 25 additions & 0 deletions cms/config/functions/fetchTopPosts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const axios = require("axios");

module.exports.getLatestCommunityActivity = async () => {
const TopPost = await axios({
url: `${process.env.DISCOURSE_DOMAIN}/top.json?period=all`,
method: "GET",
headers: {
"Api-Username": process.env.DISCOURSE_API_USERNAME,
"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,
}
);
} else {
await strapi.query("discourse").create({
TopPost: TopPost.data,
});
}
};
3 changes: 3 additions & 0 deletions cms/config/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ module.exports = ({ env }) => ({
secret: env('ADMIN_JWT_SECRET', '9e07dfd3396d88a43609466615259199'),
},
},
cron: {
enabled: true,
},
});