diff --git a/lib/src/room.dart b/lib/src/room.dart index c7117716..1da71a2f 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -747,6 +747,25 @@ class Room { ); } + String _stripBodyFallback(String body) { + if (body.startsWith('> <@')) { + var temp = ''; + var inPrefix = true; + for (final l in body.split('\n')) { + if (inPrefix && (l.isEmpty || l.startsWith('> '))) { + continue; + } + + inPrefix = false; + temp += temp.isEmpty ? l : ('\n' + l); + } + + return temp; + } else { + return body; + } + } + /// Sends an event to this room with this json as a content. Returns the /// event ID generated from the server. Future sendEvent( @@ -765,7 +784,8 @@ class Room { } if (inReplyTo != null) { - var replyText = '<${inReplyTo.senderId}> ' + inReplyTo.body; + var replyText = + '<${inReplyTo.senderId}> ' + _stripBodyFallback(inReplyTo.body); replyText = replyText.split('\n').map((line) => '> $line').join('\n'); content['format'] = 'org.matrix.custom.html'; // be sure that we strip any previous reply fallbacks diff --git a/test/room_test.dart b/test/room_test.dart index 651aa83f..f381bf05 100644 --- a/test/room_test.dart +++ b/test/room_test.dart @@ -638,6 +638,44 @@ void main() { }, }, }); + + // Reply to a reply + event = Event.fromJson({ + 'event_id': '\$replyEvent', + 'content': { + 'body': '> <@alice:example.org> Hey\n\nHello world', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '
In reply to @alice:example.org
Hey
Hello world', + 'm.relates_to': { + 'm.in_reply_to': { + 'event_id': '\$replyEvent', + }, + }, + }, + 'type': 'm.room.message', + 'sender': '@alice:example.org', + }, room); + FakeMatrixApi.calledEndpoints.clear(); + resp = + await room.sendTextEvent('Fox', 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> Hello world\n\nFox', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '
In reply to @alice:example.org
Hello world
Fox', + 'm.relates_to': { + 'm.in_reply_to': { + 'event_id': '\$replyEvent', + }, + }, + }); }); test('send reaction', () async {