[Event] Add functions to remove and resend an error event
This commit is contained in:
parent
93d904b1ae
commit
939e7e738a
|
@ -22,6 +22,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import 'package:famedlysdk/src/sync/EventUpdate.dart';
|
||||||
import 'package:famedlysdk/src/utils/ChatTime.dart';
|
import 'package:famedlysdk/src/utils/ChatTime.dart';
|
||||||
import 'package:famedlysdk/src/Client.dart';
|
import 'package:famedlysdk/src/Client.dart';
|
||||||
import './User.dart';
|
import './User.dart';
|
||||||
|
@ -148,6 +149,28 @@ class Event {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes this event if the status is < 1. This event will just be removed
|
||||||
|
/// from the database and the timelines.
|
||||||
|
Future<dynamic> remove() async {
|
||||||
|
if (status < 1) {
|
||||||
|
room.client.connection.onEvent.add(EventUpdate(
|
||||||
|
roomID: room.id,
|
||||||
|
type: "timeline",
|
||||||
|
eventType: environment,
|
||||||
|
content: {
|
||||||
|
"event_id": id,
|
||||||
|
"status": -2,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to send this event again. Only works with events of status -1.
|
||||||
|
Future<dynamic> sendAgain({String txid}) async {
|
||||||
|
if (status != -1) return;
|
||||||
|
remove();
|
||||||
|
room.sendTextEvent(text, txid: txid);
|
||||||
|
}
|
||||||
|
|
||||||
@Deprecated("Use [client.store.getEventList(Room room)] instead!")
|
@Deprecated("Use [client.store.getEventList(Room room)] instead!")
|
||||||
static Future<List<Event>> getEventList(Client matrix, Room room) async {
|
static Future<List<Event>> getEventList(Client matrix, Room room) async {
|
||||||
List<Event> eventList = await matrix.store.getEventList(room);
|
List<Event> eventList = await matrix.store.getEventList(room);
|
||||||
|
@ -176,6 +199,7 @@ enum EventTypes {
|
||||||
}
|
}
|
||||||
|
|
||||||
final Map<String, int> StatusTypes = {
|
final Map<String, int> StatusTypes = {
|
||||||
|
"REMOVE": -2,
|
||||||
"ERROR": -1,
|
"ERROR": -1,
|
||||||
"SENDING": 0,
|
"SENDING": 0,
|
||||||
"SENT": 1,
|
"SENT": 1,
|
||||||
|
|
|
@ -220,7 +220,10 @@ class Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the event in the database
|
// Save the event in the database
|
||||||
if ((status == 1 || status == -1) &&
|
if (status == -2) {
|
||||||
|
txn.rawDelete(
|
||||||
|
"DELETE FROM Events WHERE id=?", [eventContent["event_id"]]);
|
||||||
|
} else if ((status == 1 || status == -1) &&
|
||||||
eventUpdate.content["txid"] is String)
|
eventUpdate.content["txid"] is String)
|
||||||
txn.rawUpdate("UPDATE Events SET status=?, id=?, WHERE id=?",
|
txn.rawUpdate("UPDATE Events SET status=?, id=?, WHERE id=?",
|
||||||
[status, eventContent["event_id"], eventUpdate.content["txid"]]);
|
[status, eventContent["event_id"], eventUpdate.content["txid"]]);
|
||||||
|
|
|
@ -43,28 +43,37 @@ class Timeline {
|
||||||
sub ??= room.client.connection.onEvent.stream.listen(_handleEventUpdate);
|
sub ??= room.client.connection.onEvent.stream.listen(_handleEventUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int _findEvent({String event_id, String txid, String unsigned_txid}) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < events.length; i++) {
|
||||||
|
if (events[i].content.containsKey("txid") &&
|
||||||
|
events[i].content["txid"] == txid ||
|
||||||
|
events[i].id == event_id ||
|
||||||
|
(events[i].content["txid"] == unsigned_txid)) break;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
void _handleEventUpdate(EventUpdate eventUpdate) async {
|
void _handleEventUpdate(EventUpdate eventUpdate) async {
|
||||||
try {
|
try {
|
||||||
if (eventUpdate.roomID != room.id) return;
|
if (eventUpdate.roomID != room.id) return;
|
||||||
if (eventUpdate.type == "timeline" || eventUpdate.type == "history") {
|
if (eventUpdate.type == "timeline" || eventUpdate.type == "history") {
|
||||||
|
if (eventUpdate.content["status"] == -2) {
|
||||||
|
int i = _findEvent(event_id: eventUpdate.content["event_id"]);
|
||||||
|
if (i < events.length) events.removeAt(i);
|
||||||
|
}
|
||||||
// Is this event already in the timeline?
|
// Is this event already in the timeline?
|
||||||
if (eventUpdate.content["status"] == 1 ||
|
else if (eventUpdate.content["status"] == 1 ||
|
||||||
eventUpdate.content["status"] == -1 ||
|
eventUpdate.content["status"] == -1 ||
|
||||||
(eventUpdate.content.containsKey("unsigned") &&
|
(eventUpdate.content.containsKey("unsigned") &&
|
||||||
eventUpdate.content["unsigned"]["transaction_id"] is String)) {
|
eventUpdate.content["unsigned"]["transaction_id"] is String)) {
|
||||||
int i;
|
int i = _findEvent(
|
||||||
for (i = 0; i < events.length; i++) {
|
event_id: eventUpdate.content["event_id"],
|
||||||
if (events[i].content.containsKey("txid") &&
|
txid: eventUpdate.content["content"]["txid"],
|
||||||
events[i].content["txid"] ==
|
unsigned_txid: eventUpdate.content.containsKey("unsigned")
|
||||||
eventUpdate.content["content"]["txid"] ||
|
? eventUpdate.content["unsigned"]["transaction_id"]
|
||||||
events[i].id == eventUpdate.content["event_id"] ||
|
: null);
|
||||||
(eventUpdate.content.containsKey("unsigned") &&
|
|
||||||
eventUpdate.content["unsigned"]["transaction_id"]
|
|
||||||
is String &&
|
|
||||||
events[i].content["txid"] ==
|
|
||||||
eventUpdate.content["unsigned"]["transaction_id"]))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (i < events.length) {
|
if (i < events.length) {
|
||||||
events[i] = Event.fromJson(eventUpdate.content, room);
|
events[i] = Event.fromJson(eventUpdate.content, room);
|
||||||
}
|
}
|
||||||
|
|
|
@ -161,5 +161,31 @@ void main() {
|
||||||
expect(timeline.events[2].content["txid"], "errortxid");
|
expect(timeline.events[2].content["txid"], "errortxid");
|
||||||
expect(timeline.events[2].status, -1);
|
expect(timeline.events[2].status, -1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("Remove message", () async {
|
||||||
|
timeline.events[0].remove();
|
||||||
|
|
||||||
|
await new Future.delayed(new Duration(milliseconds: 50));
|
||||||
|
|
||||||
|
expect(updateCount, 13);
|
||||||
|
|
||||||
|
expect(insertList, [0, 0, 0, 0, 0, 0, 0]);
|
||||||
|
expect(timeline.events.length, 6);
|
||||||
|
expect(timeline.events[0].content["txid"], "errortxid2");
|
||||||
|
expect(timeline.events[0].status, -1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Resend message", () async {
|
||||||
|
timeline.events[0].sendAgain(txid: "1234");
|
||||||
|
|
||||||
|
await new Future.delayed(new Duration(milliseconds: 50));
|
||||||
|
|
||||||
|
expect(updateCount, 16);
|
||||||
|
|
||||||
|
expect(insertList, [0, 0, 0, 0, 0, 0, 0, 0]);
|
||||||
|
expect(timeline.events.length, 6);
|
||||||
|
expect(timeline.events[0].content["txid"], "1234");
|
||||||
|
expect(timeline.events[0].status, 1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue