From 2802901eec3f1f0898ad3abd8a03191b6fc1804e Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 29 Aug 2019 12:28:50 +0200 Subject: [PATCH] [Room] Request history with users --- lib/src/Connection.dart | 8 ++++---- lib/src/Room.dart | 29 ++++++++++++++++++++++++++++- test/FakeMatrixApi.dart | 4 ++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/src/Connection.dart b/lib/src/Connection.dart index adc202e2..3253c211 100644 --- a/lib/src/Connection.dart +++ b/lib/src/Connection.dart @@ -52,9 +52,9 @@ class Connection { })); } - String get _syncFilters => '{"room":{"state":{"lazy_load_members":true}}}'; + static String syncFilters = '{"room":{"state":{"lazy_load_members":true}}}'; - String get _firstSyncFilters => + static String firstSyncFilters = '{"room":{"include_leave":true,"state":{"lazy_load_members":true}}}'; /// Handles the connection to the Matrix Homeserver. You can change this to a @@ -284,10 +284,10 @@ class Connection { Future _sync() async { if (client.isLogged() == false) return; - String action = "/client/r0/sync?filter=$_firstSyncFilters"; + String action = "/client/r0/sync?filter=$firstSyncFilters"; if (client.prevBatch != null) { - action = "/client/r0/sync?filter=$_syncFilters"; + action = "/client/r0/sync?filter=$syncFilters"; action += "&timeout=30000"; action += "&since=${client.prevBatch}"; } diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 1db278e8..9d39d79e 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -353,7 +353,7 @@ class Room { final dynamic resp = await client.connection.jsonRequest( type: HTTPType.GET, action: - "/client/r0/rooms/$id/messages?from=${prev_batch}&dir=b&limit=$historyCount"); + "/client/r0/rooms/$id/messages?from=${prev_batch}&dir=b&limit=$historyCount&filter=${Connection.syncFilters}"); if (resp is ErrorResponse) return; @@ -364,6 +364,33 @@ class Room { resp["chunk"].length > 0 && resp["end"] is String)) return; + if (resp["state"] is List) { + client.store?.transaction(() { + for (int i = 0; i < resp["state"].length; i++) { + EventUpdate eventUpdate = EventUpdate( + type: "state", + roomID: id, + eventType: resp["state"][i]["type"], + content: resp["state"][i], + ); + client.connection.onEvent.add(eventUpdate); + client.store.storeEventUpdate(eventUpdate); + } + return; + }); + if (client.store == null) { + for (int i = 0; i < resp["state"].length; i++) { + EventUpdate eventUpdate = EventUpdate( + type: "state", + roomID: id, + eventType: resp["state"][i]["type"], + content: resp["state"][i], + ); + client.connection.onEvent.add(eventUpdate); + } + } + } + List history = resp["chunk"]; client.store?.transaction(() { for (int i = 0; i < history.length; i++) { diff --git a/test/FakeMatrixApi.dart b/test/FakeMatrixApi.dart index 98a2ee50..315645d2 100644 --- a/test/FakeMatrixApi.dart +++ b/test/FakeMatrixApi.dart @@ -39,7 +39,7 @@ class FakeMatrixApi extends MockClient { method == "GET" ? request.url.queryParameters : request.body; var res = {}; - print("$method request to $action with Data: $data"); + //print("$method request to $action with Data: $data"); // Sync requests with timeout if (data is Map && data["timeout"] is String) { @@ -92,7 +92,7 @@ class FakeMatrixApi extends MockClient { "origin_server_ts": 1432735824653, "unsigned": {"age": 1234} }, - "/client/r0/rooms/!1234:example.com/messages?from=1234&dir=b&limit=100": + "/client/r0/rooms/!1234:example.com/messages?from=1234&dir=b&limit=100&filter=%7B%22room%22:%7B%22state%22:%7B%22lazy_load_members%22:true%7D%7D%7D": (var req) => { "start": "t47429-4392820_219380_26003_2265", "end": "t47409-4357353_219380_26003_2265",