2022-09-20 12:55:00 +02:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-18 23:00:44 +01:00
|
|
|
"regexp"
|
2022-09-20 12:55:00 +02:00
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2023-04-04 04:11:03 +02:00
|
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
2022-09-20 12:55:00 +02:00
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
2022-11-21 17:01:51 +01:00
|
|
|
const (
|
|
|
|
MaxMemberCount = 500
|
|
|
|
MaxMemberNameLength = 100
|
|
|
|
)
|
2022-10-03 10:59:30 +02:00
|
|
|
|
2022-09-20 12:55:00 +02:00
|
|
|
type Member struct {
|
2022-11-20 21:09:29 +01:00
|
|
|
ID xid.ID
|
|
|
|
UserID xid.ID
|
|
|
|
Name string
|
|
|
|
DisplayName *string
|
|
|
|
Bio *string
|
2023-03-13 02:04:09 +01:00
|
|
|
Avatar *string
|
2022-11-20 21:09:29 +01:00
|
|
|
Links []string
|
2023-01-31 00:50:17 +01:00
|
|
|
Names []FieldEntry
|
|
|
|
Pronouns []PronounEntry
|
2023-04-01 17:20:59 +02:00
|
|
|
Unlisted bool
|
2022-09-20 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 10:59:30 +02:00
|
|
|
const (
|
|
|
|
ErrMemberNotFound = errors.Sentinel("member not found")
|
|
|
|
ErrMemberNameInUse = errors.Sentinel("member name already in use")
|
|
|
|
)
|
2022-09-20 12:55:00 +02:00
|
|
|
|
2023-03-18 23:00:44 +01:00
|
|
|
// member names must match this regex
|
|
|
|
var memberNameRegex = regexp.MustCompile("^[^@\\?!#/\\\\[\\]\"'$%&()+<=>^|~`,]{1,100}$")
|
|
|
|
|
|
|
|
func MemberNameValid(name string) bool {
|
|
|
|
return memberNameRegex.MatchString(name)
|
|
|
|
}
|
|
|
|
|
2023-03-12 01:31:10 +01:00
|
|
|
func (db *DB) Member(ctx context.Context, id xid.ID) (m Member, err error) {
|
|
|
|
sql, args, err := sq.Select("*").From("members").Where("id = ?", id).ToSql()
|
2022-09-20 12:55:00 +02:00
|
|
|
if err != nil {
|
2023-03-12 01:31:10 +01:00
|
|
|
return m, errors.Wrap(err, "building sql")
|
2022-09-20 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 01:31:10 +01:00
|
|
|
err = pgxscan.Get(ctx, db, &m, sql, args...)
|
2022-09-20 12:55:00 +02:00
|
|
|
if err != nil {
|
2023-03-12 01:31:10 +01:00
|
|
|
return m, errors.Wrap(err, "executing query")
|
2022-09-20 12:55:00 +02:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:31:42 +01:00
|
|
|
// UserMember returns a member scoped by user.
|
2022-09-20 12:55:00 +02:00
|
|
|
func (db *DB) UserMember(ctx context.Context, userID xid.ID, memberRef string) (m Member, err error) {
|
2023-03-12 01:31:10 +01:00
|
|
|
sql, args, err := sq.Select("*").From("members").Where("user_id = ?", userID).Where("(id = ? or name = ?)", memberRef, memberRef).ToSql()
|
2022-09-20 12:55:00 +02:00
|
|
|
if err != nil {
|
2023-03-12 01:31:10 +01:00
|
|
|
return m, errors.Wrap(err, "building sql")
|
2022-09-20 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 01:31:10 +01:00
|
|
|
err = pgxscan.Get(ctx, db, &m, sql, args...)
|
2022-09-20 12:55:00 +02:00
|
|
|
if err != nil {
|
2023-03-12 01:31:10 +01:00
|
|
|
return m, errors.Wrap(err, "executing query")
|
2022-09-20 12:55:00 +02:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:31:42 +01:00
|
|
|
// UserMembers returns all of a user's members, sorted by name.
|
2023-04-01 17:20:59 +02:00
|
|
|
func (db *DB) UserMembers(ctx context.Context, userID xid.ID, showHidden bool) (ms []Member, err error) {
|
|
|
|
builder := sq.Select("*").
|
2023-01-31 00:50:17 +01:00
|
|
|
From("members").Where("user_id = ?", userID).
|
2023-04-01 17:20:59 +02:00
|
|
|
OrderBy("name", "id")
|
|
|
|
if !showHidden {
|
|
|
|
builder = builder.Where("unlisted = ?", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
sql, args, err := builder.ToSql()
|
2022-09-20 12:55:00 +02: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 10:59:30 +02:00
|
|
|
|
|
|
|
// CreateMember creates a member.
|
2023-03-12 01:31:10 +01: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 10:59:30 +02:00
|
|
|
sql, args, err := sq.Insert("members").
|
2022-11-20 21:09:29 +01:00
|
|
|
Columns("user_id", "id", "name", "display_name", "bio", "links").
|
|
|
|
Values(userID, xid.New(), name, displayName, bio, links).
|
2023-03-12 01:31:10 +01:00
|
|
|
Suffix("RETURNING *").ToSql()
|
2022-10-03 10:59:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
2023-03-12 01:31:10 +01:00
|
|
|
err = pgxscan.Get(ctx, tx, &m, sql, args...)
|
2022-10-03 10:59:30 +02:00
|
|
|
if err != nil {
|
|
|
|
pge := &pgconn.PgError{}
|
|
|
|
if errors.As(err, &pge) {
|
2022-12-23 01:31:43 +01:00
|
|
|
// unique constraint violation
|
2022-10-03 10:59:30 +02:00
|
|
|
if pge.Code == "23505" {
|
|
|
|
return m, ErrMemberNameInUse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:31:42 +01: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 10:59:30 +02: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 17:01:51 +01:00
|
|
|
|
|
|
|
func (db *DB) UpdateMember(
|
|
|
|
ctx context.Context,
|
|
|
|
tx pgx.Tx, id xid.ID,
|
|
|
|
name, displayName, bio *string,
|
2023-04-01 17:20:59 +02:00
|
|
|
unlisted *bool,
|
2022-11-21 17:01:51 +01:00
|
|
|
links *[]string,
|
2023-03-13 02:04:09 +01:00
|
|
|
avatar *string,
|
2022-11-21 17:01:51 +01:00
|
|
|
) (m Member, err error) {
|
2023-03-13 02:04:09 +01:00
|
|
|
if name == nil && displayName == nil && bio == nil && links == nil && avatar == nil {
|
2023-03-12 01:31:10 +01:00
|
|
|
// get member
|
|
|
|
sql, args, err := sq.Select("*").From("members").Where("id = ?", id).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, tx, &m, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
return m, nil
|
2022-11-21 17:01:51 +01:00
|
|
|
}
|
|
|
|
|
2023-03-12 01:31:10 +01:00
|
|
|
builder := sq.Update("members").Where("id = ?", id).Suffix("RETURNING *")
|
2022-11-21 17:01:51 +01:00
|
|
|
if name != nil {
|
|
|
|
if *name == "" {
|
2023-03-14 17:06:35 +01:00
|
|
|
return m, errors.Wrap(err, "name was empty")
|
2022-11-21 17:01:51 +01:00
|
|
|
} else {
|
2023-03-14 17:06:35 +01:00
|
|
|
builder = builder.Set("name", *name)
|
2022-11-21 17:01:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2023-03-12 01:31:10 +01:00
|
|
|
builder = builder.Set("links", *links)
|
2022-11-21 17:01:51 +01:00
|
|
|
}
|
2023-04-01 17:20:59 +02:00
|
|
|
if unlisted != nil {
|
|
|
|
builder = builder.Set("unlisted", *unlisted)
|
|
|
|
}
|
2022-11-21 17:01:51 +01:00
|
|
|
|
2023-03-13 02:04:09 +01:00
|
|
|
if avatar != nil {
|
|
|
|
if *avatar == "" {
|
|
|
|
builder = builder.Set("avatar", nil)
|
|
|
|
} else {
|
|
|
|
builder = builder.Set("avatar", avatar)
|
|
|
|
}
|
2022-11-21 17:01:51 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 00:50:17 +01:00
|
|
|
sql, args, err := builder.ToSql()
|
2022-11-21 17:01:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return m, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
2023-03-12 01:31:10 +01:00
|
|
|
err = pgxscan.Get(ctx, tx, &m, sql, args...)
|
2022-11-21 17:01:51 +01: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")
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|