getDirectChatFromUserId method
Returns the (first) room ID from the store which is a private chat with the user userId
.
Returns null if there is none.
Implementation
String? getDirectChatFromUserId(String userId) {
final directChats = _accountData['m.direct']?.content[userId];
if (directChats is List<dynamic> && directChats.isNotEmpty) {
final potentialRooms = directChats
.cast<String>()
.map(getRoomById)
.where((room) => room != null && room.membership == Membership.join);
if (potentialRooms.isNotEmpty) {
return potentialRooms.fold<Room>(potentialRooms.first!,
(Room prev, Room? r) {
if (r == null) {
return prev;
}
final prevLast = prev.lastEvent?.originServerTs ?? DateTime(0);
final rLast = r.lastEvent?.originServerTs ?? DateTime(0);
return rLast.isAfter(prevLast) ? r : prev;
}).id;
}
}
for (final room in rooms) {
if (room.membership == Membership.invite &&
room.getState(EventTypes.RoomMember, userID!)?.senderId == userId &&
room.getState(EventTypes.RoomMember, userID!)?.content['is_direct'] ==
true) {
return room.id;
}
}
return null;
}