2021-09-28 05:08:06 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-09-28 05:08:06 +05:30
|
|
|
|
|
|
|
package misc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-05-02 19:05:45 +05:30
|
|
|
"time"
|
2021-09-28 05:08:06 +05:30
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-05-02 19:05:45 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-09-28 05:08:06 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
2022-05-02 19:05:45 +05:30
|
|
|
const cacheKeyNodeInfoUsage = "API_NodeInfoUsage"
|
|
|
|
|
2021-09-28 05:08:06 +05:30
|
|
|
// NodeInfo returns the NodeInfo for the Gitea instance to allow for federation
|
|
|
|
func NodeInfo(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /nodeinfo miscellaneous getNodeInfo
|
|
|
|
// ---
|
|
|
|
// summary: Returns the nodeinfo of the Gitea application
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/NodeInfo"
|
|
|
|
|
2022-05-02 19:05:45 +05:30
|
|
|
nodeInfoUsage := structs.NodeInfoUsage{}
|
|
|
|
if setting.Federation.ShareUserStatistics {
|
2023-12-19 14:59:05 +05:30
|
|
|
var cached bool
|
|
|
|
nodeInfoUsage, cached = ctx.Cache.Get(cacheKeyNodeInfoUsage).(structs.NodeInfoUsage)
|
|
|
|
|
2022-05-16 13:06:57 +05:30
|
|
|
if !cached {
|
2023-09-14 22:39:32 +05:30
|
|
|
usersTotal := int(user_model.CountUsers(ctx, nil))
|
2022-05-02 19:05:45 +05:30
|
|
|
now := time.Now()
|
|
|
|
timeOneMonthAgo := now.AddDate(0, -1, 0).Unix()
|
|
|
|
timeHaveYearAgo := now.AddDate(0, -6, 0).Unix()
|
2023-09-14 22:39:32 +05:30
|
|
|
usersActiveMonth := int(user_model.CountUsers(ctx, &user_model.CountUserFilter{LastLoginSince: &timeOneMonthAgo}))
|
|
|
|
usersActiveHalfyear := int(user_model.CountUsers(ctx, &user_model.CountUserFilter{LastLoginSince: &timeHaveYearAgo}))
|
2022-05-02 19:05:45 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
allIssues, _ := issues_model.CountIssues(ctx, &issues_model.IssuesOptions{})
|
2023-09-29 17:42:54 +05:30
|
|
|
allComments, _ := issues_model.CountComments(ctx, &issues_model.FindCommentsOptions{})
|
2022-05-02 19:05:45 +05:30
|
|
|
|
2022-05-16 13:06:57 +05:30
|
|
|
nodeInfoUsage = structs.NodeInfoUsage{
|
2022-05-02 19:05:45 +05:30
|
|
|
Users: structs.NodeInfoUsageUsers{
|
|
|
|
Total: usersTotal,
|
|
|
|
ActiveMonth: usersActiveMonth,
|
|
|
|
ActiveHalfyear: usersActiveHalfyear,
|
|
|
|
},
|
|
|
|
LocalPosts: int(allIssues),
|
|
|
|
LocalComments: int(allComments),
|
|
|
|
}
|
2023-12-19 14:59:05 +05:30
|
|
|
|
|
|
|
if err := ctx.Cache.Put(cacheKeyNodeInfoUsage, nodeInfoUsage, 180); err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
2022-05-02 19:05:45 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:08:06 +05:30
|
|
|
nodeInfo := &structs.NodeInfo{
|
|
|
|
Version: "2.1",
|
|
|
|
Software: structs.NodeInfoSoftware{
|
2023-02-04 15:50:21 +05:30
|
|
|
Name: "forgejo",
|
2021-09-28 05:08:06 +05:30
|
|
|
Version: setting.AppVer,
|
2023-02-04 15:50:21 +05:30
|
|
|
Repository: "https://codeberg.org/forgejo/forgejo.git",
|
|
|
|
Homepage: "https://forgejo.org/",
|
2021-09-28 05:08:06 +05:30
|
|
|
},
|
|
|
|
Protocols: []string{"activitypub"},
|
|
|
|
Services: structs.NodeInfoServices{
|
|
|
|
Inbound: []string{},
|
2022-05-02 19:05:45 +05:30
|
|
|
Outbound: []string{"rss2.0"},
|
2021-09-28 05:08:06 +05:30
|
|
|
},
|
|
|
|
OpenRegistrations: setting.Service.ShowRegistrationButton,
|
2022-05-02 19:05:45 +05:30
|
|
|
Usage: nodeInfoUsage,
|
2021-09-28 05:08:06 +05:30
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, nodeInfo)
|
|
|
|
}
|