2022-05-02 17:19:37 +02:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"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/georgysavva/scany/pgxscan"
|
|
|
|
"github.com/jackc/pgconn"
|
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
|
|
|
|
|
|
|
|
AvatarSource *string
|
|
|
|
AvatarURL *string
|
|
|
|
Links []string
|
|
|
|
|
2022-05-05 16:33:44 +02:00
|
|
|
Discord *string
|
|
|
|
DiscordUsername *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.
|
|
|
|
func (db *DB) CreateUser(ctx context.Context, username string) (u User, err error) {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
sql, args, err := sq.Insert("users").Columns("id", "username").Values(xid.New(), username).Suffix("RETURNING *").ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return u, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, db, &u, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
if v, ok := errors.Cause(err).(*pgconn.PgError); ok {
|
|
|
|
if v.Code == "23505" { // unique constraint violation
|
|
|
|
return u, ErrUsernameTaken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, errors.Cause(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
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) {
|
|
|
|
sql, args, err := sq.Select("*").From("users").Where("discord = ?", discordID).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return u, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, db, &u, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == pgx.ErrNoRows {
|
|
|
|
return u, ErrUserNotFound
|
|
|
|
}
|
|
|
|
return u, errors.Cause(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) UpdateFromDiscord(ctx context.Context, db pgxscan.Querier, du *discordgo.User) error {
|
|
|
|
builder := sq.Update("users").
|
|
|
|
Set("discord_username", du.String()).
|
|
|
|
Where("id = ?", u.ID).
|
|
|
|
Suffix("RETURNING *")
|
|
|
|
|
|
|
|
if u.AvatarSource == nil || *u.AvatarSource == "discord" {
|
|
|
|
builder = builder.
|
|
|
|
Set("avatar_source", "discord").
|
|
|
|
Set("avatar_url", du.AvatarURL("1024"))
|
|
|
|
}
|
|
|
|
|
|
|
|
sql, args, err := builder.ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
return pgxscan.Get(ctx, db, u, sql, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// User gets a user by ID.
|
|
|
|
func (db *DB) User(ctx context.Context, id xid.ID) (u User, err error) {
|
|
|
|
err = pgxscan.Get(ctx, db, &u, "select * from users where id = $1", id)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == pgx.ErrNoRows {
|
|
|
|
return u, ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, errors.Cause(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Username gets a user by username.
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == pgx.ErrNoRows {
|
|
|
|
return u, ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, errors.Cause(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func (db *DB) UpdateUser(
|
|
|
|
ctx context.Context,
|
|
|
|
tx pgx.Tx, id xid.ID,
|
|
|
|
displayName, bio *string,
|
|
|
|
links *[]string,
|
|
|
|
) (u User, err error) {
|
|
|
|
if displayName == nil && bio == nil && links == nil {
|
|
|
|
return u, ErrNothingToUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sql, args, err := builder.Suffix("RETURNING *").ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return u, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, tx, &u, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return u, errors.Wrap(err, "executing sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|