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
"codeberg.org/u1f320/pronouns.cc/backend/server"
"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 {
GitRepository string ` json:"git_repository" `
GitCommit string ` json:"git_commit" `
Users int64 ` json:"users" `
Members int64 ` json:"members" `
2023-03-14 00:16:19 +01:00
RequireInvite bool ` json:"require_invite" `
2023-03-08 14:13:04 +01:00
}
func ( s * Server ) meta ( w http . ResponseWriter , r * http . Request ) error {
ctx := r . Context ( )
var numUsers , numMembers int64
2023-04-17 16:33:05 +02:00
err := s . DB . QueryRow ( ctx , "SELECT COUNT(*) FROM users WHERE deleted_at IS NULL" ) . Scan ( & numUsers )
2023-03-08 14:13:04 +01:00
if err != nil {
return errors . Wrap ( err , "querying user count" )
}
2023-04-17 16:33:05 +02:00
err = s . DB . QueryRow ( ctx , "SELECT COUNT(*) FROM members WHERE unlisted = false AND user_id = ANY(SELECT id FROM users WHERE deleted_at IS NULL)" ) . Scan ( & numMembers )
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 ,
Users : numUsers ,
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
}