add snowflake support to some member routes
This commit is contained in:
parent
1cce0defca
commit
0171f54592
|
@ -88,7 +88,8 @@ func (db *DB) MemberBySnowflake(ctx context.Context, id common.MemberID) (m Memb
|
|||
|
||||
// UserMember returns a member scoped by user.
|
||||
func (db *DB) UserMember(ctx context.Context, userID xid.ID, memberRef string) (m Member, err error) {
|
||||
sql, args, err := sq.Select("*").From("members").Where("user_id = ?", userID).Where("(id = ? or name = ? or snowflake_id = ?)", memberRef, memberRef, memberRef).ToSql()
|
||||
sf, _ := common.ParseSnowflake(memberRef) // error can be ignored as the zero value will never be used as an ID
|
||||
sql, args, err := sq.Select("*").From("members").Where("user_id = ?", userID).Where("(id = ? or snowflake_id = ? or name = ?)", memberRef, sf, memberRef).ToSql()
|
||||
if err != nil {
|
||||
return m, errors.Wrap(err, "building sql")
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"codeberg.org/pronounscc/pronouns.cc/backend/common"
|
||||
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
||||
"codeberg.org/pronounscc/pronouns.cc/backend/log"
|
||||
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
||||
"emperror.dev/errors"
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
@ -75,20 +76,30 @@ type PartialUser struct {
|
|||
CustomPreferences db.CustomPreferences `json:"custom_preferences"`
|
||||
}
|
||||
|
||||
func (s *Server) getMember(w http.ResponseWriter, r *http.Request) error {
|
||||
func (s *Server) getMember(w http.ResponseWriter, r *http.Request) (err error) {
|
||||
ctx := r.Context()
|
||||
|
||||
id, err := xid.FromString(chi.URLParam(r, "memberRef"))
|
||||
if err != nil {
|
||||
return server.APIError{
|
||||
Code: server.ErrMemberNotFound,
|
||||
var m db.Member
|
||||
if id, err := xid.FromString(chi.URLParam(r, "memberRef")); err == nil {
|
||||
m, err = s.DB.Member(ctx, id)
|
||||
if err != nil {
|
||||
log.Errorf("getting member by xid: %v", err)
|
||||
}
|
||||
}
|
||||
// xid was not valid
|
||||
if !m.SnowflakeID.IsValid() {
|
||||
id, err := common.ParseSnowflake(chi.URLParam(r, "memberRef"))
|
||||
if err != nil {
|
||||
return server.APIError{
|
||||
Code: server.ErrMemberNotFound,
|
||||
}
|
||||
}
|
||||
|
||||
m, err := s.DB.Member(ctx, id)
|
||||
if err != nil {
|
||||
return server.APIError{
|
||||
Code: server.ErrMemberNotFound,
|
||||
m, err = s.DB.MemberBySnowflake(ctx, common.MemberID(id))
|
||||
if err != nil {
|
||||
return server.APIError{
|
||||
Code: server.ErrMemberNotFound,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue