Pronouns labels on Bluesky
1import { label } from "./label.js";
2import { DID, RELAY } from "./constants.js";
3import { EventStream } from "./types.js";
4import fs from "node:fs";
5import WebSocket from "ws";
6
7const subscribe = () => {
8 let cursor = 0;
9 let intervalID: NodeJS.Timeout;
10 const cursorFile = fs.readFileSync("cursor.txt", "utf8");
11
12 const relay = cursorFile ? RELAY.concat("&cursor=", cursorFile) : RELAY;
13 if (cursorFile) console.log(`Initiate firehose at cursor ${cursorFile}`);
14 const ws = new WebSocket(relay);
15
16 ws.on("error", console.error);
17
18 ws.on("open", () => {
19 intervalID = setInterval(() => {
20 const timestamp = new Date().toISOString();
21 console.log(`${timestamp} cursor: ${cursor}`);
22 fs.writeFile("cursor.txt", cursor.toString(), (err) => {
23 if (err) console.error(err);
24 });
25 }, 60000);
26 });
27
28 ws.on("close", () => {
29 clearInterval(intervalID);
30 });
31
32 ws.on("message", (data) => {
33 const event: EventStream = JSON.parse(data.toString());
34 cursor = event.time_us;
35 if (
36 event.type.includes("com") &&
37 event.commit?.record?.subject?.uri?.includes(DID)
38 ) {
39 label(event.did, event.commit.record.subject.uri.split("/").pop()!);
40 }
41 });
42};
43
44subscribe();