Debian Codegraph @

mtxclient/include/mtx/events/common.hpp

raw symbols SCIP
1 #pragma once
2
3 /// @file
4 /// @brief Structs used in multiple different event types.
5
6 #if __has_include(<nlohmann/json_fwd.hpp>)
7 #include <nlohmann/json_fwd.hpp>
8 #else
9 #include <nlohmann/json.hpp>
10 #endif
11
12 #include <optional>
13
14 #include "mtx/common.hpp"
15
16 namespace mtx {
17
18 //! Common structs used among many different event content types.
19 namespace common {
20
21 inline constexpr auto FORMAT_MSG_TYPE = "org.matrix.custom.html";
22
23 //! Metadata about an image thumbnail.
24 struct ThumbnailInfo
25 {
26 //! The height of the thumbnail in pixels.
27 uint64_t h = 0;
28 //! The width of the thumbnail in pixels.
29 uint64_t w = 0;
30 //! Size of the thumbnail in bytes.
31 uint64_t size = 0;
32 //! The mimetype of the thumbnail, e.g. image/jpeg.
33 std::string mimetype;
34
35 //! Deserialization method needed by @p nlohmann::json.
36 friend void from_json(const nlohmann::json &obj, ThumbnailInfo &info);
37
38 //! Serialization method needed by @p nlohmann::json.
39 friend void to_json(nlohmann::json &obj, const ThumbnailInfo &info);
40 };
41
42 //! Metadata about an image.
43 struct ImageInfo
44 {
45 //! The height of the image in pixels.
46 uint64_t h = 0;
47 //! The width of the image in pixels.
48 uint64_t w = 0;
49 //! Size of the image in bytes.
50 uint64_t size = 0;
51 //! Metadata about the image referred to in @p thumbnail_url.
52 ThumbnailInfo thumbnail_info;
53 //! The URL to a thumbnail of the image.
54 std::string thumbnail_url;
55 //! The mimetype of the image, `e.g. image/jpeg`.
56 std::string mimetype;
57 //! Encryption members. If present, they replace thumbnail_url.
58 std::optional<crypto::EncryptedFile> thumbnail_file;
59 //! experimental blurhash, see MSC2448
60 std::string blurhash;
61
62 //! Deserialization method needed by @p nlohmann::json.
63 friend void from_json(const nlohmann::json &obj, ImageInfo &info);
64
65 //! Serialization method needed by @p nlohmann::json.
66 friend void to_json(nlohmann::json &obj, const ImageInfo &info);
67 };
68
69 //! Metadata about a file.
70 struct FileInfo
71 {
72 //! The size of the file in bytes.
73 uint64_t size = 0;
74 //! Metadata about the image referred to in @p thumbnail_url.
75 ThumbnailInfo thumbnail_info;
76 //! The URL to the thumbnail of the file.
77 std::string thumbnail_url;
78 //! The mimetype of the file e.g `application/pdf`.
79 std::string mimetype;
80 //! Encryption members. If present, they replace thumbnail_url.
81 std::optional<crypto::EncryptedFile> thumbnail_file;
82
83 //! Deserialization method needed by @p nlohmann::json.
84 friend void from_json(const nlohmann::json &obj, FileInfo &info);
85
86 //! Serialization method needed by @p nlohmann::json.
87 friend void to_json(nlohmann::json &obj, const FileInfo &info);
88 };
89
90 //! Audio clip metadata.
91 struct AudioInfo
92 {
93 //! The size of the audio clip in bytes.
94 uint64_t size = 0;
95 //! The duration of the audio in milliseconds.
96 uint64_t duration = 0;
97 //! The mimetype of the audio e.g. `audio/aac`.
98 std::string mimetype;
99
100 //! Deserialization method needed by @p nlohmann::json.
101 friend void from_json(const nlohmann::json &obj, AudioInfo &info);
102
103 //! Serialization method needed by @p nlohmann::json.
104 friend void to_json(nlohmann::json &obj, const AudioInfo &info);
105 };
106
107 //! Video clip metadata.
108 struct VideoInfo
109 {
110 //! The size of the video in bytes.
111 uint64_t size = 0;
112 //! The duration of the video in milliseconds.
113 uint64_t duration = 0;
114 //! The height of the video in pixels.
115 uint64_t h = 0;
116 //! The width of the video in pixels.
117 uint64_t w = 0;
118 //! The mimetype of the video e.g. `video/mp4`.
119 std::string mimetype;
120 //! The URL to an image thumbnail of the video clip.
121 std::string thumbnail_url;
122 //! Metadata about the image referred to in @p thumbnail_url.
123 ThumbnailInfo thumbnail_info;
124 //! Encryption members. If present, they replace thumbnail_url.
125 std::optional<crypto::EncryptedFile> thumbnail_file;
126 //! experimental blurhash, see MSC2448
127 std::string blurhash;
128
129 //! Deserialization method needed by @p nlohmann::json.
130 friend void from_json(const nlohmann::json &obj, VideoInfo &info);
131
132 //! Serialization method needed by @p nlohmann::json.
133 friend void to_json(nlohmann::json &obj, const VideoInfo &info);
134 };
135
136 //! Location metadata
137 struct LocationInfo
138 {
139 //! The URL to an image thumbnail of the video clip.
140 std::string thumbnail_url;
141 //! Metadata about the image referred to in @p thumbnail_url.
142 ThumbnailInfo thumbnail_info;
143 //! Encryption members. If present, they replace thumbnail_url.
144 std::optional<crypto::EncryptedFile> thumbnail_file;
145
146 //! Deserialization method needed by @p nlohmann::json.
147 friend void from_json(const nlohmann::json &obj, LocationInfo &info);
148
149 //! Serialization method needed by @p nlohmann::json.
150 friend void to_json(nlohmann::json &obj, const LocationInfo &info);
151 };
152
153 /// @brief Mentions metadata
154 ///
155 /// @sa https://spec.matrix.org/v1.9/client-server-api/#user-and-room-mentions
156 struct Mentions
157 {
158 std::vector<std::string> user_ids;
159 bool room = false;
160
161 //! Deserialization method needed by @p nlohmann::json.
162 friend void from_json(const nlohmann::json &obj, Mentions &info);
163
164 //! Serialization method needed by @p nlohmann::json.
165 friend void to_json(nlohmann::json &obj, const Mentions &info);
166 };
167
168 /// @brief Parses mentions from a content object
169 ///
170 /// @param obj The content object of an event.
171 std::optional<Mentions>
172 parse_mentions(const nlohmann::json &obj);
173
174 /// @brief Serializes mentions to a content object
175 ///
176 /// @param obj The content object of an event.
177 void
178 add_mentions(nlohmann::json &content, const std::optional<Mentions> &m);
179
180 //! Definition of rel_type for relations.
181 enum class RelationType
182 {
183 //! m.annotation rel_type
184 Annotation,
185 //! m.reference rel_type
186 Reference,
187 //! m.replace rel_type
188 Replace,
189 //! im.nheko.relations.v1.in_reply_to rel_type
190 InReplyTo,
191 //! m.thread
192 Thread,
193 //! not one of the supported types
194 Unsupported
195 };
196
197 void
198 from_json(const nlohmann::json &obj, RelationType &type);
199
200 void
201 to_json(nlohmann::json &obj, const RelationType &type);
202
203 //! Relates to for reactions
204 struct Relation
205 {
206 //! Type of relation
207 RelationType rel_type = RelationType::Unsupported;
208 //! event id being reacted to
209 std::string event_id = "";
210 //! key is the reaction itself
211 std::optional<std::string> key = std::nullopt;
212
213 //! proprietary field to track if this is a fallback for something else
214 bool is_fallback = false;
215
216 friend void from_json(const nlohmann::json &obj, Relation &relation);
217 friend void to_json(nlohmann::json &obj, const Relation &relation);
218 };
219
220 //! Multiple relations for a event
221 struct Relations
222 {
223 //! All the relations for this event
224 std::vector<Relation> relations;
225 //! Flag, if we generated this from relates_to relations or used
226 //! im.nheko.relactions.v1.relations
227 bool synthesized = false;
228
229 [[nodiscard]] std::optional<std::string> reply_to(bool include_fallback = true) const;
230 [[nodiscard]] std::optional<std::string> replaces(bool include_fallback = true) const;
231 [[nodiscard]] std::optional<std::string> references(bool include_fallback = true) const;
232 [[nodiscard]] std::optional<std::string> thread(bool include_fallback = true) const;
233 [[nodiscard]] std::optional<Relation> annotates(bool include_fallback = true) const;
234 };
235
236 /// @brief Parses relations from a content object
237 ///
238 /// @param obj The content object of an event.
239 Relations
240 parse_relations(const nlohmann::json &obj);
241
242 /// @brief Serializes relations to a content object
243 ///
244 /// @param obj The content object of an event.
245 void
246 add_relations(nlohmann::json &obj, const Relations &relations);
247
248 /// @brief Applies also all the edit rules to the event in addition to adding the relations
249 ///
250 /// @param obj The content object of an event.
251 void
252 apply_relations(nlohmann::json &obj, const Relations &relations);
253 } // namespace common
254 } // namespace mtx