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 let 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 fs.writeFile("cursor.txt", cursorFirehose.toString(), (err) => {
24 if (err) console.error(err);
25 });
26 }, 60000);
27 });
28
29 firehose.on("commit", async (commit) => {
30 cursorFirehose = commit.seq;
31 commit.ops.forEach(async (op) => {
32 if (op.action !== "delete" && AppBskyFeedLike.isRecord(op.record)) {
33 if ((op.record.subject.uri ?? "").includes(DID)) {
34 if ((op.record.subject.uri ?? "").includes("app.bsky.feed.post")) {
35 await label(agent, commit.repo, op.record.subject.uri).catch(
36 (err) => console.error(err),
37 );
38 }
39 }
40 }
41 });
42 });
43
44 firehose.start();
45};
46
47subscribe();