diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 6e21e959..8b0db81c 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -171,6 +171,8 @@ class Room { Future sendTextEvent(String message, {String txid = null}) async { final String type = "m.room.message"; + + // Create new transaction id String messageID; final int now = DateTime.now().millisecondsSinceEpoch; if (txid == null) { @@ -182,7 +184,7 @@ class Room { EventUpdate eventUpdate = EventUpdate(type: "timeline", roomID: id, eventType: type, content: { "type": type, - "id": null, + "id": messageID, "sender": client.userID, "status": 0, "origin_server_ts": now, diff --git a/lib/src/Store.dart b/lib/src/Store.dart index 50b495d3..0de72177 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -220,19 +220,29 @@ class Store { } // Save the event in the database + if ((status == 1 || status == -1) && + eventUpdate.content["txid"] is String) + txn.rawUpdate("UPDATE Events SET status=?, id=?, WHERE id=?", + [status, eventContent["id"], eventUpdate.content["txid"]]); + else + txn.rawInsert( + "INSERT OR REPLACE INTO Events VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", [ + eventContent["event_id"], + chat_id, + eventContent["origin_server_ts"], + eventContent["sender"], + state_key, + eventContent["content"]["body"], + eventContent["type"], + json.encode(eventContent["content"]), + status + ]); - txn.rawInsert( - "INSERT OR REPLACE INTO Events VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", [ - eventContent["event_id"], - chat_id, - eventContent["origin_server_ts"], - eventContent["sender"], - state_key, - eventContent["content"]["body"], - eventContent["type"], - json.encode(eventContent["content"]), - status - ]); + // Is there a transaction id? Then delete the event with this id. + if (eventUpdate.content.containsKey("unsigned") && + eventUpdate.content["unsigned"]["transaction_id"] is String) + txn.rawDelete("DELETE FROM Events WHERE id=?", + [eventUpdate.content["unsigned"]["transaction_id"]]); } if (type == "history") return null;