You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nodeinfo.go 2.4KB

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