Merge pull request #1868 from famedly/krille/do-not-update-invalid-last-event

fix: Do not update lastEvent with state events
This commit is contained in:
Krille-chan 2024-07-03 14:33:58 +02:00 committed by GitHub
commit b248d6382a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 52 deletions

View File

@ -2435,8 +2435,7 @@ class Client extends MatrixApi {
// If last event is null or not a valid room preview event anyway,
// just use this:
if (room.lastEvent == null ||
!roomPreviewLastEvents.contains(room.lastEvent?.type)) {
if (room.lastEvent == null) {
room.lastEvent = event;
break;
}

View File

@ -98,7 +98,7 @@ class Room {
summary: RoomSummary.fromJson(Map<String, dynamic>.from(json['summary'])),
);
if (json['last_event'] != null) {
room._lastEvent = Event.fromJson(json['last_event'], room);
room.lastEvent = Event.fromJson(json['last_event'], room);
}
return room;
}
@ -365,34 +365,8 @@ class Room {
/// Wheither this is a direct chat or not
bool get isDirectChat => directChatMatrixID != null;
Event? _lastEvent;
set lastEvent(Event? event) {
_lastEvent = event;
}
Event? get lastEvent {
if (_lastEvent != null) return _lastEvent;
// Just pick the newest state event as an indicator for when the last
// activity was in this room. This is better than nothing:
var lastTime = DateTime.fromMillisecondsSinceEpoch(0);
Event? lastEvent;
states.forEach((final String key, final entry) {
final state = entry[''];
if (state == null) return;
if (state is! Event) return;
if (state.originServerTs.millisecondsSinceEpoch >
lastTime.millisecondsSinceEpoch) {
lastTime = state.originServerTs;
lastEvent = state;
}
});
return lastEvent;
}
/// Returns a list of all current typing users.
List<User> get typingUsers {
final typingMxid = ephemerals['m.typing']?.content['user_ids'];
@ -416,9 +390,8 @@ class Room {
required this.client,
Map<String, BasicRoomEvent>? roomAccountData,
RoomSummary? summary,
Event? lastEvent,
this.lastEvent,
}) : roomAccountData = roomAccountData ?? <String, BasicRoomEvent>{},
_lastEvent = lastEvent,
summary = summary ??
RoomSummary.fromJson({
'm.joined_member_count': 0,

View File

@ -323,7 +323,11 @@ void main() {
.timeout(const Duration(seconds: 5));
client.rooms.add(room);
// we build up an encrypted message so that we can test if it successfully decrypted afterwards
room.setState(
await client.handleSync(SyncUpdate(
nextBatch: '',
rooms: RoomsUpdate(join: {
room.id: JoinedRoomUpdate(
timeline: TimelineUpdate(events: [
Event(
senderId: '@test:example.com',
type: 'm.room.encrypted',
@ -341,8 +345,9 @@ void main() {
'session_id': sessionId,
},
stateKey: '',
),
);
)
]))
})));
expect(room.lastEvent?.type, 'm.room.encrypted');
// set a payload...
var sessionPayload = <String, dynamic>{