Monorepo for Tangled tangled.org

appview/{oauth,pages}: cleanup unused codes

- `AccountRegistry.OtherAccounts()` is not used anywhere
- Removed several legacy session value names from `oauth/consts.go`
- We can just embed the `oauth.GetUser()` now

Signed-off-by: Seongmin Lee <git@boltless.me>

boltless.me f3c14331 9298c31c

verified
+21 -83
+6 -14
appview/oauth/accounts.go
··· 53 return &registry 54 } 55 56 - func (o *OAuth) SaveAccounts(w http.ResponseWriter, r *http.Request, registry *AccountRegistry) error { 57 session, err := o.SessStore.Get(r, AccountsName) 58 if err != nil { 59 return err ··· 114 return nil 115 } 116 117 - func (r *AccountRegistry) OtherAccounts(activeDid string) []AccountInfo { 118 - result := make([]AccountInfo, 0, len(r.Accounts)) 119 - for _, acc := range r.Accounts { 120 - if acc.Did != activeDid { 121 - result = append(result, acc) 122 - } 123 - } 124 - return result 125 - } 126 - 127 func (o *OAuth) GetMultiAccountUser(r *http.Request) *MultiAccountUser { 128 - user := o.GetUser(r) 129 - if user == nil { 130 return nil 131 } 132 133 registry := o.GetAccounts(r) 134 return &MultiAccountUser{ 135 - Active: user, 136 Accounts: registry.Accounts, 137 } 138 }
··· 53 return &registry 54 } 55 56 + func (o *OAuth) saveAccounts(w http.ResponseWriter, r *http.Request, registry *AccountRegistry) error { 57 session, err := o.SessStore.Get(r, AccountsName) 58 if err != nil { 59 return err ··· 114 return nil 115 } 116 117 func (o *OAuth) GetMultiAccountUser(r *http.Request) *MultiAccountUser { 118 + sess, err := o.ResumeSession(r) 119 + if err != nil { 120 return nil 121 } 122 123 registry := o.GetAccounts(r) 124 return &MultiAccountUser{ 125 + Active: &User{ 126 + Did: sess.Data.AccountDID.String(), 127 + }, 128 Accounts: registry.Accounts, 129 } 130 }
-35
appview/oauth/accounts_test.go
··· 211 }) 212 } 213 214 - func TestAccountRegistry_OtherAccounts(t *testing.T) { 215 - registry := &AccountRegistry{ 216 - Accounts: []AccountInfo{ 217 - {Did: "did:plc:active", Handle: "active", SessionId: "s1"}, 218 - {Did: "did:plc:other1", Handle: "other1", SessionId: "s2"}, 219 - {Did: "did:plc:other2", Handle: "other2", SessionId: "s3"}, 220 - }, 221 - } 222 - 223 - others := registry.OtherAccounts("did:plc:active") 224 - 225 - if len(others) != 2 { 226 - t.Errorf("OtherAccounts() len = %d, want 2", len(others)) 227 - } 228 - 229 - for _, acc := range others { 230 - if acc.Did == "did:plc:active" { 231 - t.Errorf("OtherAccounts() should not include active account") 232 - } 233 - } 234 - 235 - hasDid := func(did string) bool { 236 - for _, acc := range others { 237 - if acc.Did == did { 238 - return true 239 - } 240 - } 241 - return false 242 - } 243 - 244 - if !hasDid("did:plc:other1") || !hasDid("did:plc:other2") { 245 - t.Errorf("OtherAccounts() missing expected accounts") 246 - } 247 - } 248 - 249 func TestMultiAccountUser_Did(t *testing.T) { 250 t.Run("with active user", func(t *testing.T) { 251 user := &MultiAccountUser{
··· 211 }) 212 } 213 214 func TestMultiAccountUser_Did(t *testing.T) { 215 t.Run("with active user", func(t *testing.T) { 216 user := &MultiAccountUser{
-6
appview/oauth/consts.go
··· 10 SessionDid = "did" 11 SessionId = "id" 12 SessionPds = "pds" 13 - SessionAccessJwt = "accessJwt" 14 - SessionRefreshJwt = "refreshJwt" 15 - SessionExpiry = "expiry" 16 SessionAuthenticated = "authenticated" 17 - 18 - SessionDpopPrivateJwk = "dpopPrivateJwk" 19 - SessionDpopAuthServerNonce = "dpopAuthServerNonce" 20 )
··· 10 SessionDid = "did" 11 SessionId = "id" 12 SessionPds = "pds" 13 SessionAuthenticated = "authenticated" 14 )
+3 -14
appview/oauth/oauth.go
··· 122 if err := registry.AddAccount(sessData.AccountDID.String(), handle, sessData.SessionID); err != nil { 123 return err 124 } 125 - return o.SaveAccounts(w, r, registry) 126 } 127 128 func (o *OAuth) ResumeSession(r *http.Request) (*oauth.ClientSession, error) { ··· 192 sess, err := o.ClientApp.ResumeSession(r.Context(), did, account.SessionId) 193 if err != nil { 194 registry.RemoveAccount(targetDid) 195 - _ = o.SaveAccounts(w, r, registry) 196 return fmt.Errorf("session expired for account: %w", err) 197 } 198 ··· 222 } 223 224 registry.RemoveAccount(targetDid) 225 - return o.SaveAccounts(w, r, registry) 226 } 227 228 type User struct { 229 Did string 230 - } 231 - 232 - func (o *OAuth) GetUser(r *http.Request) *User { 233 - sess, err := o.ResumeSession(r) 234 - if err != nil { 235 - return nil 236 - } 237 - 238 - return &User{ 239 - Did: sess.Data.AccountDID.String(), 240 - } 241 } 242 243 func (o *OAuth) GetDid(r *http.Request) string {
··· 122 if err := registry.AddAccount(sessData.AccountDID.String(), handle, sessData.SessionID); err != nil { 123 return err 124 } 125 + return o.saveAccounts(w, r, registry) 126 } 127 128 func (o *OAuth) ResumeSession(r *http.Request) (*oauth.ClientSession, error) { ··· 192 sess, err := o.ClientApp.ResumeSession(r.Context(), did, account.SessionId) 193 if err != nil { 194 registry.RemoveAccount(targetDid) 195 + _ = o.saveAccounts(w, r, registry) 196 return fmt.Errorf("session expired for account: %w", err) 197 } 198 ··· 222 } 223 224 registry.RemoveAccount(targetDid) 225 + return o.saveAccounts(w, r, registry) 226 } 227 228 type User struct { 229 Did string 230 } 231 232 func (o *OAuth) GetDid(r *http.Request) string {
-2
appview/pages/templates/user/login.html
··· 33 34 {{ if and .LoggedInUser .LoggedInUser.Accounts }} 35 {{ $accounts := .LoggedInUser.Accounts }} 36 - {{ if $accounts }} 37 <div class="my-4 border border-gray-200 dark:border-gray-700 rounded overflow-hidden"> 38 <div class="px-3 py-2 bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700"> 39 <span class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Saved accounts</span> ··· 67 {{ end }} 68 </div> 69 </div> 70 - {{ end }} 71 {{ end }} 72 73 <form
··· 33 34 {{ if and .LoggedInUser .LoggedInUser.Accounts }} 35 {{ $accounts := .LoggedInUser.Accounts }} 36 <div class="my-4 border border-gray-200 dark:border-gray-700 rounded overflow-hidden"> 37 <div class="px-3 py-2 bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700"> 38 <span class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Saved accounts</span> ··· 66 {{ end }} 67 </div> 68 </div> 69 {{ end }} 70 71 <form
+12 -12
appview/state/profile.go
··· 731 732 func (s *State) UploadProfileAvatar(w http.ResponseWriter, r *http.Request) { 733 l := s.logger.With("handler", "UploadProfileAvatar") 734 - user := s.oauth.GetUser(r) 735 - l = l.With("did", user.Did) 736 737 // Parse multipart form (10MB max) 738 if err := r.ParseMultipartForm(10 << 20); err != nil { ··· 779 l.Info("uploaded avatar blob", "cid", uploadBlobResp.Blob.Ref.String()) 780 781 // get current profile record from PDS to get its CID for swap 782 - getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Did, "self") 783 if err != nil { 784 l.Error("failed to get current profile record", "err", err) 785 s.pages.Notice(w, "avatar-error", "Failed to get current profile from your PDS") ··· 803 804 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 805 Collection: tangled.ActorProfileNSID, 806 - Repo: user.Did, 807 Rkey: "self", 808 Record: &lexutil.LexiconTypeDecoder{Val: profileRecord}, 809 SwapRecord: getRecordResp.Cid, ··· 817 818 l.Info("successfully updated profile with avatar") 819 820 - profile, err := db.GetProfile(s.db, user.Did) 821 if err != nil { 822 l.Warn("getting profile data from DB", "err", err) 823 - profile = &models.Profile{Did: user.Did} 824 } 825 profile.Avatar = uploadBlobResp.Blob.Ref.String() 826 ··· 845 846 func (s *State) RemoveProfileAvatar(w http.ResponseWriter, r *http.Request) { 847 l := s.logger.With("handler", "RemoveProfileAvatar") 848 - user := s.oauth.GetUser(r) 849 - l = l.With("did", user.Did) 850 851 client, err := s.oauth.AuthorizedClient(r) 852 if err != nil { ··· 855 return 856 } 857 858 - getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Did, "self") 859 if err != nil { 860 l.Error("failed to get current profile record", "err", err) 861 s.pages.Notice(w, "avatar-error", "Failed to get current profile from your PDS") ··· 879 880 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 881 Collection: tangled.ActorProfileNSID, 882 - Repo: user.Did, 883 Rkey: "self", 884 Record: &lexutil.LexiconTypeDecoder{Val: profileRecord}, 885 SwapRecord: getRecordResp.Cid, ··· 893 894 l.Info("successfully removed avatar from PDS") 895 896 - profile, err := db.GetProfile(s.db, user.Did) 897 if err != nil { 898 l.Warn("getting profile data from DB", "err", err) 899 - profile = &models.Profile{Did: user.Did} 900 } 901 profile.Avatar = "" 902
··· 731 732 func (s *State) UploadProfileAvatar(w http.ResponseWriter, r *http.Request) { 733 l := s.logger.With("handler", "UploadProfileAvatar") 734 + user := s.oauth.GetMultiAccountUser(r) 735 + l = l.With("did", user.Active.Did) 736 737 // Parse multipart form (10MB max) 738 if err := r.ParseMultipartForm(10 << 20); err != nil { ··· 779 l.Info("uploaded avatar blob", "cid", uploadBlobResp.Blob.Ref.String()) 780 781 // get current profile record from PDS to get its CID for swap 782 + getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Active.Did, "self") 783 if err != nil { 784 l.Error("failed to get current profile record", "err", err) 785 s.pages.Notice(w, "avatar-error", "Failed to get current profile from your PDS") ··· 803 804 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 805 Collection: tangled.ActorProfileNSID, 806 + Repo: user.Active.Did, 807 Rkey: "self", 808 Record: &lexutil.LexiconTypeDecoder{Val: profileRecord}, 809 SwapRecord: getRecordResp.Cid, ··· 817 818 l.Info("successfully updated profile with avatar") 819 820 + profile, err := db.GetProfile(s.db, user.Active.Did) 821 if err != nil { 822 l.Warn("getting profile data from DB", "err", err) 823 + profile = &models.Profile{Did: user.Active.Did} 824 } 825 profile.Avatar = uploadBlobResp.Blob.Ref.String() 826 ··· 845 846 func (s *State) RemoveProfileAvatar(w http.ResponseWriter, r *http.Request) { 847 l := s.logger.With("handler", "RemoveProfileAvatar") 848 + user := s.oauth.GetMultiAccountUser(r) 849 + l = l.With("did", user.Active.Did) 850 851 client, err := s.oauth.AuthorizedClient(r) 852 if err != nil { ··· 855 return 856 } 857 858 + getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Active.Did, "self") 859 if err != nil { 860 l.Error("failed to get current profile record", "err", err) 861 s.pages.Notice(w, "avatar-error", "Failed to get current profile from your PDS") ··· 879 880 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 881 Collection: tangled.ActorProfileNSID, 882 + Repo: user.Active.Did, 883 Rkey: "self", 884 Record: &lexutil.LexiconTypeDecoder{Val: profileRecord}, 885 SwapRecord: getRecordResp.Cid, ··· 893 894 l.Info("successfully removed avatar from PDS") 895 896 + profile, err := db.GetProfile(s.db, user.Active.Did) 897 if err != nil { 898 l.Warn("getting profile data from DB", "err", err) 899 + profile = &models.Profile{Did: user.Active.Did} 900 } 901 profile.Avatar = "" 902