From 3d59aae03464590ca6eaf1b6d8c438693e191ab5 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 3 Jul 2019 11:27:46 +0200 Subject: [PATCH 1/3] Fix calc displayname and add tests --- lib/src/Event.dart | 2 ++ lib/src/User.dart | 2 +- test/User_test.dart | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/src/Event.dart b/lib/src/Event.dart index 5067afb..b90dde3 100644 --- a/lib/src/Event.dart +++ b/lib/src/Event.dart @@ -137,6 +137,8 @@ class Event { print("jsonObj decode of event content failed: ${e.toString()}"); content = {}; } + else + content = {}; return Event( jsonObj["event_id"] ?? jsonObj["id"], diff --git a/lib/src/User.dart b/lib/src/User.dart index dedb1ca..cfd93c6 100644 --- a/lib/src/User.dart +++ b/lib/src/User.dart @@ -72,7 +72,7 @@ class User { /// Returns the displayname or the local part of the Matrix ID if the user /// has no displayname. - String calcDisplayname() => displayName.isEmpty + String calcDisplayname() => (displayName == null || displayName.isEmpty) ? id.replaceFirst("@", "").split(":")[0] : displayName; diff --git a/test/User_test.dart b/test/User_test.dart index 6991ff5..e6d0ae7 100644 --- a/test/User_test.dart +++ b/test/User_test.dart @@ -51,5 +51,14 @@ void main() { expect(user.powerLevel, powerLevel); expect(user.calcDisplayname(), displayName); }); + + test("calcDisplayname", () async { + final User user1 = User("@alice:example.com"); + final User user2 = User("@alice:example.com", displayName: "SuperAlice"); + final User user3 = User("@alice:example.com", displayName: ""); + expect(user1.calcDisplayname(), "alice"); + expect(user2.calcDisplayname(), "SuperAlice"); + expect(user3.calcDisplayname(), "alice"); + }); }); } From a70b82e88d7920f58179c0c44932b50256026747 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 3 Jul 2019 11:42:19 +0200 Subject: [PATCH 2/3] Get Sender from Store --- lib/src/Event.dart | 13 ++++++++----- lib/src/Store.dart | 13 ++++++++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/src/Event.dart b/lib/src/Event.dart index b90dde3..114c5aa 100644 --- a/lib/src/Event.dart +++ b/lib/src/Event.dart @@ -127,7 +127,8 @@ class Event { } /// Generate a new Event object from a json string, mostly a table row. - static Event fromJson(Map jsonObj, Room room) { + static Event fromJson(Map jsonObj, Room room, + {User senderUser, User stateKeyUser}) { Map content = jsonObj["content"]; if (content == null && jsonObj["content_json"] != null) @@ -137,14 +138,16 @@ class Event { print("jsonObj decode of event content failed: ${e.toString()}"); content = {}; } - else - content = {}; + else if (content == null) content = {}; + + if (senderUser == null) senderUser = User(jsonObj["sender"]); + if (stateKeyUser == null) stateKeyUser = User(jsonObj["state_key"]); return Event( jsonObj["event_id"] ?? jsonObj["id"], - User.fromJson(jsonObj, room), + senderUser, ChatTime(jsonObj["origin_server_ts"]), - stateKey: User(jsonObj["state_key"]), + stateKey: stateKeyUser, environment: jsonObj["type"], status: jsonObj["status"] ?? 2, content: content, diff --git a/lib/src/Store.dart b/lib/src/Store.dart index 7e6b9c6..08214b7 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -463,11 +463,16 @@ class Store { /// Returns a list of events for the given room and sets all participants. Future> getEventList(Room room) async { + List> memberRes = await db.rawQuery( + "SELECT * " + " FROM Users " + " WHERE users.chat_id=?", [room.id]); + Map userMap = {}; + for (num i = 0; i < memberRes.length; i++) + userMap[memberRes[i]["matrix_id"]] = User.fromJson(memberRes[i], room); + List> eventRes = await db.rawQuery( "SELECT * " + - " FROM Events events, User user " + + " FROM Events events " + " WHERE events.chat_id=?" + - " AND events.sender=user.matrix_id " + " GROUP BY events.id " + " ORDER BY origin_server_ts DESC", [room.id]); @@ -475,7 +480,9 @@ class Store { List eventList = []; for (num i = 0; i < eventRes.length; i++) - eventList.add(Event.fromJson(eventRes[i], room)); + eventList.add(Event.fromJson(eventRes[i], room, + senderUser: userMap[eventRes[i]["sender"]], + stateKeyUser: userMap[eventRes[i]["state_key"]])); return eventList; } From f349193ef229742057e29a580c5baf393ba85a12 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 3 Jul 2019 13:07:56 +0200 Subject: [PATCH 3/3] Fix fromJson func --- lib/src/Event.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/Event.dart b/lib/src/Event.dart index 114c5aa..cbf7b6b 100644 --- a/lib/src/Event.dart +++ b/lib/src/Event.dart @@ -140,7 +140,7 @@ class Event { } else if (content == null) content = {}; - if (senderUser == null) senderUser = User(jsonObj["sender"]); + if (senderUser == null) senderUser = User.fromJson(jsonObj, room); if (stateKeyUser == null) stateKeyUser = User(jsonObj["state_key"]); return Event(