import { OutputSchema as RepoEvent, isCommit, } from "@my/lexicon/server/types/com/atproto/sync/subscribeRepos"; import { Record as PostRecord } from "@my/lexicon/server/types/app/bsky/feed/post"; import { FirehoseSubscriptionBase, getOpsByType } from "@/util/subscription"; import { handler as subscriptionHandler } from "@/heb-ppl/subscriber"; export class FirehoseSubscription extends FirehoseSubscriptionBase { async handleEvent(evt: RepoEvent) { if (!isCommit(evt)) return; const ops = await getOpsByType(evt); await subscriptionHandler(this, ops); // // This logs the text of every post off the firehose. // // Just for fun :) // // Delete before actually using // for (const post of ops.posts.creates) { // console.log("==================================="); // console.log(post.author); // console.log(post.record.text); // console.log("==================================="); // } // const postsToDelete = ops.posts.deletes.map((del) => del.uri); // const groupedCreatedPosts = Object.groupBy( // ops.posts.creates.map((create) => ({ // create, // ...isHebPost(create.record), // })), // (o) => (o.result ? "heb" : "not"), // ); // const postsToCreate = (groupedCreatedPosts.heb ?? []).map(({ create, counts }) => { // return { // uri: create.uri, // cid: create.cid, // author_did: create.author, // indexedAt: new Date().toISOString(), // analysis: JSON.stringify({ counts }), // }; // }); // if (postsToDelete.length > 0) { // await this.db.deleteFrom("post").where("uri", "in", postsToDelete).execute(); // } // if (postsToCreate.length > 0) { // await this.db // .insertInto("post") // .values(postsToCreate) // .onConflict((oc) => oc.doNothing()) // .execute(); // } } } // function isHebPost(post: PostRecord): { // result: boolean; // counts: { // wsOrPunctCount: number; // hebCharCount: number; // normalSymbolCount: number; // biblicalSymbolCount: number; // }; // } { // const txt = post.text.normalize("NFC"); // let wsOrPunctCount = 0; // let hebCharCount = 0; // let normalSymbolCount = 0; // let biblicalSymbolCount = 0; // for (let i = 0; i < txt.length; i++) { // const c = txt.charCodeAt(i); // if (isWhitespaceOrPunctuation(c)) { // wsOrPunctCount++; // continue; // } // if (isHebLetter(c)) { // hebCharCount++; // continue; // } // if (isHebSymbolMaybeBiblical(c)) { // if (isBiblicalSymbol(c)) { // biblicalSymbolCount++; // } else { // normalSymbolCount++; // } // continue; // } // } // const counts = { // wsOrPunctCount, // hebCharCount, // normalSymbolCount, // biblicalSymbolCount, // }; // // short circuit if no heb letters found // if (hebCharCount === 0) { // return { result: false, counts }; // } // // exclude biblical copy-pasta // if (biblicalSymbolCount > 0) { // return { result: false, counts }; // } // const hebRatio = (hebCharCount + normalSymbolCount) / (txt.length - wsOrPunctCount); // const symbolsRatio = normalSymbolCount / hebCharCount; // return { // result: hebRatio > 0.5 && symbolsRatio < 0.2, // counts, // }; // } // const wOrPunctuation1 = [0x0020, 0x002f]; // const wOrPunctuation2 = [0x003a, 0x0040]; // const wOrPunctuation3 = [0x005b, 0x0060]; // const wOrPunctuation4 = [0x007b, 0x007e]; // function isWhitespaceOrPunctuation(c: number): boolean { // return ( // (wOrPunctuation1[0] <= c && c <= wOrPunctuation1[1]) || // (wOrPunctuation2[0] <= c && c <= wOrPunctuation2[1]) || // (wOrPunctuation3[0] <= c && c <= wOrPunctuation3[1]) || // (wOrPunctuation4[0] <= c && c <= wOrPunctuation4[1]) // ); // } // const alephToTaf = [0x05d0, 0x05ea]; // function isHebLetter(c: number): boolean { // return alephToTaf[0] <= c && c <= alephToTaf[1]; // } // const symbols = [0x0591, 0x05c7]; // const biblicalSymbols1 = [0x0591, 0x05af]; // const biblicalSymbols2 = [0x05bd, 0x05c0]; // const biblicalSymbols3 = [0x05c3, 0x05c7]; // function isHebSymbolMaybeBiblical(c: number): boolean { // return symbols[0] <= c && c <= symbols[1]; // } // function isBiblicalSymbol(c: number): boolean { // return ( // (biblicalSymbols1[0] <= c && c <= biblicalSymbols1[1]) || // (biblicalSymbols2[0] <= c && c <= biblicalSymbols2[1]) || // (biblicalSymbols3[0] <= c && c <= biblicalSymbols3[1]) // ); // }