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