Skip to content

Commit

Permalink
Add full EncryptionEvent to room logic. Issue #95
Browse files Browse the repository at this point in the history
  • Loading branch information
a-andreyev committed Jul 4, 2019
1 parent af623d6 commit bb98bd2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
48 changes: 48 additions & 0 deletions lib/events/encryptionevent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
//
// Created by rusakov on 26/09/2017.
// Contributed by andreev on 27/06/2019.
//

#include "encryptionevent.h"

#include "converters.h"
#include "logging.h"

#include <array>

static const std::array<QString, 1> encryptionStrings = { {
QStringLiteral("m.megolm.v1.aes-sha2")
} };

namespace QMatrixClient {
template <>
struct JsonConverter<EncryptionType>
{
static EncryptionType load(const QJsonValue& jv)
{
const auto& encryptionString = jv.toString();
for (auto it = encryptionStrings.begin();
it != encryptionStrings.end(); ++it)
if (encryptionString == *it)
return EncryptionType(it - encryptionStrings.begin());

qCWarning(EVENTS) << "Unknown EncryptionType: " << encryptionString;
return EncryptionType::Undefined;
}
};
}

using namespace QMatrixClient;

EncryptionEventContent::EncryptionEventContent(const QJsonObject& json)
: encryption(fromJson<EncryptionType>(json["algorithm"_ls]))
, algorithm(sanitized(json["algorithm"_ls].toString()))
, rotationPeriodMs(json["rotation_period_ms"_ls].toInt(604800000))
, rotationPeriodMsgs(json["rotation_period_msgs"_ls].toInt(100))
{ }

void EncryptionEventContent::fillJson(QJsonObject* o) const
{
Q_ASSERT(o);
Q_ASSERT_X(encryption != EncryptionType::Undefined, __FUNCTION__,
"The key 'algorithm' must be explicit in EncryptionEventContent");
if (encryption != EncryptionType::Undefined)
o->insert(QStringLiteral("algorithm"), algorithm);
o->insert(QStringLiteral("rotation_period_ms"), rotationPeriodMs);
o->insert(QStringLiteral("rotation_period_msgs"), rotationPeriodMsgs);
}
47 changes: 40 additions & 7 deletions lib/events/encryptionevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,58 @@

#pragma once

#include "roomevent.h"
#include "stateevent.h"
#include "eventcontent.h"

namespace QMatrixClient
{
class EncryptionEvent : public RoomEvent
class EncryptionEventContent: public EventContent::Base
{
public:
enum EncryptionType : size_t { MegolmV1AesSha2 = 0,
Undefined };

explicit EncryptionEventContent(EncryptionType et = Undefined)
: encryption(et)
{ }
explicit EncryptionEventContent(const QJsonObject& json);

EncryptionType encryption;
QString algorithm;
int rotationPeriodMs;
int rotationPeriodMsgs;

protected:
void fillJson(QJsonObject* o) const override;
};

using EncryptionType = EncryptionEventContent::EncryptionType;

class EncryptionEvent : public StateEvent<EncryptionEventContent>
{
Q_GADGET
public:
DEFINE_EVENT_TYPEID("m.room.encryption", EncryptionEvent)

explicit EncryptionEvent(const QJsonObject& obj)
: RoomEvent(typeId(), obj)
, _algorithm(contentJson()["algorithm"].toString())
using EncryptionType = EncryptionEventContent::EncryptionType;

explicit EncryptionEvent(const QJsonObject& obj = {}) // TODO: apropriate default value
: StateEvent(typeId(), obj)
{ }
EncryptionEvent(EncryptionEventContent&& c)
: StateEvent(typeId(), matrixTypeId(), c)
{ }

EncryptionType encryption() const { return content().encryption; }

QString algorithm() const { return _algorithm; }
QString algorithm() const { return content().algorithm; }
int rotationPeriodMs() const { return content().rotationPeriodMs; }
int rotationPeriodMsgs() const { return content().rotationPeriodMsgs; }

private:
QString _algorithm;
REGISTER_ENUM(EncryptionType)
};

REGISTER_EVENT_TYPE(EncryptionEvent)
DEFINE_EVENTTYPE_ALIAS(Encryption, EncryptionEvent)
} // namespace QMatrixClient
Expand Down
2 changes: 0 additions & 2 deletions lib/events/simplestateevents.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,5 @@ namespace QMatrixClient
DEFINE_EVENTTYPE_ALIAS(RoomCanonicalAlias, RoomCanonicalAliasEvent)
DEFINE_SIMPLE_STATE_EVENT(RoomTopicEvent, "m.room.topic", QString, topic)
DEFINE_EVENTTYPE_ALIAS(RoomTopic, RoomTopicEvent)
DEFINE_SIMPLE_STATE_EVENT(EncryptionEvent, "m.room.encryption",
QString, algorithm)
DEFINE_EVENTTYPE_ALIAS(RoomEncryption, EncryptionEvent)
} // namespace QMatrixClient
1 change: 1 addition & 0 deletions lib/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "csapi/tags.h"
#include "csapi/room_upgrades.h"
#include "events/simplestateevents.h"
#include "events/encryptionevent.h"
#include "events/roomcreateevent.h"
#include "events/roomtombstoneevent.h"
#include "events/roomavatarevent.h"
Expand Down
2 changes: 2 additions & 0 deletions libqmatrixclient.pri
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ HEADERS += \
$$SRCPATH/events/callinviteevent.h \
$$SRCPATH/events/accountdataevents.h \
$$SRCPATH/events/directchatevent.h \
$$SRCPATH/events/encryptionevent.h \
$$SRCPATH/events/redactionevent.h \
$$SRCPATH/events/eventloader.h \
$$SRCPATH/jobs/requestdata.h \
Expand Down Expand Up @@ -81,6 +82,7 @@ SOURCES += \
$$SRCPATH/events/callinviteevent.cpp \
$$SRCPATH/events/receiptevent.cpp \
$$SRCPATH/events/directchatevent.cpp \
$$SRCPATH/events/encryptionevent.cpp \
$$SRCPATH/jobs/requestdata.cpp \
$$SRCPATH/jobs/basejob.cpp \
$$SRCPATH/jobs/syncjob.cpp \
Expand Down

0 comments on commit bb98bd2

Please sign in to comment.