code.gitea.io/gitea@v1.22.3/routers/api/v1/misc/nodeinfo.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package misc 5 6 import ( 7 "net/http" 8 "time" 9 10 issues_model "code.gitea.io/gitea/models/issues" 11 user_model "code.gitea.io/gitea/models/user" 12 "code.gitea.io/gitea/modules/setting" 13 "code.gitea.io/gitea/modules/structs" 14 "code.gitea.io/gitea/services/context" 15 ) 16 17 const cacheKeyNodeInfoUsage = "API_NodeInfoUsage" 18 19 // NodeInfo returns the NodeInfo for the Gitea instance to allow for federation 20 func NodeInfo(ctx *context.APIContext) { 21 // swagger:operation GET /nodeinfo miscellaneous getNodeInfo 22 // --- 23 // summary: Returns the nodeinfo of the Gitea application 24 // produces: 25 // - application/json 26 // responses: 27 // "200": 28 // "$ref": "#/responses/NodeInfo" 29 30 nodeInfoUsage := structs.NodeInfoUsage{} 31 if setting.Federation.ShareUserStatistics { 32 cached, _ := ctx.Cache.GetJSON(cacheKeyNodeInfoUsage, &nodeInfoUsage) 33 if !cached { 34 usersTotal := int(user_model.CountUsers(ctx, nil)) 35 now := time.Now() 36 timeOneMonthAgo := now.AddDate(0, -1, 0).Unix() 37 timeHaveYearAgo := now.AddDate(0, -6, 0).Unix() 38 usersActiveMonth := int(user_model.CountUsers(ctx, &user_model.CountUserFilter{LastLoginSince: &timeOneMonthAgo})) 39 usersActiveHalfyear := int(user_model.CountUsers(ctx, &user_model.CountUserFilter{LastLoginSince: &timeHaveYearAgo})) 40 41 allIssues, _ := issues_model.CountIssues(ctx, &issues_model.IssuesOptions{}) 42 allComments, _ := issues_model.CountComments(ctx, &issues_model.FindCommentsOptions{}) 43 44 nodeInfoUsage = structs.NodeInfoUsage{ 45 Users: structs.NodeInfoUsageUsers{ 46 Total: usersTotal, 47 ActiveMonth: usersActiveMonth, 48 ActiveHalfyear: usersActiveHalfyear, 49 }, 50 LocalPosts: int(allIssues), 51 LocalComments: int(allComments), 52 } 53 54 if err := ctx.Cache.PutJSON(cacheKeyNodeInfoUsage, nodeInfoUsage, 180); err != nil { 55 ctx.InternalServerError(err) 56 return 57 } 58 } 59 } 60 61 nodeInfo := &structs.NodeInfo{ 62 Version: "2.1", 63 Software: structs.NodeInfoSoftware{ 64 Name: "gitea", 65 Version: setting.AppVer, 66 Repository: "https://github.com/go-gitea/gitea.git", 67 Homepage: "https://gitea.io/", 68 }, 69 Protocols: []string{"activitypub"}, 70 Services: structs.NodeInfoServices{ 71 Inbound: []string{}, 72 Outbound: []string{"rss2.0"}, 73 }, 74 OpenRegistrations: setting.Service.ShowRegistrationButton, 75 Usage: nodeInfoUsage, 76 } 77 ctx.JSON(http.StatusOK, nodeInfo) 78 }