fix: Mark unsent events as failed

In Moor this was implemented but forgotten in Hive.
Events with status 0 (not sent yet) should be marked as failed on restart.
In fact they should be marked as failed if older than 1 minute. To not have a big startup job which iterates through all events in the database
we just do a time check when opening a room where we iterate through all events anyway.
The new implementation is now in the constructor of the Event and therefore
independent from the database implementation.
This commit is contained in:
Christian Pauly 2021-07-05 07:59:43 +02:00
parent e0d3994a46
commit ebc9be250d
2 changed files with 27 additions and 5 deletions

View File

@ -116,6 +116,28 @@ class Event extends MatrixEvent {
}
this.stateKey = stateKey;
this.originServerTs = originServerTs;
// Mark event as failed to send if status is `sending` and event is older
// than the timeout. This should not happen with the deprecated Moor
// database!
if (status == 0 && room?.client?.database != null) {
// Age of this event in milliseconds
final age = DateTime.now().millisecondsSinceEpoch -
originServerTs.millisecondsSinceEpoch;
if (age > room.client.sendMessageTimeoutSeconds * 1000) {
// Update this event in database and open timelines
final json = toJson();
json['unsigned'] ??= <String, dynamic>{};
json['unsigned'][messageSendingStatusKey] = -1;
room.client.handleSync(SyncUpdate()
..rooms = (RoomsUpdate()
..join = (<String, JoinedRoomUpdate>{}..[room.id] =
(JoinedRoomUpdate()
..timeline = (TimelineUpdate()
..events = [MatrixEvent.fromJson(json)])))));
}
}
}
static Map<String, dynamic> getMapFromPayload(dynamic payload) {

View File

@ -346,7 +346,7 @@ void main() {
'sender': '@alice:example.com',
'status': 0,
'event_id': 'will-fail',
'origin_server_ts': testTimeStamp
'origin_server_ts': DateTime.now().millisecondsSinceEpoch,
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
@ -380,7 +380,7 @@ void main() {
'sender': '@alice:example.com',
'status': 0,
'event_id': 'transaction',
'origin_server_ts': testTimeStamp
'origin_server_ts': DateTime.now().millisecondsSinceEpoch,
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
@ -430,7 +430,7 @@ void main() {
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'event_id': 'transaction',
'origin_server_ts': testTimeStamp,
'origin_server_ts': DateTime.now().millisecondsSinceEpoch,
'unsigned': {
messageSendingStatusKey: 0,
'transaction_id': 'transaction',
@ -488,7 +488,7 @@ void main() {
'sender': '@alice:example.com',
'status': 0,
'event_id': 'transaction',
'origin_server_ts': testTimeStamp
'origin_server_ts': DateTime.now().millisecondsSinceEpoch,
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));
@ -537,7 +537,7 @@ void main() {
'sender': '@alice:example.com',
'status': 0,
'event_id': 'transaction',
'origin_server_ts': testTimeStamp
'origin_server_ts': DateTime.now().millisecondsSinceEpoch,
},
sortOrder: room.newSortOrder));
await Future.delayed(Duration(milliseconds: 50));