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, // If last event is null or not a valid room preview event anyway,
// just use this: // just use this:
if (room.lastEvent == null || if (room.lastEvent == null) {
!roomPreviewLastEvents.contains(room.lastEvent?.type)) {
room.lastEvent = event; room.lastEvent = event;
break; break;
} }

View File

@ -98,7 +98,7 @@ class Room {
summary: RoomSummary.fromJson(Map<String, dynamic>.from(json['summary'])), summary: RoomSummary.fromJson(Map<String, dynamic>.from(json['summary'])),
); );
if (json['last_event'] != null) { if (json['last_event'] != null) {
room._lastEvent = Event.fromJson(json['last_event'], room); room.lastEvent = Event.fromJson(json['last_event'], room);
} }
return room; return room;
} }
@ -365,33 +365,7 @@ class Room {
/// Wheither this is a direct chat or not /// Wheither this is a direct chat or not
bool get isDirectChat => directChatMatrixID != null; bool get isDirectChat => directChatMatrixID != null;
Event? _lastEvent; 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. /// Returns a list of all current typing users.
List<User> get typingUsers { List<User> get typingUsers {
@ -416,9 +390,8 @@ class Room {
required this.client, required this.client,
Map<String, BasicRoomEvent>? roomAccountData, Map<String, BasicRoomEvent>? roomAccountData,
RoomSummary? summary, RoomSummary? summary,
Event? lastEvent, this.lastEvent,
}) : roomAccountData = roomAccountData ?? <String, BasicRoomEvent>{}, }) : roomAccountData = roomAccountData ?? <String, BasicRoomEvent>{},
_lastEvent = lastEvent,
summary = summary ?? summary = summary ??
RoomSummary.fromJson({ RoomSummary.fromJson({
'm.joined_member_count': 0, 'm.joined_member_count': 0,

View File

@ -323,26 +323,31 @@ void main() {
.timeout(const Duration(seconds: 5)); .timeout(const Duration(seconds: 5));
client.rooms.add(room); client.rooms.add(room);
// we build up an encrypted message so that we can test if it successfully decrypted afterwards // we build up an encrypted message so that we can test if it successfully decrypted afterwards
room.setState( await client.handleSync(SyncUpdate(
Event( nextBatch: '',
senderId: '@test:example.com', rooms: RoomsUpdate(join: {
type: 'm.room.encrypted', room.id: JoinedRoomUpdate(
room: room, timeline: TimelineUpdate(events: [
eventId: '12345', Event(
originServerTs: DateTime.now(), senderId: '@test:example.com',
content: { type: 'm.room.encrypted',
'algorithm': AlgorithmTypes.megolmV1AesSha2, room: room,
'ciphertext': session.encrypt(json.encode({ eventId: '12345',
'type': 'm.room.message', originServerTs: DateTime.now(),
'content': {'msgtype': 'm.text', 'body': 'foxies'}, content: {
})), 'algorithm': AlgorithmTypes.megolmV1AesSha2,
'device_id': client.deviceID, 'ciphertext': session.encrypt(json.encode({
'sender_key': client.identityKey, 'type': 'm.room.message',
'session_id': sessionId, 'content': {'msgtype': 'm.text', 'body': 'foxies'},
}, })),
stateKey: '', 'device_id': client.deviceID,
), 'sender_key': client.identityKey,
); 'session_id': sessionId,
},
stateKey: '',
)
]))
})));
expect(room.lastEvent?.type, 'm.room.encrypted'); expect(room.lastEvent?.type, 'm.room.encrypted');
// set a payload... // set a payload...
var sessionPayload = <String, dynamic>{ var sessionPayload = <String, dynamic>{