2022-05-02 17:19:37 +02:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"regexp"
|
2023-03-08 10:32:18 +01:00
|
|
|
"time"
|
2022-05-02 17:19:37 +02:00
|
|
|
|
2023-01-04 22:41:29 +01:00
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/db/queries"
|
2022-05-02 17:19:37 +02:00
|
|
|
"emperror.dev/errors"
|
2022-05-04 16:27:16 +02:00
|
|
|
"github.com/bwmarrin/discordgo"
|
2022-05-02 17:19:37 +02:00
|
|
|
"github.com/jackc/pgconn"
|
2023-03-08 10:32:18 +01:00
|
|
|
"github.com/jackc/pgtype"
|
2022-05-04 16:27:16 +02:00
|
|
|
"github.com/jackc/pgx/v4"
|
2022-05-02 17:19:37 +02:00
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID xid.ID
|
|
|
|
Username string
|
|
|
|
DisplayName *string
|
|
|
|
Bio *string
|
|
|
|
|
2022-09-20 14:12:29 +02:00
|
|
|
AvatarURLs []string `db:"avatar_urls"`
|
|
|
|
Links []string
|
2022-05-02 17:19:37 +02:00
|
|
|
|
2023-01-31 00:50:17 +01:00
|
|
|
Names []FieldEntry
|
|
|
|
Pronouns []PronounEntry
|
2023-01-14 17:33:18 +01:00
|
|
|
|
2022-05-05 16:33:44 +02:00
|
|
|
Discord *string
|
|
|
|
DiscordUsername *string
|
2022-11-18 15:27:52 +01:00
|
|
|
|
|
|
|
MaxInvites int
|
2023-03-08 10:32:18 +01:00
|
|
|
|
|
|
|
DeletedAt *time.Time
|
|
|
|
SelfDelete *bool
|
|
|
|
DeleteReason *string
|
2022-05-02 17:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// usernames must match this regex
|
2022-05-17 22:35:26 +02:00
|
|
|
var usernameRegex = regexp.MustCompile(`^[\w-.]{2,40}$`)
|
2022-05-02 17:19:37 +02:00
|
|
|
|
|
|
|
const (
|
2022-05-04 16:27:16 +02:00
|
|
|
ErrUserNotFound = errors.Sentinel("user not found")
|
2022-05-02 17:19:37 +02:00
|
|
|
|
2022-05-04 16:27:16 +02:00
|
|
|
ErrUsernameTaken = errors.Sentinel("username is already taken")
|
2022-05-02 17:19:37 +02:00
|
|
|
ErrInvalidUsername = errors.Sentinel("username contains invalid characters")
|
|
|
|
ErrUsernameTooShort = errors.Sentinel("username is too short")
|
|
|
|
ErrUsernameTooLong = errors.Sentinel("username is too long")
|
|
|
|
)
|
|
|
|
|
2022-06-16 14:54:15 +02:00
|
|
|
const (
|
|
|
|
MaxUsernameLength = 40
|
|
|
|
MaxDisplayNameLength = 100
|
|
|
|
MaxUserBioLength = 1000
|
|
|
|
MaxUserLinksLength = 25
|
|
|
|
MaxLinkLength = 256
|
|
|
|
)
|
|
|
|
|
2022-05-02 17:19:37 +02:00
|
|
|
// CreateUser creates a user with the given username.
|
2022-11-18 02:17:27 +01:00
|
|
|
func (db *DB) CreateUser(ctx context.Context, tx pgx.Tx, username string) (u User, err error) {
|
2022-05-02 17:19:37 +02:00
|
|
|
// check if the username is valid
|
|
|
|
// if not, return an error depending on what failed
|
|
|
|
if !usernameRegex.MatchString(username) {
|
|
|
|
if len(username) < 2 {
|
|
|
|
return u, ErrUsernameTooShort
|
|
|
|
} else if len(username) > 40 {
|
|
|
|
return u, ErrUsernameTooLong
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, ErrInvalidUsername
|
|
|
|
}
|
|
|
|
|
2023-02-25 22:16:22 +01:00
|
|
|
sql, args, err := sq.Insert("users").Columns("id", "username").Values(xid.New(), username).Suffix("RETURNING id").ToSql()
|
2022-05-02 17:19:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return u, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
2023-02-25 22:16:22 +01:00
|
|
|
var id xid.ID
|
|
|
|
err = tx.QueryRow(ctx, sql, args...).Scan(&id)
|
2022-05-02 17:19:37 +02:00
|
|
|
if err != nil {
|
2023-02-25 22:16:22 +01:00
|
|
|
pge := &pgconn.PgError{}
|
|
|
|
if errors.As(err, &pge) {
|
|
|
|
// unique constraint violation
|
|
|
|
if pge.Code == "23505" {
|
2022-05-02 17:19:37 +02:00
|
|
|
return u, ErrUsernameTaken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, errors.Cause(err)
|
|
|
|
}
|
|
|
|
|
2023-02-25 22:16:22 +01:00
|
|
|
return db.getUser(ctx, tx, id)
|
2022-05-02 17:19:37 +02:00
|
|
|
}
|
2022-05-04 16:27:16 +02:00
|
|
|
|
|
|
|
// DiscordUser fetches a user by Discord user ID.
|
|
|
|
func (db *DB) DiscordUser(ctx context.Context, discordID string) (u User, err error) {
|
2023-03-05 04:18:35 +01:00
|
|
|
sql, args, err := sq.Select("id").From("users").Where("discord = ?", discordID).ToSql()
|
2022-05-04 16:27:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return u, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
2023-03-05 04:18:35 +01:00
|
|
|
var id xid.ID
|
|
|
|
err = db.QueryRow(ctx, sql, args...).Scan(&id)
|
2022-05-04 16:27:16 +02:00
|
|
|
if err != nil {
|
2023-03-05 04:18:35 +01:00
|
|
|
return u, errors.Wrap(err, "executing id query")
|
2022-05-04 16:27:16 +02:00
|
|
|
}
|
|
|
|
|
2023-03-05 04:18:35 +01:00
|
|
|
return db.getUser(ctx, db, id)
|
2022-05-04 16:27:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:41:29 +01:00
|
|
|
func (u *User) UpdateFromDiscord(ctx context.Context, db querier, du *discordgo.User) error {
|
2023-02-25 22:16:22 +01:00
|
|
|
sql, args, err := sq.Update("users").
|
2022-11-18 02:26:40 +01:00
|
|
|
Set("discord", du.ID).
|
2022-05-04 16:27:16 +02:00
|
|
|
Set("discord_username", du.String()).
|
|
|
|
Where("id = ?", u.ID).
|
2023-02-25 22:16:22 +01:00
|
|
|
ToSql()
|
2022-05-04 16:27:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
2023-02-25 22:16:22 +01:00
|
|
|
_, err = db.Exec(ctx, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Discord = &du.ID
|
|
|
|
username := du.String()
|
|
|
|
u.DiscordUsername = &username
|
|
|
|
|
|
|
|
return nil
|
2022-05-04 16:27:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:41:29 +01:00
|
|
|
func (db *DB) getUser(ctx context.Context, q querier, id xid.ID) (u User, err error) {
|
|
|
|
qu, err := queries.NewQuerier(q).GetUserByID(ctx, id.String())
|
2022-05-04 16:27:16 +02:00
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == pgx.ErrNoRows {
|
|
|
|
return u, ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:41:29 +01:00
|
|
|
return u, errors.Wrap(err, "getting user from database")
|
|
|
|
}
|
|
|
|
|
2023-03-08 10:32:18 +01:00
|
|
|
var deletedAt *time.Time
|
|
|
|
if qu.DeletedAt.Status == pgtype.Present {
|
|
|
|
deletedAt = &qu.DeletedAt.Time
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:41:29 +01:00
|
|
|
u = User{
|
|
|
|
ID: id,
|
|
|
|
Username: qu.Username,
|
|
|
|
DisplayName: qu.DisplayName,
|
|
|
|
Bio: qu.Bio,
|
|
|
|
AvatarURLs: qu.AvatarUrls,
|
2023-01-31 00:50:17 +01:00
|
|
|
Names: fieldEntriesFromDB(qu.Names),
|
2023-01-14 17:33:18 +01:00
|
|
|
Pronouns: pronounsFromDB(qu.Pronouns),
|
2023-01-04 22:41:29 +01:00
|
|
|
Links: qu.Links,
|
|
|
|
Discord: qu.Discord,
|
|
|
|
DiscordUsername: qu.DiscordUsername,
|
|
|
|
MaxInvites: int(qu.MaxInvites),
|
2023-03-08 10:32:18 +01:00
|
|
|
DeletedAt: deletedAt,
|
|
|
|
SelfDelete: qu.SelfDelete,
|
|
|
|
DeleteReason: qu.DeleteReason,
|
2022-05-04 16:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2022-12-23 01:31:43 +01:00
|
|
|
// User gets a user by ID.
|
|
|
|
func (db *DB) User(ctx context.Context, id xid.ID) (u User, err error) {
|
|
|
|
return db.getUser(ctx, db, id)
|
|
|
|
}
|
|
|
|
|
2022-05-04 16:27:16 +02:00
|
|
|
// Username gets a user by username.
|
|
|
|
func (db *DB) Username(ctx context.Context, name string) (u User, err error) {
|
2023-01-14 17:33:18 +01:00
|
|
|
qu, err := db.q.GetUserByUsername(ctx, name)
|
2022-05-04 16:27:16 +02:00
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == pgx.ErrNoRows {
|
|
|
|
return u, ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
2023-01-14 17:33:18 +01:00
|
|
|
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,
|
2023-01-31 00:50:17 +01:00
|
|
|
Names: fieldEntriesFromDB(qu.Names),
|
2023-01-14 17:33:18 +01:00
|
|
|
Pronouns: pronounsFromDB(qu.Pronouns),
|
|
|
|
Links: qu.Links,
|
|
|
|
Discord: qu.Discord,
|
|
|
|
DiscordUsername: qu.DiscordUsername,
|
|
|
|
MaxInvites: int(qu.MaxInvites),
|
2022-05-04 16:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
2022-05-17 22:35:26 +02:00
|
|
|
|
|
|
|
// UsernameTaken checks if the given username is already taken.
|
|
|
|
func (db *DB) UsernameTaken(ctx context.Context, username string) (valid, taken bool, err error) {
|
|
|
|
if !usernameRegex.MatchString(username) {
|
|
|
|
return false, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.QueryRow(ctx, "select exists (select id from users where username = $1)", username).Scan(&taken)
|
|
|
|
return true, taken, err
|
|
|
|
}
|
2022-06-16 14:54:15 +02:00
|
|
|
|
2022-12-23 01:31:43 +01:00
|
|
|
// UpdateUsername validates the given username, then updates the given user's name to it if valid.
|
|
|
|
func (db *DB) UpdateUsername(ctx context.Context, tx pgx.Tx, id xid.ID, newName string) error {
|
|
|
|
if !usernameRegex.MatchString(newName) {
|
|
|
|
return ErrInvalidUsername
|
|
|
|
}
|
|
|
|
|
|
|
|
sql, args, err := sq.Update("users").Set("username", newName).Where("id = ?", id).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(ctx, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
pge := &pgconn.PgError{}
|
|
|
|
if errors.As(err, &pge) {
|
|
|
|
// unique constraint violation
|
|
|
|
if pge.Code == "23505" {
|
|
|
|
return ErrUsernameTaken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-16 14:54:15 +02:00
|
|
|
func (db *DB) UpdateUser(
|
|
|
|
ctx context.Context,
|
|
|
|
tx pgx.Tx, id xid.ID,
|
|
|
|
displayName, bio *string,
|
|
|
|
links *[]string,
|
2022-09-20 12:55:00 +02:00
|
|
|
avatarURLs []string,
|
2022-06-16 14:54:15 +02:00
|
|
|
) (u User, err error) {
|
2022-09-20 13:02:48 +02:00
|
|
|
if displayName == nil && bio == nil && links == nil && avatarURLs == nil {
|
2022-12-23 01:31:43 +01:00
|
|
|
return db.getUser(ctx, tx, id)
|
2022-06-16 14:54:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
builder := sq.Update("users").Where("id = ?", id)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 12:55:00 +02:00
|
|
|
if avatarURLs != nil {
|
|
|
|
if len(avatarURLs) == 0 {
|
|
|
|
builder = builder.Set("avatar_urls", nil)
|
|
|
|
} else {
|
|
|
|
builder = builder.Set("avatar_urls", avatarURLs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 00:50:17 +01:00
|
|
|
sql, args, err := builder.ToSql()
|
2022-06-16 14:54:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return u, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
2023-01-31 00:50:17 +01:00
|
|
|
_, err = tx.Exec(ctx, sql, args...)
|
2022-06-16 14:54:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return u, errors.Wrap(err, "executing sql")
|
|
|
|
}
|
|
|
|
|
2023-01-31 00:50:17 +01:00
|
|
|
u, err = db.getUser(ctx, tx, id)
|
|
|
|
if err != nil {
|
|
|
|
return u, errors.Wrap(err, "getting updated user")
|
|
|
|
}
|
2022-06-16 14:54:15 +02:00
|
|
|
return u, nil
|
|
|
|
}
|
2023-03-08 10:32:18 +01:00
|
|
|
|
|
|
|
func (db *DB) DeleteUser(ctx context.Context, q querier, id xid.ID, selfDelete bool, reason string) error {
|
|
|
|
builder := sq.Update("users").Set("deleted_at", time.Now().UTC()).Set("self_delete", selfDelete).Where("id = ?", id)
|
|
|
|
if !selfDelete {
|
|
|
|
builder = builder.Set("delete_reason", reason)
|
|
|
|
}
|
|
|
|
sql, args, err := builder.ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = q.Exec(ctx, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|