Monorepo for Tangled tangled.org

appview/{oauth,pages}: cleanup unused codes #1022

open opened by boltless.me targeting master from sl/uvpzuszrulvq
  • 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

Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:xasnlahkri4ewmbuzly2rlc5/sh.tangled.repo.pull/3mdcq5ouz5l22
+23 -85
Diff #8
+6 -14
appview/oauth/accounts.go
··· 53 53 return &registry 54 54 } 55 55 56 - func (o *OAuth) SaveAccounts(w http.ResponseWriter, r *http.Request, registry *AccountRegistry) error { 56 + func (o *OAuth) saveAccounts(w http.ResponseWriter, r *http.Request, registry *AccountRegistry) error { 57 57 session, err := o.SessStore.Get(r, AccountsName) 58 58 if err != nil { 59 59 o.Logger.Warn("failed to decode existing accounts cookie, will create new", "err", err) ··· 114 114 return nil 115 115 } 116 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 117 func (o *OAuth) GetMultiAccountUser(r *http.Request) *MultiAccountUser { 128 - user := o.GetUser(r) 129 - if user == nil { 118 + sess, err := o.ResumeSession(r) 119 + if err != nil { 130 120 return nil 131 121 } 132 122 133 123 registry := o.GetAccounts(r) 134 124 return &MultiAccountUser{ 135 - Active: user, 125 + Active: &User{ 126 + Did: sess.Data.AccountDID.String(), 127 + }, 136 128 Accounts: registry.Accounts, 137 129 } 138 130 }
-35
appview/oauth/accounts_test.go
··· 211 211 }) 212 212 } 213 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 214 func TestMultiAccountUser_Did(t *testing.T) { 250 215 t.Run("with active user", func(t *testing.T) { 251 216 user := &MultiAccountUser{
-6
appview/oauth/consts.go
··· 10 10 SessionDid = "did" 11 11 SessionId = "id" 12 12 SessionPds = "pds" 13 - SessionAccessJwt = "accessJwt" 14 - SessionRefreshJwt = "refreshJwt" 15 - SessionExpiry = "expiry" 16 13 SessionAuthenticated = "authenticated" 17 - 18 - SessionDpopPrivateJwk = "dpopPrivateJwk" 19 - SessionDpopAuthServerNonce = "dpopAuthServerNonce" 20 14 )
+3 -14
appview/oauth/oauth.go
··· 128 128 if err := registry.AddAccount(sessData.AccountDID.String(), handle, sessData.SessionID); err != nil { 129 129 return err 130 130 } 131 - return o.SaveAccounts(w, r, registry) 131 + return o.saveAccounts(w, r, registry) 132 132 } 133 133 134 134 func (o *OAuth) ResumeSession(r *http.Request) (*oauth.ClientSession, error) { ··· 204 204 sess, err := o.ClientApp.ResumeSession(r.Context(), did, account.SessionId) 205 205 if err != nil { 206 206 registry.RemoveAccount(targetDid) 207 - _ = o.SaveAccounts(w, r, registry) 207 + _ = o.saveAccounts(w, r, registry) 208 208 return fmt.Errorf("session expired for account: %w", err) 209 209 } 210 210 ··· 234 234 } 235 235 236 236 registry.RemoveAccount(targetDid) 237 - return o.SaveAccounts(w, r, registry) 237 + return o.saveAccounts(w, r, registry) 238 238 } 239 239 240 240 type User struct { 241 241 Did string 242 242 } 243 243 244 - func (o *OAuth) GetUser(r *http.Request) *User { 245 - sess, err := o.ResumeSession(r) 246 - if err != nil { 247 - return nil 248 - } 249 - 250 - return &User{ 251 - Did: sess.Data.AccountDID.String(), 252 - } 253 - } 254 - 255 244 func (o *OAuth) GetDid(r *http.Request) string { 256 245 if u := o.GetMultiAccountUser(r); u != nil { 257 246 return u.Did()
-2
appview/pages/templates/user/login.html
··· 13 13 14 14 {{ if and .LoggedInUser .LoggedInUser.Accounts }} 15 15 {{ $accounts := .LoggedInUser.Accounts }} 16 - {{ if $accounts }} 17 16 <div class="my-4 border border-gray-200 dark:border-gray-700 rounded overflow-hidden"> 18 17 <div class="px-3 py-2 bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700"> 19 18 <span class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Saved accounts</span> ··· 48 47 </div> 49 48 </div> 50 49 {{ end }} 51 - {{ end }} 52 50 53 51 <form 54 52 class="mt-4 group"
+14 -14
appview/state/profile.go
··· 861 861 862 862 func (s *State) UploadProfileAvatar(w http.ResponseWriter, r *http.Request) { 863 863 l := s.logger.With("handler", "UploadProfileAvatar") 864 - user := s.oauth.GetUser(r) 865 - l = l.With("did", user.Did) 864 + user := s.oauth.GetMultiAccountUser(r) 865 + l = l.With("did", user.Active.Did) 866 866 867 867 // Parse multipart form (10MB max) 868 868 if err := r.ParseMultipartForm(10 << 20); err != nil { ··· 909 909 l.Info("uploaded avatar blob", "cid", uploadBlobResp.Blob.Ref.String()) 910 910 911 911 // get current profile record from PDS to get its CID for swap 912 - getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Did, "self") 912 + getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Active.Did, "self") 913 913 if err != nil { 914 914 l.Error("failed to get current profile record", "err", err) 915 915 s.pages.Notice(w, "avatar-error", "Failed to get current profile from your PDS") ··· 933 933 934 934 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 935 935 Collection: tangled.ActorProfileNSID, 936 - Repo: user.Did, 936 + Repo: user.Active.Did, 937 937 Rkey: "self", 938 938 Record: &lexutil.LexiconTypeDecoder{Val: profileRecord}, 939 939 SwapRecord: getRecordResp.Cid, ··· 947 947 948 948 l.Info("successfully updated profile with avatar") 949 949 950 - profile, err := db.GetProfile(s.db, user.Did) 950 + profile, err := db.GetProfile(s.db, user.Active.Did) 951 951 if err != nil { 952 952 l.Warn("getting profile data from DB", "err", err) 953 953 } 954 954 if profile == nil { 955 - profile = &models.Profile{Did: user.Did} 955 + profile = &models.Profile{Did: user.Active.Did} 956 956 } 957 957 profile.Avatar = uploadBlobResp.Blob.Ref.String() 958 958 ··· 977 977 978 978 func (s *State) RemoveProfileAvatar(w http.ResponseWriter, r *http.Request) { 979 979 l := s.logger.With("handler", "RemoveProfileAvatar") 980 - user := s.oauth.GetUser(r) 981 - l = l.With("did", user.Did) 980 + user := s.oauth.GetMultiAccountUser(r) 981 + l = l.With("did", user.Active.Did) 982 982 983 983 client, err := s.oauth.AuthorizedClient(r) 984 984 if err != nil { ··· 987 987 return 988 988 } 989 989 990 - getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Did, "self") 990 + getRecordResp, err := comatproto.RepoGetRecord(r.Context(), client, "", tangled.ActorProfileNSID, user.Active.Did, "self") 991 991 if err != nil { 992 992 l.Error("failed to get current profile record", "err", err) 993 993 s.pages.Notice(w, "avatar-error", "Failed to get current profile from your PDS") ··· 1011 1011 1012 1012 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1013 1013 Collection: tangled.ActorProfileNSID, 1014 - Repo: user.Did, 1014 + Repo: user.Active.Did, 1015 1015 Rkey: "self", 1016 1016 Record: &lexutil.LexiconTypeDecoder{Val: profileRecord}, 1017 1017 SwapRecord: getRecordResp.Cid, ··· 1025 1025 1026 1026 l.Info("successfully removed avatar from PDS") 1027 1027 1028 - profile, err := db.GetProfile(s.db, user.Did) 1028 + profile, err := db.GetProfile(s.db, user.Active.Did) 1029 1029 if err != nil { 1030 1030 l.Warn("getting profile data from DB", "err", err) 1031 1031 } 1032 1032 if profile == nil { 1033 - profile = &models.Profile{Did: user.Did} 1033 + profile = &models.Profile{Did: user.Active.Did} 1034 1034 } 1035 1035 profile.Avatar = "" 1036 1036 ··· 1059 1059 log.Println("invalid profile update form", err) 1060 1060 return 1061 1061 } 1062 - user := s.oauth.GetUser(r) 1062 + user := s.oauth.GetMultiAccountUser(r) 1063 1063 1064 1064 hideOthers := false 1065 1065 hideMine := false ··· 1071 1071 hideOthers = true 1072 1072 } 1073 1073 1074 - err = db.UpsertPunchcardPreference(s.db, user.Did, hideMine, hideOthers) 1074 + err = db.UpsertPunchcardPreference(s.db, user.Active.Did, hideMine, hideOthers) 1075 1075 if err != nil { 1076 1076 log.Println("failed to update punchcard preferences", err) 1077 1077 return

History

9 rounds 0 comments
sign up or login to add to the discussion
1 commit
expand
appview/{oauth,pages}: cleanup unused codes
2/3 failed, 1/3 success
expand
no conflicts, ready to merge
expand 0 comments
1 commit
expand
appview/{oauth,pages}: cleanup unused codes
2/3 timeout, 1/3 success
expand
expand 0 comments
1 commit
expand
appview/{oauth,pages}: cleanup unused codes
2/3 timeout, 1/3 success
expand
expand 0 comments
1 commit
expand
appview/{oauth,pages}: cleanup unused codes
2/3 failed, 1/3 success
expand
expand 0 comments
1 commit
expand
appview/{oauth,pages}: cleanup unused codes
2/3 failed, 1/3 success
expand
expand 0 comments
1 commit
expand
appview/{oauth,pages}: cleanup unused codes
3/3 success
expand
expand 0 comments
1 commit
expand
appview/{oauth,pages}: cleanup unused codes
3/3 success
expand
expand 0 comments
1 commit
expand
appview/{oauth,pages}: cleanup unused codes
2/3 failed, 1/3 success
expand
expand 0 comments
1 commit
expand
appview/{oauth,pages}: cleanup unused codes
2/3 failed, 1/3 success
expand
expand 0 comments