From f9d0ec077160f55ffc275f711e26e4aa9487c109 Mon Sep 17 00:00:00 2001
From: Thulinma <jaron@vietors.com>
Date: Sun, 10 Oct 2021 18:35:05 +0200
Subject: [PATCH] Implement device get/put endpoints

---
 include/mtx/requests.hpp          | 10 ++++++++++
 include/mtxclient/http/client.hpp |  9 +++++++++
 lib/http/client.cpp               | 20 ++++++++++++++++++++
 lib/structs/requests.cpp          |  6 ++++++
 4 files changed, 45 insertions(+)

diff --git a/include/mtx/requests.hpp b/include/mtx/requests.hpp
index 9c392958f..c091a3a6f 100644
--- a/include/mtx/requests.hpp
+++ b/include/mtx/requests.hpp
@@ -127,6 +127,7 @@ to_json(json &obj, const AvatarUrl &request);
 //! Request payload for the `PUT /_matrix/client/r0/profile/{userId}/displayname` endpoint.
 struct DisplayName
 {
+    //! The new display name for this device. If not given, the display name is unchanged.
     std::string displayname;
 };
 
@@ -158,6 +159,15 @@ struct TypingNotification
 void
 to_json(json &obj, const TypingNotification &request);
 
+//! Request payload for the `PUT /_matrix/client/r0/devices/{deviceId}` endpoint.
+struct DeviceUpdate
+{
+    std::string display_name;
+};
+
+void
+to_json(json &obj, const DeviceUpdate &request);
+
 //! Request payload for the `PUT /_matrix/client/r0/directory/list/room/{roomId}` endpoint
 struct PublicRoomVisibility
 {
diff --git a/include/mtxclient/http/client.hpp b/include/mtxclient/http/client.hpp
index f52c65df7..eed38caa1 100644
--- a/include/mtxclient/http/client.hpp
+++ b/include/mtxclient/http/client.hpp
@@ -80,6 +80,7 @@ struct WellKnown;
 struct PublicRoomVisibility;
 struct PublicRooms;
 struct QueryDevices;
+struct Device;
 namespace backup {
 struct SessionBackup;
 struct RoomKeysBackup;
@@ -589,6 +590,14 @@ public:
     //! List devices
     void query_devices(Callback<mtx::responses::QueryDevices> cb);
 
+    //! Gets information on a single device, by device id.
+    void get_device(const std::string &device_id, Callback<mtx::responses::Device> cb);
+
+    //! Updates the display name of the given device id.
+    void set_device_name(const std::string &device_id,
+                         const std::string &display_name,
+                         ErrCallback callback);
+
     /////! Rename device
     // void rename_device(const mtx::requests::DeviceSigningUpload,
     //                           UIAHandler uia_handler,
diff --git a/lib/http/client.cpp b/lib/http/client.cpp
index dc5a9fb6c..a1a2c46dd 100644
--- a/lib/http/client.cpp
+++ b/lib/http/client.cpp
@@ -1095,6 +1095,26 @@ Client::query_devices(Callback<mtx::responses::QueryDevices> cb)
                                            RequestErr err) { cb(res, err); });
 }
 
+void
+Client::get_device(const std::string &device_id, Callback<mtx::responses::Device> cb)
+{
+    get<mtx::responses::Device>(
+      "/client/r0/devices/" + mtx::client::utils::url_encode(device_id),
+      [cb](const mtx::responses::Device &res, HeaderFields, RequestErr err) { cb(res, err); });
+}
+
+void
+Client::set_device_name(const std::string &device_id,
+                        const std::string &display_name,
+                        ErrCallback callback)
+{
+    mtx::requests::DeviceUpdate req;
+    req.display_name = display_name;
+
+    put<mtx::requests::DeviceUpdate>(
+      "/client/r0/devices/" + mtx::client::utils::url_encode(device_id), req, callback);
+}
+
 //
 // Encryption related endpoints
 //
diff --git a/lib/structs/requests.cpp b/lib/structs/requests.cpp
index 0c1e10935..a55dfccdd 100644
--- a/lib/structs/requests.cpp
+++ b/lib/structs/requests.cpp
@@ -153,6 +153,12 @@ to_json(json &obj, const PublicRooms &request)
     }
 }
 
+void
+to_json(json &obj, const DeviceUpdate &request)
+{
+    obj["display_name"] = request.display_name;
+}
+
 void
 to_json(json &obj, const SignedOneTimeKey &request)
 {
-- 
GitLab