refactor: Let _handleRoomEvents use BasicEvent
This is the first step to reduce the use of pure json in the sync method.
This commit is contained in:
parent
55417bcc25
commit
a1c6bc7551
|
|
@ -1666,29 +1666,29 @@ class Client extends MatrixApi {
|
||||||
progress: ++handledRooms / rooms.length,
|
progress: ++handledRooms / rooms.length,
|
||||||
));
|
));
|
||||||
final id = entry.key;
|
final id = entry.key;
|
||||||
final room = entry.value;
|
final syncRoomUpdate = entry.value;
|
||||||
|
|
||||||
await database?.storeRoomUpdate(id, room, this);
|
await database?.storeRoomUpdate(id, syncRoomUpdate, this);
|
||||||
_updateRoomsByRoomUpdate(id, room);
|
final room = _updateRoomsByRoomUpdate(id, syncRoomUpdate);
|
||||||
|
|
||||||
/// Handle now all room events and save them in the database
|
/// Handle now all room events and save them in the database
|
||||||
if (room is JoinedRoomUpdate) {
|
if (syncRoomUpdate is JoinedRoomUpdate) {
|
||||||
final state = room.state;
|
final state = syncRoomUpdate.state;
|
||||||
|
|
||||||
if (state != null && state.isNotEmpty) {
|
if (state != null && state.isNotEmpty) {
|
||||||
// TODO: This method seems to be comperatively slow for some updates
|
// TODO: This method seems to be comperatively slow for some updates
|
||||||
await _handleRoomEvents(
|
await _handleRoomEvents(
|
||||||
id,
|
room,
|
||||||
state.map((i) => i.toJson()).toList(),
|
state,
|
||||||
EventUpdateType.state,
|
EventUpdateType.state,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final timelineEvents = room.timeline?.events;
|
final timelineEvents = syncRoomUpdate.timeline?.events;
|
||||||
if (timelineEvents != null && timelineEvents.isNotEmpty) {
|
if (timelineEvents != null && timelineEvents.isNotEmpty) {
|
||||||
await _handleRoomEvents(
|
await _handleRoomEvents(
|
||||||
id,
|
room,
|
||||||
timelineEvents.map((i) => i.toJson()).toList(),
|
timelineEvents,
|
||||||
direction != null
|
direction != null
|
||||||
? (direction == Direction.b
|
? (direction == Direction.b
|
||||||
? EventUpdateType.history
|
? EventUpdateType.history
|
||||||
|
|
@ -1696,66 +1696,65 @@ class Client extends MatrixApi {
|
||||||
: EventUpdateType.timeline);
|
: EventUpdateType.timeline);
|
||||||
}
|
}
|
||||||
|
|
||||||
final ephemeral = room.ephemeral;
|
final ephemeral = syncRoomUpdate.ephemeral;
|
||||||
if (ephemeral != null && ephemeral.isNotEmpty) {
|
if (ephemeral != null && ephemeral.isNotEmpty) {
|
||||||
// TODO: This method seems to be comperatively slow for some updates
|
// TODO: This method seems to be comperatively slow for some updates
|
||||||
await _handleEphemerals(
|
await _handleEphemerals(
|
||||||
id, ephemeral.map((i) => i.toJson()).toList());
|
room,
|
||||||
|
ephemeral.map((i) => i.toJson()).toList(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final accountData = room.accountData;
|
final accountData = syncRoomUpdate.accountData;
|
||||||
if (accountData != null && accountData.isNotEmpty) {
|
if (accountData != null && accountData.isNotEmpty) {
|
||||||
await _handleRoomEvents(
|
await _handleRoomEvents(
|
||||||
id,
|
room,
|
||||||
accountData.map((i) => i.toJson()).toList(),
|
accountData,
|
||||||
EventUpdateType.accountData);
|
EventUpdateType.accountData,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (room is LeftRoomUpdate) {
|
if (syncRoomUpdate is LeftRoomUpdate) {
|
||||||
final timelineEvents = room.timeline?.events;
|
final timelineEvents = syncRoomUpdate.timeline?.events;
|
||||||
if (timelineEvents != null && timelineEvents.isNotEmpty) {
|
if (timelineEvents != null && timelineEvents.isNotEmpty) {
|
||||||
await _handleRoomEvents(
|
await _handleRoomEvents(
|
||||||
id,
|
room,
|
||||||
timelineEvents.map((i) => i.toJson()).toList(),
|
timelineEvents,
|
||||||
EventUpdateType.timeline,
|
EventUpdateType.timeline,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
final accountData = room.accountData;
|
final accountData = syncRoomUpdate.accountData;
|
||||||
if (accountData != null && accountData.isNotEmpty) {
|
if (accountData != null && accountData.isNotEmpty) {
|
||||||
await _handleRoomEvents(
|
await _handleRoomEvents(
|
||||||
id,
|
room,
|
||||||
accountData.map((i) => i.toJson()).toList(),
|
accountData,
|
||||||
EventUpdateType.accountData);
|
EventUpdateType.accountData,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
final state = room.state;
|
final state = syncRoomUpdate.state;
|
||||||
if (state != null && state.isNotEmpty) {
|
if (state != null && state.isNotEmpty) {
|
||||||
await _handleRoomEvents(
|
await _handleRoomEvents(room, state, EventUpdateType.state);
|
||||||
id, state.map((i) => i.toJson()).toList(), EventUpdateType.state);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (room is InvitedRoomUpdate) {
|
if (syncRoomUpdate is InvitedRoomUpdate) {
|
||||||
final state = room.inviteState;
|
final state = syncRoomUpdate.inviteState;
|
||||||
if (state != null && state.isNotEmpty) {
|
if (state != null && state.isNotEmpty) {
|
||||||
await _handleRoomEvents(id, state.map((i) => i.toJson()).toList(),
|
await _handleRoomEvents(room, state, EventUpdateType.inviteState);
|
||||||
EventUpdateType.inviteState);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _handleEphemerals(String id, List<dynamic> events) async {
|
Future<void> _handleEphemerals(Room room, List<dynamic> events) async {
|
||||||
for (final event in events) {
|
for (final event in events) {
|
||||||
await _handleEvent(event, id, EventUpdateType.ephemeral);
|
await _handleEvent(event, room, 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
|
||||||
// there.
|
// there.
|
||||||
if (event['type'] == 'm.receipt') {
|
if (event['type'] == 'm.receipt') {
|
||||||
var room = getRoomById(id);
|
|
||||||
room ??= Room(id: id, client: this);
|
|
||||||
|
|
||||||
final receiptStateContent =
|
final receiptStateContent =
|
||||||
room.roomAccountData['m.receipt']?.content ?? {};
|
room.roomAccountData['m.receipt']?.content ?? {};
|
||||||
for (final eventEntry in event['content'].entries) {
|
for (final eventEntry in event['content'].entries) {
|
||||||
|
|
@ -1784,96 +1783,98 @@ class Client extends MatrixApi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
event['content'] = receiptStateContent;
|
event['content'] = receiptStateContent;
|
||||||
await _handleEvent(event, id, EventUpdateType.accountData);
|
await _handleEvent(event, room, EventUpdateType.accountData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _handleRoomEvents(
|
Future<void> _handleRoomEvents(
|
||||||
String chat_id, List<dynamic> events, EventUpdateType type) async {
|
Room room,
|
||||||
|
List<BasicEvent> events,
|
||||||
|
EventUpdateType type,
|
||||||
|
) async {
|
||||||
for (final event in events) {
|
for (final event in events) {
|
||||||
await _handleEvent(event, chat_id, type);
|
await _handleEvent(event.toJson(), room, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _handleEvent(
|
Future<void> _handleEvent(
|
||||||
Map<String, dynamic> event, String roomID, EventUpdateType type) async {
|
Map<String, dynamic> event,
|
||||||
if (event['type'] is String && event['content'] is Map<String, dynamic>) {
|
Room room,
|
||||||
// The client must ignore any new m.room.encryption event to prevent
|
EventUpdateType type,
|
||||||
// man-in-the-middle attacks!
|
) async {
|
||||||
final room = getRoomById(roomID);
|
// The client must ignore any new m.room.encryption event to prevent
|
||||||
if (room == null ||
|
// man-in-the-middle attacks!
|
||||||
(event['type'] == EventTypes.Encryption &&
|
if ((event['type'] == EventTypes.Encryption &&
|
||||||
room.encrypted &&
|
room.encrypted &&
|
||||||
event['content']['algorithm'] !=
|
event['content']['algorithm'] !=
|
||||||
room.getState(EventTypes.Encryption)?.content['algorithm'])) {
|
room.getState(EventTypes.Encryption)?.content['algorithm'])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var update = EventUpdate(roomID: roomID, type: type, content: event);
|
var update = EventUpdate(roomID: room.id, type: type, content: event);
|
||||||
if (event['type'] == EventTypes.Encrypted && encryptionEnabled) {
|
if (event['type'] == EventTypes.Encrypted && encryptionEnabled) {
|
||||||
update = await update.decrypt(room);
|
update = await update.decrypt(room);
|
||||||
|
}
|
||||||
|
if (event['type'] == EventTypes.Message &&
|
||||||
|
!room.isDirectChat &&
|
||||||
|
database != null &&
|
||||||
|
room.getState(EventTypes.RoomMember, event['sender']) == null) {
|
||||||
|
// In order to correctly render room list previews we need to fetch the member from the database
|
||||||
|
final user = await database?.getUser(event['sender'], room);
|
||||||
|
if (user != null) {
|
||||||
|
room.setState(user);
|
||||||
}
|
}
|
||||||
if (event['type'] == EventTypes.Message &&
|
}
|
||||||
!room.isDirectChat &&
|
_updateRoomsByEventUpdate(room, update);
|
||||||
database != null &&
|
if (type != EventUpdateType.ephemeral) {
|
||||||
room.getState(EventTypes.RoomMember, event['sender']) == null) {
|
await database?.storeEventUpdate(update, this);
|
||||||
// In order to correctly render room list previews we need to fetch the member from the database
|
}
|
||||||
final user = await database?.getUser(event['sender'], room);
|
if (encryptionEnabled) {
|
||||||
if (user != null) {
|
await encryption?.handleEventUpdate(update);
|
||||||
room.setState(user);
|
}
|
||||||
}
|
onEvent.add(update);
|
||||||
}
|
|
||||||
_updateRoomsByEventUpdate(update);
|
|
||||||
if (type != EventUpdateType.ephemeral) {
|
|
||||||
await database?.storeEventUpdate(update, this);
|
|
||||||
}
|
|
||||||
if (encryptionEnabled) {
|
|
||||||
await encryption?.handleEventUpdate(update);
|
|
||||||
}
|
|
||||||
onEvent.add(update);
|
|
||||||
|
|
||||||
final rawUnencryptedEvent = update.content;
|
final rawUnencryptedEvent = update.content;
|
||||||
|
|
||||||
if (prevBatch != null && type == EventUpdateType.timeline) {
|
if (prevBatch != null && type == EventUpdateType.timeline) {
|
||||||
if (rawUnencryptedEvent['type'] == EventTypes.CallInvite) {
|
if (rawUnencryptedEvent['type'] == EventTypes.CallInvite) {
|
||||||
onCallInvite.add(Event.fromJson(rawUnencryptedEvent, room));
|
onCallInvite.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
} else if (rawUnencryptedEvent['type'] == EventTypes.CallHangup) {
|
} else if (rawUnencryptedEvent['type'] == EventTypes.CallHangup) {
|
||||||
onCallHangup.add(Event.fromJson(rawUnencryptedEvent, room));
|
onCallHangup.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
} else if (rawUnencryptedEvent['type'] == EventTypes.CallAnswer) {
|
} else if (rawUnencryptedEvent['type'] == EventTypes.CallAnswer) {
|
||||||
onCallAnswer.add(Event.fromJson(rawUnencryptedEvent, room));
|
onCallAnswer.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
} else if (rawUnencryptedEvent['type'] == EventTypes.CallCandidates) {
|
} else if (rawUnencryptedEvent['type'] == EventTypes.CallCandidates) {
|
||||||
onCallCandidates.add(Event.fromJson(rawUnencryptedEvent, room));
|
onCallCandidates.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
} else if (rawUnencryptedEvent['type'] == EventTypes.CallSelectAnswer) {
|
} else if (rawUnencryptedEvent['type'] == EventTypes.CallSelectAnswer) {
|
||||||
onCallSelectAnswer.add(Event.fromJson(rawUnencryptedEvent, room));
|
onCallSelectAnswer.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
} else if (rawUnencryptedEvent['type'] == EventTypes.CallReject) {
|
} else if (rawUnencryptedEvent['type'] == EventTypes.CallReject) {
|
||||||
onCallReject.add(Event.fromJson(rawUnencryptedEvent, room));
|
onCallReject.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
} else if (rawUnencryptedEvent['type'] == EventTypes.CallNegotiate) {
|
} else if (rawUnencryptedEvent['type'] == EventTypes.CallNegotiate) {
|
||||||
onCallNegotiate.add(Event.fromJson(rawUnencryptedEvent, room));
|
onCallNegotiate.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
} else if (rawUnencryptedEvent['type'] == EventTypes.CallReplaces) {
|
} else if (rawUnencryptedEvent['type'] == EventTypes.CallReplaces) {
|
||||||
onCallReplaces.add(Event.fromJson(rawUnencryptedEvent, room));
|
onCallReplaces.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
} else if (rawUnencryptedEvent['type'] ==
|
} else if (rawUnencryptedEvent['type'] ==
|
||||||
EventTypes.CallAssertedIdentity ||
|
EventTypes.CallAssertedIdentity ||
|
||||||
rawUnencryptedEvent['type'] ==
|
rawUnencryptedEvent['type'] ==
|
||||||
EventTypes.CallAssertedIdentityPrefix) {
|
EventTypes.CallAssertedIdentityPrefix) {
|
||||||
onAssertedIdentityReceived
|
onAssertedIdentityReceived
|
||||||
.add(Event.fromJson(rawUnencryptedEvent, room));
|
.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
} else if (rawUnencryptedEvent['type'] ==
|
} else if (rawUnencryptedEvent['type'] ==
|
||||||
EventTypes.CallSDPStreamMetadataChanged ||
|
EventTypes.CallSDPStreamMetadataChanged ||
|
||||||
rawUnencryptedEvent['type'] ==
|
rawUnencryptedEvent['type'] ==
|
||||||
EventTypes.CallSDPStreamMetadataChangedPrefix) {
|
EventTypes.CallSDPStreamMetadataChangedPrefix) {
|
||||||
onSDPStreamMetadataChangedReceived
|
onSDPStreamMetadataChangedReceived
|
||||||
.add(Event.fromJson(rawUnencryptedEvent, room));
|
.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
// TODO(duan): Only used (org.matrix.msc3401.call) during the current test,
|
// TODO(duan): Only used (org.matrix.msc3401.call) during the current test,
|
||||||
// need to add GroupCallPrefix in matrix_api_lite
|
// need to add GroupCallPrefix in matrix_api_lite
|
||||||
} else if (rawUnencryptedEvent['type'] == EventTypes.GroupCallPrefix) {
|
} else if (rawUnencryptedEvent['type'] == EventTypes.GroupCallPrefix) {
|
||||||
onGroupCallRequest.add(Event.fromJson(rawUnencryptedEvent, room));
|
onGroupCallRequest.add(Event.fromJson(rawUnencryptedEvent, room));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _updateRoomsByRoomUpdate(String roomId, SyncRoomUpdate chatUpdate) {
|
Room _updateRoomsByRoomUpdate(String roomId, SyncRoomUpdate chatUpdate) {
|
||||||
// Update the chat list item.
|
// Update the chat list item.
|
||||||
// Search the room in the rooms
|
// Search the room in the rooms
|
||||||
final roomIndex = rooms.indexWhere((r) => r.id == roomId);
|
final roomIndex = rooms.indexWhere((r) => r.id == roomId);
|
||||||
|
|
@ -1884,24 +1885,27 @@ class Client extends MatrixApi {
|
||||||
? Membership.invite
|
? Membership.invite
|
||||||
: Membership.join;
|
: Membership.join;
|
||||||
|
|
||||||
|
final room = found
|
||||||
|
? rooms[roomIndex]
|
||||||
|
: (chatUpdate is JoinedRoomUpdate
|
||||||
|
? Room(
|
||||||
|
id: roomId,
|
||||||
|
membership: membership,
|
||||||
|
prev_batch: chatUpdate.timeline?.prevBatch,
|
||||||
|
highlightCount:
|
||||||
|
chatUpdate.unreadNotifications?.highlightCount ?? 0,
|
||||||
|
notificationCount:
|
||||||
|
chatUpdate.unreadNotifications?.notificationCount ?? 0,
|
||||||
|
summary: chatUpdate.summary,
|
||||||
|
client: this,
|
||||||
|
)
|
||||||
|
: Room(id: roomId, membership: membership, client: this));
|
||||||
|
|
||||||
// Does the chat already exist in the list rooms?
|
// Does the chat already exist in the list rooms?
|
||||||
if (!found && membership != Membership.leave) {
|
if (!found && membership != Membership.leave) {
|
||||||
final position = membership == Membership.invite ? 0 : rooms.length;
|
final position = membership == Membership.invite ? 0 : rooms.length;
|
||||||
// Add the new chat to the list
|
// Add the new chat to the list
|
||||||
final newRoom = chatUpdate is JoinedRoomUpdate
|
rooms.insert(position, room);
|
||||||
? Room(
|
|
||||||
id: roomId,
|
|
||||||
membership: membership,
|
|
||||||
prev_batch: chatUpdate.timeline?.prevBatch,
|
|
||||||
highlightCount:
|
|
||||||
chatUpdate.unreadNotifications?.highlightCount ?? 0,
|
|
||||||
notificationCount:
|
|
||||||
chatUpdate.unreadNotifications?.notificationCount ?? 0,
|
|
||||||
summary: chatUpdate.summary,
|
|
||||||
client: this,
|
|
||||||
)
|
|
||||||
: Room(id: roomId, membership: membership, client: this);
|
|
||||||
rooms.insert(position, newRoom);
|
|
||||||
}
|
}
|
||||||
// If the membership is "leave" then remove the item and stop here
|
// If the membership is "leave" then remove the item and stop here
|
||||||
else if (found && membership == Membership.leave) {
|
else if (found && membership == Membership.leave) {
|
||||||
|
|
@ -1940,14 +1944,12 @@ class Client extends MatrixApi {
|
||||||
runInRoot(rooms[roomIndex].requestHistory);
|
runInRoot(rooms[roomIndex].requestHistory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return room;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _updateRoomsByEventUpdate(EventUpdate eventUpdate) {
|
void _updateRoomsByEventUpdate(Room room, EventUpdate eventUpdate) {
|
||||||
if (eventUpdate.type == EventUpdateType.history) return;
|
if (eventUpdate.type == EventUpdateType.history) return;
|
||||||
|
|
||||||
final room = getRoomById(eventUpdate.roomID);
|
|
||||||
if (room == null) return;
|
|
||||||
|
|
||||||
switch (eventUpdate.type) {
|
switch (eventUpdate.type) {
|
||||||
case EventUpdateType.timeline:
|
case EventUpdateType.timeline:
|
||||||
case EventUpdateType.state:
|
case EventUpdateType.state:
|
||||||
|
|
|
||||||
|
|
@ -239,7 +239,7 @@ void main() {
|
||||||
|
|
||||||
final eventUpdateList = await eventUpdateListFuture;
|
final eventUpdateList = await eventUpdateListFuture;
|
||||||
|
|
||||||
expect(eventUpdateList.length, 14);
|
expect(eventUpdateList.length, 18);
|
||||||
|
|
||||||
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');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue