feat: expose active user counts in API
This commit is contained in:
parent
e8d9ccb1ac
commit
de460720da
4 changed files with 79 additions and 12 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/db"
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/server"
|
||||
"emperror.dev/errors"
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
@ -21,11 +22,18 @@ func Mount(srv *server.Server, r chi.Router) {
|
|||
}
|
||||
|
||||
type MetaResponse struct {
|
||||
GitRepository string `json:"git_repository"`
|
||||
GitCommit string `json:"git_commit"`
|
||||
Users int64 `json:"users"`
|
||||
Members int64 `json:"members"`
|
||||
RequireInvite bool `json:"require_invite"`
|
||||
GitRepository string `json:"git_repository"`
|
||||
GitCommit string `json:"git_commit"`
|
||||
Users MetaUsers `json:"users"`
|
||||
Members int64 `json:"members"`
|
||||
RequireInvite bool `json:"require_invite"`
|
||||
}
|
||||
|
||||
type MetaUsers struct {
|
||||
Total int64 `json:"total"`
|
||||
ActiveMonth int64 `json:"active_month"`
|
||||
ActiveWeek int64 `json:"active_week"`
|
||||
ActiveDay int64 `json:"active_day"`
|
||||
}
|
||||
|
||||
func (s *Server) meta(w http.ResponseWriter, r *http.Request) error {
|
||||
|
@ -36,6 +44,21 @@ func (s *Server) meta(w http.ResponseWriter, r *http.Request) error {
|
|||
return errors.Wrap(err, "querying user count")
|
||||
}
|
||||
|
||||
activeMonth, err := s.DB.ActiveUsers(ctx, db.ActiveMonth)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "querying user count")
|
||||
}
|
||||
|
||||
activeWeek, err := s.DB.ActiveUsers(ctx, db.ActiveWeek)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "querying user count")
|
||||
}
|
||||
|
||||
activeDay, err := s.DB.ActiveUsers(ctx, db.ActiveDay)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "querying user count")
|
||||
}
|
||||
|
||||
numMembers, err := s.DB.TotalMemberCount(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "querying user count")
|
||||
|
@ -44,7 +67,12 @@ func (s *Server) meta(w http.ResponseWriter, r *http.Request) error {
|
|||
render.JSON(w, r, MetaResponse{
|
||||
GitRepository: server.Repository,
|
||||
GitCommit: server.Revision,
|
||||
Users: numUsers,
|
||||
Users: MetaUsers{
|
||||
Total: numUsers,
|
||||
ActiveMonth: activeMonth,
|
||||
ActiveWeek: activeWeek,
|
||||
ActiveDay: activeDay,
|
||||
},
|
||||
Members: numMembers,
|
||||
RequireInvite: os.Getenv("REQUIRE_INVITE") == "true",
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue