feat: read/write improved fields for users, read improved names/pronouns for users
This commit is contained in:
parent
7669595586
commit
c6537c920d
8 changed files with 87 additions and 127 deletions
4
Makefile
4
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 ./...
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -51,11 +51,12 @@ export interface Pronoun {
|
|||
|
||||
export interface Field {
|
||||
name: string;
|
||||
favourite: Arr<string>;
|
||||
okay: Arr<string>;
|
||||
jokingly: Arr<string>;
|
||||
friends_only: Arr<string>;
|
||||
avoid: Arr<string>;
|
||||
entries: Arr<FieldEntry>;
|
||||
}
|
||||
|
||||
export interface FieldEntry {
|
||||
value: string;
|
||||
status: WordStatus;
|
||||
}
|
||||
|
||||
export interface APIError {
|
||||
|
|
|
@ -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<string>, 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 })
|
||||
) ?? [];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue