fix: Correctly generate reply fallbacks

This commit is contained in:
Sorunome 2021-01-14 13:34:12 +01:00
parent 347a1ee6e0
commit 88f8a57863
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
2 changed files with 82 additions and 6 deletions

View File

@ -17,6 +17,7 @@
*/
import 'dart:async';
import 'dart:convert';
import 'package:html_unescape/html_unescape.dart';
import 'package:matrix_file_e2ee/matrix_file_e2ee.dart';
@ -583,7 +584,8 @@ class Room {
if (parseMarkdown) {
final html = markdown(event['body'], emotePacks ?? this.emotePacks);
// if the decoded html is the same as the body, there is no need in sending a formatted message
if (HtmlUnescape().convert(html) != event['body']) {
if (HtmlUnescape().convert(html.replaceAll(RegExp(r'<br />\n?'), '\n')) !=
event['body']) {
event['format'] = 'org.matrix.custom.html';
event['formatted_body'] = html;
}
@ -754,9 +756,22 @@ class Room {
}
replyText = replyTextLines.join('\n');
content['format'] = 'org.matrix.custom.html';
// be sure that we strip any previous reply fallbacks
final replyHtml = (inReplyTo.formattedText.isNotEmpty
? inReplyTo.formattedText
: htmlEscape.convert(inReplyTo.body).replaceAll('\n', '<br>'))
.replaceAll(
RegExp(r'<mx-reply>.*<\/mx-reply>',
caseSensitive: false, multiLine: false, dotAll: true),
'');
final repliedHtml = content.tryGet<String>(
'formatted_body',
htmlEscape
.convert(content.tryGet<String>('body', ''))
.replaceAll('\n', '<br>'));
content['formatted_body'] =
'<mx-reply><blockquote><a href="https://matrix.to/#/${inReplyTo.room.id}/${inReplyTo.eventId}">In reply to</a> <a href="https://matrix.to/#/${inReplyTo.senderId}">${inReplyTo.senderId}</a><br>${inReplyTo.body}</blockquote></mx-reply>${content["formatted_body"] ?? content["body"]}';
content['body'] = replyText + "\n\n${content["body"] ?? ""}";
'<mx-reply><blockquote><a href="https://matrix.to/#/${inReplyTo.room.id}/${inReplyTo.eventId}">In reply to</a> <a href="https://matrix.to/#/${inReplyTo.senderId}">${inReplyTo.senderId}</a><br>$replyHtml</blockquote></mx-reply>$repliedHtml';
content['body'] = replyText + "\n\n${content.tryGet<String>('body', '')}";
content['m.relates_to'] = {
'm.in_reply_to': {
'event_id': inReplyTo.eventId,

View File

@ -420,12 +420,12 @@ void main() {
'sender': '@alice:example.org',
}, room);
FakeMatrixApi.calledEndpoints.clear();
final dynamic resp = await room.sendTextEvent('Hello world',
var resp = await room.sendTextEvent('Hello world',
txid: 'testtxid', inReplyTo: event);
expect(resp.startsWith('\$event'), true);
final entry = FakeMatrixApi.calledEndpoints.entries
var entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
final content = json.decode(entry.value.first);
var content = json.decode(entry.value.first);
expect(content, {
'body': '> <@alice:example.org> Blah\n\nHello world',
'msgtype': 'm.text',
@ -438,6 +438,67 @@ void main() {
},
},
});
event = Event.fromJson({
'event_id': '\$replyEvent',
'content': {
'body': '<b>Blah</b>\nbeep',
'msgtype': 'm.text',
},
'type': 'm.room.message',
'sender': '@alice:example.org',
}, room);
FakeMatrixApi.calledEndpoints.clear();
resp = await room.sendTextEvent('Hello world\nfox',
txid: 'testtxid', inReplyTo: event);
expect(resp.startsWith('\$event'), true);
entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
content = json.decode(entry.value.first);
expect(content, {
'body':
'> <@alice:example.org> <b>Blah</b>\n> beep\n\nHello world\nfox',
'msgtype': 'm.text',
'format': 'org.matrix.custom.html',
'formatted_body':
'<mx-reply><blockquote><a href="https://matrix.to/#/!localpart:server.abc/\$replyEvent">In reply to</a> <a href="https://matrix.to/#/@alice:example.org">@alice:example.org</a><br>&lt;b&gt;Blah&lt;&#47;b&gt;<br>beep</blockquote></mx-reply>Hello world<br>fox',
'm.relates_to': {
'm.in_reply_to': {
'event_id': '\$replyEvent',
},
},
});
event = Event.fromJson({
'event_id': '\$replyEvent',
'content': {
'format': 'org.matrix.custom.html',
'formatted_body': '<mx-reply>heya</mx-reply>meow',
'body': 'plaintext meow',
'msgtype': 'm.text',
},
'type': 'm.room.message',
'sender': '@alice:example.org',
}, room);
FakeMatrixApi.calledEndpoints.clear();
resp = await room.sendTextEvent('Hello world',
txid: 'testtxid', inReplyTo: event);
expect(resp.startsWith('\$event'), true);
entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/m.room.message/'));
content = json.decode(entry.value.first);
expect(content, {
'body': '> <@alice:example.org> plaintext meow\n\nHello world',
'msgtype': 'm.text',
'format': 'org.matrix.custom.html',
'formatted_body':
'<mx-reply><blockquote><a href="https://matrix.to/#/!localpart:server.abc/\$replyEvent">In reply to</a> <a href="https://matrix.to/#/@alice:example.org">@alice:example.org</a><br>meow</blockquote></mx-reply>Hello world',
'm.relates_to': {
'm.in_reply_to': {
'event_id': '\$replyEvent',
},
},
});
});
test('send reaction', () async {