Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor relations for edits #48

Merged
merged 4 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions include/mtx/events/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,31 +143,18 @@ from_json(const nlohmann::json &obj, VideoInfo &info);
void
to_json(nlohmann::json &obj, const VideoInfo &info);

//! In reply to data for rich replies (notice and text events)
struct InReplyTo
{
//! Event id being replied to
std::string event_id;
};

//! Deserialization method needed by @p nlohmann::json.
void
from_json(const nlohmann::json &obj, InReplyTo &in_reply_to);

//! Serialization method needed by @p nlohmann::json.
void
to_json(nlohmann::json &obj, const InReplyTo &in_reply_to);

//! Definition of rel_type for relations.
enum class RelationType
{
// m.annotation rel_type
//! m.annotation rel_type
Annotation,
// m.reference rel_type
//! m.reference rel_type
Reference,
// m.replace rel_type
//! m.replace rel_type
Replace,
// not one of the supported types
//! im.nheko.relations.v1.in_reply_to rel_type
InReplyTo,
//! not one of the supported types
Unsupported
};

Expand All @@ -178,38 +165,51 @@ void
to_json(nlohmann::json &obj, const RelationType &type);

//! Relates to for reactions
struct RelatesTo
struct Relation
{
// Type of relation
RelationType rel_type;
// event id being reacted to
std::string event_id;
// key is the reaction itself
std::optional<std::string> key;
//! Type of relation
RelationType rel_type = RelationType::Unsupported;
//! event id being reacted to
std::string event_id = "";
//! key is the reaction itself
std::optional<std::string> key = std::nullopt;
};

//! Deserialization method needed by @p nlohmann::json.
void
from_json(const nlohmann::json &obj, RelatesTo &relates_to);

//! Serialization method needed by @p nlohmann::json.
from_json(const nlohmann::json &obj, Relation &relation);
void
to_json(nlohmann::json &obj, const RelatesTo &relates_to);
to_json(nlohmann::json &obj, const Relation &relation);

//! Relates to data for rich replies (notice and text events)
struct ReplyRelatesTo
//! Multiple relations for a event
struct Relations
{
//! What the message is in reply to
InReplyTo in_reply_to;
//! All the relations for this event
std::vector<Relation> relations;
//! Flag, if we generated this from relates_to relations or used
//! im.nheko.relactions.v1.relations
bool synthesized = false;

std::optional<std::string> reply_to() const;
std::optional<std::string> replaces() const;
std::optional<std::string> references() const;
std::optional<Relation> annotates() const;
};

//! Deserialization method needed by @p nlohmann::json.
void
from_json(const nlohmann::json &obj, ReplyRelatesTo &relates_to);
/// @brief Parses relations from a content object
///
/// @param obj The content object of an event.
Relations
parse_relations(const nlohmann::json &obj);

//! Serialization method needed by @p nlohmann::json.
/// @brief Serializes relations to a content object
///
/// @param obj The content object of an event.
void
to_json(nlohmann::json &obj, const ReplyRelatesTo &relates_to);
add_relations(nlohmann::json &obj, const Relations &relations);

/// @brief Applies also all the edit rules to the event in addition to adding the relations
///
/// @param obj The content object of an event.
void
apply_relations(nlohmann::json &obj, const Relations &relations);
} // namespace common
} // namespace mtx
21 changes: 9 additions & 12 deletions include/mtx/events/encrypted.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ from_json(const nlohmann::json &obj, OlmEncrypted &event);
void
to_json(nlohmann::json &obj, const OlmEncrypted &event);

// !TODO Change the RelatesTo to handle ReplyRelatesTo type of event
//! Content of the `m.room.encrypted` event.
struct Encrypted
{
Expand All @@ -95,10 +94,8 @@ struct Encrypted
std::string sender_key;
//! Outbound group session id.
std::string session_id;
//! Relates to for rich replies
common::ReplyRelatesTo relates_to;
//! Relates to used for verification messages
common::RelatesTo r_relates_to;
//! Relations like rich replies
common::Relations relations;
};

void
Expand Down Expand Up @@ -271,7 +268,7 @@ struct KeyVerificationStart
///
/// @note Will be used only for room-verification msgs where this is used in place of
/// transaction_id.
std::optional<mtx::common::RelatesTo> relates_to;
common::Relations relations;
};

void
Expand All @@ -292,7 +289,7 @@ struct KeyVerificationReady
//! this is used for relating this message with previously sent
//! key.verification.request will be used only for room-verification msgs where this
//! is used in place of txnid
std::optional<mtx::common::RelatesTo> relates_to;
common::Relations relations;
};

void
Expand All @@ -308,7 +305,7 @@ struct KeyVerificationDone
std::optional<std::string> transaction_id;
//! this is used for relating this message with previously sent key.verification.request
//! will be used only for room-verification msgs where this is used in place of txnid
std::optional<mtx::common::RelatesTo> relates_to;
common::Relations relations;
};

void
Expand Down Expand Up @@ -344,7 +341,7 @@ struct KeyVerificationAccept
std::string commitment;
//! this is used for relating this message with previously sent key.verification.request
//! will be used only for room-verification msgs where this is used in place of txnid
std::optional<mtx::common::RelatesTo> relates_to;
common::Relations relations;
};

