diff --git a/src/ListMessageSplitter.ts b/src/ListMessageSplitter.ts new file mode 100644 index 00000000..a0bf207f --- /dev/null +++ b/src/ListMessageSplitter.ts @@ -0,0 +1,363 @@ +/** + The list splitter is meant to help with large itemized messages (such as a ban list), + which each have "headers" (bits of text that introduce the list). + + The behaviour is such that one pumps items and headers one-by-one into ListMessageSplitter, + which then splits them internally to accomodate maximum matrix event sizes, and renders them simultaniously. + + As such, a workflow with ListMessageSplitter would look something like this; + + ```js + const splitter = new ListMessageSplitter(); + + // Start a new list, input both html and text versions of the header. + splitter.add_header("Rules currently in use:", "Rules currently in use:"); + + for (rule of this.rules) { + // Add a new "paragraph", an item, se string templates here for each item. + splitter.add_paragraph( + `rule #${rule.number}: ${rule.text()}`, + `rule #${rule.number}: ${rule.text()}` + ) + } + + if (this.rules.length === 0) { + splitter.add_paragraph( + "No rules configured", + "No rules configured" + ) + } + + // Add another header, start a new list in the same message. + splitter.add_header("Servers currently observed:", "Servers currently observed:"); + + for (server of this.servers) { + splitter.add_paragraph( + `server ${server.name()}`, + `server ${server.name()}` + ) + } + + if (this.servers.length === 0) { + splitter.add_paragraph( + "No servers observed", + "No servers observed" + ) + } + + // Reply to an event with the whole deal, splitting into multiple messages as needed. + splitter.reply(mjolnir.client, roomId, event, true) + ``` +*/ + +import {MatrixClient, RichReply} from "matrix-bot-sdk"; + +// Helper type for html + text tuples. +export type MessageSnippet = { html: string, text: string }; + +// The max size a message can be, with 24_000 picked at random to accommodate for overhead. + +// Note: This amount was checked through trial and error, a conservative estimate would be +// 65_536 / 2, though this author does not know overhead estimates of matrix E2EE. + +// The overhead from E2EE. +export const OVERHEAD = 24_000 +// The max size a message can be. +export const MAX_SIZE = 65_536 - OVERHEAD; + +// The extra bits that a