From 81a8e88abcaf3eef45240dd36b003f17905ccbd4 Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Sun, 25 Jan 2026 11:02:17 +0000 Subject: [PATCH] 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 --- appview/oauth/accounts.go | 20 ++++++-------------- appview/oauth/accounts_test.go | 35 ----------------------------------- appview/oauth/consts.go | 6 ------ appview/oauth/oauth.go | 17 +++-------------- appview/state/profile.go | 24 ++++++++++++------------ appview/pages/templates/user/login.html | 2 -- 6 file(s) changed, 21 insertion(s)(+), 83 deletion(s)(-) diff --git a/appview/oauth/accounts.go b/appview/oauth/accounts.go --- a/appview/oauth/accounts.go +++ b/appview/oauth/accounts.go @@ -53,7 +53,7 @@ return ®istry } -func (o *OAuth) SaveAccounts(w http.ResponseWriter, r *http.Request, registry *AccountRegistry) error { +func (o *OAuth) saveAccounts(w http.ResponseWriter, r *http.Request, registry *AccountRegistry) error { session, err := o.SessStore.Get(r, AccountsName) if err != nil { return err @@ -114,25 +114,17 @@ return nil } -func (r *AccountRegistry) OtherAccounts(activeDid string) []AccountInfo { - result := make([]AccountInfo, 0, len(r.Accounts)) - for _, acc := range r.Accounts { - if acc.Did != activeDid { - result = append(result, acc) - } - } - return result -} - func (o *OAuth) GetMultiAccountUser(r *http.Request) *MultiAccountUser { - user := o.GetUser(r) - if user == nil { + sess, err := o.ResumeSession(r) + if err != nil { return nil } registry := o.GetAccounts(r) return &MultiAccountUser{ - Active: user, + Active: &User{ + Did: sess.Data.AccountDID.String(), + }, Accounts: registry.Accounts, } } diff --git a/appview/oauth/accounts_test.go b/appview/oauth/accounts_test.go --- a/appview/oauth/accounts_test.go +++ b/appview/oauth/accounts_test.go @@ -211,41 +211,6 @@ }) } -func TestAccountRegistry_OtherAccounts(t *testing.T) { - registry := &AccountRegistry{ - Accounts: []AccountInfo{ - {Did: "did:plc:active", Handle: "active", SessionId: "s1"}, - {Did: "did:plc:other1", Handle: "other1", SessionId: "s2"}, - {Did: "did:plc:other2", Handle: "other2", SessionId: "s3"}, - }, - } - - others := registry.OtherAccounts("did:plc:active") - - if len(others) != 2 { - t.Errorf("OtherAccounts() len = %d, want 2", len(others)) - } - - for _, acc := range others { - if acc.Did == "did:plc:active" { - t.Errorf("OtherAccounts() should not include active account") - } - } - - hasDid := func(did string) bool { - for _, acc := range others { - if acc.Did == did { - return true - } - } - return false - } - - if !hasDid("did:plc:other1") || !hasDid("did:plc:other2") { - t.Errorf("OtherAccounts() missing expected accounts") - } -} - func TestMultiAccountUser_Did(t *testing.T) { t.Run("with active user", func(t *testing.T) { user := &MultiAccountUser{ diff --git a/appview/oauth/consts.go b/appview/oauth/consts.go --- a/appview/oauth/consts.go +++ b/appview/oauth/consts.go @@ -10,11 +10,5 @@ SessionDid = "did" SessionId = "id" SessionPds = "pds" - SessionAccessJwt = "accessJwt" - SessionRefreshJwt = "refreshJwt" - SessionExpiry = "expiry" SessionAuthenticated = "authenticated" - - SessionDpopPrivateJwk = "dpopPrivateJwk" - SessionDpopAuthServerNonce = "dpopAuthServerNonce" ) diff --git a/appview/oauth/oauth.go b/appview/oauth/oauth.go --- a/appview/oauth/oauth.go +++ b/appview/oauth/oauth.go @@ -122,7 +122,7 @@ if err := registry.AddAccount(sessData.AccountDID.String(), handle, sessData.SessionID); err != nil { return err } - return o.SaveAccounts(w, r, registry) + return o.saveAccounts(w, r, registry) } func (o *OAuth) ResumeSession(r *http.Request) (*oauth.ClientSession, error) { @@ -198,7 +198,7 @@ sess, err := o.ClientApp.ResumeSession(r.Context(), did, account.SessionId) if err != nil { registry.RemoveAccount(targetDid) - _ = o.SaveAccounts(w, r, registry) + _ = o.saveAccounts(w, r, registry) return fmt.Errorf("session expired for account: %w", err) } @@ -228,22 +228,11 @@ } registry.RemoveAccount(targetDid) - return o.SaveAccounts(w, r, registry) + return o.saveAccounts(w, r, registry) } type User struct { Did string -} - -func (o *OAuth) GetUser(r *http.Request) *User { - sess, err := o.ResumeSession(r) - if err != nil { - return nil - } - - return &User{ - Did: sess.Data.AccountDID.String(), - } } func (o *OAuth) GetDid(r *http.Request) string { diff --git a/appview/state/profile.go b/appview/state/profile.go --- a/appview/state/profile.go +++ b/appview/state/profile.go @@ -731,8 +731,8 @@ func (s *State) UploadProfileAvatar(w http.ResponseWriter, r *http.Request) { l := s.logger.With("handler", "UploadProfileAvatar") - user := s.oauth.GetUser(r) - l = l.With("did", user.Did) + user := s.oauth.GetMultiAccountUser(r) + l = l.With("did", user.Active.Did) // Parse multipart form (10MB max) if err := r.ParseMultipartForm(10 << 20); err != nil { @@ -779,7 +779,7 @@ l.Info("uploaded avatar blob", "cid", uploadBlobResp.Blob.Ref.String()) // get current profile record from PDS to get its CID for swap - getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Did, "self") + getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Active.Did, "self") if err != nil { l.Error("failed to get current profile record", "err", err) s.pages.Notice(w, "avatar-error", "Failed to get current profile from your PDS") @@ -803,7 +803,7 @@ _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ Collection: tangled.ActorProfileNSID, - Repo: user.Did, + Repo: user.Active.Did, Rkey: "self", Record: &lexutil.LexiconTypeDecoder{Val: profileRecord}, SwapRecord: getRecordResp.Cid, @@ -817,10 +817,10 @@ l.Info("successfully updated profile with avatar") - profile, err := db.GetProfile(s.db, user.Did) + profile, err := db.GetProfile(s.db, user.Active.Did) if err != nil { l.Warn("getting profile data from DB", "err", err) - profile = &models.Profile{Did: user.Did} + profile = &models.Profile{Did: user.Active.Did} } profile.Avatar = uploadBlobResp.Blob.Ref.String() @@ -845,8 +845,8 @@ func (s *State) RemoveProfileAvatar(w http.ResponseWriter, r *http.Request) { l := s.logger.With("handler", "RemoveProfileAvatar") - user := s.oauth.GetUser(r) - l = l.With("did", user.Did) + user := s.oauth.GetMultiAccountUser(r) + l = l.With("did", user.Active.Did) client, err := s.oauth.AuthorizedClient(r) if err != nil { @@ -855,7 +855,7 @@ return } - getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Did, "self") + getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Active.Did, "self") if err != nil { l.Error("failed to get current profile record", "err", err) s.pages.Notice(w, "avatar-error", "Failed to get current profile from your PDS") @@ -879,7 +879,7 @@ _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ Collection: tangled.ActorProfileNSID, - Repo: user.Did, + Repo: user.Active.Did, Rkey: "self", Record: &lexutil.LexiconTypeDecoder{Val: profileRecord}, SwapRecord: getRecordResp.Cid, @@ -893,10 +893,10 @@ l.Info("successfully removed avatar from PDS") - profile, err := db.GetProfile(s.db, user.Did) + profile, err := db.GetProfile(s.db, user.Active.Did) if err != nil { l.Warn("getting profile data from DB", "err", err) - profile = &models.Profile{Did: user.Did} + profile = &models.Profile{Did: user.Active.Did} } profile.Avatar = "" diff --git a/appview/pages/templates/user/login.html b/appview/pages/templates/user/login.html --- a/appview/pages/templates/user/login.html +++ b/appview/pages/templates/user/login.html @@ -33,7 +33,6 @@ {{ if and .LoggedInUser .LoggedInUser.Accounts }} {{ $accounts := .LoggedInUser.Accounts }} - {{ if $accounts }}
Saved accounts @@ -67,7 +66,6 @@ {{ end }}
- {{ end }} {{ end }}