void
Expand Down Expand Up @@ -389,7 +386,7 @@ struct KeyVerificationCancel
std::string code;
//! this is used for relating this message with previously sent key.verification.request
//! will be used only for room-verification msgs where this is used in place of txnid
std::optional<mtx::common::RelatesTo> relates_to;
common::Relations relations;
};

void
Expand All @@ -407,7 +404,7 @@ struct KeyVerificationKey
std::string key;
//! this is used for relating this message with previously sent key.verification.request
//! will be used only for room-verification msgs where this is used in place of txnid
std::optional<mtx::common::RelatesTo> relates_to;
common::Relations relations;
};

void
Expand All @@ -429,7 +426,7 @@ struct KeyVerificationMac
std::string keys;
//! this is used for relating this message with previously sent key.verification.request
//! will be used only for room-verification msgs where this is used in place of txnid
std::optional<mtx::common::RelatesTo> relates_to;
common::Relations relations;
};

void
Expand Down
2 changes: 1 addition & 1 deletion include/mtx/events/messages/audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Audio
//! Encryption members. If present, they replace url.
std::optional<crypto::EncryptedFile> file;
//! Relates to for rich replies
mtx::common::ReplyRelatesTo relates_to;
mtx::common::Relations relations;
};

void
Expand Down
2 changes: 1 addition & 1 deletion include/mtx/events/messages/emote.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Emote
//! HTML formatted message.
std::string formatted_body;
//! Relates to for rich replies
mtx::common::ReplyRelatesTo relates_to;
mtx::common::Relations relations;
};

void
Expand Down
2 changes: 1 addition & 1 deletion include/mtx/events/messages/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct File
//! Encryption members. If present, they replace url.
std::optional<crypto::EncryptedFile> file;
//! Relates to for rich replies
mtx::common::ReplyRelatesTo relates_to;
mtx::common::Relations relations;
};

void
Expand Down
4 changes: 2 additions & 2 deletions include/mtx/events/messages/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct Image
// Encryption members. If present, they replace url.
std::optional<crypto::EncryptedFile> file;
//! Relates to for rich replies
mtx::common::ReplyRelatesTo relates_to;
mtx::common::Relations relations;
};

//! Content of `m.sticker`.
Expand All @@ -52,7 +52,7 @@ struct StickerImage
// Encryption members. If present, they replace url.
std::optional<crypto::EncryptedFile> file;
//! Relates to for rich replies
mtx::common::ReplyRelatesTo relates_to;
mtx::common::Relations relations;
};

void
Expand Down
2 changes: 1 addition & 1 deletion include/mtx/events/messages/notice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Notice
//! HTML formatted message.
std::string formatted_body;
//! Relates to for rich replies
mtx::common::ReplyRelatesTo relates_to;
mtx::common::Relations relations;
};

void
Expand Down
2 changes: 1 addition & 1 deletion include/mtx/events/messages/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Text
//! HTML formatted message.
std::string formatted_body;
//! Relates to for rich replies
mtx::common::ReplyRelatesTo relates_to;
mtx::common::Relations relations;
};

void
Expand Down
2 changes: 1 addition & 1 deletion include/mtx/events/messages/video.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Video
//! Encryption members. If present, they replace url.
std::optional<crypto::EncryptedFile> file;
//! Relates to for rich replies
mtx::common::ReplyRelatesTo relates_to;
mtx::common::Relations relations;
};

void
Expand Down
4 changes: 2 additions & 2 deletions include/mtx/events/reaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace msg {
//! Content for the `m.reaction` event.
struct Reaction
{
//! The event being reacted to
mtx::common::RelatesTo relates_to;
//! Should be an annotation relation
common::Relations relations;
};

void
Expand Down
29 changes: 26 additions & 3 deletions include/mtx/events_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
#include "mtx/events/unknown.hpp"

namespace mtx::events {
namespace detail {

template<typename, typename = void>
struct can_edit : std::false_type
{};

template<typename Content>
struct can_edit<Content, std::void_t<decltype(Content::relations)>>
: std::is_same<decltype(Content::relations), mtx::common::Relations>
{};
}

template<class Content>
[[gnu::used, llvm::used]] void
to_json(json &obj, const Event<Content> &event)
Expand All @@ -21,9 +33,20 @@ template<class Content>
[[gnu::used, llvm::used]] void
from_json(const json &obj, Event<Content> &event)
{
event.content = obj.at("content").get<Content>();
event.type = getEventType(obj.at("type").get<std::string>());
event.sender = obj.value("sender", "");
if (obj.at("content").contains("m.new_content")) {
auto new_content = obj.at("content");
for (const auto &e : obj["content"]["m.new_content"].items()) {
if (e.key() != "m.relates_to" &&
e.key() != "im.nheko.relations.v1.relations")
new_content[e.key()] = e.value();
}
event.content = new_content.get<Content>();
} else {
event.content = obj.at("content").get<Content>();
}

event.type = getEventType(obj.at("type").get<std::string>());
event.sender = obj.value("sender", "");

if constexpr (std::is_same_v<Unknown, Content>)
event.content.type = obj.at("type").get<std::string>();
Expand Down
Loading