From e1f0d9c0ad099d4dbdf2f00bad8b2dc7381464bc Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Fri, 27 Sep 2024 17:01:43 +0200 Subject: [PATCH] fix: edge cases when calculating (un)localized body We used to randomly return an empty string when the formatted body was empty, even though we never return an empty string usually. Similarly we used to return the original formatted body in an edit, when the new event has no formatted body. --- lib/src/event.dart | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index 4e9710d7..3fbc6354 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -297,9 +297,13 @@ class Event extends MatrixEvent { /// Use this to get a plain-text representation of the event, stripping things /// like spoilers and thelike. Useful for plain text notifications. - String get plaintextBody => content['format'] == 'org.matrix.custom.html' - ? HtmlToText.convert(formattedText) - : body; + String get plaintextBody => switch (formattedText) { + // if the formattedText is empty, fallback to body + '' => body, + final String s when content['format'] == 'org.matrix.custom.html' => + HtmlToText.convert(s), + _ => body, + }; /// Returns a list of [Receipt] instances for this event. List get receipts { @@ -873,12 +877,14 @@ class Event extends MatrixEvent { if (hideEdit && relationshipType == RelationshipTypes.edit && newContent != null) { - if (plaintextBody && newContent['format'] == 'org.matrix.custom.html') { - final newBody = newContent.tryGet('formatted_body'); - if (newBody != null) { - mayHaveReplyFallback = false; - body = HtmlToText.convert(newBody); - } + final newBody = + newContent.tryGet('formatted_body', TryGet.silent); + if (plaintextBody && + newContent['format'] == 'org.matrix.custom.html' && + newBody != null && + newBody.isNotEmpty) { + mayHaveReplyFallback = false; + body = HtmlToText.convert(newBody); } else { mayHaveReplyFallback = true; body = newContent.tryGet('body') ?? body;