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,33 +1783,36 @@ 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,
|
||||||
|
EventUpdateType type,
|
||||||
|
) async {
|
||||||
// The client must ignore any new m.room.encryption event to prevent
|
// The client must ignore any new m.room.encryption event to prevent
|
||||||
// man-in-the-middle attacks!
|
// man-in-the-middle attacks!
|
||||||
final room = getRoomById(roomID);
|
if ((event['type'] == EventTypes.Encryption &&
|
||||||
if (room == null ||
|
|
||||||
(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);
|
||||||
}
|
}
|
||||||
|
|
@ -1824,7 +1826,7 @@ class Client extends MatrixApi {
|
||||||
room.setState(user);
|
room.setState(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_updateRoomsByEventUpdate(update);
|
_updateRoomsByEventUpdate(room, update);
|
||||||
if (type != EventUpdateType.ephemeral) {
|
if (type != EventUpdateType.ephemeral) {
|
||||||
await database?.storeEventUpdate(update, this);
|
await database?.storeEventUpdate(update, this);
|
||||||
}
|
}
|
||||||
|
|
@ -1871,9 +1873,8 @@ class Client extends MatrixApi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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,11 +1885,9 @@ class Client extends MatrixApi {
|
||||||
? Membership.invite
|
? Membership.invite
|
||||||
: Membership.join;
|
: Membership.join;
|
||||||
|
|
||||||
// Does the chat already exist in the list rooms?
|
final room = found
|
||||||
if (!found && membership != Membership.leave) {
|
? rooms[roomIndex]
|
||||||
final position = membership == Membership.invite ? 0 : rooms.length;
|
: (chatUpdate is JoinedRoomUpdate
|
||||||
// Add the new chat to the list
|
|
||||||
final newRoom = chatUpdate is JoinedRoomUpdate
|
|
||||||
? Room(
|
? Room(
|
||||||
id: roomId,
|
id: roomId,
|
||||||
membership: membership,
|
membership: membership,
|
||||||
|
|
@ -1900,8 +1899,13 @@ class Client extends MatrixApi {
|
||||||
summary: chatUpdate.summary,
|
summary: chatUpdate.summary,
|
||||||
client: this,
|
client: this,
|
||||||
)
|
)
|
||||||
: Room(id: roomId, membership: membership, client: this);
|
: Room(id: roomId, membership: membership, client: this));
|
||||||
rooms.insert(position, newRoom);
|
|
||||||
|
// Does the chat already exist in the list rooms?
|
||||||
|
if (!found && membership != Membership.leave) {
|
||||||
|
final position = membership == Membership.invite ? 0 : rooms.length;
|
||||||
|
// Add the new chat to the list
|
||||||
|
rooms.insert(position, room);
|
||||||
}
|
}
|
||||||
// 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