2023-03-08 14:13:04 +01:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-03-14 00:16:19 +01:00
|
|
|
"os"
|
2023-03-08 14:13:04 +01:00
|
|
|
|
2023-06-03 16:18:47 +02:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
2023-03-08 14:13:04 +01:00
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
*server.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func Mount(srv *server.Server, r chi.Router) {
|
|
|
|
s := &Server{Server: srv}
|
|
|
|
|
|
|
|
r.Get("/meta", server.WrapHandler(s.meta))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MetaResponse struct {
|
2023-05-06 15:59:52 +02:00
|
|
|
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"`
|
2023-03-08 14:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) meta(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2023-04-17 23:44:21 +02:00
|
|
|
numUsers, err := s.DB.TotalUserCount(ctx)
|
2023-03-08 14:13:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "querying user count")
|
|
|
|
}
|
|
|
|
|
2023-05-06 15:59:52 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-04-17 23:44:21 +02:00
|
|
|
numMembers, err := s.DB.TotalMemberCount(ctx)
|
2023-03-08 14:13:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "querying user count")
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, MetaResponse{
|
|
|
|
GitRepository: server.Repository,
|
|
|
|
GitCommit: server.Revision,
|
2023-05-06 15:59:52 +02:00
|
|
|
Users: MetaUsers{
|
|
|
|
Total: numUsers,
|
|
|
|
ActiveMonth: activeMonth,
|
|
|
|
ActiveWeek: activeWeek,
|
|
|
|
ActiveDay: activeDay,
|
|
|
|
},
|
2023-03-08 14:13:04 +01:00
|
|
|
Members: numMembers,
|
2023-03-14 00:16:19 +01:00
|
|
|
RequireInvite: os.Getenv("REQUIRE_INVITE") == "true",
|
2023-03-08 14:13:04 +01:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|