Skip to content
Snippets Groups Projects
Commit a94839b6 authored by Thulinma's avatar Thulinma
Browse files

Device query endpoint

parent 06372bcd
No related branches found
No related tags found
No related merge requests found
...@@ -232,6 +232,7 @@ target_sources(matrix_client ...@@ -232,6 +232,7 @@ target_sources(matrix_client
lib/structs/responses/common.cpp lib/structs/responses/common.cpp
lib/structs/responses/create_room.cpp lib/structs/responses/create_room.cpp
lib/structs/responses/crypto.cpp lib/structs/responses/crypto.cpp
lib/structs/responses/device.cpp
lib/structs/responses/empty.cpp lib/structs/responses/empty.cpp
lib/structs/responses/groups.cpp lib/structs/responses/groups.cpp
lib/structs/responses/login.cpp lib/structs/responses/login.cpp
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "responses/create_room.hpp" #include "responses/create_room.hpp"
#include "responses/crypto.hpp" #include "responses/crypto.hpp"
#include "responses/device.hpp"
#include "responses/empty.hpp" #include "responses/empty.hpp"
#include "responses/groups.hpp" #include "responses/groups.hpp"
#include "responses/login.hpp" #include "responses/login.hpp"
......
#pragma once
/// @file
/// @brief device related endpoints.
#if __has_include(<nlohmann/json_fwd.hpp>)
#include <nlohmann/json_fwd.hpp>
#else
#include <nlohmann/json.hpp>
#endif
#include "mtx/common.hpp"
#include "mtx/lightweight_error.hpp"
#include <string>
#include <vector>
namespace mtx {
namespace responses {
struct Device
{
//! **Required.** Identifier of this device.
std::string device_id;
//! Display name set by the user for this device. Absent if no name has been set.
std::string display_name;
//! The IP address where this device was last seen. (May be a few minutes out of date, for
//! efficiency reasons).
std::string last_seen_ip;
//! The timestamp (in milliseconds since the unix epoch) when this devices was last seen. (May
//! be a few minutes out of date, for efficiency reasons).
size_t last_seen_ts;
};
void
from_json(const nlohmann::json &obj, Device &res);
//! Response from the `GET /_matrix/client/r0/devices` endpoint.
struct QueryDevices
{
//! Gets information about all devices for the current user.
//! A list of all registered devices for this user.
//
std::vector<Device> devices;
};
void
from_json(const nlohmann::json &obj, QueryDevices &response);
}
}
...@@ -79,6 +79,7 @@ struct Versions; ...@@ -79,6 +79,7 @@ struct Versions;
struct WellKnown; struct WellKnown;
struct PublicRoomVisibility; struct PublicRoomVisibility;
struct PublicRooms; struct PublicRooms;
struct QueryDevices;
namespace backup { namespace backup {
struct SessionBackup; struct SessionBackup;
struct RoomKeysBackup; struct RoomKeysBackup;
...@@ -581,6 +582,18 @@ public: ...@@ -581,6 +582,18 @@ public:
Callback<nlohmann::json> cb); Callback<nlohmann::json> cb);
void add_room_to_group(const std::string &room_id, const std::string &group_id, ErrCallback cb); void add_room_to_group(const std::string &room_id, const std::string &group_id, ErrCallback cb);
//
// Device related endpoints.
//
//! List devices
void query_devices(Callback<mtx::responses::QueryDevices> cb);
/////! Rename device
// void rename_device(const mtx::requests::DeviceSigningUpload,
// UIAHandler uia_handler,
// ErrCallback cb);
// //
// Encryption related endpoints. // Encryption related endpoints.
// //
......
...@@ -1082,6 +1082,19 @@ Client::add_room_to_group(const std::string &room_id, const std::string &group_i ...@@ -1082,6 +1082,19 @@ Client::add_room_to_group(const std::string &room_id, const std::string &group_i
"/client/r0/groups/" + group_id + "/admin/rooms/" + room_id, json::object(), cb); "/client/r0/groups/" + group_id + "/admin/rooms/" + room_id, json::object(), cb);
} }
//
// Device related endpoints
//
void
Client::query_devices(Callback<mtx::responses::QueryDevices> cb)
{
get<mtx::responses::QueryDevices>("/client/r0/devices",
[cb](const mtx::responses::QueryDevices &res,
HeaderFields,
RequestErr err) { cb(res, err); });
}
// //
// Encryption related endpoints // Encryption related endpoints
// //
......
#include "mtx/responses/device.hpp"
#include <nlohmann/json.hpp>
namespace mtx {
namespace responses {
void
from_json(const nlohmann::json &obj, Device &res)
{
res.device_id = obj.at("device_id").get<std::string>();
// This is needed because synapse sometimes sends null instead -_-
if (obj.contains("display_name") && obj["display_name"].is_string()) {
res.display_name = obj.value("display_name", std::string{});
}
// This is needed because synapse sometimes sends null instead -_-
if (obj.contains("last_seen_ip") && obj["last_seen_ip"].is_string()) {
res.last_seen_ip = obj.value("last_seen_ip", std::string{});
}
// This is needed because synapse sometimes sends null instead -_-
if (obj.contains("last_seen_ts") && obj["last_seen_ts"].is_number()) {
res.last_seen_ts = obj.value("last_seen_ts", size_t{});
}
}
void
from_json(const nlohmann::json &obj, QueryDevices &response)
{
response.devices = obj.at("devices").get<std::vector<Device>>();
}
}
}
...@@ -97,6 +97,7 @@ src = [ ...@@ -97,6 +97,7 @@ src = [
'lib/structs/responses/common.cpp', 'lib/structs/responses/common.cpp',
'lib/structs/responses/create_room.cpp', 'lib/structs/responses/create_room.cpp',
'lib/structs/responses/crypto.cpp', 'lib/structs/responses/crypto.cpp',
'lib/structs/responses/device.cpp',
'lib/structs/responses/empty.cpp', 'lib/structs/responses/empty.cpp',
'lib/structs/responses/groups.cpp', 'lib/structs/responses/groups.cpp',
'lib/structs/responses/login.cpp', 'lib/structs/responses/login.cpp',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment