From 376da43a29f3ebad807da2761e7a0c0b105587ec Mon Sep 17 00:00:00 2001
From: Alexey Rusakov <Kitsune-Ral@users.sf.net>
Date: Thu, 25 Aug 2022 19:58:18 +0200
Subject: More code reorganisation

- Common switchOnType() piece for key verification events is factored
  out into processIfVerificationEvent()
- Bare event JSON removed from KeyVerificationSession into constructors
  of respective events
- Connection::sendToDevice() uses assembleEncryptedContent() introduced
  in the previous commit
- commonSupportedMethods() moved out to .cpp; error/string converters
  made static
---
 lib/events/keyverificationevent.h | 77 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 73 insertions(+), 4 deletions(-)

(limited to 'lib/events')

diff --git a/lib/events/keyverificationevent.h b/lib/events/keyverificationevent.h
index cdbd5d74..f635d07b 100644
--- a/lib/events/keyverificationevent.h
+++ b/lib/events/keyverificationevent.h
@@ -18,6 +18,16 @@ public:
     explicit KeyVerificationRequestEvent(const QJsonObject& obj)
         : Event(TypeId, obj)
     {}
+    KeyVerificationRequestEvent(const QString& transactionId,
+                                const QString& fromDevice,
+                                const QStringList& methods,
+                                const QDateTime& timestamp)
+        : KeyVerificationRequestEvent(
+            basicJson(TypeId, { { "transaction_id"_ls, transactionId },
+                                { "from_device"_ls, fromDevice },
+                                { "methods"_ls, toJson(methods) },
+                                { "timestamp"_ls, toJson(timestamp) } }))
+    {}
 
     /// The device ID which is initiating the request.
     QUO_CONTENT_GETTER(QString, fromDevice)
@@ -44,6 +54,14 @@ public:
     explicit KeyVerificationReadyEvent(const QJsonObject& obj)
         : Event(TypeId, obj)
     {}
+    KeyVerificationReadyEvent(const QString& transactionId,
+                              const QString& fromDevice,
+                              const QStringList& methods)
+        : KeyVerificationReadyEvent(
+            basicJson(TypeId, { { "transaction_id"_ls, transactionId },
+                                { "from_device"_ls, fromDevice },
+                                { "methods"_ls, toJson(methods) } }))
+    {}
 
     /// The device ID which is accepting the request.
     QUO_CONTENT_GETTER(QString, fromDevice)
@@ -62,9 +80,23 @@ class QUOTIENT_API KeyVerificationStartEvent : public Event {
 public:
     DEFINE_EVENT_TYPEID("m.key.verification.start", KeyVerificationStartEvent)
 
-    explicit KeyVerificationStartEvent(const QJsonObject &obj)
+    explicit KeyVerificationStartEvent(const QJsonObject& obj)
         : Event(TypeId, obj)
     {}
+    KeyVerificationStartEvent(const QString& transactionId,
+                              const QString& fromDevice)
+        : KeyVerificationStartEvent(
+            basicJson(TypeId, { { "transaction_id"_ls, transactionId },
+                                { "from_device"_ls, fromDevice },
+                                { "method"_ls, SasV1Method },
+                                { "hashes"_ls, QJsonArray{ "sha256"_ls } },
+                                { "key_agreement_protocols"_ls,
+                                  QJsonArray{ "curve25519-hkdf-sha256"_ls } },
+                                { "message_authentication_codes"_ls,
+                                  QJsonArray{ "hkdf-hmac-sha256"_ls } },
+                                { "short_authentication_string"_ls,
+                                  QJsonArray{ "decimal"_ls, "emoji"_ls } } }))
+    {}
 
     /// The device ID which is initiating the process.
     QUO_CONTENT_GETTER(QString, fromDevice)
@@ -125,6 +157,18 @@ public:
     explicit KeyVerificationAcceptEvent(const QJsonObject& obj)
         : Event(TypeId, obj)
     {}
+    KeyVerificationAcceptEvent(const QString& transactionId,
+                               const QString& commitment)
+        : KeyVerificationAcceptEvent(basicJson(
+            TypeId, { { "transaction_id"_ls, transactionId },
+                      { "method"_ls, SasV1Method },
+                      { "key_agreement_protocol"_ls, "curve25519-hkdf-sha256" },
+                      { "hash"_ls, "sha256" },
+                      { "message_authentication_code"_ls, "hkdf-hmac-sha256" },
+                      { "short_authentication_string"_ls,
+                        QJsonArray{ "decimal"_ls, "emoji"_ls, } },
+                      { "commitment"_ls, commitment } }))
+    {}
 
     /// An opaque identifier for the verification process.
     QUO_CONTENT_GETTER(QString, transactionId)
@@ -161,9 +205,18 @@ class QUOTIENT_API KeyVerificationCancelEvent : public Event {
 public:
     DEFINE_EVENT_TYPEID("m.key.verification.cancel", KeyVerificationCancelEvent)
 
-    explicit KeyVerificationCancelEvent(const QJsonObject &obj)
+    explicit KeyVerificationCancelEvent(const QJsonObject& obj)
         : Event(TypeId, obj)
     {}
+    KeyVerificationCancelEvent(const QString& transactionId,
+                               const QString& reason)
+        : KeyVerificationCancelEvent(
+            basicJson(TypeId, {
+                                  { "transaction_id"_ls, transactionId },
+                                  { "reason"_ls, reason },
+                                  { "code"_ls, reason } // Not a typo
+                              }))
+    {}
 
     /// An opaque identifier for the verification process.
     QUO_CONTENT_GETTER(QString, transactionId)
@@ -183,9 +236,14 @@ class QUOTIENT_API KeyVerificationKeyEvent : public Event {
 public:
     DEFINE_EVENT_TYPEID("m.key.verification.key", KeyVerificationKeyEvent)
 
-    explicit KeyVerificationKeyEvent(const QJsonObject &obj)
+    explicit KeyVerificationKeyEvent(const QJsonObject& obj)
         : Event(TypeId, obj)
     {}
+    KeyVerificationKeyEvent(const QString& transactionId, const QString& key)
+        : KeyVerificationKeyEvent(
+            basicJson(TypeId, { { "transaction_id"_ls, transactionId },
+                                { "key"_ls, key } }))
+    {}
 
     /// An opaque identifier for the verification process.
     QUO_CONTENT_GETTER(QString, transactionId)
@@ -200,9 +258,16 @@ class QUOTIENT_API KeyVerificationMacEvent : public Event {
 public:
     DEFINE_EVENT_TYPEID("m.key.verification.mac", KeyVerificationMacEvent)
 
-    explicit KeyVerificationMacEvent(const QJsonObject &obj)
+    explicit KeyVerificationMacEvent(const QJsonObject& obj)
         : Event(TypeId, obj)
     {}
+    KeyVerificationMacEvent(const QString& transactionId, const QString& keys,
+                            const QJsonObject& mac)
+        : KeyVerificationMacEvent(
+            basicJson(TypeId, { { "transaction_id"_ls, transactionId },
+                                { "keys"_ls, keys },
+                                { "mac"_ls, mac } }))
+    {}
 
     /// An opaque identifier for the verification process.
     QUO_CONTENT_GETTER(QString, transactionId)
@@ -224,6 +289,10 @@ public:
     explicit KeyVerificationDoneEvent(const QJsonObject& obj)
         : Event(TypeId, obj)
     {}
+    explicit KeyVerificationDoneEvent(const QString& transactionId)
+        : KeyVerificationDoneEvent(
+            basicJson(TypeId, { { "transaction_id"_ls, transactionId } }))
+    {}
 
     /// The same transactionId as before
     QUO_CONTENT_GETTER(QString, transactionId)
-- 
cgit v1.2.3