backend for xcvr appview
at main 129 lines 4.0 kB view raw
1package recordmanager 2 3import ( 4 "context" 5 "errors" 6 "rvcx/internal/atputils" 7 "rvcx/internal/lex" 8 "rvcx/internal/oauth" 9 "rvcx/internal/types" 10 11 atoauth "github.com/bluesky-social/indigo/atproto/auth/oauth" 12) 13 14func (rm *RecordManager) AcceptProfile(p lex.ProfileRecord, did string, ctx context.Context) error { 15 err := rm.storeProfile(did, &p, ctx) 16 if err != nil { 17 return errors.New("failed to store profile: " + err.Error()) 18 } 19 return nil 20} 21 22func (rm *RecordManager) DeleteProfile(did string, cid string, ctx context.Context) error { 23 return rm.db.DeleteProfile(did, cid, ctx) 24} 25 26func (rm *RecordManager) CreateInitialProfile(sessData *atoauth.ClientSessionData, ctx context.Context) error { 27 nick := "wanderer" 28 status := "just setting up my xcvr" 29 color := uint64(3702605) 30 handle, err := rm.db.FullResolveDid(sessData.AccountDID.String(), ctx) 31 if err != nil { 32 return errors.New("i couldn't find the handle, so i couldn't create default profile record. gootbye") 33 } 34 35 p, err := rm.createProfile(&handle, &nick, &status, &color, sessData, ctx) 36 if err != nil { 37 return errors.New("AAAAA error creating profile" + err.Error()) 38 } 39 rm.log.Deprintln("initializing profile....") 40 err = rm.db.InitializeProfile(sessData.AccountDID.String(), p.DisplayName, p.DefaultNick, p.Status, p.Color, ctx) 41 if err != nil { 42 return errors.New("failed to initialize profile: " + err.Error()) 43 } 44 return nil 45 46} 47 48func (rm *RecordManager) PostProfile(cs *atoauth.ClientSession, ctx context.Context, p *types.PostProfileRequest) error { 49 err := rm.validateProfile(p) 50 if err != nil { 51 return errors.New("couldn't validate profile: " + err.Error()) 52 } 53 pr, err := rm.updateProfile(cs, p.DisplayName, p.DefaultNick, p.Status, p.Color, ctx) 54 if err != nil { 55 return errors.New("couldn't create profile: " + err.Error()) 56 } 57 err = rm.storeProfile(cs.Data.AccountDID.String(), pr, ctx) 58 if err != nil { 59 return errors.New("couldn't store profile: " + err.Error()) 60 } 61 return nil 62} 63 64func (rm *RecordManager) storeProfile(did string, p *lex.ProfileRecord, ctx context.Context) error { 65 err := rm.db.UpdateProfile(did, p.DisplayName, p.DefaultNick, p.Status, p.Color, ctx) 66 if err != nil { 67 return errors.New("error updating profile: " + err.Error()) 68 } 69 return nil 70} 71 72func (rm *RecordManager) updateProfile(cs *atoauth.ClientSession, name *string, nick *string, status *string, color *uint64, ctx context.Context) (*lex.ProfileRecord, error) { 73 profilerecord := &lex.ProfileRecord{ 74 DisplayName: name, 75 DefaultNick: nick, 76 Status: status, 77 Color: color, 78 } 79 pr, err := oauth.UpdateXCVRProfile(cs, profilerecord, ctx) 80 if err != nil { 81 return nil, err 82 } 83 return pr, nil 84} 85 86func (rm *RecordManager) createProfile(name *string, nick *string, status *string, color *uint64, sessData *atoauth.ClientSessionData, ctx context.Context) (*lex.ProfileRecord, error) { 87 profilerecord := &lex.ProfileRecord{ 88 DisplayName: name, 89 DefaultNick: nick, 90 Status: status, 91 Color: color, 92 } 93 client, err := rm.service.ResumeSession(ctx, sessData.AccountDID, sessData.SessionID) 94 if err != nil { 95 return nil, err 96 } 97 p, err := oauth.CreateXCVRProfile(client, profilerecord, ctx) 98 if err != nil { 99 return nil, errors.New("failed to create profile: " + err.Error()) 100 } 101 return p, nil 102} 103 104func (rm *RecordManager) validateProfile(p *types.PostProfileRequest) error { 105 if p.DisplayName != nil { 106 if atputils.ValidateGraphemesAndLength(*p.DisplayName, 64, 640) { 107 return errors.New("displayname too long") 108 } 109 } 110 if p.DefaultNick != nil { 111 if atputils.ValidateLength(*p.DefaultNick, 16) { 112 return errors.New("nick too long") 113 } 114 } 115 if p.Status != nil { 116 if atputils.ValidateGraphemesAndLength(*p.Status, 640, 6400) { 117 return errors.New("status too long") 118 } 119 } 120 if p.Avatar != nil { 121 // TODO think about how to do avatars! 122 } 123 if p.Color != nil { 124 if *p.Color > 16777215 || *p.Color < 0 { 125 return errors.New("color out of bounds") 126 } 127 } 128 return nil 129}