From ef83753079422ef51cb4a7a2afa79699feef5414 Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 26 Jun 2019 18:03:20 +0000 Subject: [PATCH] [Sending] Fix storage for sending events and add more tests. --- lib/src/Room.dart | 22 ++++++---------------- test/Timeline_test.dart | 32 ++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 0bfb75b..6e21e95 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -204,27 +204,17 @@ class Room { // On error, set status to -1 eventUpdate.content["status"] = -1; client.connection.onEvent.add(eventUpdate); - client.store?.db - ?.rawUpdate("UPDATE Events SET status=-1 WHERE id=?", [messageID]); + await client.store?.transaction(() { + client.store.storeEventUpdate(eventUpdate); + }); } else { final String newEventID = res["event_id"]; eventUpdate.content["status"] = 1; eventUpdate.content["id"] = newEventID; client.connection.onEvent.add(eventUpdate); - - // Store the result in database - if (client.store != null) { - final List> eventQuery = await client.store.db - .rawQuery("SELECT * FROM Events WHERE id=?", [newEventID]); - if (eventQuery.length > 0) { - client.store.db - .rawDelete("DELETE FROM Events WHERE id=?", [messageID]); - } else { - client.store.db.rawUpdate( - "UPDATE Events SET id=?, status=1 WHERE id=?", - [newEventID, messageID]); - } - } + await client.store?.transaction(() { + client.store.storeEventUpdate(eventUpdate); + }); return newEventID; } return null; diff --git a/test/Timeline_test.dart b/test/Timeline_test.dart index 7e6cf30..166858e 100644 --- a/test/Timeline_test.dart +++ b/test/Timeline_test.dart @@ -85,6 +85,7 @@ void main() { expect(updateCount, 2); expect(insertList, [0, 0]); + expect(insertList.length, timeline.events.length); expect(timeline.events.length, 2); expect(timeline.events[0].id, "1"); expect(timeline.events[0].sender.id, "@alice:example.com"); @@ -101,6 +102,7 @@ void main() { expect(updateCount, 4); expect(insertList, [0, 0, 0]); + expect(insertList.length, timeline.events.length); expect(timeline.events[0].content["txid"], "1234"); expect(timeline.events[0].id, "42"); expect(timeline.events[0].status, 1); @@ -123,19 +125,41 @@ void main() { expect(updateCount, 5); expect(insertList, [0, 0, 0]); + expect(insertList.length, timeline.events.length); expect(timeline.events[0].id, "42"); expect(timeline.events[0].status, 2); }); test("Send message with error", () async { + client.connection.onEvent.add(EventUpdate( + type: "timeline", + roomID: roomID, + eventType: "m.room.message", + content: { + "type": "m.room.message", + "content": {"msgtype": "m.text", "body": "Testcase"}, + "sender": "@alice:example.com", + "status": 0, + "id": "abc", + "origin_server_ts": testTimeStamp + })); + await new Future.delayed(new Duration(milliseconds: 50)); room.sendTextEvent("test", txid: "errortxid"); - + await new Future.delayed(new Duration(milliseconds: 50)); + room.sendTextEvent("test", txid: "errortxid2"); + await new Future.delayed(new Duration(milliseconds: 50)); + room.sendTextEvent("test", txid: "errortxid3"); await new Future.delayed(new Duration(milliseconds: 50)); - expect(updateCount, 7); - expect(insertList, [0, 0, 0, 0]); - expect(timeline.events[0].content["txid"], "errortxid"); + expect(updateCount, 12); + expect(insertList, [0, 0, 0, 0, 0, 0, 0]); + expect(insertList.length, timeline.events.length); + expect(timeline.events[0].content["txid"], "errortxid3"); expect(timeline.events[0].status, -1); + expect(timeline.events[1].content["txid"], "errortxid2"); + expect(timeline.events[1].status, -1); + expect(timeline.events[2].content["txid"], "errortxid"); + expect(timeline.events[2].status, -1); }); }); }