remove any reply preamble from parsed Message

Otherwise some message handlers might be thrown off by seeing a trigger
that's actually just the quoted part of a previous message.
The full message body is still always available via the event property.
This commit is contained in:
ducklet 2020-11-10 21:33:30 +01:00
parent c58423ae58
commit a7c5431ec1

View file

@ -1,12 +1,13 @@
import asyncio import asyncio
from dataclasses import dataclass, field from dataclasses import dataclass, field
from itertools import dropwhile
from random import random from random import random
from time import time as now from time import time as now
from typing import * from typing import *
import nio import nio
from .html import HtmlDocument, parse_html from .html import HtmlDocument, detach, find, parse_html
JobCallback = Callable[["Job"], None] JobCallback = Callable[["Job"], None]
@ -106,6 +107,18 @@ class Message:
else None else None
) )
# Remove any "reply" preamble.
is_reply = plain.startswith("> <@")
if is_reply:
lines = dropwhile(
lambda l: l.startswith("> "), plain.splitlines(keepends=False)
)
nextline = next(lines)
if nextline == "":
plain = "\n".join(lines)
if html and (reply := next(find(html, "mx-reply"), None)):
detach(reply)
self.text = plain self.text = plain
self.html = html self.html = html
self.tokens = Tokens.from_str(plain) self.tokens = Tokens.from_str(plain)