From 4e4f84754c8bb345506a62fb366bb58178c724c4 Mon Sep 17 00:00:00 2001
From: Konstantinos Sideris <sideris.konstantin@gmail.com>
Date: Thu, 7 Jun 2018 12:06:40 +0300
Subject: [PATCH] Add method for retrieving thumbnails

---
 include/mtxclient/http/client.hpp | 18 ++++++++++++-
 lib/http/client.cpp               | 16 ++++++++++++
 tests/media_api.cpp               | 43 ++++++++++++++++++-------------
 3 files changed, 58 insertions(+), 19 deletions(-)

diff --git a/include/mtxclient/http/client.hpp b/include/mtxclient/http/client.hpp
index eed8f2416..cc43a66d1 100644
--- a/include/mtxclient/http/client.hpp
+++ b/include/mtxclient/http/client.hpp
@@ -76,11 +76,23 @@ struct MessagesOpts
         uint16_t limit = 30;
 };
 
+//! Configuration for thumbnail retrieving.
+struct ThumbOpts
+{
+        //! The desired width of the thumbnail.
+        uint16_t width = 128;
+        //! The desired height of the thumbnail.
+        uint16_t height = 128;
+        //! The desired resizing method. One of: ["crop", "scale"]
+        std::string method = "crop";
+        //! A mxc URI which points to the content.
+        std::string mxc_url;
+};
+
 //! The main object that the user will interact.
 class Client : public std::enable_shared_from_this<Client>
 {
 public:
-        Client() = default;
         Client(const std::string &server = "", uint16_t port = 443);
 
         //! Wait for the client to close.
@@ -191,6 +203,10 @@ public:
                                          const std::string &content_type,
                                          const std::string &original_filename,
                                          RequestErr err)> cb);
+
+        //! Retrieve a thumbnail from the given mxc url.
+        void get_thumbnail(const ThumbOpts &opts, Callback<std::string> cb);
+
         //! Send typing notifications to the room.
         void start_typing(const std::string &room_id, uint64_t timeout, ErrCallback cb);
         //! Remove typing notifications from the room.
diff --git a/lib/http/client.cpp b/lib/http/client.cpp
index c5e72d14d..8c608fdd3 100644
--- a/lib/http/client.cpp
+++ b/lib/http/client.cpp
@@ -234,6 +234,22 @@ Client::download(const std::string &mxc_url,
         download(url.server, url.media_id, std::move(callback));
 }
 
+void
+Client::get_thumbnail(const ThumbOpts &opts, Callback<std::string> callback)
+{
+        std::map<std::string, std::string> params;
+        params.emplace("width", std::to_string(opts.width));
+        params.emplace("height", std::to_string(opts.height));
+        params.emplace("method", opts.method);
+
+        const auto mxc      = mtx::client::utils::parse_mxc_url(opts.mxc_url);
+        const auto api_path = "/media/r0/thumbnail/" + mxc.server + "/" + mxc.media_id + "?" +
+                              client::utils::query_params(params);
+        get<std::string>(
+          api_path,
+          [callback](const std::string &res, HeaderFields, RequestErr err) { callback(res, err); });
+}
+
 void
 Client::download(const std::string &server,
                  const std::string &media_id,
diff --git a/tests/media_api.cpp b/tests/media_api.cpp
index 2512eaec2..c07833b5d 100644
--- a/tests/media_api.cpp
+++ b/tests/media_api.cpp
@@ -104,24 +104,31 @@ TEST(MediaAPI, UploadImage)
 
                 const auto img = read_file("./fixtures/test.jpeg");
 
-                carl->upload(img,
-                             "image/jpeg",
-                             "test.jpeg",
-                             [carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
-                                     validate_upload(res, err);
-
-                                     carl->download(res.content_uri,
-                                                    [img](const string &data,
-                                                          const string &content_type,
-                                                          const string &original_filename,
-                                                          RequestErr err) {
-                                                            ASSERT_FALSE(err);
-                                                            EXPECT_EQ(data, img);
-                                                            EXPECT_EQ(content_type, "image/jpeg");
-                                                            EXPECT_EQ(original_filename,
-                                                                      "test.jpeg");
-                                                    });
-                             });
+                carl->upload(
+                  img,
+                  "image/jpeg",
+                  "test.jpeg",
+                  [carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
+                          validate_upload(res, err);
+
+                          ThumbOpts opts;
+                          opts.mxc_url = res.content_uri;
+                          carl->get_thumbnail(opts, [](const std::string &res, RequestErr err) {
+                                  ASSERT_FALSE(err);
+                                  ASSERT_FALSE(res.empty());
+                          });
+
+                          carl->download(res.content_uri,
+                                         [img](const string &data,
+                                               const string &content_type,
+                                               const string &original_filename,
+                                               RequestErr err) {
+                                                 ASSERT_FALSE(err);
+                                                 EXPECT_EQ(data, img);
+                                                 EXPECT_EQ(content_type, "image/jpeg");
+                                                 EXPECT_EQ(original_filename, "test.jpeg");
+                                         });
+                  });
         });
 
         carl->close();
-- 
GitLab