aboutsummaryrefslogtreecommitdiff
path: root/lib/csapi/search.cpp
blob: 9619f340d104288960d46f096e15e1d161f7fc1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/******************************************************************************
 * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN
 */

#include "search.h"

#include "converters.h"

#include <QtCore/QStringBuilder>

using namespace Quotient;

static const auto basePath = QStringLiteral("/_matrix/client/r0");

// Converters
namespace Quotient
{

template <>
struct JsonObjectConverter<SearchJob::IncludeEventContext>
{
    static void dumpTo(QJsonObject& jo,
                       const SearchJob::IncludeEventContext& pod)
    {
        addParam<IfNotEmpty>(jo, QStringLiteral("before_limit"),
                             pod.beforeLimit);
        addParam<IfNotEmpty>(jo, QStringLiteral("after_limit"), pod.afterLimit);
        addParam<IfNotEmpty>(jo, QStringLiteral("include_profile"),
                             pod.includeProfile);
    }
};

template <>
struct JsonObjectConverter<SearchJob::Group>
{
    static void dumpTo(QJsonObject& jo, const SearchJob::Group& pod)
    {
        addParam<IfNotEmpty>(jo, QStringLiteral("key"), pod.key);
    }
};

template <>
struct JsonObjectConverter<SearchJob::Groupings>
{
    static void dumpTo(QJsonObject& jo, const SearchJob::Groupings& pod)
    {
        addParam<IfNotEmpty>(jo, QStringLiteral("group_by"), pod.groupBy);
    }
};

template <>
struct JsonObjectConverter<SearchJob::RoomEventsCriteria>
{
    static void dumpTo(QJsonObject& jo, const SearchJob::RoomEventsCriteria& pod)
    {
        addParam<>(jo, QStringLiteral("search_term"), pod.searchTerm);
        addParam<IfNotEmpty>(jo, QStringLiteral("keys"), pod.keys);
        addParam<IfNotEmpty>(jo, QStringLiteral("filter"), pod.filter);
        addParam<IfNotEmpty>(jo, QStringLiteral("order_by"), pod.orderBy);
        addParam<IfNotEmpty>(jo, QStringLiteral("event_context"),
                             pod.eventContext);
        addParam<IfNotEmpty>(jo, QStringLiteral("include_state"),
                             pod.includeState);
        addParam<IfNotEmpty>(jo, QStringLiteral("groupings"), pod.groupings);
    }
};

template <>
struct JsonObjectConverter<SearchJob::Categories>
{
    static void dumpTo(QJsonObject& jo, const SearchJob::Categories& pod)
    {
        addParam<IfNotEmpty>(jo, QStringLiteral("room_events"), pod.roomEvents);
    }
};

template <>
struct JsonObjectConverter<SearchJob::UserProfile>
{
    static void fillFrom(const QJsonObject& jo, SearchJob::UserProfile& result)
    {
        fromJson(jo.value("displayname"_ls), result.displayname);
        fromJson(jo.value("avatar_url"_ls), result.avatarUrl);
    }
};

template <>
struct JsonObjectConverter<SearchJob::EventContext>
{
    static void fillFrom(const QJsonObject& jo, SearchJob::EventContext& result)
    {
        fromJson(jo.value("start"_ls), result.begin);
        fromJson(jo.value("end"_ls), result.end);
        fromJson(jo.value("profile_info"_ls), result.profileInfo);
        fromJson(jo.value("events_before"_ls), result.eventsBefore);
        fromJson(jo.value("events_after"_ls), result.eventsAfter);
    }
};

template <>
struct JsonObjectConverter<SearchJob::Result>
{
    static void fillFrom(const QJsonObject& jo, SearchJob::Result& result)
    {
        fromJson(jo.value("rank"_ls), result.rank);
        fromJson(jo.value("result"_ls), result.result);
        fromJson(jo.value("context"_ls), result.context);
    }
};

template <>
struct JsonObjectConverter<SearchJob::GroupValue>
{
    static void fillFrom(const QJsonObject& jo, SearchJob::GroupValue& result)
    {
        fromJson(jo.value("next_batch"_ls), result.nextBatch);
        fromJson(jo.value("order"_ls), result.order);
        fromJson(jo.value("results"_ls), result.results);
    }
};

template <>
struct JsonObjectConverter<SearchJob::ResultRoomEvents>
{
    static void fillFrom(const QJsonObject& jo,
                         SearchJob::ResultRoomEvents& result)
    {
        fromJson(jo.value("count"_ls), result.count);
        fromJson(jo.value("highlights"_ls), result.highlights);
        fromJson(jo.value("results"_ls), result.results);
        fromJson(jo.value("state"_ls), result.state);
        fromJson(jo.value("groups"_ls), result.groups);
        fromJson(jo.value("next_batch"_ls), result.nextBatch);
    }
};

template <>
struct JsonObjectConverter<SearchJob::ResultCategories>
{
    static void fillFrom(const QJsonObject& jo,
                         SearchJob::ResultCategories& result)
    {
        fromJson(jo.value("room_events"_ls), result.roomEvents);
    }
};

} // namespace Quotient

class SearchJob::Private
{
public:
    ResultCategories searchCategories;
};

BaseJob::Query queryToSearch(const QString& nextBatch)
{
    BaseJob::Query _q;
    addParam<IfNotEmpty>(_q, QStringLiteral("next_batch"), nextBatch);
    return _q;
}

static const auto SearchJobName = QStringLiteral("SearchJob");

SearchJob::SearchJob(const Categories& searchCategories,
                     const QString& nextBatch)
    : BaseJob(HttpVerb::Post, SearchJobName, basePath % "/search",
              queryToSearch(nextBatch))
    , d(new Private)
{
    QJsonObject _data;
    addParam<>(_data, QStringLiteral("search_categories"), 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"_ls))
        return { IncorrectResponse,
                 "The key 'search_categories' not found in the response" };
    fromJson(json.value("search_categories"_ls), d->searchCategories);

    return Success;
}