From 419e2c2ad402b23f03df12a7ba6991c4823e55ef Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Sat, 23 May 2020 01:54:35 +0200
Subject: [PATCH] Add sender to every event

---
 include/mtx/events.hpp | 59 +++++++++++-------------------------------
 tests/events.cpp       |  6 +++++
 2 files changed, 21 insertions(+), 44 deletions(-)

diff --git a/include/mtx/events.hpp b/include/mtx/events.hpp
index 881c5c428..b5b61d7d1 100644
--- a/include/mtx/events.hpp
+++ b/include/mtx/events.hpp
@@ -93,6 +93,8 @@ struct Event
         //! This *should* be namespaced similar to Java package
         //! naming conventions e.g. 'com.example.subdomain.event.type'
         EventType type;
+        //! Contains the fully-qualified ID of the user who sent this event.
+        std::string sender;
 };
 
 template<class Content>
@@ -100,6 +102,7 @@ void
 to_json(json &obj, const Event<Content> &event)
 {
         obj["content"] = event.content;
+        obj["sender"]  = event.sender;
 
         switch (event.type) {
         case EventType::KeyVerificationStart:
@@ -201,6 +204,7 @@ from_json(const json &obj, Event<Content> &event)
 {
         event.content = obj.at("content").get<Content>();
         event.type    = getEventType(obj.at("type").get<std::string>());
+        event.sender  = obj.value("sender", "");
 }
 
 //! Extension of the Event type for device events.
@@ -286,7 +290,6 @@ to_json(json &obj, const UnsignedData &event)
 template<class Content>
 struct StrippedEvent : public Event<Content>
 {
-        std::string sender;
         std::string state_key;
 };
 
@@ -294,9 +297,9 @@ template<class Content>
 void
 from_json(const json &obj, StrippedEvent<Content> &event)
 {
-        event.content   = obj.at("content").get<Content>();
-        event.type      = getEventType(obj.at("type").get<std::string>());
-        event.sender    = obj.at("sender");
+        Event<Content> &base = event;
+        from_json(obj, base);
+
         event.state_key = obj.at("state_key");
 }
 
@@ -307,7 +310,6 @@ to_json(json &obj, const StrippedEvent<Content> &event)
         Event<Content> base_event = event;
         to_json(obj, base_event);
 
-        obj["sender"]    = event.sender;
         obj["state_key"] = event.state_key;
 }
 
@@ -319,8 +321,6 @@ struct RoomEvent : public Event<Content>
         std::string event_id;
         //! The ID of the room associated with this event.
         std::string room_id;
-        //! Contains the fully-qualified ID of the user who sent this event.
-        std::string sender;
         //! Timestamp in milliseconds on originating homeserver
         //! when this event was sent.
         uint64_t origin_server_ts;
@@ -333,11 +333,10 @@ template<class Content>
 void
 from_json(const json &obj, RoomEvent<Content> &event)
 {
-        event.content = obj.at("content").get<Content>();
-        event.type    = getEventType(obj.at("type").get<std::string>());
+        Event<Content> &base = event;
+        from_json(obj, base);
 
         event.event_id         = obj.at("event_id");
-        event.sender           = obj.at("sender");
         event.origin_server_ts = obj.at("origin_server_ts");
 
         // SPEC_BUG: Not present in the state array returned by /sync.
@@ -359,7 +358,6 @@ to_json(json &obj, const RoomEvent<Content> &event)
                 obj["room_id"] = event.room_id;
 
         obj["event_id"]         = event.event_id;
-        obj["sender"]           = event.sender;
         obj["unsigned"]         = event.unsigned_data;
         obj["origin_server_ts"] = event.origin_server_ts;
 }
@@ -387,17 +385,8 @@ template<class Content>
 void
 from_json(const json &obj, StateEvent<Content> &event)
 {
-        event.content          = obj.at("content").get<Content>();
-        event.event_id         = obj.at("event_id");
-        event.origin_server_ts = obj.at("origin_server_ts");
-        event.sender           = obj.at("sender");
-        event.type             = getEventType(obj.at("type").get<std::string>());
-
-        if (obj.find("room_id") != obj.end())
-                event.room_id = obj.at("room_id");
-
-        if (obj.find("unsigned") != obj.end())
-                event.unsigned_data = obj.at("unsigned");
+        RoomEvent<Content> &base = event;
+        from_json(obj, base);
 
         event.state_key = obj.at("state_key").get<std::string>();
 }
@@ -424,17 +413,8 @@ template<class Content>
 void
 from_json(const json &obj, RedactionEvent<Content> &event)
 {
-        event.content          = obj.at("content").get<Content>();
-        event.event_id         = obj.at("event_id");
-        event.origin_server_ts = obj.at("origin_server_ts");
-        event.sender           = obj.at("sender");
-        event.type             = getEventType(obj.at("type").get<std::string>());
-
-        if (obj.find("unsigned") != obj.end())
-                event.unsigned_data = obj.at("unsigned");
-
-        if (obj.find("room_id") != obj.end())
-                event.room_id = obj.at("room_id");
+        RoomEvent<Content> &base = event;
+        from_json(obj, base);
 
         event.redacts = obj.at("redacts").get<std::string>();
 }
@@ -456,17 +436,8 @@ template<class Content>
 void
 from_json(const json &obj, EncryptedEvent<Content> &event)
 {
-        event.content          = obj.at("content").get<Content>();
-        event.event_id         = obj.at("event_id");
-        event.origin_server_ts = obj.at("origin_server_ts");
-        event.sender           = obj.at("sender");
-        event.type             = getEventType(obj.at("type").get<std::string>());
-
-        if (obj.find("unsigned") != obj.end())
-                event.unsigned_data = obj.at("unsigned");
-
-        if (obj.find("room_id") != obj.end())
-                event.room_id = obj.at("room_id");
+        RoomEvent<Content> &base = event;
+        from_json(obj, base);
 }
 
 enum class MessageType
diff --git a/tests/events.cpp b/tests/events.cpp
index 13fb7e0f5..8cf7fae8b 100644
--- a/tests/events.cpp
+++ b/tests/events.cpp
@@ -787,6 +787,7 @@ TEST(ToDevice, KeyVerificationRequest)
         "timestamp": 1559598944869,
         "transaction_id": "S0meUniqueAndOpaqueString"
     },
