Skip to content

Commit

Permalink
refactor: Update rooms by event not event update
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Jan 7, 2025
1 parent e7065af commit 5ac6c94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
33 changes: 21 additions & 12 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2797,7 +2797,7 @@ class Client extends MatrixApi {
room.setState(user);
}
}
_updateRoomsByEventUpdate(room, update);
_updateRoomsByEventUpdate(room, event, type);
if (store) {
await database?.storeEventUpdate(update, this);
}
Expand All @@ -2812,11 +2812,9 @@ class Client extends MatrixApi {
if (prevBatch != null &&
(type == EventUpdateType.timeline ||
type == EventUpdateType.decryptedTimelineQueue)) {
if ((update.content
.tryGet<String>('type')
?.startsWith(CallConstants.callEventsRegxp) ??
false)) {
final callEvent = Event.fromJson(update.content, room);
if (event is MatrixEvent &&
(event.type.startsWith(CallConstants.callEventsRegxp))) {
final callEvent = Event.fromMatrixEvent(event, room);
callEvents.add(callEvent);
}
}
Expand Down Expand Up @@ -2916,23 +2914,34 @@ class Client extends MatrixApi {
return room;
}

void _updateRoomsByEventUpdate(Room room, EventUpdate eventUpdate) {
if (eventUpdate.type == EventUpdateType.history) return;
void _updateRoomsByEventUpdate(
Room room,
StrippedStateEvent eventUpdate,
EventUpdateType type,
) {
if (type == EventUpdateType.history) return;

switch (eventUpdate.type) {
switch (type) {
case EventUpdateType.inviteState:
room.setState(StrippedStateEvent.fromJson(eventUpdate.content));
room.setState(eventUpdate);
break;
case EventUpdateType.state:
case EventUpdateType.timeline:
final event = Event.fromJson(eventUpdate.content, room);
if (eventUpdate is! MatrixEvent) {
Logs().wtf(
'Passed in a ${eventUpdate.runtimeType} with $type to _updateRoomsByEventUpdate(). This should never happen!',
);
assert(true);
return;
}
final event = Event.fromMatrixEvent(eventUpdate, room);

// Update the room state:
if (event.stateKey != null &&
(!room.partial || importantStateEvents.contains(event.type))) {
room.setState(event);
}
if (eventUpdate.type != EventUpdateType.timeline) break;
if (type != EventUpdateType.timeline) break;

// If last event is null or not a valid room preview event anyway,
// just use this:
Expand Down
2 changes: 1 addition & 1 deletion lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Event extends MatrixEvent {
status: status ??
eventStatusFromInt(
matrixEvent.unsigned
?.tryGet<int>('messageSendingStatusKey') ??
?.tryGet<int>(messageSendingStatusKey) ??
defaultStatus.intValue,
),
content: matrixEvent.content,
Expand Down

0 comments on commit 5ac6c94

Please sign in to comment.