2022-06-16 05:54:15 -07:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-06-02 18:06:26 -07:00
|
|
|
"time"
|
2022-06-16 05:54:15 -07:00
|
|
|
|
2023-06-03 07:18:47 -07:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/common"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/log"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
2022-06-16 05:54:15 -07:00
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/go-chi/render"
|
2023-04-19 02:05:01 -07:00
|
|
|
"github.com/google/uuid"
|
2023-05-25 04:40:15 -07:00
|
|
|
"github.com/rs/xid"
|
2022-06-16 05:54:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type PatchUserRequest struct {
|
2023-04-26 04:15:57 -07:00
|
|
|
Username *string `json:"name"`
|
2023-04-19 02:05:01 -07:00
|
|
|
DisplayName *string `json:"display_name"`
|
|
|
|
Bio *string `json:"bio"`
|
|
|
|
MemberTitle *string `json:"member_title"`
|
|
|
|
Links *[]string `json:"links"`
|
|
|
|
Names *[]db.FieldEntry `json:"names"`
|
|
|
|
Pronouns *[]db.PronounEntry `json:"pronouns"`
|
|
|
|
Fields *[]db.Field `json:"fields"`
|
|
|
|
Avatar *string `json:"avatar"`
|
2023-08-02 14:24:38 -07:00
|
|
|
Timezone *string `json:"timezone"`
|
2023-04-19 02:05:01 -07:00
|
|
|
ListPrivate *bool `json:"list_private"`
|
|
|
|
CustomPreferences *db.CustomPreferences `json:"custom_preferences"`
|
2023-05-25 04:40:15 -07:00
|
|
|
Flags *[]xid.ID `json:"flags"`
|
2022-06-16 05:54:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// patchUser parses a PatchUserRequest and updates the user with the given ID.
|
|
|
|
func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
2023-03-30 07:58:35 -07:00
|
|
|
if !claims.TokenWrite {
|
|
|
|
return server.APIError{Code: server.ErrMissingPermissions, Details: "This token is read-only"}
|
|
|
|
}
|
|
|
|
|
2022-06-16 05:54:15 -07:00
|
|
|
var req PatchUserRequest
|
|
|
|
err := render.Decode(r, &req)
|
|
|
|
if err != nil {
|
|
|
|
return server.APIError{Code: server.ErrBadRequest}
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:31:43 -08:00
|
|
|
// get existing user, for comparison later
|
|
|
|
u, err := s.DB.User(ctx, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting existing user")
|
|
|
|
}
|
|
|
|
|
2022-06-16 05:54:15 -07:00
|
|
|
// validate that *something* is set
|
2022-12-22 16:31:43 -08:00
|
|
|
if req.Username == nil &&
|
|
|
|
req.DisplayName == nil &&
|
2022-09-15 15:48:43 -07:00
|
|
|
req.Bio == nil &&
|
2023-04-01 08:20:59 -07:00
|
|
|
req.MemberTitle == nil &&
|
|
|
|
req.ListPrivate == nil &&
|
2022-09-15 15:48:43 -07:00
|
|
|
req.Links == nil &&
|
|
|
|
req.Fields == nil &&
|
|
|
|
req.Names == nil &&
|
2022-09-20 03:55:00 -07:00
|
|
|
req.Pronouns == nil &&
|
2023-04-19 03:00:21 -07:00
|
|
|
req.Avatar == nil &&
|
2023-05-25 04:40:15 -07:00
|
|
|
req.CustomPreferences == nil &&
|
|
|
|
req.Flags == nil {
|
2022-06-16 05:54:15 -07:00
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: "Data must not be empty",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate display name/bio
|
2023-04-02 13:50:22 -07:00
|
|
|
if common.StringLength(req.Username) > db.MaxUsernameLength {
|
2022-06-16 05:54:15 -07:00
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
2023-04-02 13:50:22 -07:00
|
|
|
Details: fmt.Sprintf("Name name too long (max %d, current %d)", db.MaxUsernameLength, common.StringLength(req.Username)),
|
2022-06-16 05:54:15 -07:00
|
|
|
}
|
|
|
|
}
|
2023-04-02 13:50:22 -07:00
|
|
|
if common.StringLength(req.DisplayName) > db.MaxDisplayNameLength {
|
2022-06-16 05:54:15 -07:00
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
2023-04-02 13:50:22 -07:00
|
|
|
Details: fmt.Sprintf("Display name too long (max %d, current %d)", db.MaxDisplayNameLength, common.StringLength(req.DisplayName)),
|
2022-06-16 05:54:15 -07:00
|
|
|
}
|
|
|
|
}
|
2023-04-02 13:50:22 -07:00
|
|
|
if common.StringLength(req.Bio) > db.MaxUserBioLength {
|
2023-04-01 08:20:59 -07:00
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
2023-04-02 13:50:22 -07:00
|
|
|
Details: fmt.Sprintf("Bio too long (max %d, current %d)", db.MaxUserBioLength, common.StringLength(req.Bio)),
|
2023-04-01 08:20:59 -07:00
|
|
|
}
|
|
|
|
}
|
2022-06-16 05:54:15 -07:00
|
|
|
|
2023-08-02 14:24:38 -07:00
|
|
|
// validate timezone
|
|
|
|
if req.Timezone != nil {
|
|
|
|
if *req.Timezone != "" {
|
|
|
|
_, err := time.LoadLocation(*req.Timezone)
|
|
|
|
if err != nil {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: fmt.Sprintf("%q is not a valid timezone", *req.Timezone),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 05:54:15 -07:00
|
|
|
// validate links
|
|
|
|
if req.Links != nil {
|
|
|
|
if len(*req.Links) > db.MaxUserLinksLength {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: fmt.Sprintf("Too many links (max %d, current %d)", db.MaxUserLinksLength, len(*req.Links)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, link := range *req.Links {
|
|
|
|
if len(link) > db.MaxLinkLength {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: fmt.Sprintf("Link %d too long (max %d, current %d)", i, db.MaxLinkLength, len(link)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 17:59:15 -07:00
|
|
|
// validate flag length
|
|
|
|
if req.Flags != nil {
|
|
|
|
if len(*req.Flags) > db.MaxPrideFlags {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: fmt.Sprintf("Too many flags (max %d, current %d)", len(*req.Flags), db.MaxPrideFlags),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 02:05:01 -07:00
|
|
|
// validate custom preferences
|
|
|
|
if req.CustomPreferences != nil {
|
2023-04-19 08:21:02 -07:00
|
|
|
if count := len(*req.CustomPreferences); count > db.MaxFields {
|
|
|
|
return server.APIError{Code: server.ErrBadRequest, Details: fmt.Sprintf("Too many custom preferences (max %d, current %d)", db.MaxFields, count)}
|
|
|
|
}
|
|
|
|
|
2023-04-19 02:05:01 -07:00
|
|
|
for k, v := range *req.CustomPreferences {
|
|
|
|
_, err := uuid.Parse(k)
|
|
|
|
if err != nil {
|
|
|
|
return server.APIError{Code: server.ErrBadRequest, Details: "One or more custom preference IDs is not a UUID."}
|
|
|
|
}
|
|
|
|
if s := v.Validate(); s != "" {
|
|
|
|
return server.APIError{Code: server.ErrBadRequest, Details: s}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-21 07:35:13 -07:00
|
|
|
customPreferences := u.CustomPreferences
|
|
|
|
if req.CustomPreferences != nil {
|
|
|
|
customPreferences = *req.CustomPreferences
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateSlicePtr("name", req.Names, customPreferences); err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateSlicePtr("pronoun", req.Pronouns, customPreferences); err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateSlicePtr("field", req.Fields, customPreferences); err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
2023-04-19 02:05:01 -07:00
|
|
|
|
2022-09-20 03:55:00 -07:00
|
|
|
// update avatar
|
2023-03-12 18:04:09 -07:00
|
|
|
var avatarHash *string = nil
|
2022-09-20 03:55:00 -07:00
|
|
|
if req.Avatar != nil {
|
2023-03-12 18:19:03 -07:00
|
|
|
if *req.Avatar == "" {
|
|
|
|
if u.Avatar != nil {
|
|
|
|
err = s.DB.DeleteUserAvatar(ctx, u.ID, *u.Avatar)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("deleting user avatar: %v", err)
|
|
|
|
return errors.Wrap(err, "deleting avatar")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
}
|
2023-03-12 18:19:03 -07:00
|
|
|
avatarHash = req.Avatar
|
|
|
|
} else {
|
|
|
|
webp, jpg, err := s.DB.ConvertAvatar(*req.Avatar)
|
|
|
|
if err != nil {
|
|
|
|
if err == db.ErrInvalidDataURI {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: "invalid avatar data URI",
|
|
|
|
}
|
|
|
|
} else if err == db.ErrInvalidContentType {
|
|
|
|
return server.APIError{
|
|
|
|
Code: server.ErrBadRequest,
|
|
|
|
Details: "invalid avatar content type",
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 03:55:00 -07:00
|
|
|
|
2023-03-12 18:19:03 -07:00
|
|
|
log.Errorf("converting user avatar: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2022-09-20 03:55:00 -07:00
|
|
|
|
2023-03-12 18:19:03 -07:00
|
|
|
hash, err := s.DB.WriteUserAvatar(ctx, claims.UserID, webp, jpg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("uploading user avatar: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
avatarHash = &hash
|
2023-03-23 02:07:51 -07:00
|
|
|
|
|
|
|
// delete current avatar if user has one
|
|
|
|
if u.Avatar != nil {
|
|
|
|
err = s.DB.DeleteUserAvatar(ctx, claims.UserID, *u.Avatar)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("deleting existing avatar: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 05:54:15 -07:00
|
|
|
// start transaction
|
|
|
|
tx, err := s.DB.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("creating transaction: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback(ctx)
|
|
|
|
|
2022-12-22 16:31:43 -08:00
|
|
|
// update username
|
|
|
|
if req.Username != nil && *req.Username != u.Username {
|
|
|
|
err = s.DB.UpdateUsername(ctx, tx, claims.UserID, *req.Username)
|
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case db.ErrUsernameTaken:
|
|
|
|
return server.APIError{Code: server.ErrUsernameTaken}
|
|
|
|
case db.ErrInvalidUsername:
|
|
|
|
return server.APIError{Code: server.ErrInvalidUsername}
|
|
|
|
default:
|
|
|
|
return errors.Wrap(err, "updating username")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 14:24:38 -07:00
|
|
|
u, err = s.DB.UpdateUser(ctx, tx, claims.UserID, req.DisplayName, req.Bio, req.MemberTitle, req.ListPrivate, req.Links, avatarHash, req.Timezone, req.CustomPreferences)
|
2022-06-16 05:54:15 -07:00
|
|
|
if err != nil && errors.Cause(err) != db.ErrNothingToUpdate {
|
|
|
|
log.Errorf("updating user: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
if req.Names != nil || req.Pronouns != nil {
|
|
|
|
names := u.Names
|
|
|
|
pronouns := u.Pronouns
|
2022-09-08 05:00:41 -07:00
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
if req.Names != nil {
|
|
|
|
names = *req.Names
|
|
|
|
}
|
|
|
|
if req.Pronouns != nil {
|
|
|
|
pronouns = *req.Pronouns
|
2022-09-15 15:48:43 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
err = s.DB.SetUserNamesPronouns(ctx, tx, claims.UserID, names, pronouns)
|
2022-09-15 15:48:43 -07:00
|
|
|
if err != nil {
|
2023-01-30 15:50:17 -08:00
|
|
|
log.Errorf("setting names for member %v: %v", claims.UserID, err)
|
2022-09-15 15:48:43 -07:00
|
|
|
return err
|
|
|
|
}
|
2023-01-30 15:50:17 -08:00
|
|
|
u.Names = names
|
|
|
|
u.Pronouns = pronouns
|
2022-09-15 15:48:43 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:50:17 -08:00
|
|
|
var fields []db.Field
|
2022-06-16 05:54:15 -07:00
|
|
|
if req.Fields != nil {
|
|
|
|
err = s.DB.SetUserFields(ctx, tx, claims.UserID, *req.Fields)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("setting fields for user %v: %v", claims.UserID, err)
|
|
|
|
return err
|
|
|
|
}
|
2022-09-15 15:48:43 -07:00
|
|
|
fields = *req.Fields
|
2022-06-16 05:54:15 -07:00
|
|
|
} else {
|
|
|
|
fields, err = s.DB.UserFields(ctx, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getting fields for user %v: %v", claims.UserID, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
// update flags
|
|
|
|
if req.Flags != nil {
|
|
|
|
err = s.DB.SetUserFlags(ctx, tx, claims.UserID, *req.Flags)
|
|
|
|
if err != nil {
|
|
|
|
if err == db.ErrInvalidFlagID {
|
|
|
|
return server.APIError{Code: server.ErrBadRequest, Details: "One or more flag IDs are unknown"}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf("updating flags for user %v: %v", claims.UserID, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 17:54:08 -07:00
|
|
|
// update last active time
|
|
|
|
err = s.DB.UpdateActiveTime(ctx, tx, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("updating last active time for user %v: %v", claims.UserID, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-16 05:54:15 -07:00
|
|
|
err = tx.Commit(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("committing transaction: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-17 06:14:31 -07:00
|
|
|
// get fedi instance name if the user has a linked fedi account
|
|
|
|
var fediInstance *string
|
|
|
|
if u.FediverseAppID != nil {
|
|
|
|
app, err := s.DB.FediverseAppByID(ctx, *u.FediverseAppID)
|
|
|
|
if err == nil {
|
|
|
|
fediInstance = &app.Instance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:40:15 -07:00
|
|
|
// get flags to return (we need to return full flag objects, not the array of IDs in the request body)
|
|
|
|
flags, err := s.DB.UserFlags(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getting user flags: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-16 05:54:15 -07:00
|
|
|
// echo the updated user back on success
|
2023-03-13 09:01:36 -07:00
|
|
|
render.JSON(w, r, GetMeResponse{
|
2023-05-25 04:40:15 -07:00
|
|
|
GetUserResponse: dbUserToResponse(u, fields, nil, flags),
|
2023-07-30 14:13:35 -07:00
|
|
|
CreatedAt: u.ID.Time(),
|
|
|
|
Timezone: u.Timezone,
|
2023-03-17 06:14:31 -07:00
|
|
|
MaxInvites: u.MaxInvites,
|
2023-03-23 06:54:43 -07:00
|
|
|
IsAdmin: u.IsAdmin,
|
2023-04-02 14:10:57 -07:00
|
|
|
ListPrivate: u.ListPrivate,
|
2023-06-02 18:06:26 -07:00
|
|
|
LastSIDReroll: u.LastSIDReroll,
|
2023-03-17 06:14:31 -07:00
|
|
|
Discord: u.Discord,
|
|
|
|
DiscordUsername: u.DiscordUsername,
|
2023-04-17 18:49:37 -07:00
|
|
|
Tumblr: u.Tumblr,
|
|
|
|
TumblrUsername: u.TumblrUsername,
|
2023-04-18 13:52:58 -07:00
|
|
|
Google: u.Google,
|
|
|
|
GoogleUsername: u.GoogleUsername,
|
2023-03-17 06:14:31 -07:00
|
|
|
Fediverse: u.Fediverse,
|
|
|
|
FediverseUsername: u.FediverseUsername,
|
|
|
|
FediverseInstance: fediInstance,
|
2023-03-13 09:01:36 -07:00
|
|
|
})
|
2022-09-08 05:00:41 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type validator interface {
|
2023-04-19 03:00:21 -07:00
|
|
|
Validate(custom db.CustomPreferences) string
|
2022-09-08 05:00:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// validateSlicePtr validates a slice of validators.
|
|
|
|
// If the slice is nil, a nil error is returned (assuming that the field is not required)
|
2023-04-19 03:00:21 -07:00
|
|
|
func validateSlicePtr[T validator](typ string, slice *[]T, custom db.CustomPreferences) *server.APIError {
|
2022-09-08 05:00:41 -07:00
|
|
|
if slice == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:55:00 -07:00
|
|
|
max := db.MaxFields
|
|
|
|
if typ != "field" {
|
|
|
|
max = db.FieldEntriesLimit
|
|
|
|
}
|
|
|
|
|
2022-09-08 05:00:41 -07:00
|
|
|
// max 25 fields
|
2022-09-20 03:55:00 -07:00
|
|
|
if len(*slice) > max {
|
|
|
|
return &server.APIError{
|
2022-09-08 05:00:41 -07:00
|
|
|
Code: server.ErrBadRequest,
|
2022-09-20 03:55:00 -07:00
|
|
|
Details: fmt.Sprintf("Too many %ss (max %d, current %d)", typ, max, len(*slice)),
|
2022-09-08 05:00:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate all fields
|
|
|
|
for i, pronouns := range *slice {
|
2023-04-19 03:00:21 -07:00
|
|
|
if s := pronouns.Validate(custom); s != "" {
|
2022-09-20 03:55:00 -07:00
|
|
|
return &server.APIError{
|
2022-09-08 05:00:41 -07:00
|
|
|
Code: server.ErrBadRequest,
|
2023-03-13 17:30:46 -07:00
|
|
|
Details: fmt.Sprintf("%s %d: %s", typ, i+1, s),
|
2022-09-08 05:00:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 05:54:15 -07:00
|
|
|
return nil
|
|
|
|
}
|
2023-06-02 18:06:26 -07:00
|
|
|
|
|
|
|
func (s *Server) rerollUserSID(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
claims, _ := server.ClaimsFromContext(ctx)
|
|
|
|
|
|
|
|
if !claims.TokenWrite {
|
|
|
|
return server.APIError{Code: server.ErrMissingPermissions, Details: "This token is read-only"}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := s.DB.User(ctx, claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting existing user")
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().Add(-time.Hour).Before(u.LastSIDReroll) {
|
|
|
|
return server.APIError{Code: server.ErrRerollingTooQuickly}
|
|
|
|
}
|
|
|
|
|
|
|
|
newID, err := s.DB.RerollUserSID(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "updating user SID")
|
|
|
|
}
|
|
|
|
|
|
|
|
u.SID = newID
|
|
|
|
render.JSON(w, r, dbUserToResponse(u, nil, nil, nil))
|
|
|
|
return nil
|
|
|
|
}
|