+    "sender": "",
     "type": "m.key.verification.request"
 })"_json;
 
@@ -822,6 +823,7 @@ TEST(ToDevice, KeyVerificationStart)
         ],
         "transaction_id": "S0meUniqueAndOpaqueString"
     },
+    "sender": "",
     "type": "m.key.verification.start"
 })"_json;
 
@@ -856,6 +858,7 @@ TEST(ToDevice, KeyVerificationAccept)
         ],
         "transaction_id": "S0meUniqueAndOpaqueString"
     },
+    "sender": "",
     "type": "m.key.verification.accept"
 })"_json;
 
@@ -883,6 +886,7 @@ TEST(ToDevice, KeyVerificationCancel)
         "reason": "User rejected the key verification request",
         "transaction_id": "S0meUniqueAndOpaqueString"
     },
+    "sender": "",
     "type": "m.key.verification.cancel"
 })"_json;
 
@@ -902,6 +906,7 @@ TEST(ToDevice, KeyVerificationKey)
         "key": "fQpGIW1Snz+pwLZu6sTy2aHy/DYWWTspTJRPyNp0PKkymfIsNffysMl6ObMMFdIJhk6g6pwlIqZ54rxo8SLmAg",
         "transaction_id": "S0meUniqueAndOpaqueString"
     },
+    "sender": "",
     "type": "m.key.verification.key"
 })"_json;
 
@@ -925,6 +930,7 @@ TEST(ToDevice, KeyVerificationMac)
         },
         "transaction_id": "S0meUniqueAndOpaqueString"
     },
+    "sender": "",
     "type": "m.key.verification.mac"
 })"_json;
 
-- 
GitLab