feat(backend): always return empty arrays instead of null

This commit is contained in:
Sam 2023-03-12 01:31:31 +01:00
parent b8a7e7443d
commit 0e72097346
No known key found for this signature in database
GPG Key ID: B4EF20DDE721CAA1
6 changed files with 61 additions and 35 deletions

View File

@ -95,10 +95,15 @@ func (s *Server) discordCallback(w http.ResponseWriter, r *http.Request) error {
return errors.Wrap(err, "saving token to database") return errors.Wrap(err, "saving token to database")
} }
fields, err := s.DB.UserFields(ctx, u.ID)
if err != nil {
return errors.Wrap(err, "querying fields")
}
render.JSON(w, r, discordCallbackResponse{ render.JSON(w, r, discordCallbackResponse{
HasAccount: true, HasAccount: true,
Token: token, Token: token,
User: dbUserToUserResponse(u), User: dbUserToUserResponse(u, fields),
}) })
return nil return nil
@ -230,7 +235,7 @@ func (s *Server) discordSignup(w http.ResponseWriter, r *http.Request) error {
// return user // return user
render.JSON(w, r, signupResponse{ render.JSON(w, r, signupResponse{
User: *dbUserToUserResponse(u), User: *dbUserToUserResponse(u, nil),
Token: token, Token: token,
}) })
return nil return nil

View File

@ -20,25 +20,31 @@ type Server struct {
} }
type userResponse struct { type userResponse struct {
ID xid.ID `json:"id"` ID xid.ID `json:"id"`
Username string `json:"name"` Username string `json:"name"`
DisplayName *string `json:"display_name"` DisplayName *string `json:"display_name"`
Bio *string `json:"bio"` Bio *string `json:"bio"`
AvatarURLs []string `json:"avatar_urls"` AvatarURLs []string `json:"avatar_urls"`
Links []string `json:"links"` Links []string `json:"links"`
Names []db.FieldEntry `json:"names"`
Pronouns []db.PronounEntry `json:"pronouns"`
Fields []db.Field `json:"fields"`
Discord *string `json:"discord"` Discord *string `json:"discord"`
DiscordUsername *string `json:"discord_username"` DiscordUsername *string `json:"discord_username"`
} }
func dbUserToUserResponse(u db.User) *userResponse { func dbUserToUserResponse(u db.User, fields []db.Field) *userResponse {
return &userResponse{ return &userResponse{
ID: u.ID, ID: u.ID,
Username: u.Username, Username: u.Username,
DisplayName: u.DisplayName, DisplayName: u.DisplayName,
Bio: u.Bio, Bio: u.Bio,
AvatarURLs: u.AvatarURLs, AvatarURLs: db.NotNull(u.AvatarURLs),
Links: u.Links, Links: db.NotNull(u.Links),
Names: db.NotNull(u.Names),
Pronouns: db.NotNull(u.Pronouns),
Fields: db.NotNull(fields),
Discord: u.Discord, Discord: u.Discord,
DiscordUsername: u.DiscordUsername, DiscordUsername: u.DiscordUsername,
} }

View File

@ -32,18 +32,18 @@ func dbMemberToMember(u db.User, m db.Member, fields []db.Field) GetMemberRespon
Name: m.Name, Name: m.Name,
DisplayName: m.DisplayName, DisplayName: m.DisplayName,
Bio: m.Bio, Bio: m.Bio,
AvatarURLs: m.AvatarURLs, AvatarURLs: db.NotNull(m.AvatarURLs),
Links: m.Links, Links: db.NotNull(m.Links),
Names: m.Names, Names: db.NotNull(m.Names),
Pronouns: m.Pronouns, Pronouns: db.NotNull(m.Pronouns),
Fields: fields, Fields: db.NotNull(fields),
User: PartialUser{ User: PartialUser{
ID: u.ID, ID: u.ID,
Username: u.Username, Username: u.Username,
DisplayName: u.DisplayName, DisplayName: u.DisplayName,
AvatarURLs: u.AvatarURLs, AvatarURLs: db.NotNull(u.AvatarURLs),
}, },
} }
} }

View File

@ -11,12 +11,14 @@ import (
) )
type memberListResponse struct { type memberListResponse struct {
ID xid.ID `json:"id"` ID xid.ID `json:"id"`
Name string `json:"name"` Name string `json:"name"`
DisplayName *string `json:"display_name"` DisplayName *string `json:"display_name"`
Bio *string `json:"bio"` Bio *string `json:"bio"`
AvatarURLs []string `json:"avatar_urls"` AvatarURLs []string `json:"avatar_urls"`
Links []string `json:"links"` Links []string `json:"links"`
Names []db.FieldEntry `json:"names"`
Pronouns []db.PronounEntry `json:"pronouns"`
} }
func membersToMemberList(ms []db.Member) []memberListResponse { func membersToMemberList(ms []db.Member) []memberListResponse {
@ -26,8 +28,10 @@ func membersToMemberList(ms []db.Member) []memberListResponse {
ID: ms[i].ID, ID: ms[i].ID,
Name: ms[i].Name, Name: ms[i].Name,
Bio: ms[i].Bio, Bio: ms[i].Bio,
AvatarURLs: ms[i].AvatarURLs, AvatarURLs: db.NotNull(ms[i].AvatarURLs),
Links: ms[i].Links, Links: db.NotNull(ms[i].Links),
Names: db.NotNull(ms[i].Names),
Pronouns: db.NotNull(ms[i].Pronouns),
} }
} }

View File

@ -68,6 +68,13 @@ func (s *Server) patchMember(w http.ResponseWriter, r *http.Request) error {
} }
} }
if req.Name != nil && *req.Name == "" {
return server.APIError{
Code: server.ErrBadRequest,
Details: "Name must not be empty",
}
}
// validate display name/bio // validate display name/bio
if req.Name != nil && len(*req.Name) > db.MaxMemberNameLength { if req.Name != nil && len(*req.Name) > db.MaxMemberNameLength {
return server.APIError{ return server.APIError{

View File

@ -32,10 +32,12 @@ 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"`
DisplayName *string `json:"display_name"` DisplayName *string `json:"display_name"`
AvatarURLs []string `json:"avatar_urls"` AvatarURLs []string `json:"avatar_urls"`
Names []db.FieldEntry `json:"names"`
Pronouns []db.PronounEntry `json:"pronouns"`
} }
func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUserResponse { func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUserResponse {
@ -44,11 +46,11 @@ func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUser
Username: u.Username, Username: u.Username,
DisplayName: u.DisplayName, DisplayName: u.DisplayName,
Bio: u.Bio, Bio: u.Bio,
AvatarURLs: u.AvatarURLs, AvatarURLs: db.NotNull(u.AvatarURLs),
Links: u.Links, Links: db.NotNull(u.Links),
Names: u.Names, Names: db.NotNull(u.Names),
Pronouns: u.Pronouns, Pronouns: db.NotNull(u.Pronouns),
Fields: fields, Fields: db.NotNull(fields),
} }
resp.Members = make([]PartialMember, len(members)) resp.Members = make([]PartialMember, len(members))
@ -57,7 +59,9 @@ func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUser
ID: members[i].ID, ID: members[i].ID,
Name: members[i].Name, Name: members[i].Name,
DisplayName: members[i].DisplayName, DisplayName: members[i].DisplayName,
AvatarURLs: members[i].AvatarURLs, AvatarURLs: db.NotNull(members[i].AvatarURLs),
Names: db.NotNull(members[i].Names),
Pronouns: db.NotNull(members[i].Pronouns),
} }
} }