diff --git a/lib/src/client.dart b/lib/src/client.dart index f16073df..4d9f5703 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -199,12 +199,25 @@ class Client extends MatrixApi { if (accountData['m.direct'] != null && accountData['m.direct'].content[userId] is List && accountData['m.direct'].content[userId].length > 0) { + final potentialRooms = {}; for (final roomId in accountData['m.direct'].content[userId]) { final room = getRoomById(roomId); if (room != null && room.membership == Membership.join) { - return roomId; + potentialRooms.add(room); } } + if (potentialRooms.isNotEmpty) { + return potentialRooms + .fold( + null, + (prev, r) => prev == null + ? r + : (prev.lastEvent.originServerTs < + r.lastEvent.originServerTs + ? r + : prev)) + .id; + } } for (var i = 0; i < rooms.length; i++) { if (rooms[i].membership == Membership.invite &&