From 0171f545922da86e453336ca91f74387bef12f9f Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 2 Sep 2023 16:34:51 +0200 Subject: [PATCH] add snowflake support to some member routes --- backend/db/member.go | 3 ++- backend/routes/v1/member/get_member.go | 29 ++++++++++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/backend/db/member.go b/backend/db/member.go index 53b6a9f..f301c89 100644 --- a/backend/db/member.go +++ b/backend/db/member.go @@ -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") } diff --git a/backend/routes/v1/member/get_member.go b/backend/routes/v1/member/get_member.go index 60fa573..2a7d5e6 100644 --- a/backend/routes/v1/member/get_member.go +++ b/backend/routes/v1/member/get_member.go @@ -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, + } } }