add snowflake support to report routes

This commit is contained in:
sam 2023-09-07 16:53:58 +02:00
parent 1b9a5deb78
commit 04db0507ba
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1

View file

@ -3,6 +3,7 @@ package mod
import ( import (
"net/http" "net/http"
"codeberg.org/pronounscc/pronouns.cc/backend/common"
"codeberg.org/pronounscc/pronouns.cc/backend/db" "codeberg.org/pronounscc/pronouns.cc/backend/db"
"codeberg.org/pronounscc/pronouns.cc/backend/log" "codeberg.org/pronounscc/pronouns.cc/backend/log"
"codeberg.org/pronounscc/pronouns.cc/backend/server" "codeberg.org/pronounscc/pronouns.cc/backend/server"
@ -18,7 +19,7 @@ type CreateReportRequest struct {
Reason string `json:"reason"` Reason string `json:"reason"`
} }
func (s *Server) createUserReport(w http.ResponseWriter, r *http.Request) error { func (s *Server) createUserReport(w http.ResponseWriter, r *http.Request) (err error) {
ctx := r.Context() ctx := r.Context()
claims, _ := server.ClaimsFromContext(ctx) claims, _ := server.ClaimsFromContext(ctx)
@ -26,19 +27,32 @@ func (s *Server) createUserReport(w http.ResponseWriter, r *http.Request) error
return server.APIError{Code: server.ErrMissingPermissions, Details: "This token is read-only"} return server.APIError{Code: server.ErrMissingPermissions, Details: "This token is read-only"}
} }
userID, err := xid.FromString(chi.URLParam(r, "id")) var u db.User
if err != nil { if id, err := xid.FromString(chi.URLParam(r, "id")); err == nil {
return server.APIError{Code: server.ErrBadRequest, Details: "Invalid user ID"} u, err = s.DB.User(ctx, id)
} if err != nil {
if err == db.ErrUserNotFound {
return server.APIError{Code: server.ErrUserNotFound}
}
u, err := s.DB.User(ctx, userID) log.Errorf("getting user %v: %v", id, err)
if err != nil { return errors.Wrap(err, "getting user")
if err == db.ErrUserNotFound { }
} else {
id, err := common.ParseSnowflake(chi.URLParam(r, "id"))
if err != nil {
return server.APIError{Code: server.ErrUserNotFound} return server.APIError{Code: server.ErrUserNotFound}
} }
log.Errorf("getting user %v: %v", userID, err) u, err = s.DB.UserBySnowflake(ctx, common.UserID(id))
return errors.Wrap(err, "getting user") if err != nil {
if err == db.ErrUserNotFound {
return server.APIError{Code: server.ErrUserNotFound}
}
log.Errorf("getting user %v: %v", id, err)
return errors.Wrap(err, "getting user")
}
} }
if u.DeletedAt != nil { if u.DeletedAt != nil {
@ -73,19 +87,32 @@ func (s *Server) createMemberReport(w http.ResponseWriter, r *http.Request) erro
return server.APIError{Code: server.ErrMissingPermissions, Details: "This token is read-only"} return server.APIError{Code: server.ErrMissingPermissions, Details: "This token is read-only"}
} }
memberID, err := xid.FromString(chi.URLParam(r, "id")) var m db.Member
if err != nil { if id, err := xid.FromString(chi.URLParam(r, "id")); err == nil {
return server.APIError{Code: server.ErrBadRequest, Details: "Invalid member ID"} m, err = s.DB.Member(ctx, id)
} if err != nil {
if err == db.ErrMemberNotFound {
return server.APIError{Code: server.ErrMemberNotFound}
}
m, err := s.DB.Member(ctx, memberID) log.Errorf("getting user %v: %v", id, err)
if err != nil { return errors.Wrap(err, "getting user")
if err == db.ErrMemberNotFound { }
return server.APIError{Code: server.ErrMemberNotFound} } else {
id, err := common.ParseSnowflake(chi.URLParam(r, "id"))
if err != nil {
return server.APIError{Code: server.ErrUserNotFound}
} }
log.Errorf("getting member %v: %v", memberID, err) m, err = s.DB.MemberBySnowflake(ctx, common.MemberID(id))
return errors.Wrap(err, "getting member") if err != nil {
if err == db.ErrMemberNotFound {
return server.APIError{Code: server.ErrMemberNotFound}
}
log.Errorf("getting user %v: %v", id, err)
return errors.Wrap(err, "getting user")
}
} }
u, err := s.DB.User(ctx, m.UserID) u, err := s.DB.User(ctx, m.UserID)