From c5ebff775e71b5d32a3b2af8f666e3f13f8970c2 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 8 Jun 2018 17:07:33 +0900 Subject: csapi: add jobs for keys management This concludes the Great Income of Jobs to libQMatrixClient - all CS API calls are now covered with job classes, yay! --- lib/csapi/definitions/device_keys.cpp | 37 ++++++ lib/csapi/definitions/device_keys.h | 32 ++++++ lib/csapi/keys.cpp | 206 ++++++++++++++++++++++++++++++++++ lib/csapi/keys.h | 115 +++++++++++++++++++ 4 files changed, 390 insertions(+) create mode 100644 lib/csapi/definitions/device_keys.cpp create mode 100644 lib/csapi/definitions/device_keys.h create mode 100644 lib/csapi/keys.cpp create mode 100644 lib/csapi/keys.h diff --git a/lib/csapi/definitions/device_keys.cpp b/lib/csapi/definitions/device_keys.cpp new file mode 100644 index 00000000..24357993 --- /dev/null +++ b/lib/csapi/definitions/device_keys.cpp @@ -0,0 +1,37 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#include "device_keys.h" + +using namespace QMatrixClient; + +QJsonObject QMatrixClient::toJson(const DeviceKeys& pod) +{ + QJsonObject _json; + addToJson<>(_json, "user_id", pod.userId); + addToJson<>(_json, "device_id", pod.deviceId); + addToJson<>(_json, "algorithms", pod.algorithms); + addToJson<>(_json, "keys", pod.keys); + addToJson<>(_json, "signatures", pod.signatures); + return _json; +} + +DeviceKeys FromJson::operator()(const QJsonValue& jv) +{ + const auto& _json = jv.toObject(); + DeviceKeys result; + result.userId = + fromJson(_json.value("user_id")); + result.deviceId = + fromJson(_json.value("device_id")); + result.algorithms = + fromJson(_json.value("algorithms")); + result.keys = + fromJson>(_json.value("keys")); + result.signatures = + fromJson>>(_json.value("signatures")); + + return result; +} + diff --git a/lib/csapi/definitions/device_keys.h b/lib/csapi/definitions/device_keys.h new file mode 100644 index 00000000..895700c1 --- /dev/null +++ b/lib/csapi/definitions/device_keys.h @@ -0,0 +1,32 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#pragma once + + +#include + +#include "converters.h" + +namespace QMatrixClient +{ + // Data structures + + struct DeviceKeys + { + QString userId; + QString deviceId; + QStringList algorithms; + QHash keys; + QHash> signatures; + }; + + QJsonObject toJson(const DeviceKeys& pod); + + template <> struct FromJson + { + DeviceKeys operator()(const QJsonValue& jv); + }; + +} // namespace QMatrixClient diff --git a/lib/csapi/keys.cpp b/lib/csapi/keys.cpp new file mode 100644 index 00000000..2d6885dd --- /dev/null +++ b/lib/csapi/keys.cpp @@ -0,0 +1,206 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#include "keys.h" + +#include "converters.h" + +#include + +using namespace QMatrixClient; + +static const auto basePath = QStringLiteral("/_matrix/client/r0"); + +class UploadKeysJob::Private +{ + public: + QHash oneTimeKeyCounts; +}; + +UploadKeysJob::UploadKeysJob(const Omittable& deviceKeys, const QHash& oneTimeKeys) + : BaseJob(HttpVerb::Post, "UploadKeysJob", + basePath % "/keys/upload") + , d(new Private) +{ + QJsonObject _data; + addToJson(_data, "device_keys", deviceKeys); + addToJson(_data, "one_time_keys", oneTimeKeys); + setRequestData(_data); +} + +UploadKeysJob::~UploadKeysJob() = default; + +const QHash& UploadKeysJob::oneTimeKeyCounts() const +{ + return d->oneTimeKeyCounts; +} + +BaseJob::Status UploadKeysJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + if (!json.contains("one_time_key_counts")) + return { JsonParseError, + "The key 'one_time_key_counts' not found in the response" }; + d->oneTimeKeyCounts = fromJson>(json.value("one_time_key_counts")); + return Success; +} + +namespace QMatrixClient +{ + // Converters + + template <> struct FromJson + { + QueryKeysJob::UnsignedDeviceInfo operator()(const QJsonValue& jv) + { + const auto& _json = jv.toObject(); + QueryKeysJob::UnsignedDeviceInfo result; + result.deviceDisplayName = + fromJson(_json.value("device_display_name")); + + return result; + } + }; + + template <> struct FromJson + { + QueryKeysJob::DeviceInformation operator()(const QJsonValue& jv) + { + const auto& _json = jv.toObject(); + QueryKeysJob::DeviceInformation result; + result.unsignedData = + fromJson(_json.value("unsigned")); + + return result; + } + }; +} // namespace QMatrixClient + +class QueryKeysJob::Private +{ + public: + QHash failures; + QHash> deviceKeys; +}; + +QueryKeysJob::QueryKeysJob(const QHash& deviceKeys, Omittable timeout, const QString& token) + : BaseJob(HttpVerb::Post, "QueryKeysJob", + basePath % "/keys/query") + , d(new Private) +{ + QJsonObject _data; + addToJson(_data, "timeout", timeout); + addToJson<>(_data, "device_keys", deviceKeys); + addToJson(_data, "token", token); + setRequestData(_data); +} + +QueryKeysJob::~QueryKeysJob() = default; + +const QHash& QueryKeysJob::failures() const +{ + return d->failures; +} + +const QHash>& QueryKeysJob::deviceKeys() const +{ + return d->deviceKeys; +} + +BaseJob::Status QueryKeysJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + d->failures = fromJson>(json.value("failures")); + d->deviceKeys = fromJson>>(json.value("device_keys")); + return Success; +} + +class ClaimKeysJob::Private +{ + public: + QHash failures; + QHash> oneTimeKeys; +}; + +ClaimKeysJob::ClaimKeysJob(const QHash>& oneTimeKeys, Omittable timeout) + : BaseJob(HttpVerb::Post, "ClaimKeysJob", + basePath % "/keys/claim") + , d(new Private) +{ + QJsonObject _data; + addToJson(_data, "timeout", timeout); + addToJson<>(_data, "one_time_keys", oneTimeKeys); + setRequestData(_data); +} + +ClaimKeysJob::~ClaimKeysJob() = default; + +const QHash& ClaimKeysJob::failures() const +{ + return d->failures; +} + +const QHash>& ClaimKeysJob::oneTimeKeys() const +{ + return d->oneTimeKeys; +} + +BaseJob::Status ClaimKeysJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + d->failures = fromJson>(json.value("failures")); + d->oneTimeKeys = fromJson>>(json.value("one_time_keys")); + return Success; +} + +class GetKeysChangesJob::Private +{ + public: + QStringList changed; + QStringList left; +}; + +BaseJob::Query queryToGetKeysChanges(const QString& from, const QString& to) +{ + BaseJob::Query _q; + _q.addQueryItem("from", from); + _q.addQueryItem("to", to); + return _q; +} + +QUrl GetKeysChangesJob::makeRequestUrl(QUrl baseUrl, const QString& from, const QString& to) +{ + return BaseJob::makeRequestUrl(std::move(baseUrl), + basePath % "/keys/changes", + queryToGetKeysChanges(from, to)); +} + +GetKeysChangesJob::GetKeysChangesJob(const QString& from, const QString& to) + : BaseJob(HttpVerb::Get, "GetKeysChangesJob", + basePath % "/keys/changes", + queryToGetKeysChanges(from, to)) + , d(new Private) +{ +} + +GetKeysChangesJob::~GetKeysChangesJob() = default; + +const QStringList& GetKeysChangesJob::changed() const +{ + return d->changed; +} + +const QStringList& GetKeysChangesJob::left() const +{ + return d->left; +} + +BaseJob::Status GetKeysChangesJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + d->changed = fromJson(json.value("changed")); + d->left = fromJson(json.value("left")); + return Success; +} + diff --git a/lib/csapi/keys.h b/lib/csapi/keys.h new file mode 100644 index 00000000..d4b0f62f --- /dev/null +++ b/lib/csapi/keys.h @@ -0,0 +1,115 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#pragma once + +#include "jobs/basejob.h" + +#include "csapi/definitions/device_keys.h" +#include +#include "converters.h" +#include +#include + +namespace QMatrixClient +{ + // Operations + + class UploadKeysJob : public BaseJob + { + public: + explicit UploadKeysJob(const Omittable& deviceKeys = none, const QHash& oneTimeKeys = {}); + ~UploadKeysJob() override; + + // Result properties + + const QHash& oneTimeKeyCounts() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; + + class QueryKeysJob : public BaseJob + { + public: + // Inner data structures + + struct UnsignedDeviceInfo + { + QString deviceDisplayName; + }; + + struct DeviceInformation : DeviceKeys + { + Omittable unsignedData; + }; + + // Construction/destruction + + explicit QueryKeysJob(const QHash& deviceKeys, Omittable timeout = none, const QString& token = {}); + ~QueryKeysJob() override; + + // Result properties + + const QHash& failures() const; + const QHash>& deviceKeys() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; + + class ClaimKeysJob : public BaseJob + { + public: + explicit ClaimKeysJob(const QHash>& oneTimeKeys, Omittable timeout = none); + ~ClaimKeysJob() override; + + // Result properties + + const QHash& failures() const; + const QHash>& oneTimeKeys() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; + + class GetKeysChangesJob : public BaseJob + { + public: + explicit GetKeysChangesJob(const QString& from, const QString& to); + + /** Construct a URL out of baseUrl and usual parameters passed to + * GetKeysChangesJob. This function can be used when + * a URL for GetKeysChangesJob is necessary but the job + * itself isn't. + */ + static QUrl makeRequestUrl(QUrl baseUrl, const QString& from, const QString& to); + + ~GetKeysChangesJob() override; + + // Result properties + + const QStringList& changed() const; + const QStringList& left() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; +} // namespace QMatrixClient -- cgit v1.2.3