Skip to content
Snippets Groups Projects
Verified Commit 8cd268b9 authored by Nicolas Werner's avatar Nicolas Werner
Browse files

Add support for intentional mentions

parent 158e991f
No related branches found
No related tags found
No related merge requests found
Pipeline #5652 failed
Showing
with 118 additions and 5 deletions
......@@ -152,6 +152,33 @@ struct LocationInfo
friend void to_json(nlohmann::json &obj, const ThumbnailInfo &info);
};
/// @brief Mentions metadata
///
/// @sa https://spec.matrix.org/v1.9/client-server-api/#user-and-room-mentions
struct Mentions
{
std::vector<std::string> user_ids;
bool room = false;
//! Deserialization method needed by @p nlohmann::json.
friend void from_json(const nlohmann::json &obj, Mentions &info);
//! Serialization method needed by @p nlohmann::json.
friend void to_json(nlohmann::json &obj, const Mentions &info);
};
/// @brief Parses mentions from a content object
///
/// @param obj The content object of an event.
std::optional<Mentions>
parse_mentions(const nlohmann::json &obj);
/// @brief Serializes mentions to a content object
///
/// @param obj The content object of an event.
void
add_mentions(nlohmann::json &content, const std::optional<Mentions> &m);
//! Definition of rel_type for relations.
enum class RelationType
{
......@@ -201,11 +228,11 @@ struct Relations
//! im.nheko.relactions.v1.relations
bool synthesized = false;
std::optional<std::string> reply_to(bool include_fallback = true) const;
std::optional<std::string> replaces(bool include_fallback = true) const;
std::optional<std::string> references(bool include_fallback = true) const;
std::optional<std::string> thread(bool include_fallback = true) const;
std::optional<Relation> annotates(bool include_fallback = true) const;
[[nodiscard]] std::optional<std::string> reply_to(bool include_fallback = true) const;
[[nodiscard]] std::optional<std::string> replaces(bool include_fallback = true) const;
[[nodiscard]] std::optional<std::string> references(bool include_fallback = true) const;
[[nodiscard]] std::optional<std::string> thread(bool include_fallback = true) const;
[[nodiscard]] std::optional<Relation> annotates(bool include_fallback = true) const;
};
/// @brief Parses relations from a content object
......
......@@ -36,6 +36,9 @@ struct Audio
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, Audio &content);
friend void to_json(nlohmann::json &obj, const Audio &content);
};
......
......@@ -31,6 +31,9 @@ struct ElementEffect
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, ElementEffect &content);
friend void to_json(nlohmann::json &obj, const ElementEffect &content);
};
......
......@@ -31,6 +31,9 @@ struct Emote
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, Emote &content);
friend void to_json(nlohmann::json &obj, const Emote &content);
};
......
......@@ -41,6 +41,9 @@ struct File
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, File &content);
friend void to_json(nlohmann::json &obj, const File &content);
};
......
......@@ -37,6 +37,9 @@ struct Image
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, Image &content);
friend void to_json(nlohmann::json &obj, const Image &content);
};
......@@ -57,6 +60,9 @@ struct StickerImage
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, StickerImage &content);
friend void to_json(nlohmann::json &obj, const StickerImage &content);
};
......
......@@ -32,6 +32,9 @@ struct Location
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, Location &content);
friend void to_json(nlohmann::json &obj, const Location &content);
};
......
......@@ -31,6 +31,9 @@ struct Notice
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, Notice &content);
friend void to_json(nlohmann::json &obj, const Notice &content);
};
......
......@@ -32,6 +32,9 @@ struct Text
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, Text &content);
friend void to_json(nlohmann::json &obj, const Text &content);
};
......
......@@ -30,6 +30,9 @@ struct Unknown
//! The original content of the message.
std::string content;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, Unknown &content);
friend void to_json(nlohmann::json &obj, const Unknown &content);
};
......
......@@ -36,6 +36,9 @@ struct Video
//! Relates to for rich replies
mtx::common::Relations relations;
//! Mentioned users by this event
std::optional<mtx::common::Mentions> mentions;
friend void from_json(const nlohmann::json &obj, Video &content);
friend void to_json(nlohmann::json &obj, const Video &content);
};
......
......@@ -169,6 +169,25 @@ to_json(json &obj, const VideoInfo &info)
obj["xyz.amorgan.blurhash"] = info.blurhash;
}
void
from_json(const json &obj, Mentions &info)
{
info.room = obj.value("room", false);
info.user_ids = obj.value("user_ids", std::vector<std::string>{});
}
void
to_json(json &obj, const Mentions &info)
{
obj = json::object();
if (info.room)
obj["room"] = true;
if (!info.user_ids.empty())
obj["user_ids"] = info.user_ids;
}
void
to_json(json &obj, const RelationType &type)
{
......@@ -213,6 +232,22 @@ from_json(const json &obj, RelationType &type)
type = RelationType::Unsupported;
}
std::optional<Mentions>
parse_mentions(const nlohmann::json &content)
{
if (auto mentions = content.find("m.mentions"); mentions != content.end())
return mentions->get<Mentions>();
return std::nullopt;
}
void
add_mentions(nlohmann::json &content, const std::optional<Mentions> &m)
{
if (m)
content["m.mentions"] = *m;
}
Relations
parse_relations(const nlohmann::json &content)
{
......
......@@ -27,6 +27,7 @@ from_json(const json &obj, Audio &content)
content.file = obj.at("file").get<crypto::EncryptedFile>();
content.relations = common::parse_relations(obj);
content.mentions = common::parse_mentions(obj);
}
void
......@@ -42,6 +43,7 @@ to_json(json &obj, const Audio &content)
obj["url"] = content.url;
common::apply_relations(obj, content.relations);
common::add_mentions(obj, content.mentions);
}
} // namespace msg
......
......@@ -23,6 +23,7 @@ from_json(const json &obj, ElementEffect &content)
content.formatted_body = obj.at("formatted_body").get<std::string>();
content.relations = common::parse_relations(obj);
content.mentions = common::parse_mentions(obj);
}
void
......@@ -37,6 +38,7 @@ to_json(json &obj, const ElementEffect &content)
}
common::apply_relations(obj, content.relations);
common::add_mentions(obj, content.mentions);
}
} // namespace msg
......
......@@ -23,6 +23,7 @@ from_json(const json &obj, Emote &content)
content.formatted_body = obj.at("formatted_body").get<std::string>();
content.relations = common::parse_relations(obj);
content.mentions = common::parse_mentions(obj);
}
void
......@@ -37,6 +38,7 @@ to_json(json &obj, const Emote &content)
}
common::apply_relations(obj, content.relations);
common::add_mentions(obj, content.mentions);
}
} // namespace msg
......
......@@ -30,6 +30,7 @@ from_json(const json &obj, File &content)
content.file = obj.at("file").get<crypto::EncryptedFile>();
content.relations = common::parse_relations(obj);
content.mentions = common::parse_mentions(obj);
}
void
......@@ -48,6 +49,7 @@ to_json(json &obj, const File &content)
obj["url"] = content.url;
common::apply_relations(obj, content.relations);
common::add_mentions(obj, content.mentions);
}
} // namespace msg
......
......@@ -26,6 +26,7 @@ from_json(const json &obj, Image &content)
content.file = obj.at("file").get<crypto::EncryptedFile>();
content.relations = common::parse_relations(obj);
content.mentions = common::parse_mentions(obj);
}
void
......@@ -41,6 +42,7 @@ to_json(json &obj, const Image &content)
obj["url"] = content.url;
common::apply_relations(obj, content.relations);
common::add_mentions(obj, content.mentions);
}
void
......@@ -57,6 +59,7 @@ from_json(const json &obj, StickerImage &content)
content.file = obj.at("file").get<crypto::EncryptedFile>();
content.relations = common::parse_relations(obj);
content.mentions = common::parse_mentions(obj);
}
void
......@@ -71,6 +74,7 @@ to_json(json &obj, const StickerImage &content)
obj["url"] = content.url;
common::apply_relations(obj, content.relations);
common::add_mentions(obj, content.mentions);
}
} // namespace msg
......
......@@ -23,6 +23,7 @@ from_json(const json &obj, Notice &content)
content.formatted_body = obj.at("formatted_body").get<std::string>();
content.relations = common::parse_relations(obj);
content.mentions = common::parse_mentions(obj);
}
void
......@@ -37,6 +38,7 @@ to_json(json &obj, const Notice &content)
}
common::apply_relations(obj, content.relations);
common::add_mentions(obj, content.mentions);
}
} // namespace msg
......
......@@ -23,6 +23,7 @@ from_json(const json &obj, Text &content)
content.formatted_body = obj.at("formatted_body").get<std::string>();
content.relations = common::parse_relations(obj);
content.mentions = common::parse_mentions(obj);
}
void
......@@ -37,6 +38,7 @@ to_json(json &obj, const Text &content)
}
common::apply_relations(obj, content.relations);
common::add_mentions(obj, content.mentions);
}
} // namespace msg
......
......@@ -17,6 +17,7 @@ from_json(const json &obj, Unknown &content)
content.body = obj.at("body").get<std::string>();
content.msgtype = obj.at("msgtype").get<std::string>();
content.relations = common::parse_relations(obj);
content.mentions = common::parse_mentions(obj);
}
void
......@@ -27,6 +28,7 @@ to_json(json &obj, const Unknown &content)
obj["msgtype"] = content.msgtype;
obj["body"] = content.body;
common::apply_relations(obj, content.relations);
common::add_mentions(obj, content.mentions);
}
} // namespace msg
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment