From 34d8a513e4512bc8b1443478f390319282cb6cbe Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Wed, 25 Feb 2026 11:51:41 +0000 Subject: [PATCH] appview/profile: fix profile card rendering on tabs the .ShowPunchcard variable was being referenced in pages that didn't have the variable set. to circumvent this, the `State.profile` helper calculates whether or not to show the punchcard, and this helper is then substituted in all profile tabs. Signed-off-by: oppiliappan --- appview/db/profile.go | 40 ++++++++--------- .../pages/templates/layouts/profilebase.html | 2 +- appview/state/profile.go | 43 ++++++++++--------- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/appview/db/profile.go b/appview/db/profile.go index 25569af2..fdc5d993 100644 --- a/appview/db/profile.go +++ b/appview/db/profile.go @@ -16,7 +16,7 @@ import ( const TimeframeMonths = 7 -func MakeProfileTimeline(e Execer, forDid string, includePunchcard bool) (*models.ProfileTimeline, error) { +func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, error) { timeline := models.ProfileTimeline{ ByMonth: make([]models.ByMonth, TimeframeMonths), } @@ -98,29 +98,27 @@ func MakeProfileTimeline(e Execer, forDid string, includePunchcard bool) (*model }) } - if includePunchcard { - punchcard, err := MakePunchcard( - e, - orm.FilterEq("did", forDid), - orm.FilterGte("date", time.Now().AddDate(0, -TimeframeMonths, 0)), - ) - if err != nil { - return nil, fmt.Errorf("error getting commits by did: %w", err) + punchcard, err := MakePunchcard( + e, + orm.FilterEq("did", forDid), + orm.FilterGte("date", time.Now().AddDate(0, -TimeframeMonths, 0)), + ) + if err != nil { + return nil, fmt.Errorf("error getting commits by did: %w", err) + } + for _, punch := range punchcard.Punches { + if punch.Date.After(now) { + continue } - for _, punch := range punchcard.Punches { - if punch.Date.After(now) { - continue - } - - monthsAgo := monthsBetween(punch.Date, now) - if monthsAgo >= TimeframeMonths { - // shouldn't happen; but times are weird - continue - } - idx := monthsAgo - timeline.ByMonth[idx].Commits += punch.Count + monthsAgo := monthsBetween(punch.Date, now) + if monthsAgo >= TimeframeMonths { + // shouldn't happen; but times are weird + continue } + + idx := monthsAgo + timeline.ByMonth[idx].Commits += punch.Count } return &timeline, nil diff --git a/appview/pages/templates/layouts/profilebase.html b/appview/pages/templates/layouts/profilebase.html index 349edddd..ce91c3ea 100644 --- a/appview/pages/templates/layouts/profilebase.html +++ b/appview/pages/templates/layouts/profilebase.html @@ -52,7 +52,7 @@
{{ template "user/fragments/profileCard" .Card }} - {{ if .ShowPunchcard }} + {{ if .Card.Punchcard }} {{ block "punchcard" .Card.Punchcard }} {{ end }} {{ end }}
diff --git a/appview/state/profile.go b/appview/state/profile.go index 392b0b04..564ee1f8 100644 --- a/appview/state/profile.go +++ b/appview/state/profile.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "log/slog" "net/http" "slices" "strings" @@ -90,16 +89,21 @@ func (s *State) profile(r *http.Request) (*pages.ProfileCard, error) { followStatus = db.GetFollowStatus(s.db, loggedInUser.Active.Did, did) } - now := time.Now() - startOfYear := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.UTC) - punchcard, err := db.MakePunchcard( - s.db, - orm.FilterEq("did", did), - orm.FilterGte("date", startOfYear.Format(time.DateOnly)), - orm.FilterLte("date", now.Format(time.DateOnly)), - ) - if err != nil { - return nil, fmt.Errorf("failed to get punchcard for %s: %w", did, err) + showPunchcard := s.shouldShowPunchcard(did, loggedInUser.Did()) + + var punchcard *models.Punchcard + if showPunchcard { + now := time.Now() + startOfYear := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.UTC) + punchcard, err = db.MakePunchcard( + s.db, + orm.FilterEq("did", did), + orm.FilterGte("date", startOfYear.Format(time.DateOnly)), + orm.FilterLte("date", now.Format(time.DateOnly)), + ) + if err != nil { + return nil, fmt.Errorf("failed to get punchcard for %s: %w", did, err) + } } return &pages.ProfileCard{ @@ -165,11 +169,7 @@ func (s *State) profileOverview(w http.ResponseWriter, r *http.Request) { } } - loggedInUser := s.oauth.GetMultiAccountUser(r) - - showPunchcard := checkIfPunchcardShouldShow(s.db, l, profile.UserDid, loggedInUser.Did()) - - timeline, err := db.MakeProfileTimeline(s.db, profile.UserDid, showPunchcard) + timeline, err := db.MakeProfileTimeline(s.db, profile.UserDid) if err != nil { l.Error("failed to create timeline", "err", err) } @@ -180,18 +180,19 @@ func (s *State) profileOverview(w http.ResponseWriter, r *http.Request) { Repos: pinnedRepos, CollaboratingRepos: pinnedCollaboratingRepos, ProfileTimeline: timeline, - ShowPunchcard: showPunchcard, }) } -func checkIfPunchcardShouldShow(e db.Execer, l *slog.Logger, targetDid, requesterDid string) bool { - targetPunchcardPreferences, err := db.GetPunchcardPreference(e, targetDid) +func (s *State) shouldShowPunchcard(targetDid, requesterDid string) bool { + l := s.logger.With("helper", "shouldShowPunchcard") + + targetPunchcardPreferences, err := db.GetPunchcardPreference(s.db, targetDid) if err != nil { l.Error("failed to get target users punchcard preferences", "err", err) return true } - requesterPunchcardPreferences, err := db.GetPunchcardPreference(e, requesterDid) + requesterPunchcardPreferences, err := db.GetPunchcardPreference(s.db, requesterDid) if err != nil { l.Error("failed to get requester users punchcard preferences", "err", err) return true @@ -446,7 +447,7 @@ func (s *State) AtomFeedPage(w http.ResponseWriter, r *http.Request) { } func (s *State) getProfileFeed(ctx context.Context, id *identity.Identity) (*feeds.Feed, error) { - timeline, err := db.MakeProfileTimeline(s.db, id.DID.String(), false) + timeline, err := db.MakeProfileTimeline(s.db, id.DID.String()) if err != nil { return nil, err } -- 2.51.2