Pronouns labels on Bluesky
1import { AppBskyFeedLike } from "@atproto/api"; 2import { Firehose } from "@skyware/firehose"; 3import { getAgent } from "./agent.js"; 4import { label } from "./label.js"; 5import { DID } from "./constants.js"; 6import fs from "node:fs"; 7 8const subscribe = async () => { 9 const agent = await getAgent(); 10 11 let cursorFirehose = 0; 12 const cursorFile = fs.readFileSync("cursor.txt", "utf8"); 13 14 const firehose = new Firehose({ cursor: cursorFile ?? "" }); 15 if (cursorFile) console.log(`Initiate firehose at cursor ${cursorFile}`); 16 17 firehose.on("error", ({ cursor, error }) => { 18 console.log(`Firehose errored on cursor: ${cursor}`, error); 19 }); 20 21 firehose.on("open", () => { 22 setInterval(() => { 23 const timestamp = new Date().toISOString(); 24 console.log(`${timestamp} cursor: ${cursorFirehose}`); 25 fs.writeFile("cursor.txt", cursorFirehose.toString(), (err) => { 26 if (err) console.error(err); 27 }); 28 }, 60000); 29 }); 30 31 firehose.on("commit", async (commit) => { 32 cursorFirehose = commit.seq; 33 commit.ops.forEach(async (op) => { 34 if (op.action !== "delete" && AppBskyFeedLike.isRecord(op.record)) { 35 if ((op.record.subject.uri ?? "").includes(DID)) { 36 if ((op.record.subject.uri ?? "").includes("app.bsky.feed.post")) { 37 await label(agent, commit.repo, op.record.subject.uri).catch( 38 (err) => console.error(err), 39 ); 40 } 41 } 42 } 43 }); 44 }); 45 46 firehose.start(); 47}; 48 49subscribe();