From c6537c920d47a9574cddd126e4fe688032babf18 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 14 Jan 2023 17:33:18 +0100 Subject: [PATCH] feat: read/write improved fields for users, read improved names/pronouns for users --- Makefile | 4 +++ backend/db/names_pronouns.go | 53 ++++++++++++++----------------- backend/db/user.go | 28 ++++++++++++++-- backend/routes/bot/bot.go | 16 +++++++--- backend/routes/user/get_user.go | 48 ++++------------------------ backend/routes/user/patch_user.go | 24 +++----------- frontend/lib/api-fetch.ts | 11 ++++--- frontend/lib/api.ts | 30 +++-------------- 8 files changed, 87 insertions(+), 127 deletions(-) diff --git a/Makefile b/Makefile index 2e52632..25af4c1 100644 --- a/Makefile +++ b/Makefile @@ -9,3 +9,7 @@ seeddb: .PHONY: backend backend: CGO_ENABLED=0 go build -v -o pronouns -ldflags="-buildid= -X codeberg.org/u1f320/pronouns.cc/backend/server.Revision=`git rev-parse --short HEAD`" ./backend + +.PHONY: generate +generate: + go generate ./... diff --git a/backend/db/names_pronouns.go b/backend/db/names_pronouns.go index 726509a..e5d0907 100644 --- a/backend/db/names_pronouns.go +++ b/backend/db/names_pronouns.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "codeberg.org/u1f320/pronouns.cc/backend/db/queries" "emperror.dev/errors" "github.com/georgysavva/scany/pgxscan" "github.com/jackc/pgx/v4" @@ -75,35 +76,6 @@ func (p Pronoun) String() string { return strings.Join(split[:1], "/") } -func (db *DB) UserNames(ctx context.Context, userID xid.ID) (ns []Name, err error) { - sql, args, err := sq.Select("id", "name", "status").From("user_names").Where("user_id = ?", userID).OrderBy("id").ToSql() - if err != nil { - return nil, errors.Wrap(err, "building sql") - } - - err = pgxscan.Select(ctx, db, &ns, sql, args...) - if err != nil { - return nil, errors.Wrap(err, "executing query") - } - return ns, nil -} - -func (db *DB) UserPronouns(ctx context.Context, userID xid.ID) (ps []Pronoun, err error) { - sql, args, err := sq. - Select("id", "display_text", "pronouns", "status"). - From("user_pronouns").Where("user_id = ?", userID). - OrderBy("id").ToSql() - if err != nil { - return nil, errors.Wrap(err, "building sql") - } - - err = pgxscan.Select(ctx, db, &ps, sql, args...) - if err != nil { - return nil, errors.Wrap(err, "executing query") - } - return ps, nil -} - func (db *DB) SetUserNames(ctx context.Context, tx pgx.Tx, userID xid.ID, names []Name) (err error) { sql, args, err := sq.Delete("user_names").Where("user_id = ?", userID).ToSql() if err != nil { @@ -242,3 +214,26 @@ func (db *DB) SetMemberPronouns(ctx context.Context, tx pgx.Tx, memberID xid.ID, } return nil } + +func namesFromDB(dn []queries.FieldEntry) []Name { + names := make([]Name, len(dn)) + for i := range dn { + names[i] = Name{ + Name: *dn[i].Value, + Status: WordStatus(*dn[i].Status), + } + } + return names +} + +func pronounsFromDB(dn []queries.PronounEntry) []Pronoun { + pronouns := make([]Pronoun, len(dn)) + for i := range dn { + pronouns[i] = Pronoun{ + DisplayText: dn[i].DisplayValue, + Pronouns: *dn[i].Value, + Status: WordStatus(*dn[i].Status), + } + } + return pronouns +} diff --git a/backend/db/user.go b/backend/db/user.go index 9c13205..4b68afe 100644 --- a/backend/db/user.go +++ b/backend/db/user.go @@ -22,6 +22,9 @@ type User struct { AvatarURLs []string `db:"avatar_urls"` Links []string + Names []Name + Pronouns []Pronoun + Discord *string DiscordUsername *string @@ -130,6 +133,8 @@ func (db *DB) getUser(ctx context.Context, q querier, id xid.ID) (u User, err er DisplayName: qu.DisplayName, Bio: qu.Bio, AvatarURLs: qu.AvatarUrls, + Names: namesFromDB(qu.Names), + Pronouns: pronounsFromDB(qu.Pronouns), Links: qu.Links, Discord: qu.Discord, DiscordUsername: qu.DiscordUsername, @@ -146,13 +151,32 @@ func (db *DB) User(ctx context.Context, id xid.ID) (u User, err error) { // Username gets a user by username. func (db *DB) Username(ctx context.Context, name string) (u User, err error) { - err = pgxscan.Get(ctx, db, &u, "select * from users where username = $1", name) + qu, err := db.q.GetUserByUsername(ctx, name) if err != nil { if errors.Cause(err) == pgx.ErrNoRows { return u, ErrUserNotFound } - return u, errors.Cause(err) + return u, errors.Wrap(err, "getting user from db") + } + + id, err := xid.FromString(qu.ID) + if err != nil { + return u, errors.Wrap(err, "parsing ID") + } + + u = User{ + ID: id, + Username: qu.Username, + DisplayName: qu.DisplayName, + Bio: qu.Bio, + AvatarURLs: qu.AvatarUrls, + Names: namesFromDB(qu.Names), + Pronouns: pronounsFromDB(qu.Pronouns), + Links: qu.Links, + Discord: qu.Discord, + DiscordUsername: qu.DiscordUsername, + MaxInvites: int(qu.MaxInvites), } return u, nil diff --git a/backend/routes/bot/bot.go b/backend/routes/bot/bot.go index 251d027..5c1aa8f 100644 --- a/backend/routes/bot/bot.go +++ b/backend/routes/bot/bot.go @@ -133,17 +133,25 @@ func (bot *Bot) userPronouns(w http.ResponseWriter, r *http.Request, ev *discord } for _, field := range fields { - if len(field.Favourite) == 0 { + var favs []db.FieldEntry + + for _, e := range field.Entries { + if e.Status == db.StatusFavourite { + favs = append(favs, e) + } + } + + if len(favs) == 0 { continue } var value string - for _, fav := range field.Favourite { - if len(value) > 500 { + for _, fav := range favs { + if len(fav.Value) > 500 { break } - value += fav + "\n" + value += fav.Value + "\n" } e.Fields = append(e.Fields, &discordgo.MessageEmbedField{ diff --git a/backend/routes/user/get_user.go b/backend/routes/user/get_user.go index dcb63a7..78b243c 100644 --- a/backend/routes/user/get_user.go +++ b/backend/routes/user/get_user.go @@ -38,7 +38,7 @@ type PartialMember struct { AvatarURLs []string `json:"avatar_urls"` } -func dbUserToResponse(u db.User, fields []db.Field, names []db.Name, pronouns []db.Pronoun, members []db.Member) GetUserResponse { +func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUserResponse { resp := GetUserResponse{ ID: u.ID, Username: u.Username, @@ -46,8 +46,8 @@ func dbUserToResponse(u db.User, fields []db.Field, names []db.Name, pronouns [] Bio: u.Bio, AvatarURLs: u.AvatarURLs, Links: u.Links, - Names: names, - Pronouns: pronouns, + Names: u.Names, + Pronouns: u.Pronouns, Fields: fields, } @@ -78,25 +78,13 @@ func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error { return err } - names, err := s.DB.UserNames(ctx, u.ID) - if err != nil { - log.Errorf("getting user names: %v", err) - return err - } - - pronouns, err := s.DB.UserPronouns(ctx, u.ID) - if err != nil { - log.Errorf("getting user pronouns: %v", 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, dbUserToResponse(u, fields, names, pronouns, members)) + render.JSON(w, r, dbUserToResponse(u, fields, members)) return nil } else if err != db.ErrUserNotFound { log.Errorf("Error getting user by ID: %v", err) @@ -116,18 +104,6 @@ func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error { return err } - names, err := s.DB.UserNames(ctx, u.ID) - if err != nil { - log.Errorf("getting user names: %v", err) - return err - } - - pronouns, err := s.DB.UserPronouns(ctx, u.ID) - if err != nil { - log.Errorf("getting user pronouns: %v", err) - return err - } - fields, err := s.DB.UserFields(ctx, u.ID) if err != nil { log.Errorf("Error getting user fields: %v", err) @@ -140,7 +116,7 @@ func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error { return err } - render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns, members)) + render.JSON(w, r, dbUserToResponse(u, fields, members)) return nil } @@ -154,18 +130,6 @@ func (s *Server) getMeUser(w http.ResponseWriter, r *http.Request) error { return err } - names, err := s.DB.UserNames(ctx, u.ID) - if err != nil { - log.Errorf("getting user names: %v", err) - return err - } - - pronouns, err := s.DB.UserPronouns(ctx, u.ID) - if err != nil { - log.Errorf("getting user pronouns: %v", err) - return err - } - fields, err := s.DB.UserFields(ctx, u.ID) if err != nil { log.Errorf("Error getting user fields: %v", err) @@ -179,7 +143,7 @@ func (s *Server) getMeUser(w http.ResponseWriter, r *http.Request) error { } render.JSON(w, r, GetMeResponse{ - GetUserResponse: dbUserToResponse(u, fields, names, pronouns, members), + GetUserResponse: dbUserToResponse(u, fields, members), Discord: u.Discord, DiscordUsername: u.DiscordUsername, }) diff --git a/backend/routes/user/patch_user.go b/backend/routes/user/patch_user.go index 3e8561b..b46e9f7 100644 --- a/backend/routes/user/patch_user.go +++ b/backend/routes/user/patch_user.go @@ -159,11 +159,7 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error { return err } - var ( - names []db.Name - pronouns []db.Pronoun - fields []db.Field - ) + var fields []db.Field if req.Names != nil { err = s.DB.SetUserNames(ctx, tx, claims.UserID, *req.Names) @@ -171,13 +167,7 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error { log.Errorf("setting names for user %v: %v", claims.UserID, err) return err } - names = *req.Names - } else { - names, err = s.DB.UserNames(ctx, claims.UserID) - if err != nil { - log.Errorf("getting names for user %v: %v", claims.UserID, err) - return err - } + u.Names = *req.Names } if req.Pronouns != nil { @@ -186,13 +176,7 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error { log.Errorf("setting pronouns for user %v: %v", claims.UserID, err) return err } - pronouns = *req.Pronouns - } else { - pronouns, err = s.DB.UserPronouns(ctx, claims.UserID) - if err != nil { - log.Errorf("getting fields for user %v: %v", claims.UserID, err) - return err - } + u.Pronouns = *req.Pronouns } if req.Fields != nil { @@ -217,7 +201,7 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error { } // echo the updated user back on success - render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns, nil)) + render.JSON(w, r, dbUserToResponse(u, fields, nil)) return nil } diff --git a/frontend/lib/api-fetch.ts b/frontend/lib/api-fetch.ts index a8eedf6..1d8f590 100644 --- a/frontend/lib/api-fetch.ts +++ b/frontend/lib/api-fetch.ts @@ -51,11 +51,12 @@ export interface Pronoun { export interface Field { name: string; - favourite: Arr; - okay: Arr; - jokingly: Arr; - friends_only: Arr; - avoid: Arr; + entries: Arr; +} + +export interface FieldEntry { + value: string; + status: WordStatus; } export interface APIError { diff --git a/frontend/lib/api.ts b/frontend/lib/api.ts index c7fcbd1..a9e8a81 100644 --- a/frontend/lib/api.ts +++ b/frontend/lib/api.ts @@ -199,31 +199,11 @@ export class Pronouns extends Label { export class Field { name: string; labels: Label[]; - constructor({ - name, - favourite, - okay, - jokingly, - friends_only, - avoid, - }: API.Field) { + constructor({ name, entries }: API.Field) { this.name = name; - function transpose(arr: API.Arr, status: LabelStatus): Label[] { - return (arr ?? []).map( - (text) => - new Label({ - displayText: null, - text, - status, - }) - ); - } - this.labels = [ - ...transpose(favourite, LabelStatus.Favourite), - ...transpose(okay, LabelStatus.Okay), - ...transpose(jokingly, LabelStatus.Jokingly), - ...transpose(friends_only, LabelStatus.FriendsOnly), - ...transpose(avoid, LabelStatus.Avoid), - ]; + this.labels = + entries?.map( + (e) => new Label({ displayText: null, text: e.value, status: e.status }) + ) ?? []; } }