From 2695d6d050c41ba1026089281cfe5b7a8173eae5 Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Wed, 3 Sep 2025 14:47:55 +0300 Subject: [PATCH] appview/{pages,db}: show follow/unfollow button on the timeline Change-Id: voxxrzkkwxzvuxtzmvssvpqnwpvoxkxl Signed-off-by: Anirudh Oppiliappan --- appview/db/timeline.go | 25 +++++--- .../timeline/fragments/timeline.html | 57 +++++++++++-------- .../templates/user/fragments/follow.html | 4 +- appview/state/state.go | 8 ++- 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/appview/db/timeline.go b/appview/db/timeline.go index a5907722..d52cc313 100644 --- a/appview/db/timeline.go +++ b/appview/db/timeline.go @@ -18,11 +18,16 @@ type TimelineEvent struct { // optional: populate only if event is Follow *Profile *FollowStats + *FollowStatus + + // optional: populate only if event is Repo + IsStarred bool + StarCount int64 } // TODO: this gathers heterogenous events from different sources and aggregates // them in code; if we did this entirely in sql, we could order and limit and paginate easily -func MakeTimeline(e Execer, limit int) ([]TimelineEvent, error) { +func MakeTimeline(e Execer, limit int, loggedInUserDid string) ([]TimelineEvent, error) { var events []TimelineEvent repos, err := getTimelineRepos(e, limit) @@ -35,7 +40,7 @@ func MakeTimeline(e Execer, limit int) ([]TimelineEvent, error) { return nil, err } - follows, err := getTimelineFollows(e, limit) + follows, err := getTimelineFollows(e, limit, loggedInUserDid) if err != nil { return nil, err } @@ -129,7 +134,7 @@ func getTimelineStars(e Execer, limit int) ([]TimelineEvent, error) { return events, nil } -func getTimelineFollows(e Execer, limit int) ([]TimelineEvent, error) { +func getTimelineFollows(e Execer, limit int, loggedInUserDid string) ([]TimelineEvent, error) { follows, err := GetFollows(e, limit) if err != nil { return nil, err @@ -159,11 +164,17 @@ func getTimelineFollows(e Execer, limit int) ([]TimelineEvent, error) { profile, _ := profiles[f.SubjectDid] followStatMap, _ := followStatMap[f.SubjectDid] + followStatus := IsNotFollowing + if followStatuses != nil { + followStatus = followStatuses[f.SubjectDid] + } + events = append(events, TimelineEvent{ - Follow: &f, - Profile: profile, - FollowStats: &followStatMap, - EventAt: f.FollowedAt, + Follow: &f, + Profile: profile, + FollowStats: &followStatMap, + FollowStatus: &followStatus, + EventAt: f.FollowedAt, }) } diff --git a/appview/pages/templates/timeline/fragments/timeline.html b/appview/pages/templates/timeline/fragments/timeline.html index 5c630dab..5b46264a 100644 --- a/appview/pages/templates/timeline/fragments/timeline.html +++ b/appview/pages/templates/timeline/fragments/timeline.html @@ -17,7 +17,7 @@ {{ else if .Star }} {{ template "timeline/fragments/starEvent" (list $ .Star) }} {{ else if .Follow }} - {{ template "timeline/fragments/followEvent" (list $ .Follow .Profile .FollowStats) }} + {{ template "timeline/fragments/followEvent" (list $ .) }} {{ end }} {{ end }} @@ -77,9 +77,10 @@ {{ define "timeline/fragments/followEvent" }} {{ $root := index . 0 }} - {{ $follow := index . 1 }} - {{ $profile := index . 2 }} - {{ $stat := index . 3 }} + {{ $event := index . 1 }} + {{ $follow := $event.Follow }} + {{ $profile := $event.Profile }} + {{ $stat := $event.FollowStats }} {{ $userHandle := resolve $follow.UserDid }} {{ $subjectHandle := resolve $follow.SubjectDid }} @@ -89,28 +90,36 @@ {{ template "user/fragments/picHandleLink" $subjectHandle }} {{ template "repo/fragments/time" $follow.FollowedAt }} -
-
- -
+
+
+
+ +
-
- - {{ $subjectHandle | truncateAt30 }} - - {{ with $profile }} - {{ with .Description }} -

{{.}}

+
+ + {{ $subjectHandle | truncateAt30 }} + + {{ with $profile }} + {{ with .Description }} +

{{.}}

+ {{ end }} {{ end }} - {{ end }} - {{ with $stat }} -
- {{ i "users" "size-4" }} - {{ .Followers }} followers - - {{ .Following }} following -
- {{ end }} + {{ with $stat }} +
+ {{ i "users" "size-4" }} + {{ .Followers }} followers + + {{ .Following }} following +
+ {{ end }} +
+ + {{ if and $root.LoggedInUser (ne $event.FollowStatus.String "IsSelf") }} +
+ {{ template "user/fragments/follow" (dict "UserDid" $follow.SubjectDid "FollowStatus" $event.FollowStatus) }} +
+ {{ end }}
{{ end }} diff --git a/appview/pages/templates/user/fragments/follow.html b/appview/pages/templates/user/fragments/follow.html index 23fd3cd9..6cb96740 100644 --- a/appview/pages/templates/user/fragments/follow.html +++ b/appview/pages/templates/user/fragments/follow.html @@ -1,6 +1,6 @@ {{ define "user/fragments/follow" }} {{ end }} diff --git a/appview/state/state.go b/appview/state/state.go index 932a129f..307a7315 100644 --- a/appview/state/state.go +++ b/appview/state/state.go @@ -211,7 +211,11 @@ func (s *State) HomeOrTimeline(w http.ResponseWriter, r *http.Request) { func (s *State) Timeline(w http.ResponseWriter, r *http.Request) { user := s.oauth.GetUser(r) - timeline, err := db.MakeTimeline(s.db, 50) + var userDid string + if user != nil { + userDid = user.Did + } + timeline, err := db.MakeTimeline(s.db, 50, userDid) if err != nil { log.Println(err) s.pages.Notice(w, "timeline", "Uh oh! Failed to load timeline.") @@ -266,7 +270,7 @@ func (s *State) UpgradeBanner(w http.ResponseWriter, r *http.Request) { } func (s *State) Home(w http.ResponseWriter, r *http.Request) { - timeline, err := db.MakeTimeline(s.db, 5) + timeline, err := db.MakeTimeline(s.db, 5, "") if err != nil { log.Println(err) s.pages.Notice(w, "timeline", "Uh oh! Failed to load timeline.") -- 2.43.0