@@ -6,12 +6,17 @@ package misc
6
6
7
7
import (
8
8
"net/http"
9
+ "time"
9
10
11
+ "code.gitea.io/gitea/models"
12
+ user_model "code.gitea.io/gitea/models/user"
10
13
"code.gitea.io/gitea/modules/context"
11
14
"code.gitea.io/gitea/modules/setting"
12
15
"code.gitea.io/gitea/modules/structs"
13
16
)
14
17
18
+ const cacheKeyNodeInfoUsage = "API_NodeInfoUsage"
19
+
15
20
// NodeInfo returns the NodeInfo for the Gitea instance to allow for federation
16
21
func NodeInfo (ctx * context.APIContext ) {
17
22
// swagger:operation GET /nodeinfo miscellaneous getNodeInfo
@@ -23,6 +28,37 @@ func NodeInfo(ctx *context.APIContext) {
23
28
// "200":
24
29
// "$ref": "#/responses/NodeInfo"
25
30
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
+
26
62
nodeInfo := & structs.NodeInfo {
27
63
Version : "2.1" ,
28
64
Software : structs.NodeInfoSoftware {
@@ -34,12 +70,10 @@ func NodeInfo(ctx *context.APIContext) {
34
70
Protocols : []string {"activitypub" },
35
71
Services : structs.NodeInfoServices {
36
72
Inbound : []string {},
37
- Outbound : []string {},
73
+ Outbound : []string {"rss2.0" },
38
74
},
39
75
OpenRegistrations : setting .Service .ShowRegistrationButton ,
40
- Usage : structs.NodeInfoUsage {
41
- Users : structs.NodeInfoUsageUsers {},
42
- },
76
+ Usage : nodeInfoUsage ,
43
77
}
44
78
ctx .JSON (http .StatusOK , nodeInfo )
45
79
}
0 commit comments