diff --git a/server/internal/db/lexicon.go b/server/internal/db/lexicon.go index 5a7a3e9..30a474e 100644 --- a/server/internal/db/lexicon.go +++ b/server/internal/db/lexicon.go @@ -3,9 +3,7 @@ package db import ( "context" "errors" - "fmt" "rvcx/internal/types" - "strings" ) func (s *Store) InitializeProfile(did string, @@ -31,64 +29,20 @@ func (s *Store) InitializeProfile(did string, return nil } -type ProfileUpdate struct { - DID string - Name *string - UpdateName bool - Nick *string - UpdateNick bool - Status *string - UpdateStatus bool - Avatar *string - UpdateAvatar bool - Mime *string - UpdateMime bool - Color *uint64 - UpdateColor bool -} - -func (s *Store) UpdateProfile(to *ProfileUpdate, ctx context.Context) error { - setParts := []string{} - args := []any{to.DID} - idx := 2 - if to.UpdateName { - setParts = append(setParts, fmt.Sprintf("display_name = $%d", idx)) - args = append(args, to.Name) - idx += 1 - } - if to.UpdateNick { - setParts = append(setParts, fmt.Sprintf("default_nick = $%d", idx)) - args = append(args, to.Nick) - idx += 1 - } - if to.UpdateStatus { - setParts = append(setParts, fmt.Sprintf("status = $%d", idx)) - args = append(args, to.Status) - idx += 1 - } - if to.UpdateAvatar { - setParts = append(setParts, fmt.Sprintf("avatar_cid = $%d", idx)) - args = append(args, to.Avatar) - idx += 1 - } - if to.UpdateMime { - setParts = append(setParts, fmt.Sprintf("avatar_mime = $%d", idx)) - args = append(args, to.Mime) - idx += 1 - } - if to.UpdateColor { - setParts = append(setParts, fmt.Sprintf("color = $%d", idx)) - args = append(args, to.Color) - idx += 1 - } - if idx == 2 { - return nil - } - sql := fmt.Sprintf("UPDATE profiles SET %s WHERE did = $1", - strings.Join(setParts, ", ")) - _, err := s.pool.Exec(ctx, sql, args...) +func (s *Store) UpdateProfile(did string, displayname *string, defaultnick *string, status *string, color *uint64, ctx context.Context) error { + _, err := s.pool.Exec(ctx, ` + INSERT INTO profiles ( + did, + display_name, + default_nick, + status, + color + ) VALUES ( + $1, $2, $3, $4, $5 + ) + `, did, displayname, defaultnick, status, color) if err != nil { - return errors.New("error updating profile: " + err.Error()) + return errors.New("i'm not sure what happened: " + err.Error()) } return nil } diff --git a/server/internal/recordmanager/profile.go b/server/internal/recordmanager/profile.go index ef5cd80..6b9ee14 100644 --- a/server/internal/recordmanager/profile.go +++ b/server/internal/recordmanager/profile.go @@ -4,7 +4,6 @@ import ( "context" "errors" "rvcx/internal/atputils" - "rvcx/internal/db" "rvcx/internal/lex" "rvcx/internal/oauth" "rvcx/internal/types" @@ -13,8 +12,7 @@ import ( ) func (rm *RecordManager) AcceptProfile(p lex.ProfileRecord, did string, ctx context.Context) error { - pu := convertToPu(p, did) - err := rm.storeProfile(pu, ctx) + err := rm.storeProfile(did, &p, ctx) if err != nil { return errors.New("failed to store profile: " + err.Error()) } @@ -25,31 +23,6 @@ func (rm *RecordManager) DeleteProfile(did string, cid string, ctx context.Conte return rm.db.DeleteProfile(did, cid, ctx) } -func convertToPu(p lex.ProfileRecord, did string) *db.ProfileUpdate { - var avatar *string - var mime *string - if p.Avatar != nil { - ava := p.Avatar.Ref.String() - avatar = &ava - mime = &p.Avatar.MimeType - } - return &db.ProfileUpdate{ - DID: did, - Name: p.DisplayName, - UpdateName: true, - Nick: p.DefaultNick, - UpdateNick: true, - Status: p.Status, - UpdateStatus: true, - Color: p.Color, - UpdateColor: true, - Avatar: avatar, - UpdateAvatar: true, - Mime: mime, - UpdateMime: true, - } -} - func (rm *RecordManager) CreateInitialProfile(sessData *atoauth.ClientSessionData, ctx context.Context) error { nick := "wanderer" status := "just setting up my xcvr" @@ -73,41 +46,41 @@ func (rm *RecordManager) CreateInitialProfile(sessData *atoauth.ClientSessionDat } func (rm *RecordManager) PostProfile(cs *atoauth.ClientSession, ctx context.Context, p *types.PostProfileRequest) error { - pu, err := rm.validateProfile(cs.Data.AccountDID.String(), p) + err := rm.validateProfile(p) if err != nil { return errors.New("couldn't validate profile: " + err.Error()) } - err = rm.updateProfile(cs, p.DisplayName, p.DefaultNick, p.Status, p.Color, ctx) + pr, err := rm.updateProfile(cs, p.DisplayName, p.DefaultNick, p.Status, p.Color, ctx) if err != nil { return errors.New("couldn't create profile: " + err.Error()) } - err = rm.storeProfile(pu, ctx) + err = rm.storeProfile(cs.Data.AccountDID.String(), pr, ctx) if err != nil { return errors.New("couldn't store profile: " + err.Error()) } return nil } -func (rm *RecordManager) storeProfile(pu *db.ProfileUpdate, ctx context.Context) error { - err := rm.db.UpdateProfile(pu, ctx) +func (rm *RecordManager) storeProfile(did string, p *lex.ProfileRecord, ctx context.Context) error { + err := rm.db.UpdateProfile(did, p.DisplayName, p.DefaultNick, p.Status, p.Color, ctx) if err != nil { return errors.New("error updating profile: " + err.Error()) } return nil } -func (rm *RecordManager) updateProfile(cs *atoauth.ClientSession, name *string, nick *string, status *string, color *uint64, ctx context.Context) error { +func (rm *RecordManager) updateProfile(cs *atoauth.ClientSession, name *string, nick *string, status *string, color *uint64, ctx context.Context) (*lex.ProfileRecord, error) { profilerecord := &lex.ProfileRecord{ DisplayName: name, DefaultNick: nick, Status: status, Color: color, } - _, err := oauth.UpdateXCVRProfile(cs, profilerecord, ctx) + pr, err := oauth.UpdateXCVRProfile(cs, profilerecord, ctx) if err != nil { - return err + return nil, err } - return nil + return pr, nil } func (rm *RecordManager) createProfile(name *string, nick *string, status *string, color *uint64, sessData *atoauth.ClientSessionData, ctx context.Context) (*lex.ProfileRecord, error) { @@ -128,41 +101,29 @@ func (rm *RecordManager) createProfile(name *string, nick *string, status *strin return p, nil } -func (rm *RecordManager) validateProfile(did string, p *types.PostProfileRequest) (*db.ProfileUpdate, error) { - var pu db.ProfileUpdate - pu.DID = did +func (rm *RecordManager) validateProfile(p *types.PostProfileRequest) error { if p.DisplayName != nil { if atputils.ValidateGraphemesAndLength(*p.DisplayName, 64, 640) { - return nil, errors.New("displayname too long") + return errors.New("displayname too long") } - pu.Name = p.DisplayName - pu.UpdateName = true } if p.DefaultNick != nil { if atputils.ValidateLength(*p.DefaultNick, 16) { - return nil, errors.New("nick too long") + return errors.New("nick too long") } - pu.Nick = p.DefaultNick - pu.UpdateNick = true } if p.Status != nil { if atputils.ValidateGraphemesAndLength(*p.Status, 640, 6400) { - return nil, errors.New("status too long") + return errors.New("status too long") } - pu.Status = p.Status - pu.UpdateStatus = true } if p.Avatar != nil { // TODO think about how to do avatars! - pu.Avatar = p.Avatar - pu.UpdateAvatar = true } if p.Color != nil { if *p.Color > 16777215 || *p.Color < 0 { - return nil, errors.New("color out of bounds") + return errors.New("color out of bounds") } - pu.Color = p.Color - pu.UpdateColor = true } - return &pu, nil + return nil } diff --git a/server/internal/types/lexicons.go b/server/internal/types/lexicons.go index 8d65bf3..8423761 100644 --- a/server/internal/types/lexicons.go +++ b/server/internal/types/lexicons.go @@ -10,8 +10,8 @@ import ( type Profile struct { DID string - DisplayName string - DefaultNick string + DisplayName *string + DefaultNick *string Status *string AvatarCID *string AvatarMIME *string