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
|
.PHONY: backend
|
||||||
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
|
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"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"codeberg.org/u1f320/pronouns.cc/backend/db/queries"
|
||||||
"emperror.dev/errors"
|
"emperror.dev/errors"
|
||||||
"github.com/georgysavva/scany/pgxscan"
|
"github.com/georgysavva/scany/pgxscan"
|
||||||
"github.com/jackc/pgx/v4"
|
"github.com/jackc/pgx/v4"
|
||||||
|
@ -75,35 +76,6 @@ func (p Pronoun) String() string {
|
||||||
return strings.Join(split[:1], "/")
|
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) {
|
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()
|
sql, args, err := sq.Delete("user_names").Where("user_id = ?", userID).ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -242,3 +214,26 @@ func (db *DB) SetMemberPronouns(ctx context.Context, tx pgx.Tx, memberID xid.ID,
|
||||||
}
|
}
|
||||||
return nil
|
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"`
|
AvatarURLs []string `db:"avatar_urls"`
|
||||||
Links []string
|
Links []string
|
||||||
|
|
||||||
|
Names []Name
|
||||||
|
Pronouns []Pronoun
|
||||||
|
|
||||||
Discord *string
|
Discord *string
|
||||||
DiscordUsername *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,
|
DisplayName: qu.DisplayName,
|
||||||
Bio: qu.Bio,
|
Bio: qu.Bio,
|
||||||
AvatarURLs: qu.AvatarUrls,
|
AvatarURLs: qu.AvatarUrls,
|
||||||
|
Names: namesFromDB(qu.Names),
|
||||||
|
Pronouns: pronounsFromDB(qu.Pronouns),
|
||||||
Links: qu.Links,
|
Links: qu.Links,
|
||||||
Discord: qu.Discord,
|
Discord: qu.Discord,
|
||||||
DiscordUsername: qu.DiscordUsername,
|
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.
|
// Username gets a user by username.
|
||||||
func (db *DB) Username(ctx context.Context, name string) (u User, err error) {
|
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 err != nil {
|
||||||
if errors.Cause(err) == pgx.ErrNoRows {
|
if errors.Cause(err) == pgx.ErrNoRows {
|
||||||
return u, ErrUserNotFound
|
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
|
return u, nil
|
||||||
|
|
|
@ -133,17 +133,25 @@ func (bot *Bot) userPronouns(w http.ResponseWriter, r *http.Request, ev *discord
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, field := range fields {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var value string
|
var value string
|
||||||
for _, fav := range field.Favourite {
|
for _, fav := range favs {
|
||||||
if len(value) > 500 {
|
if len(fav.Value) > 500 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
value += fav + "\n"
|
value += fav.Value + "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Fields = append(e.Fields, &discordgo.MessageEmbedField{
|
e.Fields = append(e.Fields, &discordgo.MessageEmbedField{
|
||||||
|
|
|
@ -38,7 +38,7 @@ type PartialMember struct {
|
||||||
AvatarURLs []string `json:"avatar_urls"`
|
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{
|
resp := GetUserResponse{
|
||||||
ID: u.ID,
|
ID: u.ID,
|
||||||
Username: u.Username,
|
Username: u.Username,
|
||||||
|
@ -46,8 +46,8 @@ func dbUserToResponse(u db.User, fields []db.Field, names []db.Name, pronouns []
|
||||||
Bio: u.Bio,
|
Bio: u.Bio,
|
||||||
AvatarURLs: u.AvatarURLs,
|
AvatarURLs: u.AvatarURLs,
|
||||||
Links: u.Links,
|
Links: u.Links,
|
||||||
Names: names,
|
Names: u.Names,
|
||||||
Pronouns: pronouns,
|
Pronouns: u.Pronouns,
|
||||||
Fields: fields,
|
Fields: fields,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,25 +78,13 @@ func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error {
|
||||||
return err
|
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)
|
members, err := s.DB.UserMembers(ctx, u.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error getting user members: %v", err)
|
log.Errorf("Error getting user members: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns, members))
|
render.JSON(w, r, dbUserToResponse(u, fields, 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)
|
||||||
|
@ -116,18 +104,6 @@ func (s *Server) getUser(w http.ResponseWriter, r *http.Request) error {
|
||||||
return err
|
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)
|
fields, err := s.DB.UserFields(ctx, u.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error getting user fields: %v", err)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
render.JSON(w, r, dbUserToResponse(u, fields, names, pronouns, members))
|
render.JSON(w, r, dbUserToResponse(u, fields, members))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,18 +130,6 @@ func (s *Server) getMeUser(w http.ResponseWriter, r *http.Request) error {
|
||||||
return err
|
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)
|
fields, err := s.DB.UserFields(ctx, u.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error getting user fields: %v", err)
|
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{
|
render.JSON(w, r, GetMeResponse{
|
||||||
GetUserResponse: dbUserToResponse(u, fields, names, pronouns, members),
|
GetUserResponse: dbUserToResponse(u, fields, members),
|
||||||
Discord: u.Discord,
|
Discord: u.Discord,
|
||||||
DiscordUsername: u.DiscordUsername,
|
DiscordUsername: u.DiscordUsername,
|
||||||
})
|
})
|
||||||
|
|
|
@ -159,11 +159,7 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var fields []db.Field
|
||||||
names []db.Name
|
|
||||||
pronouns []db.Pronoun
|
|
||||||
fields []db.Field
|
|
||||||
)
|
|
||||||
|
|
||||||
if req.Names != nil {
|
if req.Names != nil {
|
||||||
err = s.DB.SetUserNames(ctx, tx, claims.UserID, *req.Names)
|
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)
|
log.Errorf("setting names for user %v: %v", claims.UserID, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
names = *req.Names
|
u.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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.Pronouns != nil {
|
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)
|
log.Errorf("setting pronouns for user %v: %v", claims.UserID, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
pronouns = *req.Pronouns
|
u.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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.Fields != nil {
|
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
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,11 +51,12 @@ export interface Pronoun {
|
||||||
|
|
||||||
export interface Field {
|
export interface Field {
|
||||||
name: string;
|
name: string;
|
||||||
favourite: Arr<string>;
|
entries: Arr<FieldEntry>;
|
||||||
okay: Arr<string>;
|
}
|
||||||
jokingly: Arr<string>;
|
|
||||||
friends_only: Arr<string>;
|
export interface FieldEntry {
|
||||||
avoid: Arr<string>;
|
value: string;
|
||||||
|
status: WordStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface APIError {
|
export interface APIError {
|
||||||
|
|
|
@ -199,31 +199,11 @@ export class Pronouns extends Label {
|
||||||
export class Field {
|
export class Field {
|
||||||
name: string;
|
name: string;
|
||||||
labels: Label[];
|
labels: Label[];
|
||||||
constructor({
|
constructor({ name, entries }: API.Field) {
|
||||||
name,
|
|
||||||
favourite,
|
|
||||||
okay,
|
|
||||||
jokingly,
|
|
||||||
friends_only,
|
|
||||||
avoid,
|
|
||||||
}: API.Field) {
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
function transpose(arr: API.Arr<string>, status: LabelStatus): Label[] {
|
this.labels =
|
||||||
return (arr ?? []).map(
|
entries?.map(
|
||||||
(text) =>
|
(e) => new Label({ displayText: null, text: e.value, status: e.status })
|
||||||
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),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue