/
threads.go
259 lines (216 loc) · 7.69 KB
/
threads.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package endpoints
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/ericvolp12/bsky-experiments/pkg/layout"
"github.com/ericvolp12/bsky-experiments/pkg/search"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel/attribute"
)
type ThreadViewCacheEntry struct {
ThreadView []search.PostView
Expiration time.Time
}
type LayoutCacheEntry struct {
Layout []layout.ThreadViewLayout
Expiration time.Time
}
func (api *API) LayoutThread(ctx context.Context, rootPostID string, threadView []search.PostView) ([]layout.ThreadViewLayout, error) {
ctx, span := tracer.Start(ctx, "LayoutThread")
defer span.End()
// Check for the layout in the ARC Cache
entry, ok := api.LayoutCache.Get(rootPostID)
if ok && entry.Expiration.After(time.Now()) {
cacheHits.WithLabelValues("layout").Inc()
span.SetAttributes(attribute.Bool("caches.layouts.hit", true))
return entry.Layout, nil
}
cacheMisses.WithLabelValues("layout").Inc()
threadViewLayout, err := layout.SendEdgeListRequestTS(ctx, api.LayoutServiceHost, threadView)
if err != nil {
return nil, fmt.Errorf("error sending edge list request: %w", err)
}
if threadViewLayout == nil {
return nil, errors.New("layout service returned nil")
}
// Update the ARC Cache
api.LayoutCache.Add(rootPostID, LayoutCacheEntry{
Layout: threadViewLayout,
Expiration: time.Now().Add(api.LayoutCacheTTL),
})
return threadViewLayout, nil
}
func (api *API) ProcessThreadRequest(c *gin.Context) {
ctx := c.Request.Context()
ctx, span := tracer.Start(ctx, "processThreadRequest")
defer span.End()
authorID := c.Query("authorID")
authorHandle := c.Query("authorHandle")
postID := c.Query("postID")
span.SetAttributes(
attribute.String("author.id", authorID),
attribute.String("author.handle", authorHandle),
attribute.String("post.id", postID),
)
if authorID == "" && authorHandle == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "authorID or authorHandle must be provided"})
return
}
if postID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "postID must be provided"})
return
}
if authorID == "" {
authors, err := api.PostRegistry.GetAuthorsByHandle(ctx, authorHandle)
if err != nil {
log.Printf("Error getting authors: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if len(authors) == 0 {
log.Printf("Author with handle '%s' not found", authorHandle)
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Author with handle '%s' not found", authorHandle)})
return
}
authorID = authors[0].DID
span.SetAttributes(attribute.String("author.resolved_id", authorID))
}
// Get highest level post in thread
rootPost, err := api.getRootOrOldestParent(ctx, postID)
if err != nil {
if errors.As(err, &search.NotFoundError{}) {
log.Printf("Post with postID '%s' not found", postID)
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Post with postID '%s' not found", postID)})
} else {
log.Printf("Error getting root post: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
return
}
if rootPost == nil {
log.Printf("Post with postID '%s' not found", postID)
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Post with postID '%s' not found", postID)})
return
}
// Set the rootPostID in the context for the RequestLogger middleware
c.Set("rootPostID", rootPost.ID)
c.Set("rootPostAuthorDID", rootPost.AuthorDID)
// Get thread view
threadView, err := api.GetThreadView(ctx, rootPost.ID, rootPost.AuthorDID)
if err != nil {
if errors.As(err, &search.NotFoundError{}) {
log.Printf("Thread with authorID '%s' and postID '%s' not found", authorID, postID)
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Thread with authorID '%s' and postID '%s' not found", authorID, postID)})
} else {
log.Printf("Error getting thread view: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
return
}
if c.Query("layout") == "true" {
threadViewLayout, err := api.LayoutThread(ctx, rootPost.ID, threadView)
if err != nil {
log.Printf("Error laying out thread: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, threadViewLayout)
return
}
c.JSON(http.StatusOK, threadView)
}
func (api *API) GetThreadView(ctx context.Context, postID, authorID string) ([]search.PostView, error) {
ctx, span := tracer.Start(ctx, "GetThreadView")
defer span.End()
// Check for the thread in the ARC Cache
entry, ok := api.ThreadViewCache.Get(postID)
if ok && entry.Expiration.After(time.Now()) {
cacheHits.WithLabelValues("thread").Inc()
span.SetAttributes(attribute.Bool("caches.threads.hit", true))
return entry.ThreadView, nil
}
cacheMisses.WithLabelValues("thread").Inc()
threadView, err := api.PostRegistry.GetThreadView(ctx, postID, authorID)
if err != nil {
if errors.As(err, &search.NotFoundError{}) {
return nil, fmt.Errorf("thread with authorID '%s' and postID '%s' not found: %w", authorID, postID, err)
}
return nil, err
}
// Update the ARC Cache
api.ThreadViewCache.Add(postID, ThreadViewCacheEntry{
ThreadView: threadView,
Expiration: time.Now().Add(api.ThreadViewCacheTTL),
})
return threadView, nil
}
func (api *API) getRootOrOldestParent(ctx context.Context, postID string) (*search.Post, error) {
ctx, span := tracer.Start(ctx, "getRootOrOldestParent")
defer span.End()
// Get post from registry to look for root post
span.AddEvent("getRootOrOldestParent:ResolvePrimaryPost")
post, err := api.PostRegistry.GetPost(ctx, postID)
if err != nil {
if errors.As(err, &search.NotFoundError{}) {
span.SetAttributes(attribute.Bool("post.primary.found", false))
return nil, fmt.Errorf("post with postID '%s' not found: %w", postID, err)
}
return nil, err
}
span.SetAttributes(attribute.Bool("post.primary.found", true))
var rootPost *search.Post
// If post has a root post and we've stored it, grab it from the registry
if post.RootPostID != nil {
span.AddEvent("getRootOrOldestParent:ResolveRootPost")
rootPost, err = api.PostRegistry.GetPost(ctx, *post.RootPostID)
if err != nil {
// If we don't have the root post, continue to just return the oldest parent
if !errors.As(err, &search.NotFoundError{}) {
return nil, err
}
span.SetAttributes(attribute.Bool("post.root.found", false))
}
if rootPost != nil {
span.SetAttributes(attribute.Bool("post.root.found", true))
}
}
var oldestParent *search.Post
// Get the oldest parent from the registry
span.AddEvent("getRootOrOldestParent:ResolveOldestParent")
oldestParent, err = api.PostRegistry.GetOldestPresentParent(ctx, postID)
if err != nil {
if !errors.As(err, &search.NotFoundError{}) {
return nil, err
}
span.SetAttributes(attribute.Bool("post.oldest_parent.found", false))
}
if oldestParent != nil {
span.SetAttributes(attribute.Bool("post.oldest_parent.found", true))
}
// If we have both a root post and an oldest parent, check if they're the same
if rootPost != nil && oldestParent != nil {
log.Printf("Comparing root post '%s' and oldest parent '%s'", rootPost.ID, oldestParent.ID)
if rootPost.ID == oldestParent.ID {
// If they're the same, return the root post
return rootPost, nil
}
// If they're different, return the oldest parent
// This should make it possible to visualize the oldest parent in the thread view when
// it detaches from the root post due to a missing link in the graph
return oldestParent, nil
}
// If we only have a root post, return it
if rootPost != nil {
return rootPost, nil
}
// If we only have an oldest parent, return it
if oldestParent != nil {
return oldestParent, nil
}
// If we have neither, return the post
return post, nil
}