my monorepo for atproto based applications
1import {
2 OutputSchema as RepoEvent,
3 isCommit,
4} from "@my/lexicon/server/types/com/atproto/sync/subscribeRepos";
5import { Record as PostRecord } from "@my/lexicon/server/types/app/bsky/feed/post";
6import { FirehoseSubscriptionBase, getOpsByType } from "@/util/subscription";
7import { handler as subscriptionHandler } from "@/heb-ppl/subscriber";
8
9export class FirehoseSubscription extends FirehoseSubscriptionBase {
10 async handleEvent(evt: RepoEvent) {
11 if (!isCommit(evt)) return;
12
13 const ops = await getOpsByType(evt);
14
15 await subscriptionHandler(this, ops);
16
17 // // This logs the text of every post off the firehose.
18 // // Just for fun :)
19 // // Delete before actually using
20 // for (const post of ops.posts.creates) {
21 // console.log("===================================");
22 // console.log(post.author);
23 // console.log(post.record.text);
24 // console.log("===================================");
25 // }
26
27 // const postsToDelete = ops.posts.deletes.map((del) => del.uri);
28 // const groupedCreatedPosts = Object.groupBy(
29 // ops.posts.creates.map((create) => ({
30 // create,
31 // ...isHebPost(create.record),
32 // })),
33 // (o) => (o.result ? "heb" : "not"),
34 // );
35 // const postsToCreate = (groupedCreatedPosts.heb ?? []).map(({ create, counts }) => {
36 // return {
37 // uri: create.uri,
38 // cid: create.cid,
39 // author_did: create.author,
40 // indexedAt: new Date().toISOString(),
41 // analysis: JSON.stringify({ counts }),
42 // };
43 // });
44
45 // if (postsToDelete.length > 0) {
46 // await this.db.deleteFrom("post").where("uri", "in", postsToDelete).execute();
47 // }
48 // if (postsToCreate.length > 0) {
49 // await this.db
50 // .insertInto("post")
51 // .values(postsToCreate)
52 // .onConflict((oc) => oc.doNothing())
53 // .execute();
54 // }
55 }
56}
57
58// function isHebPost(post: PostRecord): {
59// result: boolean;
60// counts: {
61// wsOrPunctCount: number;
62// hebCharCount: number;
63// normalSymbolCount: number;
64// biblicalSymbolCount: number;
65// };
66// } {
67// const txt = post.text.normalize("NFC");
68
69// let wsOrPunctCount = 0;
70// let hebCharCount = 0;
71// let normalSymbolCount = 0;
72// let biblicalSymbolCount = 0;
73// for (let i = 0; i < txt.length; i++) {
74// const c = txt.charCodeAt(i);
75// if (isWhitespaceOrPunctuation(c)) {
76// wsOrPunctCount++;
77// continue;
78// }
79// if (isHebLetter(c)) {
80// hebCharCount++;
81// continue;
82// }
83// if (isHebSymbolMaybeBiblical(c)) {
84// if (isBiblicalSymbol(c)) {
85// biblicalSymbolCount++;
86// } else {
87// normalSymbolCount++;
88// }
89// continue;
90// }
91// }
92
93// const counts = {
94// wsOrPunctCount,
95// hebCharCount,
96// normalSymbolCount,
97// biblicalSymbolCount,
98// };
99
100// // short circuit if no heb letters found
101// if (hebCharCount === 0) {
102// return { result: false, counts };
103// }
104
105// // exclude biblical copy-pasta
106// if (biblicalSymbolCount > 0) {
107// return { result: false, counts };
108// }
109
110// const hebRatio = (hebCharCount + normalSymbolCount) / (txt.length - wsOrPunctCount);
111// const symbolsRatio = normalSymbolCount / hebCharCount;
112
113// return {
114// result: hebRatio > 0.5 && symbolsRatio < 0.2,
115// counts,
116// };
117// }
118
119// const wOrPunctuation1 = [0x0020, 0x002f];
120// const wOrPunctuation2 = [0x003a, 0x0040];
121// const wOrPunctuation3 = [0x005b, 0x0060];
122// const wOrPunctuation4 = [0x007b, 0x007e];
123// function isWhitespaceOrPunctuation(c: number): boolean {
124// return (
125// (wOrPunctuation1[0] <= c && c <= wOrPunctuation1[1]) ||
126// (wOrPunctuation2[0] <= c && c <= wOrPunctuation2[1]) ||
127// (wOrPunctuation3[0] <= c && c <= wOrPunctuation3[1]) ||
128// (wOrPunctuation4[0] <= c && c <= wOrPunctuation4[1])
129// );
130// }
131
132// const alephToTaf = [0x05d0, 0x05ea];
133// function isHebLetter(c: number): boolean {
134// return alephToTaf[0] <= c && c <= alephToTaf[1];
135// }
136
137// const symbols = [0x0591, 0x05c7];
138// const biblicalSymbols1 = [0x0591, 0x05af];
139// const biblicalSymbols2 = [0x05bd, 0x05c0];
140// const biblicalSymbols3 = [0x05c3, 0x05c7];
141// function isHebSymbolMaybeBiblical(c: number): boolean {
142// return symbols[0] <= c && c <= symbols[1];
143// }
144// function isBiblicalSymbol(c: number): boolean {
145// return (
146// (biblicalSymbols1[0] <= c && c <= biblicalSymbols1[1]) ||
147// (biblicalSymbols2[0] <= c && c <= biblicalSymbols2[1]) ||
148// (biblicalSymbols3[0] <= c && c <= biblicalSymbols3[1])
149// );
150// }