mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-22 19:08:58 +03:00
Parse unsigned key
This commit is contained in:
parent
9cf5bf3833
commit
24dd76ee0b
3 changed files with 181 additions and 117 deletions
|
@ -60,6 +60,24 @@ isMessageEvent(EventType type);
|
||||||
bool
|
bool
|
||||||
isStateEvent(EventType type);
|
isStateEvent(EventType type);
|
||||||
|
|
||||||
|
class UnsignedData
|
||||||
|
: public Deserializable
|
||||||
|
, public Serializable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
double age() const { return age_; }
|
||||||
|
QString transactionId() const { return transaction_id_; }
|
||||||
|
|
||||||
|
bool isEmpty() const { return age_ <= 0 && transaction_id_.isEmpty(); }
|
||||||
|
|
||||||
|
void deserialize(const QJsonValue &data) override;
|
||||||
|
QJsonObject serialize() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
double age_ = 0;
|
||||||
|
QString transaction_id_;
|
||||||
|
};
|
||||||
|
|
||||||
template<class Content>
|
template<class Content>
|
||||||
class Event
|
class Event
|
||||||
: public Deserializable
|
: public Deserializable
|
||||||
|
@ -68,6 +86,7 @@ class Event
|
||||||
public:
|
public:
|
||||||
Content content() const;
|
Content content() const;
|
||||||
EventType eventType() const;
|
EventType eventType() const;
|
||||||
|
UnsignedData unsignedData() const { return unsignedData_; }
|
||||||
|
|
||||||
void deserialize(const QJsonValue &data) override;
|
void deserialize(const QJsonValue &data) override;
|
||||||
QJsonObject serialize() const override;
|
QJsonObject serialize() const override;
|
||||||
|
@ -75,6 +94,7 @@ public:
|
||||||
private:
|
private:
|
||||||
Content content_;
|
Content content_;
|
||||||
EventType type_;
|
EventType type_;
|
||||||
|
UnsignedData unsignedData_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Content>
|
template<class Content>
|
||||||
|
@ -102,6 +122,9 @@ Event<Content>::deserialize(const QJsonValue &data)
|
||||||
|
|
||||||
content_.deserialize(object.value("content"));
|
content_.deserialize(object.value("content"));
|
||||||
type_ = extractEventType(object);
|
type_ = extractEventType(object);
|
||||||
|
|
||||||
|
if (object.contains("unsigned"))
|
||||||
|
unsignedData_.deserialize(object.value("unsigned"));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Content>
|
template<class Content>
|
||||||
|
@ -151,6 +174,9 @@ Event<Content>::serialize() const
|
||||||
|
|
||||||
object["content"] = content_.serialize();
|
object["content"] = content_.serialize();
|
||||||
|
|
||||||
|
if (!unsignedData_.isEmpty())
|
||||||
|
object["unsigned"] = unsignedData_.serialize();
|
||||||
|
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
} // namespace events
|
} // namespace events
|
||||||
|
|
|
@ -78,3 +78,29 @@ matrix::events::isMessageEvent(EventType type)
|
||||||
{
|
{
|
||||||
return type == EventType::RoomMessage;
|
return type == EventType::RoomMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
matrix::events::UnsignedData::deserialize(const QJsonValue &data)
|
||||||
|
{
|
||||||
|
if (!data.isObject())
|
||||||
|
throw DeserializationException("UnsignedData is not a JSON object");
|
||||||
|
|
||||||
|
auto object = data.toObject();
|
||||||
|
|
||||||
|
transaction_id_ = object.value("transaction_id").toString();
|
||||||
|
age_ = object.value("age").toDouble();
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject
|
||||||
|
matrix::events::UnsignedData::serialize() const
|
||||||
|
{
|
||||||
|
QJsonObject object;
|
||||||
|
|
||||||
|
if (!transaction_id_.isEmpty())
|
||||||
|
object["transaction_id"] = transaction_id_;
|
||||||
|
|
||||||
|
if (age_ > 0)
|
||||||
|
object["age"] = age_;
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
|
@ -22,8 +22,8 @@ using namespace matrix::events;
|
||||||
TEST(BaseEvent, Deserialization)
|
TEST(BaseEvent, Deserialization)
|
||||||
{
|
{
|
||||||
// NameEventContent
|
// NameEventContent
|
||||||
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Room Name" } } },
|
auto data =
|
||||||
{ "type", "m.room.name" } };
|
QJsonObject{{"content", QJsonObject{{"name", "Room Name"}}}, {"type", "m.room.name"}};
|
||||||
|
|
||||||
Event<NameEventContent> name_event;
|
Event<NameEventContent> name_event;
|
||||||
name_event.deserialize(data);
|
name_event.deserialize(data);
|
||||||
|
@ -32,41 +32,51 @@ TEST(BaseEvent, Deserialization)
|
||||||
|
|
||||||
// TopicEventContent
|
// TopicEventContent
|
||||||
data = QJsonObject{{"content", QJsonObject{{"topic", "Room Topic"}}},
|
data = QJsonObject{{"content", QJsonObject{{"topic", "Room Topic"}}},
|
||||||
|
{"unsigned", QJsonObject{{"age", 22}, {"transaction_id", "randomid"}}},
|
||||||
{"type", "m.room.topic"}};
|
{"type", "m.room.topic"}};
|
||||||
|
|
||||||
Event<TopicEventContent> topic_event;
|
Event<TopicEventContent> topic_event;
|
||||||
topic_event.deserialize(data);
|
topic_event.deserialize(data);
|
||||||
EXPECT_EQ(topic_event.content().topic(), "Room Topic");
|
EXPECT_EQ(topic_event.content().topic(), "Room Topic");
|
||||||
|
EXPECT_EQ(topic_event.unsignedData().age(), 22);
|
||||||
|
EXPECT_EQ(topic_event.unsignedData().transactionId(), "randomid");
|
||||||
EXPECT_EQ(topic_event.serialize(), data);
|
EXPECT_EQ(topic_event.serialize(), data);
|
||||||
|
|
||||||
// AvatarEventContent
|
// AvatarEventContent
|
||||||
data = QJsonObject{ { "content", QJsonObject{ { "url", "https://matrix.org" } } },
|
data = QJsonObject{
|
||||||
|
{"content", QJsonObject{{"url", "https://matrix.org"}}},
|
||||||
|
{"unsigned", QJsonObject{{"age", 1343434343}, {"transaction_id", "m33434.33"}}},
|
||||||
{"type", "m.room.avatar"}};
|
{"type", "m.room.avatar"}};
|
||||||
|
|
||||||
Event<AvatarEventContent> avatar_event;
|
Event<AvatarEventContent> avatar_event;
|
||||||
avatar_event.deserialize(data);
|
avatar_event.deserialize(data);
|
||||||
EXPECT_EQ(avatar_event.content().url().toString(), "https://matrix.org");
|
EXPECT_EQ(avatar_event.content().url().toString(), "https://matrix.org");
|
||||||
|
EXPECT_EQ(avatar_event.unsignedData().age(), 1343434343);
|
||||||
|
EXPECT_EQ(avatar_event.unsignedData().transactionId(), "m33434.33");
|
||||||
EXPECT_EQ(avatar_event.serialize(), data);
|
EXPECT_EQ(avatar_event.serialize(), data);
|
||||||
|
|
||||||
// AliasesEventContent
|
// AliasesEventContent
|
||||||
data =
|
data = QJsonObject{
|
||||||
QJsonObject{ { "content",
|
{"content",
|
||||||
QJsonObject{
|
QJsonObject{{"aliases", QJsonArray{"#test:matrix.org", "#test2:matrix.org"}}}},
|
||||||
{ "aliases", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } } },
|
{"unsigned", QJsonObject{{"transaction_id", "m33434.33"}}},
|
||||||
{"type", "m.room.aliases"}};
|
{"type", "m.room.aliases"}};
|
||||||
|
|
||||||
Event<AliasesEventContent> aliases_event;
|
Event<AliasesEventContent> aliases_event;
|
||||||
aliases_event.deserialize(data);
|
aliases_event.deserialize(data);
|
||||||
EXPECT_EQ(aliases_event.content().aliases().size(), 2);
|
EXPECT_EQ(aliases_event.content().aliases().size(), 2);
|
||||||
|
EXPECT_EQ(aliases_event.unsignedData().transactionId(), "m33434.33");
|
||||||
EXPECT_EQ(aliases_event.serialize(), data);
|
EXPECT_EQ(aliases_event.serialize(), data);
|
||||||
|
|
||||||
// CreateEventContent
|
// CreateEventContent
|
||||||
data = QJsonObject{{"content", QJsonObject{{"creator", "@alice:matrix.org"}}},
|
data = QJsonObject{{"content", QJsonObject{{"creator", "@alice:matrix.org"}}},
|
||||||
|
{"unsigned", QJsonObject{{"age", 2233}}},
|
||||||
{"type", "m.room.create"}};
|
{"type", "m.room.create"}};
|
||||||
|
|
||||||
Event<CreateEventContent> create_event;
|
Event<CreateEventContent> create_event;
|
||||||
create_event.deserialize(data);
|
create_event.deserialize(data);
|
||||||
EXPECT_EQ(create_event.content().creator(), "@alice:matrix.org");
|
EXPECT_EQ(create_event.content().creator(), "@alice:matrix.org");
|
||||||
|
EXPECT_EQ(create_event.unsignedData().age(), 2233);
|
||||||
EXPECT_EQ(create_event.serialize(), data);
|
EXPECT_EQ(create_event.serialize(), data);
|
||||||
|
|
||||||
// JoinRulesEventContent
|
// JoinRulesEventContent
|
||||||
|
@ -81,8 +91,8 @@ TEST(BaseEvent, Deserialization)
|
||||||
|
|
||||||
TEST(BaseEvent, DeserializationException)
|
TEST(BaseEvent, DeserializationException)
|
||||||
{
|
{
|
||||||
auto data = QJsonObject{ { "content", QJsonObject{ { "rule", "private" } } },
|
auto data =
|
||||||
{ "type", "m.room.join_rules" } };
|
QJsonObject{{"content", QJsonObject{{"rule", "private"}}}, {"type", "m.room.join_rules"}};
|
||||||
|
|
||||||
Event<JoinRulesEventContent> event1;
|
Event<JoinRulesEventContent> event1;
|
||||||
ASSERT_THROW(event1.deserialize(data), DeserializationException);
|
ASSERT_THROW(event1.deserialize(data), DeserializationException);
|
||||||
|
@ -92,6 +102,13 @@ TEST(BaseEvent, DeserializationException)
|
||||||
|
|
||||||
Event<JoinRulesEventContent> event2;
|
Event<JoinRulesEventContent> event2;
|
||||||
ASSERT_THROW(event2.deserialize(data), DeserializationException);
|
ASSERT_THROW(event2.deserialize(data), DeserializationException);
|
||||||
|
|
||||||
|
data = QJsonObject{{"contents", QJsonObject{{"join_rule", "private"}}},
|
||||||
|
{"unsigned", QJsonObject{{"age", "222"}}},
|
||||||
|
{"type", "m.room.join_rules"}};
|
||||||
|
|
||||||
|
Event<JoinRulesEventContent> event3;
|
||||||
|
ASSERT_THROW(event3.deserialize(data), DeserializationException);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(RoomEvent, Deserialization)
|
TEST(RoomEvent, Deserialization)
|
||||||
|
@ -178,33 +195,28 @@ TEST(EventType, Mapping)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.aliases"}}),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.aliases"}}),
|
||||||
EventType::RoomAliases);
|
EventType::RoomAliases);
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.avatar" } }),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.avatar"}}), EventType::RoomAvatar);
|
||||||
EventType::RoomAvatar);
|
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.canonical_alias"}}),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.canonical_alias"}}),
|
||||||
EventType::RoomCanonicalAlias);
|
EventType::RoomCanonicalAlias);
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.create" } }),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.create"}}), EventType::RoomCreate);
|
||||||
EventType::RoomCreate);
|
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.history_visibility"}}),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.history_visibility"}}),
|
||||||
EventType::RoomHistoryVisibility);
|
EventType::RoomHistoryVisibility);
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.join_rules"}}),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.join_rules"}}),
|
||||||
EventType::RoomJoinRules);
|
EventType::RoomJoinRules);
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.member" } }),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.member"}}), EventType::RoomMember);
|
||||||
EventType::RoomMember);
|
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.message"}}),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.message"}}),
|
||||||
EventType::RoomMessage);
|
EventType::RoomMessage);
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.name"}}), EventType::RoomName);
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.name"}}), EventType::RoomName);
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.power_levels"}}),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.power_levels"}}),
|
||||||
EventType::RoomPowerLevels);
|
EventType::RoomPowerLevels);
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.topic" } }),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.topic"}}), EventType::RoomTopic);
|
||||||
EventType::RoomTopic);
|
|
||||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.unknown"}}),
|
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.unknown"}}),
|
||||||
EventType::Unsupported);
|
EventType::Unsupported);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AliasesEventContent, Deserialization)
|
TEST(AliasesEventContent, Deserialization)
|
||||||
{
|
{
|
||||||
auto data =
|
auto data = QJsonObject{{"aliases", QJsonArray{"#test:matrix.org", "#test2:matrix.org"}}};
|
||||||
QJsonObject{ { "aliases", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } };
|
|
||||||
|
|
||||||
AliasesEventContent content;
|
AliasesEventContent content;
|
||||||
content.deserialize(data);
|
content.deserialize(data);
|
||||||
|
|
Loading…
Reference in a new issue