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
|
||||
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>
|
||||
class Event
|
||||
: public Deserializable
|
||||
|
@ -68,6 +86,7 @@ class Event
|
|||
public:
|
||||
Content content() const;
|
||||
EventType eventType() const;
|
||||
UnsignedData unsignedData() const { return unsignedData_; }
|
||||
|
||||
void deserialize(const QJsonValue &data) override;
|
||||
QJsonObject serialize() const override;
|
||||
|
@ -75,6 +94,7 @@ public:
|
|||
private:
|
||||
Content content_;
|
||||
EventType type_;
|
||||
UnsignedData unsignedData_;
|
||||
};
|
||||
|
||||
template<class Content>
|
||||
|
@ -102,6 +122,9 @@ Event<Content>::deserialize(const QJsonValue &data)
|
|||
|
||||
content_.deserialize(object.value("content"));
|
||||
type_ = extractEventType(object);
|
||||
|
||||
if (object.contains("unsigned"))
|
||||
unsignedData_.deserialize(object.value("unsigned"));
|
||||
}
|
||||
|
||||
template<class Content>
|
||||
|
@ -151,6 +174,9 @@ Event<Content>::serialize() const
|
|||
|
||||
object["content"] = content_.serialize();
|
||||
|
||||
if (!unsignedData_.isEmpty())
|
||||
object["unsigned"] = unsignedData_.serialize();
|
||||
|
||||
return object;
|
||||
}
|
||||
} // namespace events
|
||||
|
|
|
@ -78,3 +78,29 @@ matrix::events::isMessageEvent(EventType type)
|
|||
{
|
||||
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)
|
||||
{
|
||||
// NameEventContent
|
||||
auto data = QJsonObject{ { "content", QJsonObject{ { "name", "Room Name" } } },
|
||||
{ "type", "m.room.name" } };
|
||||
auto data =
|
||||
QJsonObject{{"content", QJsonObject{{"name", "Room Name"}}}, {"type", "m.room.name"}};
|
||||
|
||||
Event<NameEventContent> name_event;
|
||||
name_event.deserialize(data);
|
||||
|
@ -32,41 +32,51 @@ TEST(BaseEvent, Deserialization)
|
|||
|
||||
// TopicEventContent
|
||||
data = QJsonObject{{"content", QJsonObject{{"topic", "Room Topic"}}},
|
||||
{"unsigned", QJsonObject{{"age", 22}, {"transaction_id", "randomid"}}},
|
||||
{"type", "m.room.topic"}};
|
||||
|
||||
Event<TopicEventContent> topic_event;
|
||||
topic_event.deserialize(data);
|
||||
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);
|
||||
|
||||
// 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"}};
|
||||
|
||||
Event<AvatarEventContent> avatar_event;
|
||||
avatar_event.deserialize(data);
|
||||
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);
|
||||
|
||||
// AliasesEventContent
|
||||
data =
|
||||
QJsonObject{ { "content",
|
||||
QJsonObject{
|
||||
{ "aliases", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } } },
|
||||
data = QJsonObject{
|
||||
{"content",
|
||||
QJsonObject{{"aliases", QJsonArray{"#test:matrix.org", "#test2:matrix.org"}}}},
|
||||
{"unsigned", QJsonObject{{"transaction_id", "m33434.33"}}},
|
||||
{"type", "m.room.aliases"}};
|
||||
|
||||
Event<AliasesEventContent> aliases_event;
|
||||
aliases_event.deserialize(data);
|
||||
EXPECT_EQ(aliases_event.content().aliases().size(), 2);
|
||||
EXPECT_EQ(aliases_event.unsignedData().transactionId(), "m33434.33");
|
||||
EXPECT_EQ(aliases_event.serialize(), data);
|
||||
|
||||
// CreateEventContent
|
||||
data = QJsonObject{{"content", QJsonObject{{"creator", "@alice:matrix.org"}}},
|
||||
{"unsigned", QJsonObject{{"age", 2233}}},
|
||||
{"type", "m.room.create"}};
|
||||
|
||||
Event<CreateEventContent> create_event;
|
||||
create_event.deserialize(data);
|
||||
EXPECT_EQ(create_event.content().creator(), "@alice:matrix.org");
|
||||
EXPECT_EQ(create_event.unsignedData().age(), 2233);
|
||||
EXPECT_EQ(create_event.serialize(), data);
|
||||
|
||||
// JoinRulesEventContent
|
||||
|
@ -81,8 +91,8 @@ TEST(BaseEvent, Deserialization)
|
|||
|
||||
TEST(BaseEvent, DeserializationException)
|
||||
{
|
||||
auto data = QJsonObject{ { "content", QJsonObject{ { "rule", "private" } } },
|
||||
{ "type", "m.room.join_rules" } };
|
||||
auto data =
|
||||
QJsonObject{{"content", QJsonObject{{"rule", "private"}}}, {"type", "m.room.join_rules"}};
|
||||
|
||||
Event<JoinRulesEventContent> event1;
|
||||
ASSERT_THROW(event1.deserialize(data), DeserializationException);
|
||||
|
@ -92,6 +102,13 @@ TEST(BaseEvent, DeserializationException)
|
|||
|
||||
Event<JoinRulesEventContent> event2;
|
||||
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)
|
||||
|
@ -178,33 +195,28 @@ TEST(EventType, Mapping)
|
|||
{
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.aliases"}}),
|
||||
EventType::RoomAliases);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.avatar" } }),
|
||||
EventType::RoomAvatar);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.avatar"}}), EventType::RoomAvatar);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.canonical_alias"}}),
|
||||
EventType::RoomCanonicalAlias);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.create" } }),
|
||||
EventType::RoomCreate);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.create"}}), EventType::RoomCreate);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.history_visibility"}}),
|
||||
EventType::RoomHistoryVisibility);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.join_rules"}}),
|
||||
EventType::RoomJoinRules);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.member" } }),
|
||||
EventType::RoomMember);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.member"}}), EventType::RoomMember);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.message"}}),
|
||||
EventType::RoomMessage);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.name"}}), EventType::RoomName);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.power_levels"}}),
|
||||
EventType::RoomPowerLevels);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{ { "type", "m.room.topic" } }),
|
||||
EventType::RoomTopic);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.topic"}}), EventType::RoomTopic);
|
||||
EXPECT_EQ(extractEventType(QJsonObject{{"type", "m.room.unknown"}}),
|
||||
EventType::Unsupported);
|
||||
}
|
||||
|
||||
TEST(AliasesEventContent, Deserialization)
|
||||
{
|
||||
auto data =
|
||||
QJsonObject{ { "aliases", QJsonArray{ "#test:matrix.org", "#test2:matrix.org" } } };
|
||||
auto data = QJsonObject{{"aliases", QJsonArray{"#test:matrix.org", "#test2:matrix.org"}}};
|
||||
|
||||
AliasesEventContent content;
|
||||
content.deserialize(data);
|
||||
|
|
Loading…
Reference in a new issue