forked from mirrors/pronouns.cc
feat: read/write improved names/pronouns for users, read/write improved fields/names/pronouns for members
This commit is contained in:
parent
c6537c920d
commit
d6017f1edf
11 changed files with 231 additions and 370 deletions
|
@ -1,6 +1,11 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import "codeberg.org/u1f320/pronouns.cc/backend/db/queries"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"codeberg.org/u1f320/pronouns.cc/backend/db/queries"
|
||||||
|
)
|
||||||
|
|
||||||
type WordStatus int
|
type WordStatus int
|
||||||
|
|
||||||
|
@ -19,12 +24,63 @@ type FieldEntry struct {
|
||||||
Status WordStatus `json:"status"`
|
Status WordStatus `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fe FieldEntry) Validate() string {
|
||||||
|
if fe.Value == "" {
|
||||||
|
return "value cannot be empty"
|
||||||
|
}
|
||||||
|
|
||||||
|
if len([]rune(fe.Value)) > FieldEntryMaxLength {
|
||||||
|
return fmt.Sprintf("name must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(fe.Value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
if fe.Status == StatusUnknown || fe.Status >= wordStatusMax {
|
||||||
|
return fmt.Sprintf("status is invalid, must be between 1 and %d, is %d", wordStatusMax-1, fe.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
type PronounEntry struct {
|
type PronounEntry struct {
|
||||||
Pronouns string `json:"pronouns"`
|
Pronouns string `json:"pronouns"`
|
||||||
DisplayText *string `json:"display_text"`
|
DisplayText *string `json:"display_text"`
|
||||||
Status WordStatus `json:"status"`
|
Status WordStatus `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p PronounEntry) Validate() string {
|
||||||
|
if p.Pronouns == "" {
|
||||||
|
return "pronouns cannot be empty"
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.DisplayText != nil {
|
||||||
|
if len([]rune(*p.DisplayText)) > FieldEntryMaxLength {
|
||||||
|
return fmt.Sprintf("display_text must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(*p.DisplayText)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len([]rune(p.Pronouns)) > FieldEntryMaxLength {
|
||||||
|
return fmt.Sprintf("pronouns must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(p.Pronouns)))
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.Status == StatusUnknown || p.Status >= wordStatusMax {
|
||||||
|
return fmt.Sprintf("status is invalid, must be between 1 and %d, is %d", wordStatusMax-1, p.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PronounEntry) String() string {
|
||||||
|
if p.DisplayText != nil {
|
||||||
|
return *p.DisplayText
|
||||||
|
}
|
||||||
|
|
||||||
|
split := strings.Split(p.Pronouns, "/")
|
||||||
|
if len(split) <= 2 {
|
||||||
|
return strings.Join(split, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(split[:1], "/")
|
||||||
|
}
|
||||||
|
|
||||||
func dbEntriesToFieldEntries(entries []queries.FieldEntry) []FieldEntry {
|
func dbEntriesToFieldEntries(entries []queries.FieldEntry) []FieldEntry {
|
||||||
out := make([]FieldEntry, len(entries))
|
out := make([]FieldEntry, len(entries))
|
||||||
for i := range entries {
|
for i := range entries {
|
||||||
|
@ -35,22 +91,13 @@ func dbEntriesToFieldEntries(entries []queries.FieldEntry) []FieldEntry {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbPronounEntriesToPronounEntries(entries []queries.PronounEntry) []PronounEntry {
|
|
||||||
out := make([]PronounEntry, len(entries))
|
|
||||||
for i := range entries {
|
|
||||||
out[i] = PronounEntry{
|
|
||||||
*entries[i].Value, entries[i].DisplayValue, WordStatus(*entries[i].Status),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func entriesToDBEntries(entries []FieldEntry) []queries.FieldEntry {
|
func entriesToDBEntries(entries []FieldEntry) []queries.FieldEntry {
|
||||||
out := make([]queries.FieldEntry, len(entries))
|
out := make([]queries.FieldEntry, len(entries))
|
||||||
for i := range entries {
|
for i := range entries {
|
||||||
status := int32(entries[i].Status)
|
status := int32(entries[i].Status)
|
||||||
out[i] = queries.FieldEntry{
|
out[i] = queries.FieldEntry{
|
||||||
&entries[i].Value, &status,
|
Value: &entries[i].Value,
|
||||||
|
Status: &status,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
|
@ -61,7 +108,9 @@ func pronounEntriesToDBEntries(entries []PronounEntry) []queries.PronounEntry {
|
||||||
for i := range entries {
|
for i := range entries {
|
||||||
status := int32(entries[i].Status)
|
status := int32(entries[i].Status)
|
||||||
out[i] = queries.PronounEntry{
|
out[i] = queries.PronounEntry{
|
||||||
&entries[i].Pronouns, entries[i].DisplayText, &status,
|
Value: &entries[i].Pronouns,
|
||||||
|
DisplayValue: entries[i].DisplayText,
|
||||||
|
Status: &status,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
|
|
|
@ -3,6 +3,7 @@ package db
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"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/pgconn"
|
"github.com/jackc/pgconn"
|
||||||
|
@ -23,6 +24,8 @@ type Member struct {
|
||||||
Bio *string
|
Bio *string
|
||||||
AvatarURLs []string `db:"avatar_urls"`
|
AvatarURLs []string `db:"avatar_urls"`
|
||||||
Links []string
|
Links []string
|
||||||
|
Names []FieldEntry
|
||||||
|
Pronouns []PronounEntry
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -30,19 +33,27 @@ const (
|
||||||
ErrMemberNameInUse = errors.Sentinel("member name already in use")
|
ErrMemberNameInUse = errors.Sentinel("member name already in use")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (db *DB) getMember(ctx context.Context, q pgxscan.Querier, id xid.ID) (m Member, err error) {
|
func (db *DB) getMember(ctx context.Context, q querier, id xid.ID) (m Member, err error) {
|
||||||
sql, args, err := sq.Select("*").From("members").Where("id = ?", id).ToSql()
|
qm, err := queries.NewQuerier(q).GetMemberByID(ctx, id.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return m, errors.Wrap(err, "building sql")
|
return m, errors.Wrap(err, "getting member from db")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pgxscan.Get(ctx, q, &m, sql, args...)
|
userID, err := xid.FromString(qm.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Cause(err) == pgx.ErrNoRows {
|
return m, errors.Wrap(err, "parsing user ID")
|
||||||
return m, ErrMemberNotFound
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, errors.Wrap(err, "retrieving member")
|
m = Member{
|
||||||
|
ID: id,
|
||||||
|
UserID: userID,
|
||||||
|
Name: qm.Name,
|
||||||
|
DisplayName: qm.DisplayName,
|
||||||
|
Bio: qm.Bio,
|
||||||
|
AvatarURLs: qm.AvatarUrls,
|
||||||
|
Links: qm.Links,
|
||||||
|
Names: fieldEntriesFromDB(qm.Names),
|
||||||
|
Pronouns: pronounsFromDB(qm.Pronouns),
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
@ -53,26 +64,35 @@ func (db *DB) Member(ctx context.Context, id xid.ID) (m Member, err error) {
|
||||||
|
|
||||||
// UserMember returns a member scoped by user.
|
// UserMember returns a member scoped by user.
|
||||||
func (db *DB) UserMember(ctx context.Context, userID xid.ID, memberRef string) (m Member, err error) {
|
func (db *DB) UserMember(ctx context.Context, userID xid.ID, memberRef string) (m Member, err error) {
|
||||||
sql, args, err := sq.Select("*").From("members").
|
qm, err := db.q.GetMemberByName(ctx, userID.String(), memberRef)
|
||||||
Where("user_id = ? and (id = ? or name = ?)", userID, memberRef, memberRef).ToSql()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return m, errors.Wrap(err, "building sql")
|
return m, errors.Wrap(err, "getting member from db")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pgxscan.Get(ctx, db, &m, sql, args...)
|
memberID, err := xid.FromString(qm.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Cause(err) == pgx.ErrNoRows {
|
return m, errors.Wrap(err, "parsing member ID")
|
||||||
return m, ErrMemberNotFound
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, errors.Wrap(err, "retrieving member")
|
m = Member{
|
||||||
|
ID: memberID,
|
||||||
|
UserID: userID,
|
||||||
|
Name: qm.Name,
|
||||||
|
DisplayName: qm.DisplayName,
|
||||||
|
Bio: qm.Bio,
|
||||||
|
AvatarURLs: qm.AvatarUrls,
|
||||||
|
Links: qm.Links,
|
||||||
|
Names: fieldEntriesFromDB(qm.Names),
|
||||||
|
Pronouns: pronounsFromDB(qm.Pronouns),
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserMembers returns all of a user's members, sorted by name.
|
// UserMembers returns all of a user's members, sorted by name.
|
||||||
func (db *DB) UserMembers(ctx context.Context, userID xid.ID) (ms []Member, err error) {
|
func (db *DB) UserMembers(ctx context.Context, userID xid.ID) (ms []Member, err error) {
|
||||||
sql, args, err := sq.Select("*").From("members").Where("user_id = ?", userID).OrderBy("name", "id").ToSql()
|
sql, args, err := sq.Select("id", "user_id", "name", "display_name", "bio", "avatar_urls").
|
||||||
|
From("members").Where("user_id = ?", userID).
|
||||||
|
OrderBy("name", "id").ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "building sql")
|
return nil, errors.Wrap(err, "building sql")
|
||||||
}
|
}
|
||||||
|
@ -93,12 +113,13 @@ func (db *DB) CreateMember(ctx context.Context, tx pgx.Tx, userID xid.ID, name s
|
||||||
sql, args, err := sq.Insert("members").
|
sql, args, err := sq.Insert("members").
|
||||||
Columns("user_id", "id", "name", "display_name", "bio", "links").
|
Columns("user_id", "id", "name", "display_name", "bio", "links").
|
||||||
Values(userID, xid.New(), name, displayName, bio, links).
|
Values(userID, xid.New(), name, displayName, bio, links).
|
||||||
Suffix("RETURNING *").ToSql()
|
Suffix("RETURNING id").ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return m, errors.Wrap(err, "building sql")
|
return m, errors.Wrap(err, "building sql")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pgxscan.Get(ctx, tx, &m, sql, args...)
|
var id xid.ID
|
||||||
|
err = tx.QueryRow(ctx, sql, args...).Scan(&id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pge := &pgconn.PgError{}
|
pge := &pgconn.PgError{}
|
||||||
if errors.As(err, &pge) {
|
if errors.As(err, &pge) {
|
||||||
|
@ -111,6 +132,11 @@ func (db *DB) CreateMember(ctx context.Context, tx pgx.Tx, userID xid.ID, name s
|
||||||
return m, errors.Wrap(err, "executing query")
|
return m, errors.Wrap(err, "executing query")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m, err = db.getMember(ctx, tx, id)
|
||||||
|
if err != nil {
|
||||||
|
return m, errors.Wrap(err, "getting created member")
|
||||||
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,12 +218,12 @@ func (db *DB) UpdateMember(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sql, args, err := builder.Suffix("RETURNING *").ToSql()
|
sql, args, err := builder.ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return m, errors.Wrap(err, "building sql")
|
return m, errors.Wrap(err, "building sql")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pgxscan.Get(ctx, tx, &m, sql, args...)
|
_, err = tx.Exec(ctx, sql, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pge := &pgconn.PgError{}
|
pge := &pgconn.PgError{}
|
||||||
if errors.As(err, &pge) {
|
if errors.As(err, &pge) {
|
||||||
|
@ -209,5 +235,10 @@ func (db *DB) UpdateMember(
|
||||||
return m, errors.Wrap(err, "executing sql")
|
return m, errors.Wrap(err, "executing sql")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m, err = db.getMember(ctx, tx, id)
|
||||||
|
if err != nil {
|
||||||
|
return m, errors.Wrap(err, "getting member")
|
||||||
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,234 +2,52 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"codeberg.org/u1f320/pronouns.cc/backend/db/queries"
|
"codeberg.org/u1f320/pronouns.cc/backend/db/queries"
|
||||||
"emperror.dev/errors"
|
"emperror.dev/errors"
|
||||||
"github.com/georgysavva/scany/pgxscan"
|
|
||||||
"github.com/jackc/pgx/v4"
|
"github.com/jackc/pgx/v4"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Name struct {
|
func (db *DB) SetUserNamesPronouns(ctx context.Context, tx pgx.Tx, userID xid.ID, names []FieldEntry, pronouns []PronounEntry) (err error) {
|
||||||
ID int64 `json:"-"`
|
_, err = queries.NewQuerier(tx).UpdateUserNamesPronouns(ctx, queries.UpdateUserNamesPronounsParams{
|
||||||
Name string `json:"name"`
|
ID: userID.String(),
|
||||||
Status WordStatus `json:"status"`
|
Names: entriesToDBEntries(names),
|
||||||
}
|
Pronouns: pronounEntriesToDBEntries(pronouns),
|
||||||
|
})
|
||||||
func (n Name) Validate() string {
|
|
||||||
if n.Name == "" {
|
|
||||||
return "name cannot be empty"
|
|
||||||
}
|
|
||||||
|
|
||||||
if len([]rune(n.Name)) > FieldEntryMaxLength {
|
|
||||||
return fmt.Sprintf("name must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(n.Name)))
|
|
||||||
}
|
|
||||||
|
|
||||||
if n.Status == StatusUnknown || n.Status >= wordStatusMax {
|
|
||||||
return fmt.Sprintf("status is invalid, must be between 1 and %d, is %d", wordStatusMax-1, n.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
type Pronoun struct {
|
|
||||||
ID int64 `json:"-"`
|
|
||||||
DisplayText *string `json:"display_text"`
|
|
||||||
Pronouns string `json:"pronouns"`
|
|
||||||
Status WordStatus `json:"status"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p Pronoun) Validate() string {
|
|
||||||
if p.Pronouns == "" {
|
|
||||||
return "pronouns cannot be empty"
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.DisplayText != nil {
|
|
||||||
if len([]rune(*p.DisplayText)) > FieldEntryMaxLength {
|
|
||||||
return fmt.Sprintf("display_text must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(*p.DisplayText)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len([]rune(p.Pronouns)) > FieldEntryMaxLength {
|
|
||||||
return fmt.Sprintf("pronouns must be %d characters or less, is %d", FieldEntryMaxLength, len([]rune(p.Pronouns)))
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.Status == StatusUnknown || p.Status >= wordStatusMax {
|
|
||||||
return fmt.Sprintf("status is invalid, must be between 1 and %d, is %d", wordStatusMax-1, p.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p Pronoun) String() string {
|
|
||||||
if p.DisplayText != nil {
|
|
||||||
return *p.DisplayText
|
|
||||||
}
|
|
||||||
|
|
||||||
split := strings.Split(p.Pronouns, "/")
|
|
||||||
if len(split) <= 2 {
|
|
||||||
return strings.Join(split, "/")
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(split[:1], "/")
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
return errors.Wrap(err, "building sql")
|
return errors.Wrap(err, "executing update names/pronouns query")
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tx.Exec(ctx, sql, args...)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "deleting existing names")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tx.CopyFrom(ctx,
|
|
||||||
pgx.Identifier{"user_names"},
|
|
||||||
[]string{"user_id", "name", "status"},
|
|
||||||
pgx.CopyFromSlice(len(names), func(i int) ([]any, error) {
|
|
||||||
return []any{
|
|
||||||
userID,
|
|
||||||
names[i].Name,
|
|
||||||
names[i].Status,
|
|
||||||
}, nil
|
|
||||||
}))
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "inserting new names")
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) SetUserPronouns(ctx context.Context, tx pgx.Tx, userID xid.ID, names []Pronoun) (err error) {
|
func (db *DB) SetMemberNamesPronouns(ctx context.Context, tx pgx.Tx, memberID xid.ID, names []FieldEntry, pronouns []PronounEntry) (err error) {
|
||||||
sql, args, err := sq.Delete("user_pronouns").Where("user_id = ?", userID).ToSql()
|
_, err = queries.NewQuerier(tx).UpdateMemberNamesPronouns(ctx, queries.UpdateMemberNamesPronounsParams{
|
||||||
|
ID: memberID.String(),
|
||||||
|
Names: entriesToDBEntries(names),
|
||||||
|
Pronouns: pronounEntriesToDBEntries(pronouns),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "building sql")
|
return errors.Wrap(err, "executing update names/pronouns query")
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tx.Exec(ctx, sql, args...)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "deleting existing pronouns")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tx.CopyFrom(ctx,
|
|
||||||
pgx.Identifier{"user_pronouns"},
|
|
||||||
[]string{"user_id", "pronouns", "display_text", "status"},
|
|
||||||
pgx.CopyFromSlice(len(names), func(i int) ([]any, error) {
|
|
||||||
return []any{
|
|
||||||
userID,
|
|
||||||
names[i].Pronouns,
|
|
||||||
names[i].DisplayText,
|
|
||||||
names[i].Status,
|
|
||||||
}, nil
|
|
||||||
}))
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "inserting new pronouns")
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) MemberNames(ctx context.Context, memberID xid.ID) (ns []Name, err error) {
|
func fieldEntriesFromDB(dn []queries.FieldEntry) []FieldEntry {
|
||||||
sql, args, err := sq.Select("id", "name", "status").From("member_names").Where("member_id = ?", memberID).OrderBy("id").ToSql()
|
names := make([]FieldEntry, len(dn))
|
||||||
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) MemberPronouns(ctx context.Context, memberID xid.ID) (ps []Pronoun, err error) {
|
|
||||||
sql, args, err := sq.
|
|
||||||
Select("id", "display_text", "pronouns", "status").
|
|
||||||
From("member_pronouns").Where("member_id = ?", memberID).
|
|
||||||
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) SetMemberNames(ctx context.Context, tx pgx.Tx, memberID xid.ID, names []Name) (err error) {
|
|
||||||
sql, args, err := sq.Delete("member_names").Where("member_id = ?", memberID).ToSql()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "building sql")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tx.Exec(ctx, sql, args...)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "deleting existing names")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tx.CopyFrom(ctx,
|
|
||||||
pgx.Identifier{"member_names"},
|
|
||||||
[]string{"member_id", "name", "status"},
|
|
||||||
pgx.CopyFromSlice(len(names), func(i int) ([]any, error) {
|
|
||||||
return []any{
|
|
||||||
memberID,
|
|
||||||
names[i].Name,
|
|
||||||
names[i].Status,
|
|
||||||
}, nil
|
|
||||||
}))
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "inserting new names")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) SetMemberPronouns(ctx context.Context, tx pgx.Tx, memberID xid.ID, names []Pronoun) (err error) {
|
|
||||||
sql, args, err := sq.Delete("member_pronouns").Where("member_id = ?", memberID).ToSql()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "building sql")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tx.Exec(ctx, sql, args...)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "deleting existing pronouns")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tx.CopyFrom(ctx,
|
|
||||||
pgx.Identifier{"member_pronouns"},
|
|
||||||
[]string{"member_id", "pronouns", "display_text", "status"},
|
|
||||||
pgx.CopyFromSlice(len(names), func(i int) ([]any, error) {
|
|
||||||
return []any{
|
|
||||||
memberID,
|
|
||||||
names[i].Pronouns,
|
|
||||||
names[i].DisplayText,
|
|
||||||
names[i].Status,
|
|
||||||
}, nil
|
|
||||||
}))
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "inserting new pronouns")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func namesFromDB(dn []queries.FieldEntry) []Name {
|
|
||||||
names := make([]Name, len(dn))
|
|
||||||
for i := range dn {
|
for i := range dn {
|
||||||
names[i] = Name{
|
names[i] = FieldEntry{
|
||||||
Name: *dn[i].Value,
|
Value: *dn[i].Value,
|
||||||
Status: WordStatus(*dn[i].Status),
|
Status: WordStatus(*dn[i].Status),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return names
|
return names
|
||||||
}
|
}
|
||||||
|
|
||||||
func pronounsFromDB(dn []queries.PronounEntry) []Pronoun {
|
func pronounsFromDB(dn []queries.PronounEntry) []PronounEntry {
|
||||||
pronouns := make([]Pronoun, len(dn))
|
pronouns := make([]PronounEntry, len(dn))
|
||||||
for i := range dn {
|
for i := range dn {
|
||||||
pronouns[i] = Pronoun{
|
pronouns[i] = PronounEntry{
|
||||||
DisplayText: dn[i].DisplayValue,
|
DisplayText: dn[i].DisplayValue,
|
||||||
Pronouns: *dn[i].Value,
|
Pronouns: *dn[i].Value,
|
||||||
Status: WordStatus(*dn[i].Status),
|
Status: WordStatus(*dn[i].Status),
|
||||||
|
|
|
@ -22,8 +22,8 @@ type User struct {
|
||||||
AvatarURLs []string `db:"avatar_urls"`
|
AvatarURLs []string `db:"avatar_urls"`
|
||||||
Links []string
|
Links []string
|
||||||
|
|
||||||
Names []Name
|
Names []FieldEntry
|
||||||
Pronouns []Pronoun
|
Pronouns []PronounEntry
|
||||||
|
|
||||||
Discord *string
|
Discord *string
|
||||||
DiscordUsername *string
|
DiscordUsername *string
|
||||||
|
@ -133,7 +133,7 @@ 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),
|
Names: fieldEntriesFromDB(qu.Names),
|
||||||
Pronouns: pronounsFromDB(qu.Pronouns),
|
Pronouns: pronounsFromDB(qu.Pronouns),
|
||||||
Links: qu.Links,
|
Links: qu.Links,
|
||||||
Discord: qu.Discord,
|
Discord: qu.Discord,
|
||||||
|
@ -171,7 +171,7 @@ func (db *DB) Username(ctx context.Context, name string) (u User, err error) {
|
||||||
DisplayName: qu.DisplayName,
|
DisplayName: qu.DisplayName,
|
||||||
Bio: qu.Bio,
|
Bio: qu.Bio,
|
||||||
AvatarURLs: qu.AvatarUrls,
|
AvatarURLs: qu.AvatarUrls,
|
||||||
Names: namesFromDB(qu.Names),
|
Names: fieldEntriesFromDB(qu.Names),
|
||||||
Pronouns: pronounsFromDB(qu.Pronouns),
|
Pronouns: pronounsFromDB(qu.Pronouns),
|
||||||
Links: qu.Links,
|
Links: qu.Links,
|
||||||
Discord: qu.Discord,
|
Discord: qu.Discord,
|
||||||
|
@ -260,15 +260,19 @@ func (db *DB) UpdateUser(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sql, args, err := builder.Suffix("RETURNING *").ToSql()
|
sql, args, err := builder.ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return u, errors.Wrap(err, "building sql")
|
return u, errors.Wrap(err, "building sql")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pgxscan.Get(ctx, tx, &u, sql, args...)
|
_, err = tx.Exec(ctx, sql, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return u, errors.Wrap(err, "executing sql")
|
return u, errors.Wrap(err, "executing sql")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u, err = db.getUser(ctx, tx, id)
|
||||||
|
if err != nil {
|
||||||
|
return u, errors.Wrap(err, "getting updated user")
|
||||||
|
}
|
||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ type CreateMemberRequest struct {
|
||||||
Bio string `json:"bio"`
|
Bio string `json:"bio"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
Links []string `json:"links"`
|
Links []string `json:"links"`
|
||||||
Names []db.Name `json:"names"`
|
Names []db.FieldEntry `json:"names"`
|
||||||
Pronouns []db.Pronoun `json:"pronouns"`
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
||||||
Fields []db.Field `json:"fields"`
|
Fields []db.Field `json:"fields"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,16 +92,14 @@ func (s *Server) createMember(w http.ResponseWriter, r *http.Request) (err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// set names, pronouns, fields
|
// set names, pronouns, fields
|
||||||
err = s.DB.SetMemberNames(ctx, tx, m.ID, cmr.Names)
|
err = s.DB.SetMemberNamesPronouns(ctx, tx, m.ID, cmr.Names, cmr.Pronouns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("setting names for member %v: %v", m.ID, err)
|
log.Errorf("setting names and pronouns for member %v: %v", m.ID, err)
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = s.DB.SetMemberPronouns(ctx, tx, m.ID, cmr.Pronouns)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("setting pronouns for member %v: %v", m.ID, err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
m.Names = cmr.Names
|
||||||
|
m.Pronouns = cmr.Pronouns
|
||||||
|
|
||||||
err = s.DB.SetMemberFields(ctx, tx, m.ID, cmr.Fields)
|
err = s.DB.SetMemberFields(ctx, tx, m.ID, cmr.Fields)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("setting fields for member %v: %v", m.ID, err)
|
log.Errorf("setting fields for member %v: %v", m.ID, err)
|
||||||
|
@ -144,7 +142,7 @@ func (s *Server) createMember(w http.ResponseWriter, r *http.Request) (err error
|
||||||
return errors.Wrap(err, "committing transaction")
|
return errors.Wrap(err, "committing transaction")
|
||||||
}
|
}
|
||||||
|
|
||||||
render.JSON(w, r, dbMemberToMember(u, m, cmr.Names, cmr.Pronouns, cmr.Fields))
|
render.JSON(w, r, dbMemberToMember(u, m, cmr.Fields))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,14 +19,14 @@ type GetMemberResponse struct {
|
||||||
AvatarURLs []string `json:"avatar_urls"`
|
AvatarURLs []string `json:"avatar_urls"`
|
||||||
Links []string `json:"links"`
|
Links []string `json:"links"`
|
||||||
|
|
||||||
Names []db.Name `json:"names"`
|
Names []db.FieldEntry `json:"names"`
|
||||||
Pronouns []db.Pronoun `json:"pronouns"`
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
||||||
Fields []db.Field `json:"fields"`
|
Fields []db.Field `json:"fields"`
|
||||||
|
|
||||||
User PartialUser `json:"user"`
|
User PartialUser `json:"user"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbMemberToMember(u db.User, m db.Member, names []db.Name, pronouns []db.Pronoun, fields []db.Field) GetMemberResponse {
|
func dbMemberToMember(u db.User, m db.Member, fields []db.Field) GetMemberResponse {
|
||||||
return GetMemberResponse{
|
return GetMemberResponse{
|
||||||
ID: m.ID,
|
ID: m.ID,
|
||||||
Name: m.Name,
|
Name: m.Name,
|
||||||
|
@ -35,8 +35,8 @@ func dbMemberToMember(u db.User, m db.Member, names []db.Name, pronouns []db.Pro
|
||||||
AvatarURLs: m.AvatarURLs,
|
AvatarURLs: m.AvatarURLs,
|
||||||
Links: m.Links,
|
Links: m.Links,
|
||||||
|
|
||||||
Names: names,
|
Names: m.Names,
|
||||||
Pronouns: pronouns,
|
Pronouns: m.Pronouns,
|
||||||
Fields: fields,
|
Fields: fields,
|
||||||
|
|
||||||
User: PartialUser{
|
User: PartialUser{
|
||||||
|
@ -77,22 +77,12 @@ func (s *Server) getMember(w http.ResponseWriter, r *http.Request) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
names, err := s.DB.MemberNames(ctx, m.ID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
pronouns, err := s.DB.MemberPronouns(ctx, m.ID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fields, err := s.DB.MemberFields(ctx, m.ID)
|
fields, err := s.DB.MemberFields(ctx, m.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
render.JSON(w, r, dbMemberToMember(u, m, names, pronouns, fields))
|
render.JSON(w, r, dbMemberToMember(u, m, fields))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,22 +103,12 @@ func (s *Server) getUserMember(w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
names, err := s.DB.MemberNames(ctx, m.ID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
pronouns, err := s.DB.MemberPronouns(ctx, m.ID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fields, err := s.DB.MemberFields(ctx, m.ID)
|
fields, err := s.DB.MemberFields(ctx, m.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
render.JSON(w, r, dbMemberToMember(u, m, names, pronouns, fields))
|
render.JSON(w, r, dbMemberToMember(u, m, fields))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@ type PatchMemberRequest struct {
|
||||||
Bio *string `json:"bio"`
|
Bio *string `json:"bio"`
|
||||||
DisplayName *string `json:"display_name"`
|
DisplayName *string `json:"display_name"`
|
||||||
Links *[]string `json:"links"`
|
Links *[]string `json:"links"`
|
||||||
Names *[]db.Name `json:"names"`
|
Names *[]db.FieldEntry `json:"names"`
|
||||||
Pronouns *[]db.Pronoun `json:"pronouns"`
|
Pronouns *[]db.PronounEntry `json:"pronouns"`
|
||||||
Fields *[]db.Field `json:"fields"`
|
Fields *[]db.Field `json:"fields"`
|
||||||
Avatar *string `json:"avatar"`
|
Avatar *string `json:"avatar"`
|
||||||
}
|
}
|
||||||
|
@ -169,42 +169,27 @@ func (s *Server) patchMember(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
if req.Names != nil || req.Pronouns != nil {
|
||||||
names []db.Name
|
names := m.Names
|
||||||
pronouns []db.Pronoun
|
pronouns := m.Pronouns
|
||||||
fields []db.Field
|
|
||||||
)
|
|
||||||
|
|
||||||
if req.Names != nil {
|
if req.Names != nil {
|
||||||
err = s.DB.SetMemberNames(ctx, tx, id, *req.Names)
|
names = *req.Names
|
||||||
|
}
|
||||||
|
if req.Pronouns != nil {
|
||||||
|
pronouns = *req.Pronouns
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.DB.SetMemberNamesPronouns(ctx, tx, id, names, pronouns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("setting names for member %v: %v", id, err)
|
log.Errorf("setting names for member %v: %v", id, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
names = *req.Names
|
m.Names = names
|
||||||
} else {
|
m.Pronouns = pronouns
|
||||||
names, err = s.DB.MemberNames(ctx, id)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("getting names for member %v: %v", id, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Pronouns != nil {
|
|
||||||
err = s.DB.SetMemberPronouns(ctx, tx, id, *req.Pronouns)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("setting pronouns for member %v: %v", id, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
pronouns = *req.Pronouns
|
|
||||||
} else {
|
|
||||||
pronouns, err = s.DB.MemberPronouns(ctx, id)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("getting fields for member %v: %v", id, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fields []db.Field
|
||||||
if req.Fields != nil {
|
if req.Fields != nil {
|
||||||
err = s.DB.SetMemberFields(ctx, tx, id, *req.Fields)
|
err = s.DB.SetMemberFields(ctx, tx, id, *req.Fields)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -232,6 +217,6 @@ func (s *Server) patchMember(w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// echo the updated member back on success
|
// echo the updated member back on success
|
||||||
render.JSON(w, r, dbMemberToMember(u, m, names, pronouns, fields))
|
render.JSON(w, r, dbMemberToMember(u, m, fields))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,8 @@ type GetUserResponse struct {
|
||||||
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.Name `json:"names"`
|
Names []db.FieldEntry `json:"names"`
|
||||||
Pronouns []db.Pronoun `json:"pronouns"`
|
Pronouns []db.PronounEntry `json:"pronouns"`
|
||||||
Members []PartialMember `json:"members"`
|
Members []PartialMember `json:"members"`
|
||||||
Fields []db.Field `json:"fields"`
|
Fields []db.Field `json:"fields"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ type PatchUserRequest struct {
|
||||||
DisplayName *string `json:"display_name"`
|
DisplayName *string `json:"display_name"`
|
||||||
Bio *string `json:"bio"`
|
Bio *string `json:"bio"`
|
||||||
Links *[]string `json:"links"`
|
Links *[]string `json:"links"`
|
||||||
Names *[]db.Name `json:"names"`
|
Names *[]db.FieldEntry `json:"names"`
|
||||||
Pronouns *[]db.Pronoun `json:"pronouns"`
|
Pronouns *[]db.PronounEntry `json:"pronouns"`
|
||||||
Fields *[]db.Field `json:"fields"`
|
Fields *[]db.Field `json:"fields"`
|
||||||
Avatar *string `json:"avatar"`
|
Avatar *string `json:"avatar"`
|
||||||
}
|
}
|
||||||
|
@ -159,26 +159,27 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var fields []db.Field
|
if req.Names != nil || req.Pronouns != nil {
|
||||||
|
names := u.Names
|
||||||
|
pronouns := u.Pronouns
|
||||||
|
|
||||||
if req.Names != nil {
|
if req.Names != nil {
|
||||||
err = s.DB.SetUserNames(ctx, tx, claims.UserID, *req.Names)
|
names = *req.Names
|
||||||
if err != nil {
|
|
||||||
log.Errorf("setting names for user %v: %v", claims.UserID, err)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
u.Names = *req.Names
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Pronouns != nil {
|
if req.Pronouns != nil {
|
||||||
err = s.DB.SetUserPronouns(ctx, tx, claims.UserID, *req.Pronouns)
|
pronouns = *req.Pronouns
|
||||||
if err != nil {
|
|
||||||
log.Errorf("setting pronouns for user %v: %v", claims.UserID, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
u.Pronouns = *req.Pronouns
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = s.DB.SetUserNamesPronouns(ctx, tx, claims.UserID, names, pronouns)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("setting names for member %v: %v", claims.UserID, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
u.Names = names
|
||||||
|
u.Pronouns = pronouns
|
||||||
|
}
|
||||||
|
|
||||||
|
var fields []db.Field
|
||||||
if req.Fields != nil {
|
if req.Fields != nil {
|
||||||
err = s.DB.SetUserFields(ctx, tx, claims.UserID, *req.Fields)
|
err = s.DB.SetUserFields(ctx, tx, claims.UserID, *req.Fields)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -18,7 +18,7 @@ export type PartialMember = PartialPerson;
|
||||||
export interface _Person extends PartialPerson {
|
export interface _Person extends PartialPerson {
|
||||||
bio: string | null;
|
bio: string | null;
|
||||||
links: Arr<string>;
|
links: Arr<string>;
|
||||||
names: Arr<Name>;
|
names: Arr<FieldEntry>;
|
||||||
pronouns: Arr<Pronoun>;
|
pronouns: Arr<Pronoun>;
|
||||||
fields: Arr<Field>;
|
fields: Arr<Field>;
|
||||||
}
|
}
|
||||||
|
@ -38,11 +38,6 @@ export interface MeUser extends User {
|
||||||
discord_username: string | null;
|
discord_username: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Name {
|
|
||||||
name: string;
|
|
||||||
status: WordStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Pronoun {
|
export interface Pronoun {
|
||||||
display_text?: string;
|
display_text?: string;
|
||||||
pronouns: string;
|
pronouns: string;
|
||||||
|
|
|
@ -164,11 +164,11 @@ export class Label {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Name extends Label {
|
export class Name extends Label {
|
||||||
constructor({ name, status }: API.Name) {
|
constructor({ value, status }: API.FieldEntry) {
|
||||||
super({
|
super({
|
||||||
type: LabelType.Name,
|
type: LabelType.Name,
|
||||||
displayText: null,
|
displayText: null,
|
||||||
text: name,
|
text: value,
|
||||||
status,
|
status,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue