From 5b7654c5d4512abc38806a0f44efc199029ceef4 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Tue, 13 Apr 2021 21:51:30 +0200
Subject: [PATCH] Add dummy events for E2EE recovery

---
 include/mtx/events/collections.hpp |  3 +++
 include/mtx/events/encrypted.hpp   | 10 ++++++++++
 include/mtx/events/event_type.hpp  |  5 ++++-
 include/mtxclient/http/client.hpp  |  1 +
 lib/http/client.cpp                |  1 +
 lib/structs/events.cpp             |  4 ++++
 lib/structs/events/collections.cpp |  2 ++
 lib/structs/events/encrypted.cpp   | 10 ++++++++++
 lib/structs/responses/common.cpp   | 13 +++++++++++++
 9 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/include/mtx/events/collections.hpp b/include/mtx/events/collections.hpp
index 0bbf18331..df4a17ee5 100644
--- a/include/mtx/events/collections.hpp
+++ b/include/mtx/events/collections.hpp
@@ -58,6 +58,7 @@ using DeviceEvents = std::variant<events::DeviceEvent<msgs::RoomKey>,
                                   events::DeviceEvent<msgs::KeyRequest>,
                                   events::DeviceEvent<msgs::OlmEncrypted>,
                                   events::DeviceEvent<msgs::Encrypted>,
+                                  events::DeviceEvent<msgs::Dummy>,
                                   events::DeviceEvent<msgs::KeyVerificationRequest>,
                                   events::DeviceEvent<msgs::KeyVerificationStart>,
                                   events::DeviceEvent<msgs::KeyVerificationReady>,
@@ -287,6 +288,8 @@ template<>
 constexpr inline EventType to_device_content_to_type<mtx::events::msg::Encrypted> =
   EventType::RoomEncrypted;
 template<>
+constexpr inline EventType to_device_content_to_type<mtx::events::msg::Dummy> = EventType::Dummy;
+template<>
 constexpr inline EventType to_device_content_to_type<mtx::events::msg::KeyVerificationRequest> =
   EventType::KeyVerificationRequest;
 template<>
diff --git a/include/mtx/events/encrypted.hpp b/include/mtx/events/encrypted.hpp
index 7eeef31b1..eb31cc44e 100644
--- a/include/mtx/events/encrypted.hpp
+++ b/include/mtx/events/encrypted.hpp
@@ -104,6 +104,16 @@ from_json(const nlohmann::json &obj, Encrypted &event);
 void
 to_json(nlohmann::json &obj, const Encrypted &event);
 
+//! Content of the `m.dummy` event.
+struct Dummy
+{};
+
+void
+from_json(const nlohmann::json &obj, Dummy &event);
+
+void
+to_json(nlohmann::json &obj, const Dummy &event);
+
 //! Content of the `m.room_key` event.
 struct RoomKey
 {
diff --git a/include/mtx/events/event_type.hpp b/include/mtx/events/event_type.hpp
index 16ba250f2..e602a18cb 100644
--- a/include/mtx/events/event_type.hpp
+++ b/include/mtx/events/event_type.hpp
@@ -115,7 +115,10 @@ enum class EventType
         //! m.image_pack.rooms, currently im.ponies.emote_rooms
         ImagePackRooms,
 
-        // Unsupported event
+        //! `m.dummy`, used in crypto for example
+        Dummy,
+
+        //! Unsupported event
         Unsupported,
 };
 
diff --git a/include/mtxclient/http/client.hpp b/include/mtxclient/http/client.hpp
index c6fd3af9f..5bb888ea4 100644
--- a/include/mtxclient/http/client.hpp
+++ b/include/mtxclient/http/client.hpp
@@ -743,6 +743,7 @@ MTXCLIENT_SEND_TO_DEVICE_FWD(mtx::events::msg::ForwardedRoomKey)
 MTXCLIENT_SEND_TO_DEVICE_FWD(mtx::events::msg::KeyRequest)
 MTXCLIENT_SEND_TO_DEVICE_FWD(mtx::events::msg::OlmEncrypted)
 MTXCLIENT_SEND_TO_DEVICE_FWD(mtx::events::msg::Encrypted)
+MTXCLIENT_SEND_TO_DEVICE_FWD(mtx::events::msg::Dummy)
 MTXCLIENT_SEND_TO_DEVICE_FWD(mtx::events::msg::KeyVerificationRequest)
 MTXCLIENT_SEND_TO_DEVICE_FWD(mtx::events::msg::KeyVerificationStart)
 MTXCLIENT_SEND_TO_DEVICE_FWD(mtx::events::msg::KeyVerificationReady)
diff --git a/lib/http/client.cpp b/lib/http/client.cpp
index 826f41227..f4ac8da65 100644
--- a/lib/http/client.cpp
+++ b/lib/http/client.cpp
@@ -1483,6 +1483,7 @@ MTXCLIENT_SEND_TO_DEVICE(mtx::events::msg::ForwardedRoomKey)
 MTXCLIENT_SEND_TO_DEVICE(mtx::events::msg::KeyRequest)
 MTXCLIENT_SEND_TO_DEVICE(mtx::events::msg::OlmEncrypted)
 MTXCLIENT_SEND_TO_DEVICE(mtx::events::msg::Encrypted)
