2022-09-20 03:55:00 -07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/db/queries"
|
2022-09-20 03:55:00 -07:00
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/georgysavva/scany/pgxscan"
|
2022-10-03 01:59:30 -07:00
|
|
|
"github.com/jackc/pgconn"
|
2022-09-20 03:55:00 -07:00
|
|
|
"github.com/jackc/pgx/v4"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
2022-11-21 08:01:51 -08:00
|
|
|
const (
|
|
|
|
MaxMemberCount = 500
|
|
|
|
MaxMemberNameLength = 100
|
|
|
|
)
|
2022-10-03 01:59:30 -07:00
|
|
|
|
2022-09-20 03:55:00 -07:00
|
|
|
type Member struct {
|
2022-11-20 12:09:29 -08:00
|
|
|
ID xid.ID
|
|
|
|
UserID xid.ID
|
|
|
|
Name string
|
|
|
|
DisplayName *string
|
|
|
|
Bio *string
|
|
|
|
AvatarURLs []string `db:"avatar_urls"`
|
|
|
|
Links []string
|
2023-01-30 15:50:17 -08:00
|
|
|
Names []FieldEntry
|
|
|
|
Pronouns []PronounEntry
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2022-10-03 01:59:30 -07:00
|
|
|
const (
|
|
|
|
ErrMemberNotFound = errors.Sentinel("member not found")
|
|
|
|
ErrMemberNameInUse = errors.Sentinel("member name already in use")
|
|
|
|
)
|
2022-09-20 03:55:00 -07:00
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
func (db *DB) getMember(ctx context.Context, q querier, id xid.ID) (m Member, err error) {
|
|
|
|
qm, err := queries.NewQuerier(q).GetMemberByID(ctx, id.String())
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
2023-01-30 15:50:17 -08:00
|
|
|
return m, errors.Wrap(err, "getting member from db")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
userID, err := xid.FromString(qm.UserID)
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
2023-01-30 15:50:17 -08:00
|
|
|
return m, errors.Wrap(err, "parsing user ID")
|
|
|
|
}
|
2022-09-20 03:55:00 -07:00
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
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),
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:31:43 -08:00
|
|
|
func (db *DB) Member(ctx context.Context, id xid.ID) (m Member, err error) {
|
|
|
|
return db.getMember(ctx, db, id)
|
|
|
|
}
|
|
|
|
|
2022-11-22 04:31:42 -08:00
|
|
|
// UserMember returns a member scoped by user.
|
2022-09-20 03:55:00 -07:00
|
|
|
func (db *DB) UserMember(ctx context.Context, userID xid.ID, memberRef string) (m Member, err error) {
|
2023-01-30 15:50:17 -08:00
|
|
|
qm, err := db.q.GetMemberByName(ctx, userID.String(), memberRef)
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
2023-01-30 15:50:17 -08:00
|
|
|
return m, errors.Wrap(err, "getting member from db")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
memberID, err := xid.FromString(qm.ID)
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
2023-01-30 15:50:17 -08:00
|
|
|
return m, errors.Wrap(err, "parsing member ID")
|
|
|
|
}
|
2022-09-20 03:55:00 -07:00
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
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),
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-11-22 04:31:42 -08:00
|
|
|
// UserMembers returns all of a user's members, sorted by name.
|
2022-09-20 03:55:00 -07:00
|
|
|
func (db *DB) UserMembers(ctx context.Context, userID xid.ID) (ms []Member, err error) {
|
2023-01-30 15:50:17 -08:00
|
|
|
sql, args, err := sq.Select("id", "user_id", "name", "display_name", "bio", "avatar_urls").
|
|
|
|
From("members").Where("user_id = ?", userID).
|
|
|
|
OrderBy("name", "id").ToSql()
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Select(ctx, db, &ms, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "retrieving members")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ms == nil {
|
|
|
|
ms = make([]Member, 0)
|
|
|
|
}
|
|
|
|
return ms, nil
|
|
|
|
}
|
2022-10-03 01:59:30 -07:00
|
|
|
|
|
|
|
// CreateMember creates a member.
|
2022-11-20 12:09:29 -08:00
|
|
|
func (db *DB) CreateMember(ctx context.Context, tx pgx.Tx, userID xid.ID, name string, displayName *string, bio string, links []string) (m Member, err error) {
|
2022-10-03 01:59:30 -07:00
|
|
|
sql, args, err := sq.Insert("members").
|
2022-11-20 12:09:29 -08:00
|
|
|
Columns("user_id", "id", "name", "display_name", "bio", "links").
|
|
|
|
Values(userID, xid.New(), name, displayName, bio, links).
|
2023-01-30 15:50:17 -08:00
|
|
|
Suffix("RETURNING id").ToSql()
|
2022-10-03 01:59:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
var id xid.ID
|
|
|
|
err = tx.QueryRow(ctx, sql, args...).Scan(&id)
|
2022-10-03 01:59:30 -07:00
|
|
|
if err != nil {
|
|
|
|
pge := &pgconn.PgError{}
|
|
|
|
if errors.As(err, &pge) {
|
2022-12-22 16:31:43 -08:00
|
|
|
// unique constraint violation
|
2022-10-03 01:59:30 -07:00
|
|
|
if pge.Code == "23505" {
|
|
|
|
return m, ErrMemberNameInUse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
m, err = db.getMember(ctx, tx, id)
|
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "getting created member")
|
|
|
|
}
|
|
|
|
|
2022-10-03 01:59:30 -07:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-11-22 04:31:42 -08:00
|
|
|
// DeleteMember deletes a member by the given ID. This is irreversible.
|
|
|
|
func (db *DB) DeleteMember(ctx context.Context, id xid.ID) (err error) {
|
|
|
|
sql, args, err := sq.Delete("members").Where("id = ?", id).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(ctx, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "deleting member")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-03 01:59:30 -07:00
|
|
|
// MemberCount returns the number of members that the given user has.
|
|
|
|
func (db *DB) MemberCount(ctx context.Context, userID xid.ID) (n int64, err error) {
|
|
|
|
sql, args, err := sq.Select("count(id)").From("members").Where("user_id = ?", userID).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.QueryRow(ctx, sql, args...).Scan(&n)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
2022-11-21 08:01:51 -08:00
|
|
|
|
|
|
|
func (db *DB) UpdateMember(
|
|
|
|
ctx context.Context,
|
|
|
|
tx pgx.Tx, id xid.ID,
|
|
|
|
name, displayName, bio *string,
|
|
|
|
links *[]string,
|
|
|
|
avatarURLs []string,
|
|
|
|
) (m Member, err error) {
|
|
|
|
if name == nil && displayName == nil && bio == nil && links == nil && avatarURLs == nil {
|
2022-12-22 16:31:43 -08:00
|
|
|
return db.getMember(ctx, tx, id)
|
2022-11-21 08:01:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
builder := sq.Update("members").Where("id = ?", id)
|
|
|
|
if name != nil {
|
|
|
|
if *name == "" {
|
|
|
|
builder = builder.Set("name", nil)
|
|
|
|
} else {
|
|
|
|
builder = builder.Set("name", *displayName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if displayName != nil {
|
|
|
|
if *displayName == "" {
|
|
|
|
builder = builder.Set("display_name", nil)
|
|
|
|
} else {
|
|
|
|
builder = builder.Set("display_name", *displayName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bio != nil {
|
|
|
|
if *bio == "" {
|
|
|
|
builder = builder.Set("bio", nil)
|
|
|
|
} else {
|
|
|
|
builder = builder.Set("bio", *bio)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if links != nil {
|
|
|
|
if len(*links) == 0 {
|
|
|
|
builder = builder.Set("links", nil)
|
|
|
|
} else {
|
|
|
|
builder = builder.Set("links", *links)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if avatarURLs != nil {
|
|
|
|
if len(avatarURLs) == 0 {
|
|
|
|
builder = builder.Set("avatar_urls", nil)
|
|
|
|
} else {
|
|
|
|
builder = builder.Set("avatar_urls", avatarURLs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
sql, args, err := builder.ToSql()
|
2022-11-21 08:01:51 -08:00
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
_, err = tx.Exec(ctx, sql, args...)
|
2022-11-21 08:01:51 -08:00
|
|
|
if err != nil {
|
|
|
|
pge := &pgconn.PgError{}
|
|
|
|
if errors.As(err, &pge) {
|
|
|
|
if pge.Code == "23505" {
|
|
|
|
return m, ErrMemberNameInUse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, errors.Wrap(err, "executing sql")
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
m, err = db.getMember(ctx, tx, id)
|
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "getting member")
|
|
|
|
}
|
|
|
|
|
2022-11-21 08:01:51 -08:00
|
|
|
return m, nil
|
|
|
|
}
|