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 = async () => {
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 const ws = new WebSocket(relay);
14
15 ws.on("error", console.error);
16
17 ws.on("open", () => {
18 intervalID = setInterval(() => {
19 const timestamp = new Date().toISOString();
20 console.log(`${timestamp} cursor: ${cursor}`);
21 fs.writeFile("cursor.txt", cursor.toString(), (err) => {
22 if (err) console.error(err);
23 });
24 }, 60000);
25 });
26
27 ws.on("close", function close() {
28 clearInterval(intervalID);
29 });
30
31 ws.on("message", async (data) => {
32 const event: EventStream = JSON.parse(data.toString());
33 cursor = event.time_us;
34 if (event.type.includes("com")) {
35 if (event.commit.record?.subject.uri.includes(DID)) {
36 await label(
37 event.did,
38 event.commit.record.subject.uri.split("/").pop()!,
39 );
40 }
41 }
42 });
43};
44
45subscribe();