1+
2+ const fetch = require ( 'node-fetch' ) ;
3+ const fs = require ( 'fs' ) ;
4+
5+ const DISCOURSE_URL = 'https://forums.powershell.org' ;
6+
7+ async function fetchDiscourseActivity ( ) {
8+ try {
9+ const activities = [ ] ;
10+
11+ // 1. Get latest topics
12+ const topicsResponse = await fetch ( `${ DISCOURSE_URL } /latest.json` ) ;
13+ const topicsData = await topicsResponse . json ( ) ;
14+
15+ // Get 2 most recent topics
16+ topicsData . topic_list . topics . slice ( 0 , 2 ) . forEach ( topic => {
17+ activities . push ( {
18+ message : topic . title . length > 50 ? topic . title . substring ( 0 , 50 ) + '...' : topic . title ,
19+ time : getRelativeTime ( topic . created_at ) ,
20+ type : 'topic' ,
21+ color : 'bg-blue-500' ,
22+ url : `${ DISCOURSE_URL } /t/${ topic . slug } /${ topic . id } `
23+ } ) ;
24+ } ) ;
25+
26+ // 2. Get site statistics
27+ const statsResponse = await fetch ( `${ DISCOURSE_URL } /site/statistics.json` ) ;
28+ const statsData = await statsResponse . json ( ) ;
29+
30+ // Add a stats-based activity
31+ activities . push ( {
32+ message : `${ statsData . topics_7_days || 0 } new topics this week` ,
33+ time : 'This week' ,
34+ type : 'stats' ,
35+ color : 'bg-green-500'
36+ } ) ;
37+
38+ // 3. Get recent posts (if API allows)
39+ try {
40+ const postsResponse = await fetch ( `${ DISCOURSE_URL } /posts.json` ) ;
41+ const postsData = await postsResponse . json ( ) ;
42+
43+ if ( postsData . latest_posts && postsData . latest_posts . length > 0 ) {
44+ const recentPost = postsData . latest_posts [ 0 ] ;
45+ activities . push ( {
46+ message : `New reply in "${ recentPost . topic_title ? recentPost . topic_title . substring ( 0 , 40 ) + '...' : 'discussion' } "` ,
47+ time : getRelativeTime ( recentPost . created_at ) ,
48+ type : 'reply' ,
49+ color : 'bg-purple-500'
50+ } ) ;
51+ }
52+ } catch ( error ) {
53+ console . log ( 'Posts endpoint not available, skipping...' ) ;
54+ }
55+
56+ // 4. Create community stats object
57+ const communityStats = {
58+ activities : activities . slice ( 0 , 4 ) , // Limit to 4 items
59+ stats : {
60+ total_topics : statsData . topics_count || 0 ,
61+ total_posts : statsData . posts_count || 0 ,
62+ active_users : statsData . users_count || 0 ,
63+ topics_this_week : statsData . topics_7_days || 0
64+ } ,
65+ last_updated : new Date ( ) . toISOString ( )
66+ } ;
67+
68+ // Save to Hugo data file
69+ fs . writeFileSync ( './data/community-stats.json' , JSON . stringify ( communityStats , null , 2 ) ) ;
70+
71+ console . log ( '✅ Discourse activity fetched successfully' ) ;
72+ console . log ( `📊 Found ${ activities . length } activities` ) ;
73+ console . log ( `👥 ${ communityStats . stats . active_users } total users` ) ;
74+
75+ } catch ( error ) {
76+ console . error ( '❌ Error fetching Discourse data:' , error ) ;
77+
78+ // Fallback to static data if API fails
79+ const fallbackData = {
80+ activities : [
81+ {
82+ message : "PowerShell 7.4.1 released with security updates" ,
83+ time : "Last week" ,
84+ type : "release" ,
85+ color : "bg-green-500"
86+ } ,
87+ {
88+ message : "New Azure PowerShell module available" ,
89+ time : "2 weeks ago" ,
90+ type : "update" ,
91+ color : "bg-blue-500"
92+ } ,
93+ {
94+ message : "Community module spotlight: PSWriteHTML" ,
95+ time : "3 weeks ago" ,
96+ type : "community" ,
97+ color : "bg-purple-500"
98+ } ,
99+ {
100+ message : "PowerShell Gallery security improvements" ,
101+ time : "1 month ago" ,
102+ type : "security" ,
103+ color : "bg-orange-500"
104+ }
105+ ] ,
106+ stats : {
107+ total_topics : 0 ,
108+ total_posts : 0 ,
109+ active_users : 0 ,
110+ topics_this_week : 0
111+ } ,
112+ last_updated : new Date ( ) . toISOString ( ) ,
113+ fallback : true
114+ } ;
115+
116+ fs . writeFileSync ( './data/community-stats.json' , JSON . stringify ( fallbackData , null , 2 ) ) ;
117+ }
118+ }
119+
120+ function getRelativeTime ( dateString ) {
121+ const now = new Date ( ) ;
122+ const date = new Date ( dateString ) ;
123+ const diffMs = now - date ;
124+ const diffHours = Math . floor ( diffMs / ( 1000 * 60 * 60 ) ) ;
125+
126+ if ( diffHours < 1 ) return 'Just now' ;
127+ if ( diffHours < 24 ) return `${ diffHours } hours ago` ;
128+ const diffDays = Math . floor ( diffHours / 24 ) ;
129+ if ( diffDays === 1 ) return 'Yesterday' ;
130+ if ( diffDays < 7 ) return `${ diffDays } days ago` ;
131+ const diffWeeks = Math . floor ( diffDays / 7 ) ;
132+ if ( diffWeeks === 1 ) return 'Last week' ;
133+ if ( diffWeeks < 4 ) return `${ diffWeeks } weeks ago` ;
134+ return 'Last month' ;
135+ }
136+
137+ // Run the function
138+ fetchDiscourseActivity ( ) ;
0 commit comments