This repository has no description
1package timeline
2
3import (
4 "net/http"
5 "sort"
6
7 "github.com/bluesky-social/indigo/atproto/syntax"
8 "tangled.org/core/appview/db"
9 "tangled.org/core/appview/models"
10 "tangled.org/core/appview/oauth"
11 "tangled.org/core/appview/pages"
12 "tangled.org/core/appview/pagination"
13 "tangled.org/core/orm"
14)
15
16func (t *Timeline) Timeline(w http.ResponseWriter, r *http.Request) {
17 user := t.oauth.GetMultiAccountUser(r)
18
19 const timelineTabCookie = "timeline-tab"
20 var followingOnly bool
21 if _, hasParam := r.URL.Query()["following"]; hasParam {
22 followingOnly = r.URL.Query().Get("following") == "true" && user != nil
23 } else if user != nil {
24 if c, err := r.Cookie(timelineTabCookie); err == nil {
25 followingOnly = c.Value == "following"
26 }
27 }
28
29 if user != nil {
30 val := "global"
31 if followingOnly {
32 val = "following"
33 }
34 http.SetCookie(w, &http.Cookie{
35 Name: timelineTabCookie,
36 Value: val,
37 Path: "/",
38 MaxAge: 365 * 24 * 60 * 60,
39 HttpOnly: true,
40 SameSite: http.SameSiteLaxMode,
41 })
42 }
43
44 var userDid string
45 if user != nil {
46 userDid = user.Did
47 }
48 timeline, err := db.MakeTimeline(t.db, 50, userDid, followingOnly)
49 if err != nil {
50 t.logger.Error("failed to make timeline", "err", err)
51 t.pages.Notice(w, "timeline", "Uh oh! Failed to load timeline.")
52 }
53
54 repos, err := db.GetTopStarredReposLastWeek(t.db)
55 if err != nil {
56 t.logger.Error("failed to get top starred repos", "err", err)
57 t.pages.Notice(w, "topstarredrepos", "Unable to load.")
58 return
59 }
60
61 gfiLabel, err := db.GetLabelDefinition(t.db, orm.FilterEq("at_uri", t.config.Label.GoodFirstIssue))
62 if err != nil {
63 // non-fatal
64 }
65
66 var notifications []*models.NotificationWithEntity
67 if user != nil {
68 notifications, err = db.GetNotificationsWithEntities(
69 t.db,
70 pagination.Page{Limit: 5, Offset: 0},
71 orm.FilterEq("recipient_did", user.Did),
72 )
73 if err != nil {
74 t.logger.Error("failed to get notifications for timeline", "err", err)
75 }
76 }
77
78 var vouchSuggestions []models.VouchSuggestion
79 if user != nil {
80 vouchSuggestions, err = db.GetVouchSuggestions(t.db, user.Did, 3)
81 if err != nil {
82 t.logger.Error("failed to get vouch suggestions", "err", err)
83 }
84 if len(vouchSuggestions) > 0 {
85 suggestionDids := make([]syntax.DID, len(vouchSuggestions))
86 for i, sv := range vouchSuggestions {
87 suggestionDids[i] = syntax.DID(sv.Did)
88 }
89 relationships, err := db.GetVouchRelationshipsBatch(t.db, syntax.DID(user.Did), suggestionDids)
90 if err != nil {
91 t.logger.Error("failed to get vouch relationships for suggestions", "err", err)
92 } else {
93 for i := range vouchSuggestions {
94 vouchSuggestions[i].VouchRelationship = relationships[vouchSuggestions[i].Did]
95 }
96 }
97 }
98 }
99
100 var recents []pages.RecentItem
101 if user != nil {
102 recents, err = t.buildRecents(user.Did)
103 if err != nil {
104 t.logger.Error("failed to build recents for timeline", "err", err)
105 }
106 }
107
108 var canFocus bool
109 if user != nil {
110 focusCount, _ := db.CountFocusNotifs(t.db, user.Did)
111 canFocus = focusCount > 1
112 }
113
114 err = t.pages.Timeline(w, pages.TimelineParams{
115 BaseParams: pages.BaseParamsFromContext(r.Context()),
116 Timeline: timeline,
117 Repos: repos,
118 GfiLabel: gfiLabel,
119 VouchSuggestions: vouchSuggestions,
120 Notifications: notifications,
121 Recents: recents,
122 FollowingOnly: followingOnly,
123 RecentBlogPosts: t.recentPosts,
124 ShowNewsletter: t.showNewsletter(user),
125 CanFocus: canFocus,
126 })
127 if err != nil {
128 t.logger.Error("failed to render timeline", "err", err)
129 }
130}
131
132func (t *Timeline) buildRecents(userDid string) ([]pages.RecentItem, error) {
133 links, err := db.GetRecentLinks(t.db, orm.FilterEq("user_did", userDid))
134 if err != nil {
135 return nil, err
136 }
137 if len(links) == 0 {
138 return nil, nil
139 }
140
141 // group targets by type.
142 var repoDids, issueAtUris, pullAtUris []string
143 for _, l := range links {
144 switch l.LinkType {
145 case models.RecentLinkTypeRepo:
146 repoDids = append(repoDids, l.Target)
147 case models.RecentLinkTypeIssue:
148 issueAtUris = append(issueAtUris, l.Target)
149 case models.RecentLinkTypePull:
150 pullAtUris = append(pullAtUris, l.Target)
151 }
152 }
153
154 // fetch repos by DID.
155 repoByDid := make(map[string]*models.Repo)
156 if len(repoDids) > 0 {
157 fetched, err := db.GetRepos(t.db, orm.FilterIn("repo_did", repoDids))
158 if err != nil {
159 return nil, err
160 }
161 for i := range fetched {
162 repoByDid[fetched[i].RepoDid] = &fetched[i]
163 }
164 }
165
166 // fetch issues by aturi
167 issueByAtUri := make(map[string]*models.Issue)
168 if len(issueAtUris) > 0 {
169 issues, err := db.GetIssues(t.db, orm.FilterIn("at_uri", issueAtUris))
170 if err != nil {
171 return nil, err
172 }
173 for _, issue := range issues {
174 issueByAtUri[issue.AtUri().String()] = &issue
175 }
176 }
177
178 // fetch pulls by aturi
179 pullByAtUri := make(map[string]*models.Pull)
180 if len(pullAtUris) > 0 {
181 fetched, err := db.GetPulls(t.db, orm.FilterIn("at_uri", pullAtUris))
182 if err != nil {
183 return nil, err
184 }
185 for _, p := range fetched {
186 pullByAtUri[p.AtUri().String()] = p
187 }
188 }
189
190 // build result in original link order
191 var items []pages.RecentItem
192 for _, l := range links {
193 item := pages.RecentItem{Link: l}
194 switch l.LinkType {
195 case models.RecentLinkTypeRepo:
196 item.Repo = repoByDid[l.Target]
197 case models.RecentLinkTypeIssue:
198 item.Issue = issueByAtUri[l.Target]
199 case models.RecentLinkTypePull:
200 item.Pull = pullByAtUri[l.Target]
201 }
202 // skip if the entity could not be resolved (e.g. deleted).
203 if item.Repo == nil && item.Issue == nil && item.Pull == nil {
204 continue
205 }
206 items = append(items, item)
207 }
208
209 // re-sort by visited descending to restore recency order after map lookups.
210 sort.Slice(items, func(i, j int) bool {
211 return items[i].Link.Visited.After(items[j].Link.Visited)
212 })
213
214 return items, nil
215}
216
217// showNewsletter decides whether the newsletter widget/CTA should render.
218// Anonymous visitors always see it (they can dismiss via localStorage);
219// logged-in users whose newsletter_preferences row exists (either
220// subscribed or dismissed) do not.
221func (t *Timeline) showNewsletter(user *oauth.MultiAccountUser) bool {
222 if user == nil {
223 return true
224 }
225 pref, err := db.GetNewsletterPref(t.db, user.Did)
226 if err != nil {
227 t.logger.Error("failed to read newsletter preference", "did", user.Did, "err", err)
228 return true
229 }
230 return pref == nil
231}