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
|
package fetchers
import (
"encoding/json"
"fmt"
"llostre/backend/auth"
"llostre/backend/general"
"llostre/backend/notes"
"log/slog"
)
func FetchAppBskyFeedPosts(logger *slog.Logger, did, pdsURL, slingshotURL string, session *auth.OAuthState, cache *Cache) ([]notes.Note, error) {
// get author of the note
author, found := cache.GetProfile(did)
var err error
if !found {
author, err = FetchAppBskyActorProfile(logger, did, pdsURL, slingshotURL, session, cache)
}
if err != nil {
return nil, err
}
// try slingshot first if given
if slingshotURL != "" {
records, err := general.FetchRawRecordCollection(logger, did, "app.bsky.feed.post", slingshotURL, session)
if err != nil {
logger.Error("Fetching from slingshot failed")
} else {
return parseAppBskyFeedPostCollection(logger, slingshotURL, session, records, author, cache)
}
}
records, err := general.FetchRawRecordCollection(logger, did, "app.bsky.feed.post", pdsURL, session)
if err != nil {
return nil, err
}
return parseAppBskyFeedPostCollection(logger, slingshotURL, session, records, author, cache)
}
func GetAppBskyFeedPostByURI(logger *slog.Logger, uri, slingshotURL string, session *auth.OAuthState, cache *Cache) (*notes.Note, error) {
// check cache, return if it was cached without auth or if it was cached with auth and we have auth
logger.Debug("Getting app.bsky.feed.post", "uri", uri)
if cached, found := cache.Get(uri); found {
logger.Debug("Found in the cache", "public", !cached.AuthorNoUnauth)
if !cached.AuthorNoUnauth {
return cached, nil
}
// author requires auth - if we have a session, serve it
// if the token was invalid, trust that it would have been caught by the "refresh token" call in main
if session != nil {
return cached, nil
}
}
return FetchAppBskyFeedPostByURI(logger, uri, slingshotURL, session, cache)
}
func FetchAppBskyFeedPostByURI(logger *slog.Logger, uri, slingshotURL string, session *auth.OAuthState, cache *Cache) (*notes.Note, error) {
repo, _, _, err := general.ParseURI(uri)
// need to resolve DID to PDS
pdsURL, err := FetchPDS(repo)
if err != nil {
return nil, err
}
logger.Debug("fetched origin PDS", "did", repo, "pdsURL", pdsURL)
// get author of the note
author, found := cache.GetProfile(repo)
if !found {
author, err = FetchAppBskyActorProfile(logger, repo, pdsURL, slingshotURL, session, cache)
}
if err != nil {
return nil, err
}
// try slingshot first if given
if slingshotURL != "" {
rawRecord, err := general.FetchRawRecordByURI(logger, uri, slingshotURL, session)
if err != nil {
logger.Error("fetching from slingshot failed", "error", err)
} else {
return parseAppBskyFeedPostRecord(rawRecord, author, cache)
}
}
rawRecord, err := general.FetchRawRecordByURI(logger, uri, pdsURL, session)
if err != nil {
logger.Error("Fetching from PDS failed")
return nil, err
}
return parseAppBskyFeedPostRecord(rawRecord, author, cache)
}
func parseAppBskyFeedPostRecord(rawRecord *general.RawRecord, author *notes.Author, cache *Cache) (*notes.Note, error) {
if rawRecord == nil {
return nil, fmt.Errorf("unable to parse nil record")
}
record := notes.AppBskyFeedPostRecord{
URI: rawRecord.URI,
}
err := json.Unmarshal(rawRecord.Value, &record.Value)
if err != nil {
return nil, err
}
note, err := notes.NoteFromAppBskyFeedPostRecord(record, author)
if err != nil {
return nil, err
}
// cache it
cache.Set(note.URI, note)
return note, nil
}
func parseAppBskyFeedPostCollection(logger *slog.Logger, slingshotURL string, session *auth.OAuthState, records []general.RawRecord, author *notes.Author, cache *Cache) ([]notes.Note, error) {
posts := make([]notes.Note, 0, len(records))
for _, record := range records {
note, err := parseAppBskyFeedPostRecord(&record, author, cache)
if err != nil {
logger.Error("failed to parse post record", "error", err)
continue
}
if note.ReplyTo != nil {
parents, err := FetchParents(logger, *note.ReplyTo, slingshotURL, session, cache, 10)
if err != nil {
logger.Warn("failed to fetch parent chain", "uri", note.URI, "error", err)
}
note.Parents = parents
}
cache.Set(note.URI, note)
posts = append(posts, *note)
}
return notes.DeduplicateThreads(posts), nil
}
func FetchParents(logger *slog.Logger, replyToURI string, slingshotURL string, session *auth.OAuthState, cache *Cache, maxDepth int) ([]notes.Note, error) {
parents := []notes.Note{}
currentURI := replyToURI
for currentURI != "" {
note, err := GetAppBskyFeedPostByURI(logger, currentURI, slingshotURL, session, cache)
if err != nil {
logger.Warn("failed to fetch parent", "uri", currentURI, "error", err)
note = notes.MissingNote(currentURI)
cache.Set(currentURI, note)
parents = append(parents, *note)
break
}
// preemptively fetch posts from author to avoid hammering servers if there's a back and forth
if _, found := cache.GetProfile(note.AuthorDID); !found {
logger.Debug("caching posts from thread participant", "did", note.AuthorDID)
authorPDS, err := FetchPDS(note.AuthorDID)
if err != nil {
logger.Warn("failed to fetch parent", "uri", currentURI, "error", err)
break
}
go FetchAppBskyFeedPosts(logger, note.AuthorDID, authorPDS, slingshotURL, session, cache)
}
parents = append(parents, *note)
if note.ReplyTo == nil {
break
}
currentURI = *note.ReplyTo
}
return parents, nil
}
|