libQuotient
A Qt library for building matrix clients
search.h
Go to the documentation of this file.
1 // THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN
2 
3 #pragma once
4 
5 #include <Quotient/csapi/definitions/room_event_filter.h>
6 
7 #include <Quotient/events/roomevent.h>
8 #include <Quotient/events/stateevent.h>
9 #include <Quotient/jobs/basejob.h>
10 
11 namespace Quotient {
12 
13 //! \brief Perform a server-side search.
14 //!
15 //! Performs a full text search across different categories.
16 class QUOTIENT_API SearchJob : public BaseJob {
17 public:
18  // Inner data structures
19 
20  //! Configures whether any context for the events
21  //! returned are included in the response.
22  struct QUOTIENT_API IncludeEventContext {
23  //! How many events before the result are
24  //! returned. By default, this is `5`.
25  std::optional<int> beforeLimit{};
26 
27  //! How many events after the result are
28  //! returned. By default, this is `5`.
29  std::optional<int> afterLimit{};
30 
31  //! Requests that the server returns the
32  //! historic profile information for the users
33  //! that sent the events that were returned.
34  //! By default, this is `false`.
35  std::optional<bool> includeProfile{};
36  };
37 
38  //! Configuration for group.
39  struct QUOTIENT_API Group {
40  //! Key that defines the group.
41  QString key{};
42  };
43 
44  //! Requests that the server partitions the result set
45  //! based on the provided list of keys.
46  struct QUOTIENT_API Groupings {
47  //! List of groups to request.
48  QVector<Group> groupBy{};
49  };
50 
51  //! Mapping of category name to search criteria.
52  struct QUOTIENT_API RoomEventsCriteria {
53  //! The string to search events for
54  QString searchTerm;
55 
56  //! The keys to search. Defaults to all.
57  QStringList keys{};
58 
59  //! This takes a [filter](/client-server-api/#filtering).
60  RoomEventFilter filter{};
61 
62  //! The order in which to search for results.
63  //! By default, this is `"rank"`.
64  QString orderBy{};
65 
66  //! Configures whether any context for the events
67  //! returned are included in the response.
68  std::optional<IncludeEventContext> eventContext{};
69 
70  //! Requests the server return the current state for
71  //! each room returned.
72  std::optional<bool> includeState{};
73 
74  //! Requests that the server partitions the result set
75  //! based on the provided list of keys.
76  std::optional<Groupings> groupings{};
77  };
78 
79  //! Describes which categories to search in and their criteria.
80  struct QUOTIENT_API Categories {
81  //! Mapping of category name to search criteria.
82  std::optional<RoomEventsCriteria> roomEvents{};
83  };
84 
85  struct QUOTIENT_API UserProfile {
86  QString displayname{};
87 
88  QUrl avatarUrl{};
89  };
90 
91  //! Context for result, if requested.
92  struct QUOTIENT_API EventContext {
93  //! Pagination token for the start of the chunk
94  QString begin{};
95 
96  //! Pagination token for the end of the chunk
97  QString end{};
98 
99  //! The historic profile information of the
100  //! users that sent the events returned.
101  //!
102  //! The key is the user ID for which
103  //! the profile belongs to.
104  QHash<UserId, UserProfile> profileInfo{};
105 
106  //! Events just before the result.
107  RoomEvents eventsBefore{};
108 
109  //! Events just after the result.
110  RoomEvents eventsAfter{};
111  };
112 
113  //! The result object.
114  struct QUOTIENT_API Result {
115  //! A number that describes how closely this result matches the search. Higher is closer.
116  std::optional<double> rank{};
117 
118  //! The event that matched.
119  RoomEventPtr result{};
120 
121  //! Context for result, if requested.
122  std::optional<EventContext> context{};
123  };
124 
125  //! The results for a particular group value.
126  struct QUOTIENT_API GroupValue {
127  //! Token that can be used to get the next batch
128  //! of results in the group, by passing as the
129  //! `next_batch` parameter to the next call. If
130  //! this field is absent, there are no more
131  //! results in this group.
132  QString nextBatch{};
133 
134  //! Key that can be used to order different
135  //! groups.
136  std::optional<int> order{};
137 
138  //! Which results are in this group.
139  QStringList results{};
140  };
141 
142  //! Mapping of category name to search criteria.
143  struct QUOTIENT_API ResultRoomEvents {
144  //! An approximate count of the total number of results found.
145  std::optional<int> count{};
146 
147  //! List of words which should be highlighted, useful for stemming which may change the
148  //! query terms.
149  QStringList highlights{};
150 
151  //! List of results in the requested order.
152  std::vector<Result> results{};
153 
154  //! The current state for every room in the results.
155  //! This is included if the request had the
156  //! `include_state` key set with a value of `true`.
157  //!
158  //! The key is the room ID for which the `State
159  //! Event` array belongs to.
160  std::unordered_map<RoomId, StateEvents> state{};
161 
162  //! Any groups that were requested.
163  //!
164  //! The outer `string` key is the group key requested (eg: `room_id`
165  //! or `sender`). The inner `string` key is the grouped value (eg:
166  //! a room's ID or a user's ID).
167  QHash<QString, QHash<QString, GroupValue>> groups{};
168 
169  //! Token that can be used to get the next batch of
170  //! results, by passing as the `next_batch` parameter to
171  //! the next call. If this field is absent, there are no
172  //! more results.
173  QString nextBatch{};
174  };
175 
176  //! Describes which categories to search in and their criteria.
177  struct QUOTIENT_API ResultCategories {
178  //! Mapping of category name to search criteria.
179  std::optional<ResultRoomEvents> roomEvents{};
180  };
181 
182  // Construction/destruction
183 
184  //! \param searchCategories
185  //! Describes which categories to search in and their criteria.
186  //!
187  //! \param nextBatch
188  //! The point to return events from. If given, this should be a
189  //! `next_batch` result from a previous call to this endpoint.
190  explicit SearchJob(const Categories& searchCategories, const QString& nextBatch = {});
191 
192  // Result properties
193 
194  //! Describes which categories to search in and their criteria.
195  ResultCategories searchCategories() const
196  {
197  return loadFromJson<ResultCategories>("search_categories"_L1);
198  }
199 };
200 
201 inline auto collectResponse(const SearchJob* job) { return job->searchCategories(); }
202 
203 template <>
204 struct QUOTIENT_API JsonObjectConverter<SearchJob::IncludeEventContext> {
205  static void dumpTo(QJsonObject& jo, const SearchJob::IncludeEventContext& pod)
206  {
207  addParam<IfNotEmpty>(jo, "before_limit"_L1, pod.beforeLimit);
208  addParam<IfNotEmpty>(jo, "after_limit"_L1, pod.afterLimit);
209  addParam<IfNotEmpty>(jo, "include_profile"_L1, pod.includeProfile);
210  }
211 };
212 
213 template <>
214 struct QUOTIENT_API JsonObjectConverter<SearchJob::Group> {
215  static void dumpTo(QJsonObject& jo, const SearchJob::Group& pod)
216  {
217  addParam<IfNotEmpty>(jo, "key"_L1, pod.key);
218  }
219 };
220 
221 template <>
222 struct QUOTIENT_API JsonObjectConverter<SearchJob::Groupings> {
223  static void dumpTo(QJsonObject& jo, const SearchJob::Groupings& pod)
224  {
225  addParam<IfNotEmpty>(jo, "group_by"_L1, pod.groupBy);
226  }
227 };
228 
229 template <>
230 struct QUOTIENT_API JsonObjectConverter<SearchJob::RoomEventsCriteria> {
231  static void dumpTo(QJsonObject& jo, const SearchJob::RoomEventsCriteria& pod)
232  {
233  addParam<>(jo, "search_term"_L1, pod.searchTerm);
234  addParam<IfNotEmpty>(jo, "keys"_L1, pod.keys);
235  addParam<IfNotEmpty>(jo, "filter"_L1, pod.filter);
236  addParam<IfNotEmpty>(jo, "order_by"_L1, pod.orderBy);
237  addParam<IfNotEmpty>(jo, "event_context"_L1, pod.eventContext);
238  addParam<IfNotEmpty>(jo, "include_state"_L1, pod.includeState);
239  addParam<IfNotEmpty>(jo, "groupings"_L1, pod.groupings);
240  }
241 };
242 
243 template <>
244 struct QUOTIENT_API JsonObjectConverter<SearchJob::Categories> {
245  static void dumpTo(QJsonObject& jo, const SearchJob::Categories& pod)
246  {
247  addParam<IfNotEmpty>(jo, "room_events"_L1, pod.roomEvents);
248  }
249 };
250 
251 template <>
252 struct QUOTIENT_API JsonObjectConverter<SearchJob::UserProfile> {
253  static void fillFrom(const QJsonObject& jo, SearchJob::UserProfile& result)
254  {
255  fillFromJson(jo.value("displayname"_L1), result.displayname);
256  fillFromJson(jo.value("avatar_url"_L1), result.avatarUrl);
257  }
258 };
259 
260 template <>
261 struct QUOTIENT_API JsonObjectConverter<SearchJob::EventContext> {
262  static void fillFrom(const QJsonObject& jo, SearchJob::EventContext& result)
263  {
264  fillFromJson(jo.value("start"_L1), result.begin);
265  fillFromJson(jo.value("end"_L1), result.end);
266  fillFromJson(jo.value("profile_info"_L1), result.profileInfo);
267  fillFromJson(jo.value("events_before"_L1), result.eventsBefore);
268  fillFromJson(jo.value("events_after"_L1), result.eventsAfter);
269  }
270 };
271 
272 template <>
273 struct QUOTIENT_API JsonObjectConverter<SearchJob::Result> {
274  static void fillFrom(const QJsonObject& jo, SearchJob::Result& result)
275  {
276  fillFromJson(jo.value("rank"_L1), result.rank);
277  fillFromJson(jo.value("result"_L1), result.result);
278  fillFromJson(jo.value("context"_L1), result.context);
279  }
280 };
281 
282 template <>
283 struct QUOTIENT_API JsonObjectConverter<SearchJob::GroupValue> {
284  static void fillFrom(const QJsonObject& jo, SearchJob::GroupValue& result)
285  {
286  fillFromJson(jo.value("next_batch"_L1), result.nextBatch);
287  fillFromJson(jo.value("order"_L1), result.order);
288  fillFromJson(jo.value("results"_L1), result.results);
289  }
290 };
291 
292 template <>
293 struct QUOTIENT_API JsonObjectConverter<SearchJob::ResultRoomEvents> {
294  static void fillFrom(const QJsonObject& jo, SearchJob::ResultRoomEvents& result)
295  {
296  fillFromJson(jo.value("count"_L1), result.count);
297  fillFromJson(jo.value("highlights"_L1), result.highlights);
298  fillFromJson(jo.value("results"_L1), result.results);
299  fillFromJson(jo.value("state"_L1), result.state);
300  fillFromJson(jo.value("groups"_L1), result.groups);
301  fillFromJson(jo.value("next_batch"_L1), result.nextBatch);
302  }
303 };
304 
305 template <>
306 struct QUOTIENT_API JsonObjectConverter<SearchJob::ResultCategories> {
307  static void fillFrom(const QJsonObject& jo, SearchJob::ResultCategories& result)
308  {
309  fillFromJson(jo.value("room_events"_L1), result.roomEvents);
310  }
311 };
312 
313 } // namespace Quotient