Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F113986
D193.1732499739.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D193.1732499739.diff
View Options
diff --git a/src/base/event.hpp b/src/base/event.hpp
--- a/src/base/event.hpp
+++ b/src/base/event.hpp
@@ -87,6 +87,13 @@
*/
std::pair<std::string/* relType */, std::string/* eventId */> relationship() const;
+ /**
+ * Get the m.relates_to object in the event.
+ *
+ * @return The m.relates_to object of this event.
+ */
+ JsonWrap mRelatesTo() const;
+
template<class Archive>
void serialize(Archive &ar, std::uint32_t const /*version*/ ) {
ar & m_json & m_decryptedJson & m_decrypted & m_encrypted;
diff --git a/src/base/event.cpp b/src/base/event.cpp
--- a/src/base/event.cpp
+++ b/src/base/event.cpp
@@ -163,6 +163,20 @@
return {"", ""};
}
+ JsonWrap Event::mRelatesTo() const
+ {
+ if (hasAtThat(originalJson().get(),
+ "/content/m.relates_to"_json_pointer,
+ &json::is_object)) {
+ return originalJson().get()["/content/m.relates_to"_json_pointer];
+ } else if (encrypted() && hasAtThat(raw().get(),
+ "/content/m.relates_to"_json_pointer,
+ &json::is_object)) {
+ return raw().get()["/content/m.relates_to"_json_pointer];
+ }
+ return json::object();
+ }
+
bool operator==(const Event &a, const Event &b)
{
return a.id() == b.id()
diff --git a/src/tests/event-test.cpp b/src/tests/event-test.cpp
--- a/src/tests/event-test.cpp
+++ b/src/tests/event-test.cpp
@@ -72,6 +72,7 @@
WHEN("m.relates_to is a normal relation") {
REQUIRE(Event(eventJson).relationship() == std::pair<std::string, std::string>{"moe.kazv.mxc.some-type", "$another"});
REQUIRE(Event(eventJson).replyingTo() == "");
+ REQUIRE(Event(eventJson).mRelatesTo().get() == eventJson["content"]["m.relates_to"]);
}
WHEN("m.relates_to is a reply") {
@@ -82,26 +83,31 @@
};
REQUIRE(Event(eventJson).relationship() == std::pair<std::string, std::string>{"m.in_reply_to", "$another"});
REQUIRE(Event(eventJson).replyingTo() == "$another");
+ REQUIRE(Event(eventJson).mRelatesTo().get() == eventJson["content"]["m.relates_to"]);
}
WHEN("m.relates_to is missing rel_type") {
eventJson["content"]["m.relates_to"].erase("rel_type");
REQUIRE(Event(eventJson).relationship() == std::pair<std::string, std::string>{"", ""});
+ REQUIRE(Event(eventJson).mRelatesTo().get() == eventJson["content"]["m.relates_to"]);
}
WHEN("m.relates_to is missing event_id") {
eventJson["content"]["m.relates_to"].erase("event_id");
REQUIRE(Event(eventJson).relationship() == std::pair<std::string, std::string>{"", ""});
+ REQUIRE(Event(eventJson).mRelatesTo().get() == eventJson["content"]["m.relates_to"]);
}
WHEN("m.relates_to is not an object") {
eventJson["content"]["m.relates_to"] = json::array();
REQUIRE(Event(eventJson).relationship() == std::pair<std::string, std::string>{"", ""});
+ REQUIRE(Event(eventJson).mRelatesTo().get() == json::object());
}
WHEN("m.relates_to does not exist") {
eventJson["content"].erase("m.relates_to");
REQUIRE(Event(eventJson).relationship() == std::pair<std::string, std::string>{"", ""});
+ REQUIRE(Event(eventJson).mRelatesTo().get() == json::object());
}
}
@@ -136,6 +142,7 @@
auto event = Event(plainJsonWithRel).setDecryptedJson(eventJsonWithRel, Event::Decrypted);
THEN("should use the relationship within plaintext") {
REQUIRE(event.relationship() == std::pair<std::string, std::string>{"moe.kazv.mxc.some-other-type", "$another-2"});
+ REQUIRE(event.mRelatesTo().get() == plainJsonWithRel["content"]["m.relates_to"]);
}
}
@@ -143,6 +150,7 @@
auto event = Event(plainJson).setDecryptedJson(eventJsonWithRel, Event::Decrypted);
THEN("should use the relationship within ciphertext") {
REQUIRE(event.relationship() == std::pair<std::string, std::string>{"moe.kazv.mxc.some-type", "$another"});
+ REQUIRE(event.mRelatesTo().get() == eventJsonWithRel["content"]["m.relates_to"]);
}
}
@@ -150,6 +158,7 @@
auto event = Event(plainJsonWithRel).setDecryptedJson(eventJson, Event::Decrypted);
THEN("should use the relationship within plaintext") {
REQUIRE(event.relationship() == std::pair<std::string, std::string>{"moe.kazv.mxc.some-other-type", "$another-2"});
+ REQUIRE(event.mRelatesTo().get() == plainJsonWithRel["content"]["m.relates_to"]);
}
}
@@ -157,6 +166,7 @@
auto event = Event(plainJson).setDecryptedJson(eventJson, Event::Decrypted);
THEN("should return empty pair") {
REQUIRE(event.relationship() == std::pair<std::string, std::string>{"", ""});
+ REQUIRE(event.mRelatesTo().get() == json::object());
}
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 24, 5:55 PM (8 h, 29 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39534
Default Alt Text
D193.1732499739.diff (4 KB)
Attached To
Mode
D193: Add function to get the original m.relates_to object of events
Attached
Detach File
Event Timeline
Log In to Comment