Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Commit 8dd2cef

Browse files
committed
Process +reposts and +replies from configs; resolves #7
1 parent 97150b4 commit 8dd2cef

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

cloudflare-worker/bsky-feedgen.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function fromPost(response) {
4848
return docs;
4949
}
5050

51-
function fromUser(queryIdx, response, params) {
51+
function fromUser(query, queryIdx, response, params) {
5252
let docs = [];
5353
let feed = response.feed;
5454
if (Array.isArray(feed)) {
@@ -57,12 +57,10 @@ function fromUser(queryIdx, response, params) {
5757
for (let itemIdx = 0; itemIdx < feed.length; itemIdx++) {
5858
let feedItem = feed[itemIdx];
5959
if (feedItem.post !== undefined && feedItem.post.record !== undefined) {
60-
// TODO allow replies
61-
if (feedItem.reply !== undefined) {
60+
if (feedItem.reply !== undefined && query.includeReplies !== true) {
6261
continue;
6362
}
64-
// TODO allow reposts
65-
if (feedItem.reason !== undefined) {
63+
if (feedItem.reason !== undefined && query.includeReposts !== true) {
6664
continue;
6765
}
6866
filteredFeed.push(feedItem);
@@ -74,10 +72,21 @@ function fromUser(queryIdx, response, params) {
7472
let nextCursor = response.cursor;
7573
for (let itemIdx = 0; itemIdx < feed.length; itemIdx++) {
7674
let feedItem = feed[itemIdx];
75+
let postReason = null;
7776
if (feedItem.post !== undefined && feedItem.post.record !== undefined) {
78-
let timestampStr = feedItem.post.record.createdAt;
79-
let timestamp = new Date(timestampStr).valueOf() * 1000000;
8077
let atURL = feedItem.post.uri;
78+
let timestamp = null;
79+
if (feedItem.reason !== undefined) {
80+
let timestampStr = feedItem.reason.indexedAt;
81+
timestamp = new Date(timestampStr).valueOf() * 1000000;
82+
postReason = {
83+
$type: "app.bsky.feed.defs#skeletonReasonRepost",
84+
// TODO: add repost URI field
85+
};
86+
} else {
87+
let timestampStr = feedItem.post.record.createdAt;
88+
timestamp = new Date(timestampStr).valueOf() * 1000000;
89+
}
8190

8291
docs.push({
8392
type: "user",
@@ -88,6 +97,7 @@ function fromUser(queryIdx, response, params) {
8897
total: feed.length,
8998
cursor: cursor,
9099
nextCursor: nextCursor,
100+
postReason: postReason,
91101
});
92102
}
93103
}
@@ -285,7 +295,7 @@ export async function getFeedSkeleton(request, env) {
285295
let cursor = objSafeGet(queryCursor, "cursor", null);
286296
let response = await fetchUser(session, query.value, cursor);
287297
if (response !== null) {
288-
items.push(...fromUser(queryIdx, response, { cursor: cursor }));
298+
items.push(...fromUser(query, queryIdx, response, { cursor: cursor }));
289299
}
290300
} else if (query.type === "post") {
291301
if (showPins) {
@@ -307,7 +317,12 @@ export async function getFeedSkeleton(request, env) {
307317

308318
let feed = [];
309319
for (let item of items) {
310-
feed.push({ post: item.atURL });
320+
let postReason = item.postReason;
321+
let feedItem = { post: item.atURL };
322+
if (postReason !== null) {
323+
// TODO add feedItem["reason"]
324+
}
325+
feed.push(feedItem);
311326
}
312327

313328
let cursor = saveCursor(items, numQueries);
@@ -361,11 +376,25 @@ function buildQueries(allTerms, cursorParam = null) {
361376
value: term,
362377
});
363378
} else {
364-
let userDid = term.replace("at://", "");
379+
let userDid = null;
380+
let includeReplies = false;
381+
let includeReposts = false;
382+
let words = term.split(" ");
383+
for (let word of words) {
384+
if (word.indexOf("at://") > -1) {
385+
userDid = word.replace("at://", "");
386+
} else if (word === "+replies") {
387+
includeReplies = true;
388+
} else if (word === "+reposts") {
389+
includeReposts = true;
390+
}
391+
}
365392
queries.push({
366393
type: "user",
367394
value: userDid,
368395
cursor: cursor,
396+
includeReplies: includeReplies,
397+
includeReposts: includeReposts,
369398
});
370399
}
371400
} else {

render-configs.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
LIST_ITEM_REGEX = re.compile(r"^- ")
1111
POST_REGEX = re.compile(r"^.*[\./]bsky\.app/profile/(.+?)/post/([a-z0-9]+)")
12-
PROFILE_REGEX = re.compile(r"^.*[\./]bsky\.app/profile/([^/]+)")
12+
PROFILE_REGEX = re.compile(r"^.*[\./]bsky\.app/profile/([^/ ]+)( .+)?$")
1313

1414

1515
def resolve_handles(handles):
@@ -30,16 +30,36 @@ def render_search_terms(search_terms):
3030
# strip out list item markers
3131
terms = [re.compile(LIST_ITEM_REGEX).sub("", term) for term in search_terms]
3232

33+
all_handles = {}
34+
3335
# collect handles and pins
3436
for term in terms:
3537
post_matches = POST_REGEX.match(term)
3638
profile_matches = PROFILE_REGEX.match(term)
3739
if post_matches:
38-
handle = post_matches.group(1)
40+
handle = post_matches.group(1).lower()
3941
handles.add(handle)
4042
if profile_matches:
41-
handle = profile_matches.group(1)
43+
handle = profile_matches.group(1).lower()
4244
handles.add(handle)
45+
if handle not in all_handles:
46+
all_handles[handle] = {
47+
"handle": handle,
48+
"replies": False,
49+
"reposts": False,
50+
}
51+
flags = profile_matches.group(2)
52+
if flags is not None:
53+
words = flags.split(" ")
54+
for word in words:
55+
word = word.strip().lower()
56+
if word:
57+
if word == "+replies":
58+
all_handles[handle]["replies"] = True
59+
elif word == "+reposts":
60+
all_handles[handle]["reposts"] = True
61+
else:
62+
print(f"WARN: Unknown flag {word}", file=sys.stderr)
4363

4464
# resolve handles
4565
dids = resolve_handles(handles)
@@ -62,7 +82,14 @@ def render_search_terms(search_terms):
6282
did = dids[handle]
6383
if did:
6484
at_url = f"at://{did}"
65-
rendered_terms.append(at_url)
85+
words = [at_url]
86+
flags = all_handles[handle]
87+
if flags["replies"]:
88+
words.append("+replies")
89+
if flags["reposts"]:
90+
words.append("+reposts")
91+
flat = " ".join(words)
92+
rendered_terms.append(flat)
6693
else:
6794
print(f"WARN: Failed to resolve handle {handle}", file=sys.stderr)
6895
else:

0 commit comments

Comments
 (0)