@@ -6,12 +6,17 @@ package misc
66
77import (
88 "net/http"
9+ "time"
910
11+ "code.gitea.io/gitea/models"
12+ user_model "code.gitea.io/gitea/models/user"
1013 "code.gitea.io/gitea/modules/context"
1114 "code.gitea.io/gitea/modules/setting"
1215 "code.gitea.io/gitea/modules/structs"
1316)
1417
18+ const cacheKeyNodeInfoUsage = "API_NodeInfoUsage"
19+
1520// NodeInfo returns the NodeInfo for the Gitea instance to allow for federation
1621func NodeInfo (ctx * context.APIContext ) {
1722 // swagger:operation GET /nodeinfo miscellaneous getNodeInfo
@@ -23,6 +28,37 @@ func NodeInfo(ctx *context.APIContext) {
2328 // "200":
2429 // "$ref": "#/responses/NodeInfo"
2530
31+ nodeInfoUsage := structs.NodeInfoUsage {}
32+ if setting .Federation .ShareUserStatistics {
33+ info , ok := ctx .Cache .Get (cacheKeyNodeInfoUsage ).(structs.NodeInfoUsage )
34+ if ! ok {
35+ usersTotal := int (user_model .CountUsers (nil ))
36+ now := time .Now ()
37+ timeOneMonthAgo := now .AddDate (0 , - 1 , 0 ).Unix ()
38+ timeHaveYearAgo := now .AddDate (0 , - 6 , 0 ).Unix ()
39+ usersActiveMonth := int (user_model .CountUsers (& user_model.CountUserFilter {LastLoginSince : & timeOneMonthAgo }))
40+ usersActiveHalfyear := int (user_model .CountUsers (& user_model.CountUserFilter {LastLoginSince : & timeHaveYearAgo }))
41+
42+ allIssues , _ := models .CountIssues (& models.IssuesOptions {})
43+ allComments , _ := models .CountComments (& models.FindCommentsOptions {})
44+
45+ info = structs.NodeInfoUsage {
46+ Users : structs.NodeInfoUsageUsers {
47+ Total : usersTotal ,
48+ ActiveMonth : usersActiveMonth ,
49+ ActiveHalfyear : usersActiveHalfyear ,
50+ },
51+ LocalPosts : int (allIssues ),
52+ LocalComments : int (allComments ),
53+ }
54+ if err := ctx .Cache .Put (cacheKeyNodeInfoUsage , nodeInfoUsage , 180 ); err != nil {
55+ ctx .InternalServerError (err )
56+ return
57+ }
58+ }
59+ nodeInfoUsage = info
60+ }
61+
2662 nodeInfo := & structs.NodeInfo {
2763 Version : "2.1" ,
2864 Software : structs.NodeInfoSoftware {
@@ -34,12 +70,10 @@ func NodeInfo(ctx *context.APIContext) {
3470 Protocols : []string {"activitypub" },
3571 Services : structs.NodeInfoServices {
3672 Inbound : []string {},
37- Outbound : []string {},
73+ Outbound : []string {"rss2.0" },
3874 },
3975 OpenRegistrations : setting .Service .ShowRegistrationButton ,
40- Usage : structs.NodeInfoUsage {
41- Users : structs.NodeInfoUsageUsers {},
42- },
76+ Usage : nodeInfoUsage ,
4377 }
4478 ctx .JSON (http .StatusOK , nodeInfo )
4579}
0 commit comments