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 | //! experimental blurhash, see MSC2448 |
146 | std::string blurhash; |
147 | |
148 | //! Deserialization method needed by @p nlohmann::json. |
149 | friend void from_json(const nlohmann::json &obj, ThumbnailInfo &info); |
150 | |
151 | //! Serialization method needed by @p nlohmann::json. |
152 | friend void to_json(nlohmann::json &obj, const ThumbnailInfo &info); |
153 | }; |
154 | |
155 | //! Definition of rel_type for relations. |
156 | enum class RelationType |
157 | { |
158 | //! m.annotation rel_type |
159 | Annotation, |
160 | //! m.reference rel_type |
161 | Reference, |
162 | //! m.replace rel_type |
163 | Replace, |
164 | //! im.nheko.relations.v1.in_reply_to rel_type |
165 | InReplyTo, |
166 | //! m.thread |
167 | Thread, |
168 | //! not one of the supported types |
169 | Unsupported |
170 | }; |
171 | |
172 | void |
173 | from_json(const nlohmann::json &obj, RelationType &type); |
174 | |
175 | void |
176 | to_json(nlohmann::json &obj, const RelationType &type); |
177 | |
178 | //! Relates to for reactions |
179 | struct Relation |
180 | { |
181 | //! Type of relation |
182 | RelationType rel_type = RelationType::Unsupported; |
183 | //! event id being reacted to |
184 | std::string event_id = "" ; |
185 | //! key is the reaction itself |
186 | std::optional<std::string> key = std::nullopt; |
187 | |
188 | //! proprietary field to track if this is a fallback for something else |
189 | bool is_fallback = false; |
190 | |
191 | friend void from_json(const nlohmann::json &obj, Relation &relation); |
192 | friend void to_json(nlohmann::json &obj, const Relation &relation); |
193 | }; |
194 | |
195 | //! Multiple relations for a event |
196 | struct Relations |
197 | { |
198 | //! All the relations for this event |
199 | std::vector<Relation> relations; |
200 | //! Flag, if we generated this from relates_to relations or used |
201 | //! im.nheko.relactions.v1.relations |
202 | bool synthesized = false; |
203 | |
204 | std::optional<std::string> reply_to(bool include_fallback = true) const; |
205 | std::optional<std::string> replaces(bool include_fallback = true) const; |
206 | std::optional<std::string> references(bool include_fallback = true) const; |
207 | std::optional<std::string> thread(bool include_fallback = true) const; |
208 | std::optional<Relation> annotates(bool include_fallback = true) const; |
209 | }; |
210 | |
211 | /// @brief Parses relations from a content object |
212 | /// |
213 | /// @param obj The content object of an event. |
214 | Relations |
215 | parse_relations(const nlohmann::json &obj); |
216 | |
217 | /// @brief Serializes relations to a content object |
218 | /// |
219 | /// @param obj The content object of an event. |
220 | void |
221 | add_relations(nlohmann::json &obj, const Relations &relations); |
222 | |
223 | /// @brief Applies also all the edit rules to the event in addition to adding the relations |
224 | /// |
225 | /// @param obj The content object of an event. |
226 | void |
227 | apply_relations(nlohmann::json &obj, const Relations &relations); |
228 | } // namespace common |
229 | } // namespace mtx |
230 | |