From ff3c70978d3d33b65f3e5878c2876fb74c152232 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 6 May 2018 11:45:36 +0900 Subject: lib/csapi: SearchJob --- lib/csapi/search.cpp | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/csapi/search.h | 119 +++++++++++++++++++++++++++++ 2 files changed, 330 insertions(+) create mode 100644 lib/csapi/search.cpp create mode 100644 lib/csapi/search.h (limited to 'lib') diff --git a/lib/csapi/search.cpp b/lib/csapi/search.cpp new file mode 100644 index 00000000..21a195c2 --- /dev/null +++ b/lib/csapi/search.cpp @@ -0,0 +1,211 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#include "search.h" + +#include + +using namespace QMatrixClient; + +static const auto basePath = QStringLiteral("/_matrix/client/r0"); + +namespace QMatrixClient +{ + // Converters + + QJsonObject toJson(const SearchJob::IncludeEventContext& pod) + { + QJsonObject o; + o.insert("before_limit", toJson(pod.beforeLimit)); + o.insert("after_limit", toJson(pod.afterLimit)); + o.insert("include_profile", toJson(pod.includeProfile)); + + return o; + } + + QJsonObject toJson(const SearchJob::Group& pod) + { + QJsonObject o; + o.insert("key", toJson(pod.key)); + + return o; + } + + QJsonObject toJson(const SearchJob::Groupings& pod) + { + QJsonObject o; + o.insert("group_by", toJson(pod.groupBy)); + + return o; + } + + QJsonObject toJson(const SearchJob::RoomEventsCriteria& pod) + { + QJsonObject o; + o.insert("search_term", toJson(pod.searchTerm)); + o.insert("keys", toJson(pod.keys)); + o.insert("filter", toJson(pod.filter)); + o.insert("order_by", toJson(pod.orderBy)); + o.insert("event_context", toJson(pod.eventContext)); + o.insert("include_state", toJson(pod.includeState)); + o.insert("groupings", toJson(pod.groupings)); + + return o; + } + + QJsonObject toJson(const SearchJob::Categories& pod) + { + QJsonObject o; + o.insert("room_events", toJson(pod.roomEvents)); + + return o; + } + + template <> struct FromJson + { + SearchJob::UserProfile operator()(const QJsonValue& jv) + { + const auto& o = jv.toObject(); + SearchJob::UserProfile result; + result.displayname = + fromJson(o.value("displayname")); + result.avatarUrl = + fromJson(o.value("avatar_url")); + + return result; + } + }; + + template <> struct FromJson + { + SearchJob::EventContext operator()(const QJsonValue& jv) + { + const auto& o = jv.toObject(); + SearchJob::EventContext result; + result.begin = + fromJson(o.value("start")); + result.end = + fromJson(o.value("end")); + result.profileInfo = + fromJson>(o.value("profile_info")); + result.eventsBefore = + fromJson(o.value("events_before")); + result.eventsAfter = + fromJson(o.value("events_after")); + + return result; + } + }; + + template <> struct FromJson + { + SearchJob::Result operator()(const QJsonValue& jv) + { + const auto& o = jv.toObject(); + SearchJob::Result result; + result.rank = + fromJson(o.value("rank")); + result.result = + fromJson(o.value("result")); + result.context = + fromJson(o.value("context")); + + return result; + } + }; + + template <> struct FromJson + { + SearchJob::GroupValue operator()(const QJsonValue& jv) + { + const auto& o = jv.toObject(); + SearchJob::GroupValue result; + result.nextBatch = + fromJson(o.value("next_batch")); + result.order = + fromJson(o.value("order")); + result.results = + fromJson(o.value("results")); + + return result; + } + }; + + template <> struct FromJson + { + SearchJob::ResultRoomEvents operator()(const QJsonValue& jv) + { + const auto& o = jv.toObject(); + SearchJob::ResultRoomEvents result; + result.count = + fromJson(o.value("count")); + result.results = + fromJson>(o.value("results")); + result.state = + fromJson>(o.value("state")); + result.groups = + fromJson>>(o.value("groups")); + result.nextBatch = + fromJson(o.value("next_batch")); + + return result; + } + }; + + template <> struct FromJson + { + SearchJob::ResultCategories operator()(const QJsonValue& jv) + { + const auto& o = jv.toObject(); + SearchJob::ResultCategories result; + result.roomEvents = + fromJson(o.value("room_events")); + + return result; + } + }; +} // namespace QMatrixClient + +class SearchJob::Private +{ + public: + ResultCategories searchCategories; +}; + +BaseJob::Query queryToSearch(const QString& nextBatch) +{ + BaseJob::Query _q; + if (!nextBatch.isEmpty()) + _q.addQueryItem("next_batch", nextBatch); + return _q; +} + +SearchJob::SearchJob(const Categories& searchCategories, const QString& nextBatch) + : BaseJob(HttpVerb::Post, "SearchJob", + basePath % "/search", + queryToSearch(nextBatch)) + , d(new Private) +{ + QJsonObject _data; + _data.insert("search_categories", toJson(searchCategories)); + setRequestData(_data); +} + +SearchJob::~SearchJob() = default; + +const SearchJob::ResultCategories& SearchJob::searchCategories() const +{ + return d->searchCategories; +} + +BaseJob::Status SearchJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + if (!json.contains("search_categories")) + return { JsonParseError, + "The key 'search_categories' not found in the response" }; + d->searchCategories = fromJson(json.value("search_categories")); + return Success; +} + diff --git a/lib/csapi/search.h b/lib/csapi/search.h new file mode 100644 index 00000000..cde160a9 --- /dev/null +++ b/lib/csapi/search.h @@ -0,0 +1,119 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#pragma once + +#include "jobs/basejob.h" + +#include +#include +#include +#include "events/event.h" +#include +#include + +#include "converters.h" + +namespace QMatrixClient +{ + // Operations + + class SearchJob : public BaseJob + { + public: + // Inner data structures + + struct IncludeEventContext + { + int beforeLimit; + int afterLimit; + bool includeProfile; + }; + + struct Group + { + QString key; + }; + + struct Groupings + { + QVector groupBy; + }; + + struct RoomEventsCriteria + { + QString searchTerm; + QStringList keys; + QJsonObject filter; + QString orderBy; + IncludeEventContext eventContext; + bool includeState; + Groupings groupings; + }; + + struct Categories + { + RoomEventsCriteria roomEvents; + }; + + struct UserProfile + { + QString displayname; + QString avatarUrl; + }; + + struct EventContext + { + QString begin; + QString end; + QHash profileInfo; + RoomEvents eventsBefore; + RoomEvents eventsAfter; + }; + + struct Result + { + double rank; + RoomEventPtr result; + EventContext context; + }; + + struct GroupValue + { + QString nextBatch; + int order; + QStringList results; + }; + + struct ResultRoomEvents + { + qint64 count; + std::vector results; + std::unordered_map state; + QHash> groups; + QString nextBatch; + }; + + struct ResultCategories + { + ResultRoomEvents roomEvents; + }; + + // Construction/destruction + + explicit SearchJob(const Categories& searchCategories, const QString& nextBatch = {}); + ~SearchJob() override; + + // Result properties + + const ResultCategories& searchCategories() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; +} // namespace QMatrixClient -- cgit v1.2.3