refactor: Handle Room Account Data outside of Room Event Updates
This also makes sure that room account data does not get unnecessarily serialized and deserialized before storing it in the database. For this it changes the code flow at multiple places.
This commit is contained in:
parent
4c7bdb71cb
commit
2c42c12e72
|
|
@ -2559,7 +2559,7 @@ class Client extends MatrixApi {
|
|||
|
||||
final room = getRoomById(roomId);
|
||||
if (room != null) {
|
||||
final List<BasicEvent> events = [];
|
||||
final events = <Event>[];
|
||||
for (final event in _eventsPendingDecryption) {
|
||||
if (event.event.room.id != roomId) continue;
|
||||
if (!sessionIds.contains(
|
||||
|
|
@ -2571,7 +2571,7 @@ class Client extends MatrixApi {
|
|||
final decryptedEvent =
|
||||
await encryption!.decryptRoomEvent(event.event);
|
||||
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;
|
||||
if (accountData != null && accountData.isNotEmpty) {
|
||||
await _handleRoomEvents(
|
||||
room,
|
||||
accountData,
|
||||
EventUpdateType.accountData,
|
||||
);
|
||||
for (final event in accountData) {
|
||||
await database?.storeRoomAccountData(event);
|
||||
room.roomAccountData[event.type] = event;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2671,12 +2670,9 @@ class Client extends MatrixApi {
|
|||
}
|
||||
final accountData = syncRoomUpdate.accountData;
|
||||
if (accountData != null && accountData.isNotEmpty) {
|
||||
await _handleRoomEvents(
|
||||
room,
|
||||
accountData,
|
||||
EventUpdateType.accountData,
|
||||
store: false,
|
||||
);
|
||||
for (final event in accountData) {
|
||||
room.roomAccountData[event.type] = event;
|
||||
}
|
||||
}
|
||||
final state = syncRoomUpdate.state;
|
||||
if (state != null && state.isNotEmpty) {
|
||||
|
|
@ -2720,17 +2716,13 @@ class Client extends MatrixApi {
|
|||
await receiptStateContent.update(e, room);
|
||||
}
|
||||
|
||||
await _handleRoomEvents(
|
||||
room,
|
||||
[
|
||||
BasicRoomEvent(
|
||||
final event = BasicRoomEvent(
|
||||
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(
|
||||
Room room,
|
||||
List<BasicEvent> events,
|
||||
List<StrippedStateEvent> events,
|
||||
EventUpdateType type, {
|
||||
bool store = true,
|
||||
}) async {
|
||||
|
|
@ -2785,7 +2777,7 @@ class Client extends MatrixApi {
|
|||
);
|
||||
|
||||
// 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;
|
||||
if (userId != null) {
|
||||
// We do not re-request the profile here as this would lead to
|
||||
|
|
@ -2978,10 +2970,6 @@ class Client extends MatrixApi {
|
|||
room.lastEvent = event;
|
||||
|
||||
break;
|
||||
case EventUpdateType.accountData:
|
||||
room.roomAccountData[eventUpdate.content['type']] =
|
||||
BasicRoomEvent.fromJson(eventUpdate.content);
|
||||
break;
|
||||
case EventUpdateType.history:
|
||||
case EventUpdateType.decryptedTimelineQueue:
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -129,6 +129,8 @@ abstract class DatabaseApi {
|
|||
|
||||
Future storeAccountData(String type, Map<String, Object?> content);
|
||||
|
||||
Future storeRoomAccountData(BasicRoomEvent event);
|
||||
|
||||
Future<Map<String, DeviceKeysList>> getUserDeviceKeys(Client client);
|
||||
|
||||
Future<SSSSCache?> getSSSSCache(String type);
|
||||
|
|
|
|||
|
|
@ -1095,6 +1095,15 @@ class HiveCollectionsDatabase extends DatabaseApi {
|
|||
return;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> storeRoomAccountData(BasicRoomEvent event) async {
|
||||
await _roomAccountDataBox.put(
|
||||
TupleKey(event.roomId ?? '', event.type).toString(),
|
||||
copyMap(event.toJson()),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> storeEventUpdate(EventUpdate eventUpdate, Client client) async {
|
||||
final tmpRoom = client.getRoomById(eventUpdate.roomID) ??
|
||||
|
|
@ -1244,17 +1253,6 @@ class HiveCollectionsDatabase extends DatabaseApi {
|
|||
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
|
||||
|
|
|
|||
|
|
@ -1058,6 +1058,15 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
|||
return;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> storeRoomAccountData(BasicRoomEvent event) async {
|
||||
await _roomAccountDataBox.put(
|
||||
TupleKey(event.roomId ?? '', event.type).toString(),
|
||||
event.toJson(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> storeEventUpdate(EventUpdate eventUpdate, Client client) async {
|
||||
final tmpRoom = client.getRoomById(eventUpdate.roomID) ??
|
||||
|
|
@ -1214,17 +1223,6 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage {
|
|||
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
|
||||
|
|
|
|||
|
|
@ -26,9 +26,6 @@ enum EventUpdateType {
|
|||
/// Messages that have been fetched when requesting past history
|
||||
history,
|
||||
|
||||
/// Updates to account data
|
||||
accountData,
|
||||
|
||||
/// The state of an invite
|
||||
inviteState,
|
||||
|
||||
|
|
|
|||
|
|
@ -282,7 +282,7 @@ void main() {
|
|||
|
||||
final eventUpdateList = await eventUpdateListFuture;
|
||||
|
||||
expect(eventUpdateList.length, 18);
|
||||
expect(eventUpdateList.length, 13);
|
||||
|
||||
expect(eventUpdateList[0].content['type'], 'm.room.member');
|
||||
expect(eventUpdateList[0].roomID, '!726s6s6q:example.com');
|
||||
|
|
@ -308,28 +308,35 @@ void main() {
|
|||
expect(eventUpdateList[5].roomID, '!726s6s6q:example.com');
|
||||
expect(eventUpdateList[5].type, EventUpdateType.timeline);
|
||||
|
||||
expect(eventUpdateList[6].content['type'], LatestReceiptState.eventType);
|
||||
expect(eventUpdateList[6].roomID, '!726s6s6q:example.com');
|
||||
expect(eventUpdateList[6].type, EventUpdateType.accountData);
|
||||
expect(eventUpdateList[6].content['type'], 'm.room.member');
|
||||
expect(eventUpdateList[6].roomID, '!calls:example.com');
|
||||
expect(eventUpdateList[6].type, EventUpdateType.state);
|
||||
|
||||
expect(eventUpdateList[7].content['type'], 'm.tag');
|
||||
expect(eventUpdateList[7].roomID, '!726s6s6q:example.com');
|
||||
expect(eventUpdateList[7].type, EventUpdateType.accountData);
|
||||
expect(eventUpdateList[7].content['type'], 'm.room.member');
|
||||
expect(eventUpdateList[7].roomID, '!calls:example.com');
|
||||
expect(eventUpdateList[7].type, EventUpdateType.state);
|
||||
|
||||
expect(
|
||||
eventUpdateList[8].content['type'],
|
||||
matrix
|
||||
.getRoomById('!726s6s6q:example.com')
|
||||
?.roomAccountData['org.example.custom.room.config']
|
||||
?.type,
|
||||
'org.example.custom.room.config',
|
||||
);
|
||||
expect(eventUpdateList[8].roomID, '!726s6s6q:example.com');
|
||||
expect(eventUpdateList[8].type, EventUpdateType.accountData);
|
||||
|
||||
expect(eventUpdateList[9].content['type'], 'm.room.member');
|
||||
expect(eventUpdateList[9].roomID, '!calls:example.com');
|
||||
expect(eventUpdateList[9].type, EventUpdateType.state);
|
||||
|
||||
expect(eventUpdateList[10].content['type'], 'm.room.member');
|
||||
expect(eventUpdateList[10].roomID, '!calls:example.com');
|
||||
expect(eventUpdateList[10].type, EventUpdateType.state);
|
||||
expect(
|
||||
matrix
|
||||
.getRoomById('!726s6s6q:example.com')
|
||||
?.roomAccountData[LatestReceiptState.eventType]
|
||||
?.type,
|
||||
LatestReceiptState.eventType,
|
||||
);
|
||||
expect(
|
||||
matrix
|
||||
.getRoomById('!726s6s6q:example.com')
|
||||
?.roomAccountData['m.tag']
|
||||
?.type,
|
||||
'm.tag',
|
||||
);
|
||||
|
||||
expect(
|
||||
matrix
|
||||
|
|
|
|||
|
|
@ -254,6 +254,14 @@ void main() {
|
|||
client,
|
||||
);
|
||||
|
||||
await database.storeRoomAccountData(
|
||||
BasicRoomEvent(
|
||||
content: {'foo': 'bar'},
|
||||
type: 'm.test',
|
||||
roomId: roomid,
|
||||
),
|
||||
);
|
||||
|
||||
await database.storeEventUpdate(
|
||||
EventUpdate(
|
||||
roomID: roomid,
|
||||
|
|
@ -277,6 +285,8 @@ void main() {
|
|||
|
||||
expect(room?.name, 'start');
|
||||
|
||||
expect(room?.roomAccountData['m.test']?.content, {'foo': 'bar'});
|
||||
|
||||
await database.storeEventUpdate(
|
||||
EventUpdate(
|
||||
roomID: roomid,
|
||||
|
|
|
|||
Loading…
Reference in New Issue