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 9fe945db 2d0188fb

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
··· 248 } 249 250 type LoginParams struct { 251 - ReturnUrl string 252 - ErrorCode string 253 - AddAccount bool 254 - LoggedInUser *oauth.MultiAccountUser 255 } 256 257 func (p *Pages) Login(w io.Writer, params LoginParams) error {
··· 248 } 249 250 type LoginParams struct { 251 + ReturnUrl string 252 + ErrorCode string 253 + AddAccount bool 254 + Accounts []oauth.AccountInfo 255 } 256 257 func (p *Pages) Login(w io.Writer, params LoginParams) error {
+2 -3
appview/pages/templates/user/login.html
··· 11 </div> 12 {{ end }} 13 14 - {{ if and .LoggedInUser .LoggedInUser.Accounts }} 15 - {{ $accounts := .LoggedInUser.Accounts }} 16 <div class="my-4 border border-gray-200 dark:border-gray-700 rounded overflow-hidden"> 17 <div class="px-3 py-2 bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700"> 18 <span class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Saved accounts</span> 19 </div> 20 <div class="divide-y divide-gray-200 dark:divide-gray-700"> 21 - {{ range $accounts }} 22 <div class="flex items-center justify-between px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700"> 23 <button 24 type="button"
··· 11 </div> 12 {{ end }} 13 14 + {{ if .Accounts }} 15 <div class="my-4 border border-gray-200 dark:border-gray-700 rounded overflow-hidden"> 16 <div class="px-3 py-2 bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700"> 17 <span class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Saved accounts</span> 18 </div> 19 <div class="divide-y divide-gray-200 dark:divide-gray-700"> 20 + {{ range .Accounts }} 21 <div class="flex items-center justify-between px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700"> 22 <button 23 type="button"
+1 -1
appview/pulls/pulls.go
··· 195 mergeCheckResponse := s.mergeCheck(r, f, pull, stack) 196 branchDeleteStatus := s.branchDeleteStatus(r, f, pull) 197 resubmitResult := pages.Unknown 198 - if user != nil && user.Active != nil && user.Active.Did == pull.OwnerDid { 199 resubmitResult = s.resubmitCheck(r, f, pull, stack) 200 } 201
··· 195 mergeCheckResponse := s.mergeCheck(r, f, pull, stack) 196 branchDeleteStatus := s.branchDeleteStatus(r, f, pull) 197 resubmitResult := pages.Unknown 198 + if user != nil && user.Active.Did == pull.OwnerDid { 199 resubmitResult = s.resubmitCheck(r, f, pull, stack) 200 } 201
+1 -1
appview/reporesolver/resolver.go
··· 75 repoAt := repo.RepoAt() 76 isStarred := false 77 roles := repoinfo.RolesInRepo{} 78 - if user != nil && user.Active != nil { 79 isStarred = db.GetStarStatus(rr.execer, user.Active.Did, repoAt) 80 roles.Roles = rr.enforcer.GetPermissionsInRepo(user.Active.Did, repo.Knot, repo.DidSlashRepo()) 81 }
··· 75 repoAt := repo.RepoAt() 76 isStarred := false 77 roles := repoinfo.RolesInRepo{} 78 + if user != nil { 79 isStarred = db.GetStarStatus(rr.execer, user.Active.Did, repoAt) 80 roles.Roles = rr.enforcer.GetPermissionsInRepo(user.Active.Did, repo.Knot, repo.DidSlashRepo()) 81 }
+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") ··· 87 l := s.logger.With("handler", "Logout") 88 89 currentUser := s.oauth.GetMultiAccountUser(r) 90 - if currentUser == nil || currentUser.Active == nil { 91 s.pages.HxRedirect(w, "/login") 92 return 93 }
··· 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") ··· 77 l := s.logger.With("handler", "Logout") 78 79 currentUser := s.oauth.GetMultiAccountUser(r) 80 + if currentUser == nil { 81 s.pages.HxRedirect(w, "/login") 82 return 83 }
+1 -1
appview/state/timeline.go
··· 47 filtered := false 48 49 var userDid string 50 - if user != nil && user.Active != nil { 51 userDid = user.Active.Did 52 } 53 timeline, err := db.MakeTimeline(s.db, 50, userDid, filtered)
··· 47 filtered := false 48 49 var userDid string 50 + if user != nil { 51 userDid = user.Active.Did 52 } 53 timeline, err := db.MakeTimeline(s.db, 50, userDid, filtered)