feat: add /users/@me/members/{memberRef} route (closes #62)

This commit is contained in:
Sam 2023-05-23 16:32:02 +02:00
parent 23f79b0fec
commit 9a70245c2d
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1
4 changed files with 30 additions and 3 deletions

File diff suppressed because one or more lines are too long

View file

@ -6,6 +6,7 @@ import (
"codeberg.org/u1f320/pronouns.cc/backend/db" "codeberg.org/u1f320/pronouns.cc/backend/db"
"codeberg.org/u1f320/pronouns.cc/backend/server" "codeberg.org/u1f320/pronouns.cc/backend/server"
"emperror.dev/errors"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/render" "github.com/go-chi/render"
"github.com/rs/xid" "github.com/rs/xid"
@ -140,6 +141,31 @@ func (s *Server) getUserMember(w http.ResponseWriter, r *http.Request) error {
return nil return nil
} }
func (s *Server) getMeMember(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
claims, _ := server.ClaimsFromContext(ctx)
u, err := s.DB.User(ctx, claims.UserID)
if err != nil {
return errors.Wrap(err, "getting me user")
}
m, err := s.DB.UserMember(ctx, claims.UserID, chi.URLParam(r, "memberRef"))
if err != nil {
return server.APIError{
Code: server.ErrMemberNotFound,
}
}
fields, err := s.DB.MemberFields(ctx, m.ID)
if err != nil {
return err
}
render.JSON(w, r, dbMemberToMember(u, m, fields, true))
return nil
}
func (s *Server) parseUser(ctx context.Context, userRef string) (u db.User, err error) { func (s *Server) parseUser(ctx context.Context, userRef string) (u db.User, err error) {
if id, err := xid.FromString(userRef); err != nil { if id, err := xid.FromString(userRef); err != nil {
u, err := s.DB.User(ctx, id) u, err := s.DB.User(ctx, id)

View file

@ -19,6 +19,7 @@ func Mount(srv *server.Server, r chi.Router) {
// user-scoped member lookup (including custom urls) // user-scoped member lookup (including custom urls)
r.Get("/users/{userRef}/members/{memberRef}", server.WrapHandler(s.getUserMember)) r.Get("/users/{userRef}/members/{memberRef}", server.WrapHandler(s.getUserMember))
r.With(server.MustAuth).Get("/users/@me/members/{memberRef}", server.WrapHandler(s.getMeMember))
r.Route("/members", func(r chi.Router) { r.Route("/members", func(r chi.Router) {
// any member by ID // any member by ID

View file

@ -261,7 +261,7 @@ paths:
summary: Get a member by ID or name summary: Get a member by ID or name
parameters: parameters:
- name: userRef - name: userRef
description: A user ID or username. description: "A user ID, username, or `@me` for yourself."
schema: schema:
type: string type: string
in: path in: path