Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

nodeinfo.go 2.4KB

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