bitcoiner.social/ansible/playbooks/host_tasks/gabite.bitcoiner.social/files/scripts/prune.ts

68 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

import { readLines } from 'https://deno.land/std@0.201.0/io/mod.ts';
import { DB } from "https://deno.land/x/sqlite@v3.8/mod.ts";
const db = new DB("pubkeys.db");
const subscriberResults = db.query("SELECT pubkey FROM subscribers ORDER BY pubkey DESC") as string[][];
const foafResults = db.query("SELECT DISTINCT foaf_pubkey FROM foaf ORDER BY foaf_pubkey DESC") as string[][];
const subscriberSet = new Set(subscriberResults.map(subscriber => subscriber[0]));
const foafSet = new Set(foafResults.map(foaf => foaf[0]));
const trustEventAgeLimit = 90;
const foafEventAgeLimit = 30;
const untrustEventAgeLimit = 7;
const importantKindAgeLimits: Record<number, number> = {
0: 365, // NIP-01: profiles
3: 365, // NIP-01: contacts
9735: 180, // NIP-57: zap receipts
24133: 365, // NIP-46: nostr connect
13194: 365, // NIP-47: nostr wallet connect
10002: 730, // NIP-65
};
const exportEventsStdin = async (): Promise<void> => {
if (Deno.isatty(Deno.stdin.rid)) {
Deno.exit(1);
}
for await (const line of readLines(Deno.stdin)) {
if (line.length === 0) {
return;
}
exportLine(line);
}
};
const exportLine = (line: string): void => {
const eventJson = JSON.parse(line);
const created_at = new Date(eventJson.created_at * 1000); // convert seconds to ms
if (subscriberSet.has(eventJson.pubkey)) {
// Tier 1: subscribers
const kindAgeLimit = (importantKindAgeLimits[eventJson.kind] || trustEventAgeLimit) * 24 * 60 * 60 * 1000; // convert days to ms
const ageLimitDate = new Date(Date.now() - kindAgeLimit);
if (created_at > ageLimitDate) {
console.log(line); // keep events from the last year
}
} else if (foafSet.has(eventJson.pubkey)) {
// Tier 2: foaf
const kindAgeLimit = (importantKindAgeLimits[eventJson.kind] || foafEventAgeLimit) * 24 * 60 * 60 * 1000; // convert days to ms
const ageLimitDate = new Date(Date.now() - kindAgeLimit);
if (created_at > ageLimitDate) {
console.log(line); // keep events from the last year
}
} else {
// Tier 3: untrust
const kindAgeLimit = (importantKindAgeLimits[eventJson.kind] || untrustEventAgeLimit) * 24 * 60 * 60 * 1000; // convert days to ms
const ageLimitDate = new Date(Date.now() - kindAgeLimit);
if (created_at > ageLimitDate) {
console.log(line); // keep recent events
}
}
};
await exportEventsStdin();