Skip to content

Commit ccb32ed

Browse files
committed
updated scripts
1 parent ea8bfd7 commit ccb32ed

1 file changed

Lines changed: 95 additions & 39 deletions

File tree

.github/scripts/fetch-discourse-activity.js

Lines changed: 95 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// .github/scripts/fetch-discourse-activity.js
2-
// Fixed for ES modules
2+
// Improved to get truly recent activity, not pinned topics
33

44
import fetch from 'node-fetch';
55
import fs from 'fs';
@@ -10,14 +10,24 @@ async function fetchDiscourseActivity() {
1010
try {
1111
const activities = [];
1212

13-
// 1. Get latest topics
14-
console.log('Fetching latest topics...');
15-
const topicsResponse = await fetch(`${DISCOURSE_URL}/latest.json`);
13+
// 1. Get truly recent topics (not pinned ones)
14+
console.log('Fetching recent topics...');
15+
const topicsResponse = await fetch(`${DISCOURSE_URL}/new.json`);
1616
const topicsData = await topicsResponse.json();
1717

18-
// Get 2 most recent topics
18+
// Filter out pinned topics and get recent ones
1919
if (topicsData.topic_list && topicsData.topic_list.topics) {
20-
topicsData.topic_list.topics.slice(0, 2).forEach(topic => {
20+
const recentTopics = topicsData.topic_list.topics
21+
.filter(topic => !topic.pinned && !topic.pinned_globally) // Skip pinned topics
22+
.filter(topic => {
23+
// Only include topics from the last 30 days
24+
const topicDate = new Date(topic.created_at);
25+
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
26+
return topicDate > thirtyDaysAgo;
27+
})
28+
.slice(0, 2); // Get 2 most recent
29+
30+
recentTopics.forEach(topic => {
2131
activities.push({
2232
message: topic.title.length > 50 ? topic.title.substring(0, 50) + '...' : topic.title,
2333
time: getRelativeTime(topic.created_at),
@@ -28,38 +38,79 @@ async function fetchDiscourseActivity() {
2838
});
2939
}
3040

31-
// 2. Get site statistics
32-
console.log('Fetching site statistics...');
33-
const statsResponse = await fetch(`${DISCOURSE_URL}/site/statistics.json`);
34-
const statsData = await statsResponse.json();
35-
36-
// Add a stats-based activity
37-
activities.push({
38-
message: `${statsData.topics_7_days || 0} new topics this week`,
39-
time: 'This week',
40-
type: 'stats',
41-
color: 'bg-green-500'
42-
});
43-
44-
// 3. Try to get recent posts (may not be available publicly)
41+
// 2. Try getting recent posts from a different endpoint
42+
console.log('Fetching recent posts...');
4543
try {
46-
console.log('Trying to fetch recent posts...');
4744
const postsResponse = await fetch(`${DISCOURSE_URL}/posts.json`);
4845
if (postsResponse.ok) {
4946
const postsData = await postsResponse.json();
5047

5148
if (postsData.latest_posts && postsData.latest_posts.length > 0) {
52-
const recentPost = postsData.latest_posts[0];
53-
activities.push({
54-
message: `New reply in "${recentPost.topic_title ? recentPost.topic_title.substring(0, 40) + '...' : 'discussion'}"`,
55-
time: getRelativeTime(recentPost.created_at),
56-
type: 'reply',
57-
color: 'bg-purple-500'
58-
});
49+
// Get the most recent non-pinned post
50+
const recentPost = postsData.latest_posts
51+
.filter(post => {
52+
const postDate = new Date(post.created_at);
53+
const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
54+
return postDate > sevenDaysAgo;
55+
})[0];
56+
57+
if (recentPost) {
58+
activities.push({
59+
message: `New reply in "${recentPost.topic_title ? recentPost.topic_title.substring(0, 40) + '...' : 'discussion'}"`,
60+
time: getRelativeTime(recentPost.created_at),
61+
type: 'reply',
62+
color: 'bg-purple-500'
63+
});
64+
}
5965
}
6066
}
6167
} catch (error) {
62-
console.log('Posts endpoint not available publicly, skipping...');
68+
console.log('Posts endpoint not available, trying alternative...');
69+
70+
// Alternative: Get recent activity from top endpoint
71+
try {
72+
const topResponse = await fetch(`${DISCOURSE_URL}/top/weekly.json`);
73+
if (topResponse.ok) {
74+
const topData = await topResponse.json();
75+
if (topData.topic_list && topData.topic_list.topics.length > 0) {
76+
const activeTopic = topData.topic_list.topics[0];
77+
activities.push({
78+
message: `Popular this week: "${activeTopic.title.substring(0, 45)}..."`,
79+
time: 'This week',
80+
type: 'popular',
81+
color: 'bg-green-500'
82+
});
83+
}
84+
}
85+
} catch (altError) {
86+
console.log('Alternative endpoint also failed');
87+
}
88+
}
89+
90+
// 3. Get site statistics
91+
console.log('Fetching site statistics...');
92+
const statsResponse = await fetch(`${DISCOURSE_URL}/site/statistics.json`);
93+
const statsData = await statsResponse.json();
94+
95+
// Add a stats-based activity
96+
const topicsThisWeek = statsData.topics_7_days || 0;
97+
if (topicsThisWeek > 0) {
98+
activities.push({
99+
message: `${topicsThisWeek} new topics this week`,
100+
time: 'This week',
101+
type: 'stats',
102+
color: 'bg-green-500'
103+
});
104+
}
105+
106+
// If we don't have enough activities, add some generic ones
107+
if (activities.length < 3) {
108+
activities.push({
109+
message: "Active PowerShell discussions ongoing",
110+
time: "Ongoing",
111+
type: "community",
112+
color: "bg-blue-500"
113+
});
63114
}
64115

65116
// 4. Create community stats object
@@ -87,28 +138,33 @@ async function fetchDiscourseActivity() {
87138
console.log(`👥 ${communityStats.stats.active_users} total users`);
88139
console.log(`📝 ${communityStats.stats.topics_this_week} topics this week`);
89140

141+
// Log the activities for debugging
142+
activities.forEach((activity, index) => {
143+
console.log(`${index + 1}. ${activity.message} (${activity.time})`);
144+
});
145+
90146
} catch (error) {
91147
console.error('❌ Error fetching Discourse data:', error);
92148

93-
// Fallback to static data if API fails
149+
// Fallback to more relevant static data
94150
const fallbackData = {
95151
activities: [
96152
{
97-
message: "PowerShell 7.4.1 released with security updates",
98-
time: "Last week",
153+
message: "PowerShell 7.4.1 security update available",
154+
time: "2 weeks ago",
99155
type: "release",
100156
color: "bg-green-500"
101157
},
102158
{
103-
message: "New Azure PowerShell module available",
104-
time: "2 weeks ago",
105-
type: "update",
159+
message: "Active community helping with automation",
160+
time: "Ongoing",
161+
type: "community",
106162
color: "bg-blue-500"
107163
},
108164
{
109-
message: "Community module spotlight: PSWriteHTML",
165+
message: "New Azure PowerShell modules released",
110166
time: "3 weeks ago",
111-
type: "community",
167+
type: "update",
112168
color: "bg-purple-500"
113169
},
114170
{
@@ -121,8 +177,8 @@ async function fetchDiscourseActivity() {
121177
stats: {
122178
total_topics: 15420,
123179
total_posts: 85230,
124-
active_users: 12500,
125-
topics_this_week: 45
180+
active_users: 9612, // Use the real number you saw
181+
topics_this_week: 4 // Use the real number you saw
126182
},
127183
last_updated: new Date().toISOString(),
128184
fallback: true

0 commit comments

Comments
 (0)