From 027ad82ca5c71891400e64654985660f6beda59d Mon Sep 17 00:00:00 2001
From: Konstantinos Sideris <sideris.konstantin@gmail.com>
Date: Mon, 14 May 2018 14:01:41 +0300
Subject: [PATCH] Pass options to sync through a configuration struct

---
 examples/room_feed.cpp | 21 ++++++++++---
 src/client.cpp         | 18 +++++------
 src/client.hpp         | 20 +++++++++---
 tests/client_api.cpp   | 71 ++++++++++++++++++++----------------------
 tests/e2ee.cpp         | 12 +++----
 5 files changed, 77 insertions(+), 65 deletions(-)

diff --git a/examples/room_feed.cpp b/examples/room_feed.cpp
index 25f391ad9..ef9fb39a1 100644
--- a/examples/room_feed.cpp
+++ b/examples/room_feed.cpp
@@ -87,10 +87,13 @@ print_message(const TimelineEvent &event)
 void
 sync_handler(const mtx::responses::Sync &res, RequestErr err)
 {
+        SyncOpts opts;
+
         if (err) {
                 cout << "sync error:\n";
                 print_errors(err);
-                client->sync("", client->next_batch_token(), false, 30000, &sync_handler);
+                opts.since = client->next_batch_token();
+                client->sync(opts, &sync_handler);
                 return;
         }
 
@@ -99,28 +102,33 @@ sync_handler(const mtx::responses::Sync &res, RequestErr err)
                         print_message(msg);
         }
 
+        opts.since = res.next_batch;
         client->set_next_batch_token(res.next_batch);
-        client->sync("", client->next_batch_token(), false, 30000, &sync_handler);
+        client->sync(opts, &sync_handler);
 }
 
 // Callback to executed after the first (initial) /sync request completes.
 void
 initial_sync_handler(const mtx::responses::Sync &res, RequestErr err)
 {
+        SyncOpts opts;
+
         if (err) {
                 cout << "error during initial sync:\n";
                 print_errors(err);
 
                 if (err->status_code != boost::beast::http::status::ok) {
                         cout << "retrying initial sync ...\n";
-                        client->sync("", "", false, 0, &initial_sync_handler);
+                        opts.timeout = 0;
+                        client->sync(opts, &initial_sync_handler);
                 }
 
                 return;
         }
 
+        opts.since = res.next_batch;
         client->set_next_batch_token(res.next_batch);
-        client->sync("", client->next_batch_token(), false, 30000, &sync_handler);
+        client->sync(opts, &sync_handler);
 }
 
 void
@@ -132,8 +140,11 @@ login_handler(const mtx::responses::Login &res, RequestErr err)
         }
 
         cout << "Logged in as: " << res.user_id.to_string() << "\n";
+        SyncOpts opts;
+        opts.timeout = 0;
+
         client->set_access_token(res.access_token);
-        client->sync("", "", false, 0, &initial_sync_handler);
+        client->sync(opts, &initial_sync_handler);
 }
 
 int
diff --git a/src/client.cpp b/src/client.cpp
index 9d0c1bcaa..bba44d753 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -189,24 +189,20 @@ Client::invite_user(const mtx::identifiers::Room &room_id,
 }
 
 void
