2021-09-27 19:38:06 -04:00
// Copyright 2021 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2021-09-27 19:38:06 -04:00
package misc
import (
"net/http"
2022-05-02 15:35:45 +02:00
"time"
2021-09-27 19:38:06 -04:00
2022-06-13 17:37:59 +08:00
issues_model "code.gitea.io/gitea/models/issues"
2022-05-02 15:35:45 +02:00
user_model "code.gitea.io/gitea/models/user"
2021-09-27 19:38:06 -04:00
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
2024-02-27 15:12:22 +08:00
"code.gitea.io/gitea/services/context"
2021-09-27 19:38:06 -04:00
)
2022-05-02 15:35:45 +02:00
const cacheKeyNodeInfoUsage = "API_NodeInfoUsage"
2021-09-27 19:38:06 -04:00
// 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 15:35:45 +02:00
nodeInfoUsage := structs . NodeInfoUsage { }
if setting . Federation . ShareUserStatistics {
2023-12-19 17:29:05 +08:00
var cached bool
nodeInfoUsage , cached = ctx . Cache . Get ( cacheKeyNodeInfoUsage ) . ( structs . NodeInfoUsage )
2022-05-16 08:36:57 +01:00
if ! cached {
2023-09-14 19:09:32 +02:00
usersTotal := int ( user_model . CountUsers ( ctx , nil ) )
2022-05-02 15:35:45 +02:00
now := time . Now ( )
timeOneMonthAgo := now . AddDate ( 0 , - 1 , 0 ) . Unix ( )
timeHaveYearAgo := now . AddDate ( 0 , - 6 , 0 ) . Unix ( )
2023-09-14 19:09:32 +02:00
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 15:35:45 +02:00
2022-11-19 09:12:33 +01:00
allIssues , _ := issues_model . CountIssues ( ctx , & issues_model . IssuesOptions { } )
2023-09-29 14:12:54 +02:00
allComments , _ := issues_model . CountComments ( ctx , & issues_model . FindCommentsOptions { } )
2022-05-02 15:35:45 +02:00
2022-05-16 08:36:57 +01:00
nodeInfoUsage = structs . NodeInfoUsage {
2022-05-02 15:35:45 +02:00
Users : structs . NodeInfoUsageUsers {
Total : usersTotal ,
ActiveMonth : usersActiveMonth ,
ActiveHalfyear : usersActiveHalfyear ,
} ,
LocalPosts : int ( allIssues ) ,
LocalComments : int ( allComments ) ,
}
2023-12-19 17:29:05 +08:00
if err := ctx . Cache . Put ( cacheKeyNodeInfoUsage , nodeInfoUsage , 180 ) ; err != nil {
ctx . InternalServerError ( err )
return
2022-05-02 15:35:45 +02:00
}
}
}
2021-09-27 19:38:06 -04:00
nodeInfo := & structs . NodeInfo {
Version : "2.1" ,
Software : structs . NodeInfoSoftware {
Name : "gitea" ,
Version : setting . AppVer ,
Repository : "https://github.com/go-gitea/gitea.git" ,
Homepage : "https://gitea.io/" ,
} ,
Protocols : [ ] string { "activitypub" } ,
Services : structs . NodeInfoServices {
Inbound : [ ] string { } ,
2022-05-02 15:35:45 +02:00
Outbound : [ ] string { "rss2.0" } ,
2021-09-27 19:38:06 -04:00
} ,
OpenRegistrations : setting . Service . ShowRegistrationButton ,
2022-05-02 15:35:45 +02:00
Usage : nodeInfoUsage ,
2021-09-27 19:38:06 -04:00
}
ctx . JSON ( http . StatusOK , nodeInfo )
}