eleven / ocho Public

Clone
git clone https://amadan.net/eleven/ocho

Plain git — no account needed to clone.

Download

Download this file

1// Media, Links & Docs — the pure extraction + in-memory per-thread index
2// behind the thread's media overlay (mediaview.js). Same posture as the
3// search index (search.js): rows decrypt lazily into MEMORY, never to disk —
4// the store deliberately holds only ciphertext at rest. decryptRow is
5// injected so this module stays free of keys and Node-testable.
6// Relative import: Node tests load this file directly.
7import { tokenizeText, blockTokens } from "./format.js";
8
9// One decrypted payload -> its media/links/docs entries, or null when the
10// message contributes nothing. Typed app events and reactions are invisible
11// here (same rule as search); tombstones never reach us (index skips them).
12export function extractEntries(data, meta) {
13 if (!data || data.type) return null;
14 const base = { sid: meta.id, ts: data.ts || meta.ts || 0, uid: data.uid || "", name: data.name || "" };
15 const media = [];
16 const docs = [];
17 const links = [];
18 for (const a of data.attachments || []) {
19 if (!a || !a.id) continue;
20 const t = a.type || "";
21 if (t.startsWith("image/") || t.startsWith("video/")) media.push({ ...base, att: a });
22 else docs.push({ ...base, att: a });
23 }
24 // Every link in the prose — the feed previews only the first (firstLink),
25 // but the Links tab is an inventory. Fenced code is skipped for the same
26 // reason firstLink skips it: a URL pasted as code isn't a shared link.
27 if (data.text) {
28 for (const b of blockTokens(data.text)) {
29 if (b.type === "code") continue;
30 for (const tok of tokenizeText(b.text)) if (tok.href) links.push({ ...base, url: tok.href });
31 }
32 }
33 if (!media.length && !docs.length && !links.length) return null;
34 return { media, links, docs };
35}
36
37export function monthLabel(ts) {
38 return new Date(ts).toLocaleDateString([], { month: "long", year: "numeric" });
39}
40
41// Group an already-sorted (newest-first) entry list into month sections.
42// Consecutive grouping is enough BECAUSE the input is sorted.
43export function bucketByMonth(entries) {
44 const out = [];
45 for (const e of entries) {
46 const d = new Date(e.ts || 0);
47 const key = d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0");
48 if (!out.length || out[out.length - 1].key !== key) out.push({ key, label: monthLabel(e.ts || 0), items: [] });
49 out[out.length - 1].items.push(e);
50 }
51 return out;
52}
53
54export class ThreadMediaIndex {
55 constructor(decryptRow) {
56 this.decryptRow = decryptRow; // async (row, tid) -> payload | null
57 this.threads = new Map(); // tid -> { seen:Set<sid>, media:[], links:[], docs:[] }
58 }
59
60 bucket(tid) {
61 let b = this.threads.get(tid);
62 if (!b) {
63 b = { seen: new Set(), media: [], links: [], docs: [] };
64 this.threads.set(tid, b);
65 }
66 return b;
67 }
68
69 // Fold rows not yet seen. Idempotent per sid; a failed decrypt still marks
70 // seen (same as search — a row that won't decrypt now won't decrypt later).
71 async ensureThread(tid, rows) {
72 const b = this.bucket(tid);
73 for (const row of rows || []) {
74 if (!row || !row.id || b.seen.has(row.id) || row.tombstone_of) continue;
75 b.seen.add(row.id);
76 let data = null;
77 try {
78 data = await this.decryptRow(row, tid);
79 } catch {}
80 if (!data) continue;
81 const ex = extractEntries(data, { id: row.id, ts: (row.created_at || 0) * 1000 });
82 if (!ex) continue;
83 b.media.push(...ex.media);
84 b.links.push(...ex.links);
85 b.docs.push(...ex.docs);
86 }
87 for (const k of ["media", "links", "docs"]) b[k].sort((x, y) => y.sid - x.sid);
88 }
89
90 // Moderation filters at QUERY time so un-muting needs no re-scan.
91 tab(tid, name, { hiddenUids }) {
92 return this.bucket(tid)[name].filter((e) => !hiddenUids.has(e.uid));
93 }
94
95 counts(tid, opts) {
96 return {
97 media: this.tab(tid, "media", opts).length,
98 links: this.tab(tid, "links", opts).length,
99 docs: this.tab(tid, "docs", opts).length,
100 };
101 }
102
103 // An edit re-extracts (links can change); a delete just never re-folds.
104 invalidate(tid, sid) {
105 const b = this.threads.get(tid);
106 if (!b) return;
107 b.seen.delete(sid);
108 for (const k of ["media", "links", "docs"]) b[k] = b[k].filter((e) => e.sid !== sid);
109 }
110
111 dropThread(tid) {
112 this.threads.delete(tid);
113 }
114}
115
116// Search within a tab: a case-insensitive substring over what each entry
117// actually shows — the sender's name (every tab), the URL and its unfurl
118// title (links; `titleFor(url)` returns the title if it's been fetched, else
119// ""), and the filename (docs, and media that carry one). Empty/blank query =
120// everything. Pure, so the three UIs share one definition of "matches".
121export function filterEntries(entries, query, titleFor = () => "") {
122 const q = (query || "").trim().toLowerCase();
123 if (!q) return entries;
124 return entries.filter((e) => {
125 if ((e.name || "").toLowerCase().includes(q)) return true;
126 if (e.url) {
127 if (e.url.toLowerCase().includes(q)) return true;
128 if ((titleFor(e.url) || "").toLowerCase().includes(q)) return true;
129 }
130 if (e.att && (e.att.name || "").toLowerCase().includes(q)) return true;
131 return false;
132 });
133}
134