-Client::sync(const std::string &filter,
-             const std::string &since,
-             bool full_state,
-             uint16_t timeout,
-             Callback<nlohmann::json> callback)
+Client::sync(const SyncOpts &opts, Callback<nlohmann::json> callback)
 {
         std::map<std::string, std::string> params;
 
-        if (!filter.empty())
-                params.emplace("filter", filter);
+        if (!opts.filter.empty())
+                params.emplace("filter", opts.filter);
 
-        if (!since.empty())
-                params.emplace("since", since);
+        if (!opts.since.empty())
+                params.emplace("since", opts.since);
 
-        if (full_state)
+        if (opts.full_state)
                 params.emplace("full_state", "true");
 
-        params.emplace("timeout", std::to_string(timeout));
+        params.emplace("timeout", std::to_string(opts.timeout));
 
         get<nlohmann::json>("/client/r0/sync?" + utils::query_params(params),
                             [callback](const nlohmann::json &res, HeaderFields, RequestErr err) {
diff --git a/src/client.hpp b/src/client.hpp
index 5b75d2ee1..06d846ad6 100644
--- a/src/client.hpp
+++ b/src/client.hpp
@@ -48,6 +48,19 @@ using Callback = std::function<void(const Response &, RequestErr)>;
 template<class Response>
 using HeadersCallback = std::function<void(const Response &, HeaderFields, RequestErr)>;
 
+//! Sync configuration options.
+struct SyncOpts
+{
+        //! Filter to apply.
+        std::string filter;
+        //! Sync pagination token.
+        std::string since;
+        //! The amount of msecs to wait for long polling.
+        uint16_t timeout = 30'000;
+        //! Wheter to include the full state of each room.
+        bool full_state = false;
+};
+
 //! The main object that the user will interact.
 class Client : public std::enable_shared_from_this<Client>
 {
@@ -120,12 +133,9 @@ public:
         void invite_user(const mtx::identifiers::Room &room_id,
                          const std::string &user_id,
                          Callback<mtx::responses::RoomInvite> cb);
+
         //! Perform sync.
-        void sync(const std::string &filter,
-                  const std::string &since,
-                  bool full_state,
-                  uint16_t timeout,
-                  Callback<nlohmann::json> cb);
+        void sync(const SyncOpts &opts, Callback<nlohmann::json> cb);
 
         //! Paginate through room messages.
         void messages(const mtx::identifiers::Room &room_id,
diff --git a/tests/client_api.cpp b/tests/client_api.cpp
index 411323e04..49f75ae9b 100644
--- a/tests/client_api.cpp
+++ b/tests/client_api.cpp
@@ -578,12 +578,13 @@ TEST(ClientAPI, Sync)
           req, [mtx_client](const mtx::responses::CreateRoom &, RequestErr err) {
                   check_error(err);
 
-                  mtx_client->sync(
-                    "", "", false, 0, [](const mtx::responses::Sync &res, RequestErr err) {
-                            check_error(err);
-                            ASSERT_TRUE(res.rooms.join.size() > 0);
-                            ASSERT_TRUE(res.next_batch.size() > 0);
-                    });
+                  SyncOpts opts;
+                  opts.timeout = 0;
+                  mtx_client->sync(opts, [](const mtx::responses::Sync &res, RequestErr err) {
+                          check_error(err);
+                          ASSERT_TRUE(res.rooms.join.size() > 0);
+                          ASSERT_TRUE(res.next_batch.size() > 0);
+                  });
           });
 
         mtx_client->close();
@@ -627,10 +628,9 @@ TEST(ClientAPI, Typing)
                         const auto room_id = res.room_id.to_string();
                         atomic_bool can_continue(false);
 
-                        alice->sync("",
-                                    "",
-                                    false,
-                                    0,
+                        SyncOpts opts;
+                        opts.timeout = 0;
+                        alice->sync(opts,
                                     [room_id, &can_continue](const mtx::responses::Sync &res,
                                                              RequestErr err) {
                                             check_error(err);
@@ -650,12 +650,10 @@ TEST(ClientAPI, Typing)
                         alice->stop_typing(res.room_id, [alice, room_id](RequestErr err) {
                                 check_error(err);
 
+                                SyncOpts opts;
+                                opts.timeout = 0;
                                 alice->sync(
-                                  "",
-                                  "",
-                                  false,
-                                  0,
-                                  [room_id](const mtx::responses::Sync &res, RequestErr err) {
+                                  opts, [room_id](const mtx::responses::Sync &res, RequestErr err) {
                                           check_error(err);
                                           auto room = res.rooms.join.at(room_id);
                                           EXPECT_EQ(room.ephemeral.typing.size(), 0);
@@ -725,11 +723,10 @@ TEST(ClientAPI, SendMessages)
                             while (event_ids.size() != 2)
                                     sleep();
 
+                            SyncOpts opts;
+                            opts.timeout = 0;
                             alice->sync(
-                              "",
-                              "",
-                              false,
-                              0,
+                              opts,
                               [room_id, event_ids](const mtx::responses::Sync &res,
                                                    RequestErr err) {
                                       check_error(err);
@@ -812,12 +809,10 @@ TEST(ClientAPI, SendStateEvents)
                   while (event_ids.size() != 2)
                           sleep();
 
+                  SyncOpts opts;
+                  opts.timeout = 0;
                   alice->sync(
-                    "",
-                    "",
-                    false,
-                    0,
-                    [room_id, event_ids](const mtx::responses::Sync &res, RequestErr err) {
+                    opts, [room_id, event_ids](const mtx::responses::Sync &res, RequestErr err) {
                             check_error(err);
 
                             auto ids = get_event_ids<TimelineEvents>(
@@ -947,21 +942,19 @@ TEST(ClientAPI, ReadMarkers)
                 while (event_id.size() == 0)
                         sleep();
 
-                alice->sync("",
-                            "",
-                            false,
-                            0,
-                            [room_id, event_id](const mtx::responses::Sync &res, RequestErr err) {
-                                    check_error(err);
+                SyncOpts opts;
+                opts.timeout = 0;
+                alice->sync(
+                  opts, [room_id, event_id](const mtx::responses::Sync &res, RequestErr err) {
+                          check_error(err);
 
-                                    auto receipts =
-                                      res.rooms.join.at(room_id.to_string()).ephemeral.receipts;
-                                    EXPECT_EQ(receipts.size(), 1);
+                          auto receipts = res.rooms.join.at(room_id.to_string()).ephemeral.receipts;
+                          EXPECT_EQ(receipts.size(), 1);
 
-                                    auto users = receipts[event_id];
-                                    EXPECT_EQ(users.size(), 1);
-                                    ASSERT_TRUE(users["@alice:localhost"] > 0);
-                            });
+                          auto users = receipts[event_id];
+                          EXPECT_EQ(users.size(), 1);
+                          ASSERT_TRUE(users["@alice:localhost"] > 0);
+                  });
         });
 
         alice->close();
@@ -985,7 +978,9 @@ TEST(ClientAPI, SendToDevice)
         alice->send_to_device("m.test", body, [bob](RequestErr err) {
                 check_error(err);
 
-                bob->sync("", "", false, 0, [](const mtx::responses::Sync &res, RequestErr err) {
+                SyncOpts opts;
+                opts.timeout = 0;
+                bob->sync(opts, [](const mtx::responses::Sync &res, RequestErr err) {
                         check_error(err);
 
                         EXPECT_EQ(res.to_device.size(), 1);
diff --git a/tests/e2ee.cpp b/tests/e2ee.cpp
index 46361b455..0502efbc1 100644
--- a/tests/e2ee.cpp
+++ b/tests/e2ee.cpp
@@ -404,12 +404,10 @@ TEST(Encryption, KeyChanges)
                   check_error(err);
 
                   // Carl syncs to get the first next_batch token.
+                  SyncOpts opts;
+                  opts.timeout = 0;
                   carl->sync(
-                    "",
-                    "",
-                    false,
-                    0,
-                    [carl, carl_olm](const mtx::responses::Sync &res, RequestErr err) {
+                    opts, [carl, carl_olm](const mtx::responses::Sync &res, RequestErr err) {
                             check_error(err);
                             const auto next_batch_token = res.next_batch;
 
@@ -486,7 +484,9 @@ TEST(Encryption, EnableEncryption)
         while (responses != 2)
                 sleep();
 
-        carl->sync("", "", false, 0, [&joined_room](const Sync &res, RequestErr err) {
+        SyncOpts opts;
+        opts.timeout = 0;
+        carl->sync(opts, [&joined_room](const Sync &res, RequestErr err) {
                 check_error(err);
 
                 auto events = res.rooms.join.at(joined_room.to_string()).timeline.events;
-- 
GitLab