From a7c5431ec1d1cb2ea5a0c1e614b675031e0964d2 Mon Sep 17 00:00:00 2001 From: ducklet Date: Tue, 10 Nov 2020 21:33:30 +0100 Subject: [PATCH] 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. --- hotdog/models.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/hotdog/models.py b/hotdog/models.py index 89d937d..8142a0f 100644 --- a/hotdog/models.py +++ b/hotdog/models.py @@ -1,12 +1,13 @@ import asyncio from dataclasses import dataclass, field +from itertools import dropwhile from random import random from time import time as now from typing import * import nio -from .html import HtmlDocument, parse_html +from .html import HtmlDocument, detach, find, parse_html JobCallback = Callable[["Job"], None] @@ -106,6 +107,18 @@ class Message: 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.html = html self.tokens = Tokens.from_str(plain)