code.gitea.io/gitea@v1.21.7/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/context" 13 "code.gitea.io/gitea/modules/setting" 14 "code.gitea.io/gitea/modules/structs" 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 := false 33 if setting.CacheService.Enabled { 34 nodeInfoUsage, cached = ctx.Cache.Get(cacheKeyNodeInfoUsage).(structs.NodeInfoUsage) 35 } 36 if !cached { 37 usersTotal := int(user_model.CountUsers(ctx, nil)) 38 now := time.Now() 39 timeOneMonthAgo := now.AddDate(0, -1, 0).Unix() 40 timeHaveYearAgo := now.AddDate(0, -6, 0).Unix() 41 usersActiveMonth := int(user_model.CountUsers(ctx, &user_model.CountUserFilter{LastLoginSince: &timeOneMonthAgo})) 42 usersActiveHalfyear := int(user_model.CountUsers(ctx, &user_model.CountUserFilter{LastLoginSince: &timeHaveYearAgo})) 43 44 allIssues, _ := issues_model.CountIssues(ctx, &issues_model.IssuesOptions{}) 45 allComments, _ := issues_model.CountComments(ctx, &issues_model.FindCommentsOptions{}) 46 47 nodeInfoUsage = structs.NodeInfoUsage{ 48 Users: structs.NodeInfoUsageUsers{ 49 Total: usersTotal, 50 ActiveMonth: usersActiveMonth, 51 ActiveHalfyear: usersActiveHalfyear, 52 }, 53 LocalPosts: int(allIssues), 54 LocalComments: int(allComments), 55 } 56 if setting.CacheService.Enabled { 57 if err := ctx.Cache.Put(cacheKeyNodeInfoUsage, nodeInfoUsage, 180); err != nil { 58 ctx.InternalServerError(err) 59 return 60 } 61 } 62 } 63 } 64 65 nodeInfo := &structs.NodeInfo{ 66 Version: "2.1", 67 Software: structs.NodeInfoSoftware{ 68 Name: "gitea", 69 Version: setting.AppVer, 70 Repository: "https://github.com/go-gitea/gitea.git", 71 Homepage: "https://gitea.io/", 72 }, 73 Protocols: []string{"activitypub"}, 74 Services: structs.NodeInfoServices{ 75 Inbound: []string{}, 76 Outbound: []string{"rss2.0"}, 77 }, 78 OpenRegistrations: setting.Service.ShowRegistrationButton, 79 Usage: nodeInfoUsage, 80 } 81 ctx.JSON(http.StatusOK, nodeInfo) 82 }