Hopefully fix room list sorting

This commit is contained in:
Sorunome 2020-05-26 06:39:51 +00:00 committed by Christian Pauly
parent 0854cb81af
commit 011e427056
2 changed files with 18 additions and 10 deletions

View file

@ -428,10 +428,10 @@ class Event {
/// Trys to decrypt this event and persists it in the database afterwards /// Trys to decrypt this event and persists it in the database afterwards
Future<Event> decryptAndStore([String updateType = 'timeline']) async { Future<Event> decryptAndStore([String updateType = 'timeline']) async {
final newEvent = decrypted; final newEvent = decrypted;
if (newEvent.type == EventTypes.Encrypted || room.client.database == null) { if (newEvent.type == EventTypes.Encrypted) {
return newEvent; // decryption failed or we don't have a database return newEvent; // decryption failed
} }
await room.client.database.storeEventUpdate( await room.client.database?.storeEventUpdate(
room.client.id, room.client.id,
EventUpdate( EventUpdate(
eventType: newEvent.typeKey, eventType: newEvent.typeKey,
@ -441,6 +441,9 @@ class Event {
sortOrder: newEvent.sortOrder, sortOrder: newEvent.sortOrder,
), ),
); );
if (updateType != 'history') {
room.setState(newEvent);
}
return newEvent; return newEvent;
} }

View file

@ -273,10 +273,7 @@ class Room {
Future<void> _tryAgainDecryptLastMessage() async { Future<void> _tryAgainDecryptLastMessage() async {
if (getState('m.room.encrypted') != null) { if (getState('m.room.encrypted') != null) {
final decrypted = await getState('m.room.encrypted').decryptAndStore(); await getState('m.room.encrypted').decryptAndStore();
if (decrypted.type != EventTypes.Encrypted) {
setState(decrypted);
}
} }
} }
@ -405,14 +402,22 @@ class Room {
String notificationSettings; String notificationSettings;
Event get lastEvent { Event get lastEvent {
var lastSortOrder = -1e32; // this bound to be small enough // as lastEvent calculation is based on the state events we unfortunately cannot
// use sortOrder here: With many state events we just know which ones are the
// newest ones, without knowing in which order they actually happened. As such,
// using the origin_server_ts is the best guess for this algorithm. While not
// perfect, it is only used for the room preview in the room list and sorting
// said room list, so it should be good enough.
var lastTime = DateTime.fromMillisecondsSinceEpoch(0);
var lastEvent = getState('m.room.message'); var lastEvent = getState('m.room.message');
if (lastEvent == null) { if (lastEvent == null) {
states.forEach((final String key, final entry) { states.forEach((final String key, final entry) {
if (!entry.containsKey('')) return; if (!entry.containsKey('')) return;
final Event state = entry['']; final Event state = entry[''];
if (state.sortOrder != null && state.sortOrder > lastSortOrder) { if (state.time != null &&
lastSortOrder = state.sortOrder; state.time.millisecondsSinceEpoch >
lastTime.millisecondsSinceEpoch) {
lastTime = state.time;
lastEvent = state; lastEvent = state;
} }
}); });