-
Notifications
You must be signed in to change notification settings - Fork 65
Add ListMessageSplitter and apply to rules command #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShadowJonathan
wants to merge
3
commits into
matrix-org:main
Choose a base branch
from
ShadowJonathan:list-splitter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| import {LogService} from "matrix-bot-sdk"; | ||
|
|
||
| export type MessageSnippet = { html: string, text: string }; | ||
|
|
||
| // The max size a message can be, with 24_000 picked at random to accommodate for overhead. | ||
| const OVERHEAD = 24_000 | ||
Gnuxie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const MAX_SIZE = 65_536 - OVERHEAD; | ||
|
|
||
| // The extra bits that a <ul> tag wrapping would add to a message | ||
| const UL_TAG_WRAP_SIZE = "<ul></ul>".length; | ||
| const HTML_LIST_ITEM_EXTRA_SIZE = "<li></li>".length; | ||
| const TEXT_LIST_ITEM_EXTRA_SIZE = " * \n".length; | ||
|
|
||
| export class MessageListItem { | ||
| public html: string; | ||
| public text: string; | ||
ShadowJonathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| constructor( | ||
| html: string, | ||
| text: string, | ||
| ) { | ||
| if (html.length > (MAX_SIZE / 2)) { | ||
| throw new Error("HTML string too long for one item") | ||
| } else if (text.length > (MAX_SIZE / 2)) { | ||
| throw new Error("text string too long for one item") | ||
| } | ||
| this.html = html; | ||
| this.text = text; | ||
| } | ||
|
|
||
| public size(): number { | ||
| return this.html.length + HTML_LIST_ITEM_EXTRA_SIZE | ||
| + this.text.length + TEXT_LIST_ITEM_EXTRA_SIZE | ||
| } | ||
| } | ||
|
|
||
| export class MessageListHeader extends MessageListItem { | ||
| public size(): number { | ||
| return this.html.length + this.text.length | ||
| } | ||
| } | ||
|
|
||
| class MessageListing { | ||
| public items: MessageListItem[] = []; | ||
|
|
||
| constructor(public header: MessageListHeader | null) { | ||
| } | ||
|
|
||
| public split_at_size(desired_size: number): {sized: MessageListing | null, rest: MessageListing | null} { | ||
ShadowJonathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (this.size() <= desired_size) { | ||
| return {sized: this, rest: null}; | ||
| } else { | ||
| let sized = new MessageListing(this.header); | ||
| if (sized.size() > desired_size) { | ||
| // If the header alone is too much, just give up. | ||
| return {sized: null, rest: this} | ||
| } | ||
|
|
||
| let rest = new MessageListing(null); | ||
| rest.items = [...this.items]; | ||
|
|
||
| let current_item = rest.items.shift(); | ||
|
|
||
| while (current_item !== undefined) { | ||
| // Add the new item tentatively | ||
| sized.items.push(current_item); | ||
|
|
||
| if (sized.size() > desired_size) { | ||
| // If we went over the limit, return the last item and return the results | ||
| rest.items.unshift(sized.items.pop()!) | ||
|
|
||
| if (sized.items.length > 0) { | ||
| return {sized, rest}; | ||
| } else { | ||
| return {sized: null, rest} | ||
| } | ||
| } | ||
|
|
||
| current_item = rest.items.shift(); | ||
| } | ||
|
|
||
| // ??? We somehow did not go over the size limit when testing it per item? | ||
| // This is a weird state, as we should have already caught this with the | ||
| // `this.size() <= desired_size` conditional. | ||
| LogService.warn("ListMessageSplitter", `encountered end of while loop, required max ${desired_size} size, current item is ${sized.size()}`) | ||
ShadowJonathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return {sized, rest: null} | ||
| } | ||
| } | ||
ShadowJonathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public size(): number { | ||
| let with_header = this.header !== null ? this.header.size() + "\n".length + "<br>".length : 0; | ||
|
|
||
| return with_header | ||
| + (this.items.length > 0 ? UL_TAG_WRAP_SIZE : 0) | ||
| + this.items.reduce((prev, curr, _idx, _arr) => prev + curr.size(), 0); | ||
ShadowJonathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public render(): MessageSnippet { | ||
| let current: MessageSnippet = { | ||
| html: "", | ||
| text: "" | ||
| }; | ||
|
|
||
| for (let item of this.items) { | ||
| current.html += `<li>${item.html}</li>` | ||
| current.text += ` * ${item.text}\n` | ||
| } | ||
|
|
||
| current.html = this.items.length > 0 ? `<ul>${current.html}</ul>` : ""; | ||
|
|
||
| if (this.header !== null) { | ||
| current.html = this.header.html + "<br>".length + current.html; | ||
| current.text = this.header.text + "\n" + current.text; | ||
| } | ||
|
|
||
| return current; | ||
| } | ||
| } | ||
|
|
||
| /// A class that allows splitting items and headers into multiple messages. | ||
| export class ListMessageSplitter { | ||
| private items: (MessageListItem | MessageListHeader)[] = []; | ||
|
|
||
| constructor() { | ||
| } | ||
|
|
||
| public add_header(html: string, text: string) { | ||
| this.add(new MessageListHeader(html, text)) | ||
| } | ||
|
|
||
| public add_paragraph(html: string, text: string) { | ||
| this.add(new MessageListItem(html, text)) | ||
| } | ||
|
|
||
| public add(paragraph: MessageListItem | MessageListHeader) { | ||
| this.items.push(paragraph) | ||
| } | ||
|
|
||
| // Convert this.items into listings. | ||
| private get_listings(): MessageListing[] { | ||
| let current_listings: MessageListing[] = []; | ||
ShadowJonathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let current_listing = new MessageListing(null); | ||
|
|
||
| for (let item of this.items) { | ||
| if (item instanceof MessageListHeader) { | ||
| if (current_listing.header !== null) { | ||
| current_listings.push(current_listing); | ||
|
|
||
| current_listing = new MessageListing(item); | ||
| } else { | ||
| current_listing.header = item; | ||
ShadowJonathan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } else { | ||
| current_listing.items.push(item); | ||
| } | ||
| } | ||
|
|
||
| current_listings.push(current_listing); | ||
|
|
||
| return current_listings; | ||
| } | ||
|
|
||
| // Split the listings until they do not hit MAX_SIZE anymore. | ||
| private split_listings(listings: MessageListing[]): MessageListing[][] { | ||
| let result: MessageListing[][] = []; | ||
|
|
||
| let current: MessageListing[] = []; | ||
| let current_size = 0; | ||
|
|
||
| let listing: MessageListing | null; | ||
| for (listing of listings) { | ||
| while (listing !== null) { | ||
| let { sized, rest } = listing.split_at_size(MAX_SIZE - current_size); | ||
|
|
||
| if (sized !== null) { | ||
| current.push(sized); | ||
| current_size += sized.size() | ||
| } else { | ||
| result.push(current); | ||
| current = []; | ||
| current_size = 0; | ||
| } | ||
|
|
||
| listing = rest; | ||
| } | ||
| } | ||
|
|
||
| result.push(current); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public render(): { html: string, text: string }[] { | ||
| let rendered: { html: string, text: string }[] = []; | ||
|
|
||
| let listings = this.get_listings(); | ||
| let chunks = this.split_listings(listings); | ||
|
|
||
| for (let chunk of chunks) { | ||
| let current = { | ||
| html: "", | ||
| text: "", | ||
| } | ||
|
|
||
| for (let listing of chunk) { | ||
| let {html, text} = listing.render(); | ||
| current.html += html; | ||
| current.text += text; | ||
| } | ||
|
|
||
| rendered.push(current) | ||
| } | ||
|
|
||
| return rendered; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.