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
1 changed files with 47 additions and 20 deletions

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,20 +27,33 @@ 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)
}
u, err := s.DB.User(ctx, userID)
if err != nil { if err != nil {
if err == db.ErrUserNotFound { if err == db.ErrUserNotFound {
return server.APIError{Code: server.ErrUserNotFound} return server.APIError{Code: server.ErrUserNotFound}
} }
log.Errorf("getting user %v: %v", userID, err) log.Errorf("getting user %v: %v", id, err)
return errors.Wrap(err, "getting user") return errors.Wrap(err, "getting user")
} }
} else {
id, err := common.ParseSnowflake(chi.URLParam(r, "id"))
if err != nil {
return server.APIError{Code: server.ErrUserNotFound}
}
u, err = s.DB.UserBySnowflake(ctx, common.UserID(id))
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 {
return server.APIError{Code: server.ErrUserNotFound} return server.APIError{Code: server.ErrUserNotFound}
@ -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)
}
m, err := s.DB.Member(ctx, memberID)
if err != nil { if err != nil {
if err == db.ErrMemberNotFound { if err == db.ErrMemberNotFound {
return server.APIError{Code: server.ErrMemberNotFound} return server.APIError{Code: server.ErrMemberNotFound}
} }
log.Errorf("getting member %v: %v", memberID, err) log.Errorf("getting user %v: %v", id, err)
return errors.Wrap(err, "getting member") return errors.Wrap(err, "getting user")
}
} else {
id, err := common.ParseSnowflake(chi.URLParam(r, "id"))
if err != nil {
return server.APIError{Code: server.ErrUserNotFound}
}
m, err = s.DB.MemberBySnowflake(ctx, common.MemberID(id))
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)