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

Commit

Permalink
Merge pull request #6 from jcsalterego/pinned-posts
Browse files Browse the repository at this point in the history
Pinned Posts
  • Loading branch information
jcsalterego authored Jun 10, 2023
2 parents 62f9d9f + b8c110f commit b20d8b0
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 11 deletions.
7 changes: 6 additions & 1 deletion CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ Cute animals feed

# searchTerms

> Maximum of five search terms. Test these in [https://bsky.app/search](https://bsky.app/search). `AND` is implicit, so `cat dog` on one line will require both `cat` and `dog`. You can use quotes as well `"hot dog"`.
> Search Terms are now more powerful. There are two types:
>
> - Pinned posts: links such as `https://bsky.app/profile/saddymayo.bsky.social/post/3jxju2wwap22e` will pin at the top of the feed. One link per line, please.
> - Keywords (maximum 5 terms): Test these in [https://bsky.app/search](https://bsky.app/search). `AND` is implicit, so `cat dog` on one line will require both `cat` and `dog`. You can use quotes as well `"hot dog"`.
>
- https://bsky.app/profile/saddymayo.bsky.social/post/3jxju2wwap22e
- cats
- dogs
- penguins
Expand Down
38 changes: 35 additions & 3 deletions cloudflare-worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,34 @@ function jsonResponse(obj) {
return response;
}

function bucketTerms(allTerms, opts={}) {
let maxSearchTerms = opts["maxSearchTerms"] || MAX_SEARCH_TERMS;
let pinnedPosts = [];
let searchTerms = [];

for (let term of allTerms) {
if (term.startsWith("at://")) {
pinnedPosts.push(term);
} else {
searchTerms.push(term);
}
}

return {
pinnedPosts: pinnedPosts,
searchTerms: searchTerms.slice(0, maxSearchTerms),
}
}

async function getFeedSkeleton(request) {
let url = new URL(request.url);
let feedAtUrl = url.searchParams.get("feed");
const url = new URL(request.url);
const feedAtUrl = url.searchParams.get("feed");
if (feedAtUrl === null) {
console.warn(`feed parameter missing from query string`);
return feedJsonResponse([]);
}
const cursorParam = url.searchParams.get("cursor");
const showPins = cursorParam === null;
let words = feedAtUrl.split("/");
let feedId = words[words.length - 1];
let config = CONFIGS[feedId];
Expand All @@ -56,7 +77,14 @@ async function getFeedSkeleton(request) {
limit = DEFAULT_LIMIT;
}

let searchTerms = config.searchTerms.slice(0, MAX_SEARCH_TERMS);
let allTerms = bucketTerms(config.searchTerms, {
maxSearchTerms: MAX_SEARCH_TERMS,
});
let searchTerms = allTerms.searchTerms;
let pinnedPosts = allTerms.pinnedPosts;
if (!showPins) {
pinnedPosts = [];
}
let responsePromises = [];

for (let searchTerm of searchTerms) {
Expand Down Expand Up @@ -88,6 +116,9 @@ async function getFeedSkeleton(request) {
a === b ? 0 : a < b ? -1 : 1
);
var feed = [];
for (let pinnedPost of pinnedPosts) {
feed.push({ post: pinnedPost });
}
for (let timestampUrl of timestampURLs) {
let atUrl = timestampUrl[1];
feed.push({ post: atUrl });
Expand Down Expand Up @@ -124,6 +155,7 @@ const CONFIGS = {
"description": "Cute animals feed",
"searchTerms": [
"cats",
"at://did:plc:ozppa2bsq6bdnajyweoir2i2/app.bsky.feed.post/3jxju2wwap22e",
"dogs",
"penguins",
"red pandas",
Expand Down
5 changes: 4 additions & 1 deletion configs/BASKETBALL_EMOJIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ Posts with 🏀

# searchTerms

> Maximum of five search terms. Test these in [https://bsky.app/search](https://bsky.app/search). `AND` is implicit, so `cat dog` on one line will require both `cat` and `dog`. You can use quotes as well `"hot dog"`.
> Search Terms are now more powerful. There are two types:
>
> - Pinned posts: links such as `https://bsky.app/profile/saddymayo.bsky.social/post/3jxju2wwap22e` will pin at the top of the feed. One link per line, please.
> - Keywords (maximum 5 terms): Test these in [https://bsky.app/search](https://bsky.app/search). `AND` is implicit, so `cat dog` on one line will require both `cat` and `dog`. You can use quotes as well `"hot dog"`.
- 🏀

Expand Down
5 changes: 4 additions & 1 deletion configs/GAMING_EMOJIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ Posts with 👾🎮🕹️

# searchTerms

> Maximum of five search terms. Test these in [https://bsky.app/search](https://bsky.app/search). `AND` is implicit, so `cat dog` on one line will require both `cat` and `dog`. You can use quotes as well `"hot dog"`.
> Search Terms are now more powerful. There are two types:
>
> - Pinned posts: links such as `https://bsky.app/profile/saddymayo.bsky.social/post/3jxju2wwap22e` will pin at the top of the feed. One link per line, please.
> - Keywords (maximum 5 terms): Test these in [https://bsky.app/search](https://bsky.app/search). `AND` is implicit, so `cat dog` on one line will require both `cat` and `dog`. You can use quotes as well `"hot dog"`.
- 👾
- 🎮
Expand Down
5 changes: 4 additions & 1 deletion configs/SCIENCE_EMOJIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ Posts with 🧪🥼🔭

# searchTerms

> Maximum of five search terms. Test these in [https://bsky.app/search](https://bsky.app/search). `AND` is implicit, so `cat dog` on one line will require both `cat` and `dog`. You can use quotes as well `"hot dog"`.
> Search Terms are now more powerful. There are two types:
>
> - Pinned posts: links such as `https://bsky.app/profile/saddymayo.bsky.social/post/3jxju2wwap22e` will pin at the top of the feed. One link per line, please.
> - Keywords (maximum 5 terms): Test these in [https://bsky.app/search](https://bsky.app/search). `AND` is implicit, so `cat dog` on one line will require both `cat` and `dog`. You can use quotes as well `"hot dog"`.
- 🧪
- 🥼
Expand Down
1 change: 1 addition & 0 deletions feed-generator/configs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "Cute animals feed",
"searchTerms": [
"cats",
"at://did:plc:ozppa2bsq6bdnajyweoir2i2/app.bsky.feed.post/3jxju2wwap22e",
"dogs",
"penguins",
"red pandas",
Expand Down
58 changes: 54 additions & 4 deletions render-configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,59 @@
import json
import os
import re
import sys

import requests

WORKER_SENTINEL = "\n\n// CONFIGS\n\n"
LIST_ITEM_REGEX = re.compile(r"^- ")
POST_REGEX = re.compile(r"^.*[\./]bsky\.app/profile/(.+?)/post/([a-z0-9]+)")


def resolve_handles(handles):
dids = {}
for handle in handles:
url = f"https://bsky.social/xrpc/com.atproto.identity.resolveHandle?handle={handle}"
response = requests.get(url)
if response.status_code == 200:
did = response.json()["did"]
dids[handle] = did
return dids


def render_search_terms(search_terms):
handles = set()
rendered_terms = []

# strip out list item markers
terms = [re.compile(LIST_ITEM_REGEX).sub("", term) for term in search_terms]

# collect handles and pins
for term in terms:
post_matches = POST_REGEX.match(term)
if post_matches:
handle = post_matches.group(1)
handles.add(handle)

# resolve handles
dids = resolve_handles(handles)

# replace handles with DIDs
for term in terms:
post_matches = POST_REGEX.match(term)
if post_matches:
handle = post_matches.group(1)
rkey = post_matches.group(2)
did = dids[handle]
if did:
at_url = f"at://{did}/app.bsky.feed.post/{rkey}"
rendered_terms.append(at_url)
else:
print(f"WARN: Failed to resolve handle {handle}", file=sys.stderr)
else:
rendered_terms.append(term)

return rendered_terms


def parse_config(dirname, markdown_contents):
Expand All @@ -28,9 +79,7 @@ def parse_config(dirname, markdown_contents):
for key in flat_keys:
config[key] = " ".join(config[key])
if "searchTerms" in config:
config["searchTerms"] = [
re.compile(r"^- ").sub("", term) for term in config["searchTerms"]
]
config["searchTerms"] = render_search_terms(config["searchTerms"])
if "avatar" in config:
matches = re.compile("^.*\((.+)\)$").match(config["avatar"])
if matches:
Expand All @@ -46,7 +95,7 @@ def parse_config(dirname, markdown_contents):
config["displayName"] = display_name

if "isEnabled" in config:
config["isEnabled"] = (config["isEnabled"].lower() == "true")
config["isEnabled"] = config["isEnabled"].lower() == "true"
else:
# for legacy support, if the section is missing, set to True
config["isEnabled"] = True
Expand Down Expand Up @@ -91,4 +140,5 @@ def main():
save_json_configs("feed-generator/configs.json", configs)
replace_json_configs("cloudflare-worker/worker.js", configs)


main()

0 comments on commit b20d8b0

Please sign in to comment.