+MTXCLIENT_SEND_TO_DEVICE(mtx::events::msg::Dummy)
 MTXCLIENT_SEND_TO_DEVICE(mtx::events::msg::KeyVerificationRequest)
 MTXCLIENT_SEND_TO_DEVICE(mtx::events::msg::KeyVerificationStart)
 MTXCLIENT_SEND_TO_DEVICE(mtx::events::msg::KeyVerificationReady)
diff --git a/lib/structs/events.cpp b/lib/structs/events.cpp
index a16f42ff7..090f50d84 100644
--- a/lib/structs/events.cpp
+++ b/lib/structs/events.cpp
@@ -44,6 +44,8 @@ getEventType(const std::string &type)
                 return EventType::RoomCreate;
         else if (type == "m.room.encrypted")
                 return EventType::RoomEncrypted;
+        else if (type == "m.dummy")
+                return EventType::Dummy;
         else if (type == "m.room.encryption")
                 return EventType::RoomEncryption;
         else if (type == "m.room.guest_access")
@@ -144,6 +146,8 @@ to_string(EventType type)
                 return "m.room.create";
         case EventType::RoomEncrypted:
                 return "m.room.encrypted";
+        case EventType::Dummy:
+                return "m.dummy";
         case EventType::RoomEncryption:
                 return "m.room.encryption";
         case EventType::RoomGuestAccess:
diff --git a/lib/structs/events/collections.cpp b/lib/structs/events/collections.cpp
index f49578f51..bf22a3792 100644
--- a/lib/structs/events/collections.cpp
+++ b/lib/structs/events/collections.cpp
@@ -85,6 +85,7 @@ MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::DeviceEvent, msgs::ForwardedRoomKey
 MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::DeviceEvent, msgs::KeyRequest)
 MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::DeviceEvent, msgs::SecretRequest)
 MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::DeviceEvent, msgs::SecretSend)
+MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::DeviceEvent, msgs::Dummy)
 MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::DeviceEvent, Unknown)
 
 MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::EphemeralEvent, ephemeral::Typing)
@@ -325,6 +326,7 @@ from_json(const json &obj, TimelineEvent &e)
         case events::EventType::NhekoHiddenEvents:
         case events::EventType::ImagePackInAccountData:
         case events::EventType::ImagePackRooms:
+        case events::EventType::Dummy:
                 return;
         }
 }
diff --git a/lib/structs/events/encrypted.cpp b/lib/structs/events/encrypted.cpp
index 04d80dee7..7bbcb2457 100644
--- a/lib/structs/events/encrypted.cpp
+++ b/lib/structs/events/encrypted.cpp
@@ -116,6 +116,16 @@ to_json(json &obj, const Encrypted &content)
         common::add_relations(obj, content.relations);
 }
 
+void
+from_json(const json &, Dummy &)
+{}
+
+void
+to_json(json &obj, const Dummy &)
+{
+        obj = json::object();
+}
+
 void
 from_json(const json &obj, RoomKey &event)
 {
diff --git a/lib/structs/responses/common.cpp b/lib/structs/responses/common.cpp
index 9e7bfd3f6..8a9804618 100644
--- a/lib/structs/responses/common.cpp
+++ b/lib/structs/responses/common.cpp
@@ -160,6 +160,7 @@ parse_room_account_data_events(
                 case events::EventType::RoomCanonicalAlias:
                 case events::EventType::RoomCreate:
                 case events::EventType::RoomEncrypted:
+                case events::EventType::Dummy:
                 case events::EventType::RoomEncryption:
                 case events::EventType::RoomGuestAccess:
                 case events::EventType::RoomHistoryVisibility:
@@ -629,6 +630,7 @@ parse_timeline_events(const json &events,
                 case events::EventType::NhekoHiddenEvents:
                 case events::EventType::ImagePackRooms:
                 case events::EventType::ImagePackInAccountData:
+                case events::EventType::Dummy:
                         continue;
                 }
         }
@@ -664,6 +666,15 @@ parse_device_events(const json &events,
 
                         break;
                 }
+                case events::EventType::Dummy: {
+                        try {
+                                container.emplace_back(events::DeviceEvent<Dummy>(e));
+                        } catch (json::exception &err) {
+                                log_error(err, e);
+                        }
+
+                        break;
+                }
                 case events::EventType::RoomKey: {
                         try {
                                 container.emplace_back(events::DeviceEvent<RoomKey>(e));
@@ -970,6 +981,7 @@ parse_state_events(const json &events,
                 case events::EventType::NhekoHiddenEvents:
                 case events::EventType::ImagePackRooms:
                 case events::EventType::ImagePackInAccountData:
+                case events::EventType::Dummy:
                         continue;
                 }
         }
@@ -1137,6 +1149,7 @@ parse_stripped_events(const json &events,
                 case events::EventType::ImagePackInAccountData:
                 case events::EventType::ImagePackInRoom:
                 case events::EventType::ImagePackRooms:
+                case events::EventType::Dummy:
                         continue;
                 }
         }
-- 
GitLab