Monorepo for Tangled tangled.org

appview/{pages,state}: simplify LoginPage params

We don't need to pass full `MultiAccountUser` here. just `[]AccountInfo`
should be enough. This way, we can make `MultiAccountUser` to always
hold an active user.

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

boltless.me ca543b15 c745ef9e

verified
+17 -49
+2 -5
appview/oauth/accounts.go
··· 23 } 24 25 type MultiAccountUser struct { 26 - Active *User 27 Accounts []AccountInfo 28 } 29 30 func (m *MultiAccountUser) Did() string { 31 - if m.Active == nil { 32 - return "" 33 - } 34 return m.Active.Did 35 } 36 ··· 122 123 registry := o.GetAccounts(r) 124 return &MultiAccountUser{ 125 - Active: &User{ 126 Did: sess.Data.AccountDID.String(), 127 }, 128 Accounts: registry.Accounts,
··· 23 } 24 25 type MultiAccountUser struct { 26 + Active User 27 Accounts []AccountInfo 28 } 29 30 func (m *MultiAccountUser) Did() string { 31 return m.Active.Did 32 } 33 ··· 119 120 registry := o.GetAccounts(r) 121 return &MultiAccountUser{ 122 + Active: User{ 123 Did: sess.Data.AccountDID.String(), 124 }, 125 Accounts: registry.Accounts,
-18
appview/oauth/accounts_test.go
··· 210 } 211 }) 212 } 213 - 214 - func TestMultiAccountUser_Did(t *testing.T) { 215 - t.Run("with active user", func(t *testing.T) { 216 - user := &MultiAccountUser{ 217 - Active: &User{Did: "did:plc:test"}, 218 - } 219 - if user.Did() != "did:plc:test" { 220 - t.Errorf("Did() = %s, want did:plc:test", user.Did()) 221 - } 222 - }) 223 - 224 - t.Run("with nil active", func(t *testing.T) { 225 - user := &MultiAccountUser{Active: nil} 226 - if user.Did() != "" { 227 - t.Errorf("Did() = %s, want empty string", user.Did()) 228 - } 229 - }) 230 - }
··· 210 } 211 }) 212 }
+4 -4
appview/pages/pages.go
··· 229 } 230 231 type LoginParams struct { 232 - ReturnUrl string 233 - ErrorCode string 234 - AddAccount bool 235 - LoggedInUser *oauth.MultiAccountUser 236 } 237 238 func (p *Pages) Login(w io.Writer, params LoginParams) error {
··· 229 } 230 231 type LoginParams struct { 232 + ReturnUrl string 233 + ErrorCode string 234 + AddAccount bool 235 + Accounts []oauth.AccountInfo 236 } 237 238 func (p *Pages) Login(w io.Writer, params LoginParams) error {
+2 -3
appview/pages/templates/user/login.html
··· 31 </div> 32 {{ end }} 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> 39 </div> 40 <div class="divide-y divide-gray-200 dark:divide-gray-700"> 41 - {{ range $accounts }} 42 <div class="flex items-center justify-between px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700"> 43 <button 44 type="button"
··· 31 </div> 32 {{ end }} 33 34 + {{ if .Accounts }} 35 <div class="my-4 border border-gray-200 dark:border-gray-700 rounded overflow-hidden"> 36 <div class="px-3 py-2 bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700"> 37 <span class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Saved accounts</span> 38 </div> 39 <div class="divide-y divide-gray-200 dark:divide-gray-700"> 40 + {{ range .Accounts }} 41 <div class="flex items-center justify-between px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700"> 42 <button 43 type="button"
+1 -1
appview/pulls/pulls.go
··· 191 mergeCheckResponse := s.mergeCheck(r, f, pull, stack) 192 branchDeleteStatus := s.branchDeleteStatus(r, f, pull) 193 resubmitResult := pages.Unknown 194 - if user != nil && user.Active != nil && user.Active.Did == pull.OwnerDid { 195 resubmitResult = s.resubmitCheck(r, f, pull, stack) 196 } 197
··· 191 mergeCheckResponse := s.mergeCheck(r, f, pull, stack) 192 branchDeleteStatus := s.branchDeleteStatus(r, f, pull) 193 resubmitResult := pages.Unknown 194 + if user != nil && user.Active.Did == pull.OwnerDid { 195 resubmitResult = s.resubmitCheck(r, f, pull, stack) 196 } 197
+1 -1
appview/reporesolver/resolver.go
··· 69 repoAt := repo.RepoAt() 70 isStarred := false 71 roles := repoinfo.RolesInRepo{} 72 - if user != nil && user.Active != nil { 73 isStarred = db.GetStarStatus(rr.execer, user.Active.Did, repoAt) 74 roles.Roles = rr.enforcer.GetPermissionsInRepo(user.Active.Did, repo.Knot, repo.DidSlashRepo()) 75 }
··· 69 repoAt := repo.RepoAt() 70 isStarred := false 71 roles := repoinfo.RolesInRepo{} 72 + if user != nil { 73 isStarred = db.GetStarStatus(rr.execer, user.Active.Did, repoAt) 74 roles.Roles = rr.enforcer.GetPermissionsInRepo(user.Active.Did, repo.Knot, repo.DidSlashRepo()) 75 }
+6 -16
appview/state/login.go
··· 5 "net/http" 6 "strings" 7 8 - "tangled.org/core/appview/oauth" 9 "tangled.org/core/appview/pages" 10 ) 11 ··· 18 errorCode := r.URL.Query().Get("error") 19 addAccount := r.URL.Query().Get("mode") == "add_account" 20 21 - user := s.oauth.GetMultiAccountUser(r) 22 - if user == nil { 23 - registry := s.oauth.GetAccounts(r) 24 - if len(registry.Accounts) > 0 { 25 - user = &oauth.MultiAccountUser{ 26 - Active: nil, 27 - Accounts: registry.Accounts, 28 - } 29 - } 30 - } 31 s.pages.Login(w, pages.LoginParams{ 32 - ReturnUrl: returnURL, 33 - ErrorCode: errorCode, 34 - AddAccount: addAccount, 35 - LoggedInUser: user, 36 }) 37 case http.MethodPost: 38 handle := r.FormValue("handle") ··· 80 l := s.logger.With("handler", "Logout") 81 82 currentUser := s.oauth.GetMultiAccountUser(r) 83 - if currentUser == nil || currentUser.Active == nil { 84 s.pages.HxRedirect(w, "/login") 85 return 86 }
··· 5 "net/http" 6 "strings" 7 8 "tangled.org/core/appview/pages" 9 ) 10 ··· 17 errorCode := r.URL.Query().Get("error") 18 addAccount := r.URL.Query().Get("mode") == "add_account" 19 20 + registry := s.oauth.GetAccounts(r) 21 s.pages.Login(w, pages.LoginParams{ 22 + ReturnUrl: returnURL, 23 + ErrorCode: errorCode, 24 + AddAccount: addAccount, 25 + Accounts: registry.Accounts, 26 }) 27 case http.MethodPost: 28 handle := r.FormValue("handle") ··· 70 l := s.logger.With("handler", "Logout") 71 72 currentUser := s.oauth.GetMultiAccountUser(r) 73 + if currentUser == nil { 74 s.pages.HxRedirect(w, "/login") 75 return 76 }
+1 -1
appview/state/state.go
··· 248 filtered := false 249 250 var userDid string 251 - if user != nil && user.Active != nil { 252 userDid = user.Active.Did 253 } 254 timeline, err := db.MakeTimeline(s.db, 50, userDid, filtered)
··· 248 filtered := false 249 250 var userDid string 251 + if user != nil { 252 userDid = user.Active.Did 253 } 254 timeline, err := db.MakeTimeline(s.db, 50, userDid, filtered)