Merge pull request #2005 from famedly/krille/direct-chat-type-safe

refactor: Make direct chat getter type safe
This commit is contained in:
Krille-chan 2025-11-11 09:24:40 +01:00 committed by GitHub
commit 42964f388f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 15 deletions

View File

@ -2735,8 +2735,6 @@ class FakeMatrixApi extends BaseClient {
(var req) => {}, (var req) => {},
'/client/v3/user/%40test%3AfakeServer.notExisting/rooms/!localpart%3Aserver.abc/account_data/m.marked_unread': '/client/v3/user/%40test%3AfakeServer.notExisting/rooms/!localpart%3Aserver.abc/account_data/m.marked_unread':
(var req) => {}, (var req) => {},
'/client/v3/user/%40test%3AfakeServer.notExisting/account_data/m.direct':
(var req) => {},
'/client/v3/user/%40othertest%3AfakeServer.notExisting/account_data/m.direct': '/client/v3/user/%40othertest%3AfakeServer.notExisting/account_data/m.direct':
(var req) => {}, (var req) => {},
'/client/v3/profile/%40alice%3Aexample.com/displayname': (var reqI) => {}, '/client/v3/profile/%40alice%3Aexample.com/displayname': (var reqI) => {},

View File

@ -439,8 +439,13 @@ class Client extends MatrixApi {
return null; return null;
} }
Map<String, dynamic> get directChats => Map<String, List<String>> get directChats =>
_accountData['m.direct']?.content ?? {}; (_accountData['m.direct']?.content ?? {}).map(
(userId, list) => MapEntry(
userId,
(list is! List) ? [] : list.whereType<String>().toList(),
),
);
/// Returns the first room ID from the store (the room with the latest event) /// Returns the first room ID from the store (the room with the latest event)
/// which is a private chat with the user [userId]. /// which is a private chat with the user [userId].

View File

@ -349,7 +349,7 @@ class Room {
final cache = _cachedDirectChatMatrixId; final cache = _cachedDirectChatMatrixId;
if (cache != null) { if (cache != null) {
final roomIds = client.directChats[cache]; final roomIds = client.directChats[cache];
if (roomIds is List && roomIds.contains(id)) { if (roomIds != null && roomIds.contains(id)) {
return cache; return cache;
} }
} }
@ -1519,21 +1519,21 @@ class Room {
/// Sets this room as a direct chat for this user if not already. /// Sets this room as a direct chat for this user if not already.
Future<void> addToDirectChat(String userID) async { Future<void> addToDirectChat(String userID) async {
final directChats = client.directChats; final dmRooms = List<String>.from(client.directChats[userID] ?? []);
if (directChats[userID] is List) { if (dmRooms.contains(id)) {
if (!directChats[userID].contains(id)) { Logs().d('Already a direct chat.');
directChats[userID].add(id);
} else {
return; return;
} // Is already in direct chats
} else {
directChats[userID] = [id];
} }
dmRooms.add(id);
await client.setAccountData( await client.setAccountData(
client.userID!, client.userID!,
'm.direct', 'm.direct',
directChats, {
...client.directChats,
userID: dmRooms,
},
); );
return; return;
} }

View File

@ -139,6 +139,7 @@ void main() async {
await user1.setPower(50); await user1.setPower(50);
}); });
test('startDirectChat', () async { test('startDirectChat', () async {
FakeMatrixApi.client = user1.room.client;
await user1.startDirectChat(waitForSync: false); await user1.startDirectChat(waitForSync: false);
}); });
test('getPresence', () async { test('getPresence', () async {