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.
This commit is contained in:
Nicolas Werner 2024-09-27 17:01:43 +02:00
parent a497a66012
commit e1f0d9c0ad
No known key found for this signature in database
GPG Key ID: B38119FF80087618
1 changed files with 15 additions and 9 deletions

View File

@ -297,9 +297,13 @@ class Event extends MatrixEvent {
/// Use this to get a plain-text representation of the event, stripping things /// Use this to get a plain-text representation of the event, stripping things
/// like spoilers and thelike. Useful for plain text notifications. /// like spoilers and thelike. Useful for plain text notifications.
String get plaintextBody => content['format'] == 'org.matrix.custom.html' String get plaintextBody => switch (formattedText) {
? HtmlToText.convert(formattedText) // if the formattedText is empty, fallback to body
: 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. /// Returns a list of [Receipt] instances for this event.
List<Receipt> get receipts { List<Receipt> get receipts {
@ -873,12 +877,14 @@ class Event extends MatrixEvent {
if (hideEdit && if (hideEdit &&
relationshipType == RelationshipTypes.edit && relationshipType == RelationshipTypes.edit &&
newContent != null) { newContent != null) {
if (plaintextBody && newContent['format'] == 'org.matrix.custom.html') { final newBody =
final newBody = newContent.tryGet<String>('formatted_body'); newContent.tryGet<String>('formatted_body', TryGet.silent);
if (newBody != null) { if (plaintextBody &&
mayHaveReplyFallback = false; newContent['format'] == 'org.matrix.custom.html' &&
body = HtmlToText.convert(newBody); newBody != null &&
} newBody.isNotEmpty) {
mayHaveReplyFallback = false;
body = HtmlToText.convert(newBody);
} else { } else {
mayHaveReplyFallback = true; mayHaveReplyFallback = true;
body = newContent.tryGet<String>('body') ?? body; body = newContent.tryGet<String>('body') ?? body;