refactor: Use handleRoomEvents method instead of handleEvent

This makes it easier to update
the state by a whole timeline.
This commit is contained in:
Christian Pauly 2022-06-28 09:24:32 +02:00
parent fc229c5715
commit c591c1d4b5
1 changed files with 69 additions and 77 deletions

View File

@ -1749,7 +1749,7 @@ class Client extends MatrixApi {
Future<void> _handleEphemerals(Room room, List<BasicRoomEvent> events) async { Future<void> _handleEphemerals(Room room, List<BasicRoomEvent> events) async {
for (final event in events) { for (final event in events) {
await _handleEvent(event, room, EventUpdateType.ephemeral); await _handleRoomEvents(room, [event], EventUpdateType.ephemeral);
// Receipt events are deltas between two states. We will create a // Receipt events are deltas between two states. We will create a
// fake room account data event for this and store the difference // fake room account data event for this and store the difference
@ -1783,7 +1783,7 @@ class Client extends MatrixApi {
} }
} }
event.content = receiptStateContent; event.content = receiptStateContent;
await _handleEvent(event, room, EventUpdateType.accountData); await _handleRoomEvents(room, [event], EventUpdateType.accountData);
} }
} }
} }
@ -1794,84 +1794,76 @@ class Client extends MatrixApi {
EventUpdateType type, EventUpdateType type,
) async { ) async {
for (final event in events) { for (final event in events) {
await _handleEvent(event, room, type); // The client must ignore any new m.room.encryption event to prevent
} // man-in-the-middle attacks!
} if ((event.type == EventTypes.Encryption &&
room.encrypted &&
Future<void> _handleEvent( event.content['algorithm'] !=
BasicEvent event, room.getState(EventTypes.Encryption)?.content['algorithm'])) {
Room room, return;
EventUpdateType type,
) async {
// The client must ignore any new m.room.encryption event to prevent
// man-in-the-middle attacks!
if ((event.type == EventTypes.Encryption &&
room.encrypted &&
event.content['algorithm'] !=
room.getState(EventTypes.Encryption)?.content['algorithm'])) {
return;
}
var update =
EventUpdate(roomID: room.id, type: type, content: event.toJson());
if (event.type == EventTypes.Encrypted && encryptionEnabled) {
update = await update.decrypt(room);
}
if (event.type == EventTypes.Message &&
!room.isDirectChat &&
database != null &&
event is MatrixEvent &&
room.getState(EventTypes.RoomMember, event.senderId) == null) {
// In order to correctly render room list previews we need to fetch the member from the database
final user = await database?.getUser(event.senderId, room);
if (user != null) {
room.setState(user);
} }
}
_updateRoomsByEventUpdate(room, update);
if (type != EventUpdateType.ephemeral) {
await database?.storeEventUpdate(update, this);
}
if (encryptionEnabled) {
await encryption?.handleEventUpdate(update);
}
onEvent.add(update);
final rawUnencryptedEvent = update.content; var update =
EventUpdate(roomID: room.id, type: type, content: event.toJson());
if (event.type == EventTypes.Encrypted && encryptionEnabled) {
update = await update.decrypt(room);
}
if (event.type == EventTypes.Message &&
!room.isDirectChat &&
database != null &&
event is MatrixEvent &&
room.getState(EventTypes.RoomMember, event.senderId) == null) {
// In order to correctly render room list previews we need to fetch the member from the database
final user = await database?.getUser(event.senderId, room);
if (user != null) {
room.setState(user);
}
}
_updateRoomsByEventUpdate(room, update);
if (type != EventUpdateType.ephemeral) {
await database?.storeEventUpdate(update, this);
}
if (encryptionEnabled) {
await encryption?.handleEventUpdate(update);
}
onEvent.add(update);
if (prevBatch != null && type == EventUpdateType.timeline) { final rawUnencryptedEvent = update.content;
if (rawUnencryptedEvent['type'] == EventTypes.CallInvite) {
onCallInvite.add(Event.fromJson(rawUnencryptedEvent, room)); if (prevBatch != null && type == EventUpdateType.timeline) {
} else if (rawUnencryptedEvent['type'] == EventTypes.CallHangup) { if (rawUnencryptedEvent['type'] == EventTypes.CallInvite) {
onCallHangup.add(Event.fromJson(rawUnencryptedEvent, room)); onCallInvite.add(Event.fromJson(rawUnencryptedEvent, room));
} else if (rawUnencryptedEvent['type'] == EventTypes.CallAnswer) { } else if (rawUnencryptedEvent['type'] == EventTypes.CallHangup) {
onCallAnswer.add(Event.fromJson(rawUnencryptedEvent, room)); onCallHangup.add(Event.fromJson(rawUnencryptedEvent, room));
} else if (rawUnencryptedEvent['type'] == EventTypes.CallCandidates) { } else if (rawUnencryptedEvent['type'] == EventTypes.CallAnswer) {
onCallCandidates.add(Event.fromJson(rawUnencryptedEvent, room)); onCallAnswer.add(Event.fromJson(rawUnencryptedEvent, room));
} else if (rawUnencryptedEvent['type'] == EventTypes.CallSelectAnswer) { } else if (rawUnencryptedEvent['type'] == EventTypes.CallCandidates) {
onCallSelectAnswer.add(Event.fromJson(rawUnencryptedEvent, room)); onCallCandidates.add(Event.fromJson(rawUnencryptedEvent, room));
} else if (rawUnencryptedEvent['type'] == EventTypes.CallReject) { } else if (rawUnencryptedEvent['type'] == EventTypes.CallSelectAnswer) {
onCallReject.add(Event.fromJson(rawUnencryptedEvent, room)); onCallSelectAnswer.add(Event.fromJson(rawUnencryptedEvent, room));
} else if (rawUnencryptedEvent['type'] == EventTypes.CallNegotiate) { } else if (rawUnencryptedEvent['type'] == EventTypes.CallReject) {
onCallNegotiate.add(Event.fromJson(rawUnencryptedEvent, room)); onCallReject.add(Event.fromJson(rawUnencryptedEvent, room));
} else if (rawUnencryptedEvent['type'] == EventTypes.CallReplaces) { } else if (rawUnencryptedEvent['type'] == EventTypes.CallNegotiate) {
onCallReplaces.add(Event.fromJson(rawUnencryptedEvent, room)); onCallNegotiate.add(Event.fromJson(rawUnencryptedEvent, room));
} else if (rawUnencryptedEvent['type'] == } else if (rawUnencryptedEvent['type'] == EventTypes.CallReplaces) {
EventTypes.CallAssertedIdentity || onCallReplaces.add(Event.fromJson(rawUnencryptedEvent, room));
rawUnencryptedEvent['type'] == } else if (rawUnencryptedEvent['type'] ==
EventTypes.CallAssertedIdentityPrefix) { EventTypes.CallAssertedIdentity ||
onAssertedIdentityReceived rawUnencryptedEvent['type'] ==
.add(Event.fromJson(rawUnencryptedEvent, room)); EventTypes.CallAssertedIdentityPrefix) {
} else if (rawUnencryptedEvent['type'] == onAssertedIdentityReceived
EventTypes.CallSDPStreamMetadataChanged || .add(Event.fromJson(rawUnencryptedEvent, room));
rawUnencryptedEvent['type'] == } else if (rawUnencryptedEvent['type'] ==
EventTypes.CallSDPStreamMetadataChangedPrefix) { EventTypes.CallSDPStreamMetadataChanged ||
onSDPStreamMetadataChangedReceived rawUnencryptedEvent['type'] ==
.add(Event.fromJson(rawUnencryptedEvent, room)); EventTypes.CallSDPStreamMetadataChangedPrefix) {
// TODO(duan): Only used (org.matrix.msc3401.call) during the current test, onSDPStreamMetadataChangedReceived
// need to add GroupCallPrefix in matrix_api_lite .add(Event.fromJson(rawUnencryptedEvent, room));
} else if (rawUnencryptedEvent['type'] == EventTypes.GroupCallPrefix) { // TODO(duan): Only used (org.matrix.msc3401.call) during the current test,
onGroupCallRequest.add(Event.fromJson(rawUnencryptedEvent, room)); // need to add GroupCallPrefix in matrix_api_lite
} else if (rawUnencryptedEvent['type'] == EventTypes.GroupCallPrefix) {
onGroupCallRequest.add(Event.fromJson(rawUnencryptedEvent, room));
}
} }
} }
} }