Merge pull request #1988 from famedly/krille/handle-room-account-data-outside-of-handle-room-events

refactor: Handle Room Account Data outside of Room Event Updates
This commit is contained in:
Karthikeyan S 2024-12-31 15:41:31 +05:30 committed by GitHub
commit d89c8a3b54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 72 additions and 72 deletions

View File

@ -2559,7 +2559,7 @@ class Client extends MatrixApi {
final room = getRoomById(roomId); final room = getRoomById(roomId);
if (room != null) { if (room != null) {
final List<BasicEvent> events = []; final events = <Event>[];
for (final event in _eventsPendingDecryption) { for (final event in _eventsPendingDecryption) {
if (event.event.room.id != roomId) continue; if (event.event.room.id != roomId) continue;
if (!sessionIds.contains( if (!sessionIds.contains(
@ -2571,7 +2571,7 @@ class Client extends MatrixApi {
final decryptedEvent = final decryptedEvent =
await encryption!.decryptRoomEvent(event.event); await encryption!.decryptRoomEvent(event.event);
if (decryptedEvent.type != EventTypes.Encrypted) { if (decryptedEvent.type != EventTypes.Encrypted) {
events.add(BasicEvent.fromJson(decryptedEvent.content)); events.add(decryptedEvent);
} }
} }
@ -2651,11 +2651,10 @@ class Client extends MatrixApi {
final accountData = syncRoomUpdate.accountData; final accountData = syncRoomUpdate.accountData;
if (accountData != null && accountData.isNotEmpty) { if (accountData != null && accountData.isNotEmpty) {
await _handleRoomEvents( for (final event in accountData) {
room, await database?.storeRoomAccountData(event);
accountData, room.roomAccountData[event.type] = event;
EventUpdateType.accountData, }
);
} }
} }
@ -2671,12 +2670,9 @@ class Client extends MatrixApi {
} }
final accountData = syncRoomUpdate.accountData; final accountData = syncRoomUpdate.accountData;
if (accountData != null && accountData.isNotEmpty) { if (accountData != null && accountData.isNotEmpty) {
await _handleRoomEvents( for (final event in accountData) {
room, room.roomAccountData[event.type] = event;
accountData, }
EventUpdateType.accountData,
store: false,
);
} }
final state = syncRoomUpdate.state; final state = syncRoomUpdate.state;
if (state != null && state.isNotEmpty) { if (state != null && state.isNotEmpty) {
@ -2720,17 +2716,13 @@ class Client extends MatrixApi {
await receiptStateContent.update(e, room); await receiptStateContent.update(e, room);
} }
await _handleRoomEvents( final event = BasicRoomEvent(
room, type: LatestReceiptState.eventType,
[ roomId: room.id,
BasicRoomEvent( content: receiptStateContent.toJson(),
type: LatestReceiptState.eventType,
roomId: room.id,
content: receiptStateContent.toJson(),
),
],
EventUpdateType.accountData,
); );
await database?.storeRoomAccountData(event);
room.roomAccountData[event.type] = event;
} }
} }
@ -2739,7 +2731,7 @@ class Client extends MatrixApi {
Future<void> _handleRoomEvents( Future<void> _handleRoomEvents(
Room room, Room room,
List<BasicEvent> events, List<StrippedStateEvent> events,
EventUpdateType type, { EventUpdateType type, {
bool store = true, bool store = true,
}) async { }) async {
@ -2785,7 +2777,7 @@ class Client extends MatrixApi {
); );
// Any kind of member change? We should invalidate the profile then: // Any kind of member change? We should invalidate the profile then:
if (event is StrippedStateEvent && event.type == EventTypes.RoomMember) { if (event.type == EventTypes.RoomMember) {
final userId = event.stateKey; final userId = event.stateKey;
if (userId != null) { if (userId != null) {
// We do not re-request the profile here as this would lead to // We do not re-request the profile here as this would lead to
@ -2978,10 +2970,6 @@ class Client extends MatrixApi {
room.lastEvent = event; room.lastEvent = event;
break; break;
case EventUpdateType.accountData:
room.roomAccountData[eventUpdate.content['type']] =
BasicRoomEvent.fromJson(eventUpdate.content);
break;
case EventUpdateType.history: case EventUpdateType.history:
case EventUpdateType.decryptedTimelineQueue: case EventUpdateType.decryptedTimelineQueue:
break; break;

View File

@ -129,6 +129,8 @@ abstract class DatabaseApi {
Future storeAccountData(String type, Map<String, Object?> content); Future storeAccountData(String type, Map<String, Object?> content);
Future storeRoomAccountData(BasicRoomEvent event);
Future<Map<String, DeviceKeysList>> getUserDeviceKeys(Client client); Future<Map<String, DeviceKeysList>> getUserDeviceKeys(Client client);
Future<SSSSCache?> getSSSSCache(String type); Future<SSSSCache?> getSSSSCache(String type);

View File

@ -1095,6 +1095,15 @@ class HiveCollectionsDatabase extends DatabaseApi {
return; return;
} }
@override
Future<void> storeRoomAccountData(BasicRoomEvent event) async {
await _roomAccountDataBox.put(
TupleKey(event.roomId ?? '', event.type).toString(),
copyMap(event.toJson()),
);
return;
}
@override @override
Future<void> storeEventUpdate(EventUpdate eventUpdate, Client client) async { Future<void> storeEventUpdate(EventUpdate eventUpdate, Client client) async {
final tmpRoom = client.getRoomById(eventUpdate.roomID) ?? final tmpRoom = client.getRoomById(eventUpdate.roomID) ??
@ -1244,17 +1253,6 @@ class HiveCollectionsDatabase extends DatabaseApi {
await _roomStateBox.put(key, stateMap); await _roomStateBox.put(key, stateMap);
} }
} }
// Store a room account data event
if (eventUpdate.type == EventUpdateType.accountData) {
await _roomAccountDataBox.put(
TupleKey(
eventUpdate.roomID,
eventUpdate.content['type'],
).toString(),
eventUpdate.content,
);
}
} }
@override @override

View File

@ -1058,6 +1058,15 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
return; return;
} }
@override
Future<void> storeRoomAccountData(BasicRoomEvent event) async {
await _roomAccountDataBox.put(
TupleKey(event.roomId ?? '', event.type).toString(),
event.toJson(),
);
return;
}
@override @override
Future<void> storeEventUpdate(EventUpdate eventUpdate, Client client) async { Future<void> storeEventUpdate(EventUpdate eventUpdate, Client client) async {
final tmpRoom = client.getRoomById(eventUpdate.roomID) ?? final tmpRoom = client.getRoomById(eventUpdate.roomID) ??
@ -1214,17 +1223,6 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
await roomStateBox.put(key, eventUpdate.content); await roomStateBox.put(key, eventUpdate.content);
} }
} }
// Store a room account data event
if (eventUpdate.type == EventUpdateType.accountData) {
await _roomAccountDataBox.put(
TupleKey(
eventUpdate.roomID,
eventUpdate.content['type'],
).toString(),
eventUpdate.content,
);
}
} }
@override @override

View File

@ -26,9 +26,6 @@ enum EventUpdateType {
/// Messages that have been fetched when requesting past history /// Messages that have been fetched when requesting past history
history, history,
/// Updates to account data
accountData,
/// The state of an invite /// The state of an invite
inviteState, inviteState,

View File

@ -282,7 +282,7 @@ void main() {
final eventUpdateList = await eventUpdateListFuture; final eventUpdateList = await eventUpdateListFuture;
expect(eventUpdateList.length, 18); expect(eventUpdateList.length, 13);
expect(eventUpdateList[0].content['type'], 'm.room.member'); expect(eventUpdateList[0].content['type'], 'm.room.member');
expect(eventUpdateList[0].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[0].roomID, '!726s6s6q:example.com');
@ -308,28 +308,35 @@ void main() {
expect(eventUpdateList[5].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[5].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[5].type, EventUpdateType.timeline); expect(eventUpdateList[5].type, EventUpdateType.timeline);
expect(eventUpdateList[6].content['type'], LatestReceiptState.eventType); expect(eventUpdateList[6].content['type'], 'm.room.member');
expect(eventUpdateList[6].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[6].roomID, '!calls:example.com');
expect(eventUpdateList[6].type, EventUpdateType.accountData); expect(eventUpdateList[6].type, EventUpdateType.state);
expect(eventUpdateList[7].content['type'], 'm.tag'); expect(eventUpdateList[7].content['type'], 'm.room.member');
expect(eventUpdateList[7].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[7].roomID, '!calls:example.com');
expect(eventUpdateList[7].type, EventUpdateType.accountData); expect(eventUpdateList[7].type, EventUpdateType.state);
expect( expect(
eventUpdateList[8].content['type'], matrix
.getRoomById('!726s6s6q:example.com')
?.roomAccountData['org.example.custom.room.config']
?.type,
'org.example.custom.room.config', 'org.example.custom.room.config',
); );
expect(eventUpdateList[8].roomID, '!726s6s6q:example.com'); expect(
expect(eventUpdateList[8].type, EventUpdateType.accountData); matrix
.getRoomById('!726s6s6q:example.com')
expect(eventUpdateList[9].content['type'], 'm.room.member'); ?.roomAccountData[LatestReceiptState.eventType]
expect(eventUpdateList[9].roomID, '!calls:example.com'); ?.type,
expect(eventUpdateList[9].type, EventUpdateType.state); LatestReceiptState.eventType,
);
expect(eventUpdateList[10].content['type'], 'm.room.member'); expect(
expect(eventUpdateList[10].roomID, '!calls:example.com'); matrix
expect(eventUpdateList[10].type, EventUpdateType.state); .getRoomById('!726s6s6q:example.com')
?.roomAccountData['m.tag']
?.type,
'm.tag',
);
expect( expect(
matrix matrix

View File

@ -254,6 +254,14 @@ void main() {
client, client,
); );
await database.storeRoomAccountData(
BasicRoomEvent(
content: {'foo': 'bar'},
type: 'm.test',
roomId: roomid,
),
);
await database.storeEventUpdate( await database.storeEventUpdate(
EventUpdate( EventUpdate(
roomID: roomid, roomID: roomid,
@ -277,6 +285,8 @@ void main() {
expect(room?.name, 'start'); expect(room?.name, 'start');
expect(room?.roomAccountData['m.test']?.content, {'foo': 'bar'});
await database.storeEventUpdate( await database.storeEventUpdate(
EventUpdate( EventUpdate(
roomID: roomid, roomID: roomid,