import { encounteredIDs } from "https://esm.town/v/buttondown/encounteredIDs";
import { email } from "https://esm.town/v/std/email?v=9";
import { set } from "https://esm.town/v/std/set?v=11";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export const runner = async () => {
const ALERT_STRINGS = [
"buttondown",
"buttondown.email",
];
const USERNAME_DENYLIST = [
"buttondown.bsky.social",
];
const promises = ALERT_STRINGS.map(async function(keyword) {
let posts = await fetchJSON(
`https://search.bsky.social/search/posts?q=${keyword}`,
);
let newPosts = posts.filter((post) =>
!encounteredIDs.includes(post.tid)
&& !USERNAME_DENYLIST.includes(post.user.handle)
);
newPosts.map((post) => {
encounteredIDs.push(post.tid);
});
await Promise.all(newPosts.map(async (post) => {
const id = post.tid.split("/")[1];
await email({
html: `https://bsky.app/profile/${post.user.did}/post/${id}`,
subject: `New post in Bluesky for ${keyword} (${id})`,
});
}));
});
await Promise.all(promises);
await set("encounteredIDs", encounteredIDs);
};