From b7ed1508ccdec47243646106b3d272d6af5edc0b Mon Sep 17 00:00:00 2001 From: dusk Date: Sun, 17 Aug 2025 14:26:32 +0300 Subject: [PATCH] appview: state: dedup profile pages Change-Id: lonosovplokkluzqmztkzyumrkznsqwn - rename existing ProfilePage to ProfileHomePage Signed-off-by: dusk --- appview/pages/pages.go | 4 +- appview/state/profile.go | 175 +++++++++++++++++---------------------- 2 files changed, 78 insertions(+), 101 deletions(-) diff --git a/appview/pages/pages.go b/appview/pages/pages.go index 9744e3e..4dd9d97 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -401,7 +401,7 @@ func (p *Pages) ForkRepo(w io.Writer, params ForkRepoParams) error { return p.execute("repo/fork", w, params) } -type ProfilePageParams struct { +type ProfileHomePageParams struct { LoggedInUser *oauth.User Repos []db.Repo CollaboratingRepos []db.Repo @@ -420,7 +420,7 @@ type ProfileCard struct { Profile *db.Profile } -func (p *Pages) ProfilePage(w io.Writer, params ProfilePageParams) error { +func (p *Pages) ProfileHomePage(w io.Writer, params ProfileHomePageParams) error { return p.execute("user/profile", w, params) } diff --git a/appview/state/profile.go b/appview/state/profile.go index f381ca1..ab01fb1 100644 --- a/appview/state/profile.go +++ b/appview/state/profile.go @@ -25,7 +25,7 @@ func (s *State) Profile(w http.ResponseWriter, r *http.Request) { tabVal := r.URL.Query().Get("tab") switch tabVal { case "": - s.profilePage(w, r) + s.profileHomePage(w, r) case "repos": s.reposPage(w, r) case "followers": @@ -35,33 +35,74 @@ func (s *State) Profile(w http.ResponseWriter, r *http.Request) { } } -func (s *State) profilePage(w http.ResponseWriter, r *http.Request) { +type ProfilePageParams struct { + Id identity.Identity + LoggedInUser *oauth.User + Card pages.ProfileCard +} + +func (s *State) profilePage(w http.ResponseWriter, r *http.Request) *ProfilePageParams { didOrHandle := chi.URLParam(r, "user") if didOrHandle == "" { - http.Error(w, "Bad request", http.StatusBadRequest) - return + http.Error(w, "bad request", http.StatusBadRequest) + return nil } ident, ok := r.Context().Value("resolvedId").(identity.Identity) if !ok { - s.pages.Error404(w) - return + log.Printf("malformed middleware") + w.WriteHeader(http.StatusInternalServerError) + return nil } + did := ident.DID.String() - profile, err := db.GetProfile(s.db, ident.DID.String()) + profile, err := db.GetProfile(s.db, did) if err != nil { - log.Printf("getting profile data for %s: %s", ident.DID.String(), err) + log.Printf("getting profile data for %s: %s", did, err) } + followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did) + if err != nil { + log.Printf("getting follow stats for %s: %s", did, err) + } + + loggedInUser := s.oauth.GetUser(r) + followStatus := db.IsNotFollowing + if loggedInUser != nil { + followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, did) + } + + return &ProfilePageParams{ + Id: ident, + LoggedInUser: loggedInUser, + Card: pages.ProfileCard{ + UserDid: did, + UserHandle: ident.Handle.String(), + Profile: profile, + FollowStatus: followStatus, + FollowersCount: followersCount, + FollowingCount: followingCount, + }, + } +} + +func (s *State) profileHomePage(w http.ResponseWriter, r *http.Request) { + pageWithProfile := s.profilePage(w, r) + if pageWithProfile == nil { + return + } + + id := pageWithProfile.Id repos, err := db.GetRepos( s.db, 0, - db.FilterEq("did", ident.DID.String()), + db.FilterEq("did", id.DID), ) if err != nil { - log.Printf("getting repos for %s: %s", ident.DID.String(), err) + log.Printf("getting repos for %s: %s", id.DID, err) } + profile := pageWithProfile.Card.Profile // filter out ones that are pinned pinnedRepos := []db.Repo{} for i, r := range repos { @@ -76,9 +117,9 @@ func (s *State) profilePage(w http.ResponseWriter, r *http.Request) { } } - collaboratingRepos, err := db.CollaboratingIn(s.db, ident.DID.String()) + collaboratingRepos, err := db.CollaboratingIn(s.db, id.DID.String()) if err != nil { - log.Printf("getting collaborating repos for %s: %s", ident.DID.String(), err) + log.Printf("getting collaborating repos for %s: %s", id.DID, err) } pinnedCollaboratingRepos := []db.Repo{} @@ -89,9 +130,9 @@ func (s *State) profilePage(w http.ResponseWriter, r *http.Request) { } } - timeline, err := db.MakeProfileTimeline(s.db, ident.DID.String()) + timeline, err := db.MakeProfileTimeline(s.db, id.DID.String()) if err != nil { - log.Printf("failed to create profile timeline for %s: %s", ident.DID.String(), err) + log.Printf("failed to create profile timeline for %s: %s", id.DID, err) } var didsToResolve []string @@ -113,89 +154,48 @@ func (s *State) profilePage(w http.ResponseWriter, r *http.Request) { } } - followers, following, err := db.GetFollowerFollowingCount(s.db, ident.DID.String()) - if err != nil { - log.Printf("getting follow stats repos for %s: %s", ident.DID.String(), err) - } - - loggedInUser := s.oauth.GetUser(r) - followStatus := db.IsNotFollowing - if loggedInUser != nil { - followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, ident.DID.String()) - } - now := time.Now() startOfYear := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.UTC) punchcard, err := db.MakePunchcard( s.db, - db.FilterEq("did", ident.DID.String()), + db.FilterEq("did", id.DID), db.FilterGte("date", startOfYear.Format(time.DateOnly)), db.FilterLte("date", now.Format(time.DateOnly)), ) if err != nil { - log.Println("failed to get punchcard for did", "did", ident.DID.String(), "err", err) + log.Println("failed to get punchcard for did", "did", id.DID, "err", err) } - s.pages.ProfilePage(w, pages.ProfilePageParams{ - LoggedInUser: loggedInUser, + s.pages.ProfileHomePage(w, pages.ProfileHomePageParams{ + LoggedInUser: pageWithProfile.LoggedInUser, Repos: pinnedRepos, CollaboratingRepos: pinnedCollaboratingRepos, - Card: pages.ProfileCard{ - UserDid: ident.DID.String(), - UserHandle: ident.Handle.String(), - Profile: profile, - FollowStatus: followStatus, - FollowersCount: followers, - FollowingCount: following, - }, - Punchcard: punchcard, - ProfileTimeline: timeline, + Card: pageWithProfile.Card, + Punchcard: punchcard, + ProfileTimeline: timeline, }) } func (s *State) reposPage(w http.ResponseWriter, r *http.Request) { - ident, ok := r.Context().Value("resolvedId").(identity.Identity) - if !ok { - s.pages.Error404(w) + pageWithProfile := s.profilePage(w, r) + if pageWithProfile == nil { return } - profile, err := db.GetProfile(s.db, ident.DID.String()) - if err != nil { - log.Printf("getting profile data for %s: %s", ident.DID.String(), err) - } - + id := pageWithProfile.Id repos, err := db.GetRepos( s.db, 0, - db.FilterEq("did", ident.DID.String()), + db.FilterEq("did", id.DID), ) if err != nil { - log.Printf("getting repos for %s: %s", ident.DID.String(), err) - } - - loggedInUser := s.oauth.GetUser(r) - followStatus := db.IsNotFollowing - if loggedInUser != nil { - followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, ident.DID.String()) - } - - followers, following, err := db.GetFollowerFollowingCount(s.db, ident.DID.String()) - if err != nil { - log.Printf("getting follow stats repos for %s: %s", ident.DID.String(), err) + log.Printf("getting repos for %s: %s", id.DID, err) } s.pages.ReposPage(w, pages.ReposPageParams{ - LoggedInUser: loggedInUser, + LoggedInUser: pageWithProfile.LoggedInUser, Repos: repos, - Card: pages.ProfileCard{ - UserDid: ident.DID.String(), - UserHandle: ident.Handle.String(), - Profile: profile, - FollowStatus: followStatus, - FollowersCount: followers, - FollowingCount: following, - }, + Card: pageWithProfile.Card, }) } @@ -206,23 +206,16 @@ type FollowsPageParams struct { } func (s *State) followPage(w http.ResponseWriter, r *http.Request, fetchFollows func(db.Execer, string) ([]db.Follow, error), extractDid func(db.Follow) string) *FollowsPageParams { - ident, ok := r.Context().Value("resolvedId").(identity.Identity) - if !ok { - s.pages.Error404(w) + pageWithProfile := s.profilePage(w, r) + if pageWithProfile == nil { return nil } - did := ident.DID.String() - - profile, err := db.GetProfile(s.db, did) - if err != nil { - log.Printf("getting profile data for %s: %s", did, err) - } - loggedInUser := s.oauth.GetUser(r) + id := pageWithProfile.Id - follows, err := fetchFollows(s.db, did) + follows, err := fetchFollows(s.db, id.DID.String()) if err != nil { - log.Printf("getting followers for %s: %s", did, err) + log.Printf("getting followers for %s: %s", id.DID, err) } if len(follows) == 0 { @@ -240,6 +233,7 @@ func (s *State) followPage(w http.ResponseWriter, r *http.Request, fetchFollows return nil } + loggedInUser := pageWithProfile.LoggedInUser var loggedInUserFollowing map[string]struct{} if loggedInUser != nil { following, err := db.GetFollowing(s.db, loggedInUser.Did) @@ -284,27 +278,10 @@ func (s *State) followPage(w http.ResponseWriter, r *http.Request, fetchFollows }) } - followStatus := db.IsNotFollowing - if loggedInUser != nil { - followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, did) - } - - followersCount, followingCount, err := db.GetFollowerFollowingCount(s.db, did) - if err != nil { - log.Printf("getting follow stats followers for %s: %s", did, err) - } - return &FollowsPageParams{ LoggedInUser: loggedInUser, Follows: followCards, - Card: pages.ProfileCard{ - UserDid: did, - UserHandle: ident.Handle.String(), - Profile: profile, - FollowStatus: followStatus, - FollowersCount: followersCount, - FollowingCount: followingCount, - }, + Card: pageWithProfile.Card, } } -- 2.43.0