aboutsummaryrefslogtreecommitdiff
path: root/lib/csapi
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-06 11:45:36 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-06 11:45:36 +0900
commitff3c70978d3d33b65f3e5878c2876fb74c152232 (patch)
tree84650a12fd079862f2889f985d0cecc520df4c2f /lib/csapi
parent1f62e677ecaffea39ae2fdcc07ac4e1a09d027a1 (diff)
downloadlibquotient-ff3c70978d3d33b65f3e5878c2876fb74c152232.tar.gz
libquotient-ff3c70978d3d33b65f3e5878c2876fb74c152232.zip
lib/csapi: SearchJob
Diffstat (limited to 'lib/csapi')
-rw-r--r--lib/csapi/search.cpp211
-rw-r--r--lib/csapi/search.h119
2 files changed, 330 insertions, 0 deletions
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 <QtCore/QStringBuilder>
+
+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>
+ {
+ SearchJob::UserProfile operator()(const QJsonValue& jv)
+ {
+ const auto& o = jv.toObject();
+ SearchJob::UserProfile result;
+ result.displayname =
+ fromJson<QString>(o.value("displayname"));
+ result.avatarUrl =
+ fromJson<QString>(o.value("avatar_url"));
+
+ return result;
+ }
+ };
+
+ template <> struct FromJson<SearchJob::EventContext>
+ {
+ SearchJob::EventContext operator()(const QJsonValue& jv)
+ {
+ const auto& o = jv.toObject();
+ SearchJob::EventContext result;
+ result.begin =
+ fromJson<QString>(o.value("start"));
+ result.end =
+ fromJson<QString>(o.value("end"));
+ result.profileInfo =
+ fromJson<QHash<QString, SearchJob::UserProfile>>(o.value("profile_info"));
+ result.eventsBefore =
+ fromJson<RoomEvents>(o.value("events_before"));
+ result.eventsAfter =
+ fromJson<RoomEvents>(o.value("events_after"));
+
+ return result;
+ }
+ };
+
+ template <> struct FromJson<SearchJob::Result>
+ {
+ SearchJob::Result operator()(const QJsonValue& jv)
+ {
+ const auto& o = jv.toObject();
+ SearchJob::Result result;
+ result.rank =
+ fromJson<double>(o.value("rank"));
+ result.result =
+ fromJson<RoomEventPtr>(o.value("result"));
+ result.context =
+ fromJson<SearchJob::EventContext>(o.value("context"));
+
+ return result;
+ }
+ };
+
+ template <> struct FromJson<SearchJob::GroupValue>
+ {
+ SearchJob::GroupValue operator()(const QJsonValue& jv)
+ {
+ const auto& o = jv.toObject();
+ SearchJob::GroupValue result;
+ result.nextBatch =
+ fromJson<QString>(o.value("next_batch"));
+ result.order =
+ fromJson<int>(o.value("order"));
+ result.results =
+ fromJson<QStringList>(o.value("results"));
+
+ return result;
+ }
+ };
+
+ template <> struct FromJson<SearchJob::ResultRoomEvents>
+ {
+ SearchJob::ResultRoomEvents operator()(const QJsonValue& jv)
+ {
+ const auto& o = jv.toObject();
+ SearchJob::ResultRoomEvents result;
+ result.count =
+ fromJson<qint64>(o.value("count"));
+ result.results =
+ fromJson<std::vector<SearchJob::Result>>(o.value("results"));
+ result.state =
+ fromJson<std::unordered_map<QString, StateEvents>>(o.value("state"));
+ result.groups =
+ fromJson<QHash<QString, QHash<QString, SearchJob::GroupValue>>>(o.value("groups"));
+ result.nextBatch =
+ fromJson<QString>(o.value("next_batch"));
+
+ return result;
+ }
+ };
+
+ template <> struct FromJson<SearchJob::ResultCategories>
+ {
+ SearchJob::ResultCategories operator()(const QJsonValue& jv)
+ {
+ const auto& o = jv.toObject();
+ SearchJob::ResultCategories result;
+ result.roomEvents =
+ fromJson<SearchJob::ResultRoomEvents>(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<ResultCategories>(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 <unordered_map>
+#include <QtCore/QHash>
+#include <QtCore/QJsonObject>
+#include "events/event.h"
+#include <QtCore/QStringList>
+#include <QtCore/QVector>
+
+#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<Group> 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<QString, UserProfile> 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<Result> results;
+ std::unordered_map<QString, StateEvents> state;
+ QHash<QString, QHash<QString, GroupValue>> 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<Private> d;
+ };
+} // namespace QMatrixClient