fix(backend): return members in /users/{userRef}

This commit is contained in:
Sam 2022-11-21 02:38:08 +01:00
parent 37c01753c9
commit 8ab4c2a91b
2 changed files with 40 additions and 9 deletions

View File

@ -32,13 +32,14 @@ type GetMeResponse struct {
} }
type PartialMember struct { type PartialMember struct {
ID xid.ID `json:"id"` ID xid.ID `json:"id"`
Name string `json:"name"` Name string `json:"name"`
AvatarURL *string `json:"avatar_url"` DisplayName *string `json:"display_name"`
AvatarURLs []string `json:"avatar_urls"`
} }
func dbUserToResponse(u db.User, fields []db.Field, names []db.Name, pronouns []db.Pronoun) GetUserResponse { func dbUserToResponse(u db.User, fields []db.Field, names []db.Name, pronouns []db.Pronoun, members []db.Member) GetUserResponse {
return GetUserResponse{ resp := GetUserResponse{
ID: u.ID, ID: u.ID,
Username: u.Username, Username: u.Username,
DisplayName: u.DisplayName, DisplayName: u.DisplayName,
@ -49,6 +50,18 @@ func dbUserToResponse(u db.User, fields []db.Field, names []db.Name, pronouns []
Pronouns: pronouns, Pronouns: pronouns,
Fields: fields, Fields: fields,
} }
resp.Members = make([]PartialMember, len(members))
for i := range members {
resp.Members[i] = PartialMember{
ID: members[i].ID,
Name: members[i].Name,
DisplayName: members[i].DisplayName,
AvatarURLs: members[i].AvatarURLs,
}
}
return resp
} }
func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error { func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error {
@ -77,7 +90,13 @@ func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error {
return err return err
} }
render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns)) members, err := s.DB.UserMembers(ctx, u.ID)
if err != nil {
log.Errorf("Error getting user members: %v", err)
return err
}
render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns, members))
return nil return nil
} else if err != db.ErrUserNotFound { } else if err != db.ErrUserNotFound {
log.Errorf("Error getting user by ID: %v", err) log.Errorf("Error getting user by ID: %v", err)
@ -115,7 +134,13 @@ func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error {
return err return err
} }
render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns)) members, err := s.DB.UserMembers(ctx, u.ID)
if err != nil {
log.Errorf("Error getting user members: %v", err)
return err
}
render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns, members))
return nil return nil
} }
@ -147,8 +172,14 @@ func (s *Server) getMeUser(w http.ResponseWriter, r *http.Request) error {
return err return err
} }
members, err := s.DB.UserMembers(ctx, u.ID)
if err != nil {
log.Errorf("Error getting user members: %v", err)
return err
}
render.JSON(w, r, GetMeResponse{ render.JSON(w, r, GetMeResponse{
GetUserResponse: dbUserToResponse(u, fields, names, pronouns), GetUserResponse: dbUserToResponse(u, fields, names, pronouns, members),
Discord: u.Discord, Discord: u.Discord,
DiscordUsername: u.DiscordUsername, DiscordUsername: u.DiscordUsername,
}) })

View File

@ -194,7 +194,7 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
} }
// echo the updated user back on success // echo the updated user back on success
render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns)) render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns, nil))
return nil return nil
} }