From aaced2b8a96e0c637919e19bd17a6c86a7f2876e Mon Sep 17 00:00:00 2001 From: Hamzeh Date: Fri, 6 Jan 2023 22:51:50 +0330 Subject: [PATCH] Support location message type --- CMakeLists.txt | 1 + include/mtx.hpp | 1 + include/mtx/events/collections.hpp | 6 ++- include/mtx/events/common.hpp | 4 +- include/mtxclient/http/client.hpp | 1 + lib/http/client.cpp | 1 + lib/structs/events/collections.cpp | 6 ++- lib/structs/events/common.cpp | 29 ++++++++++++++ lib/structs/events/messages/location.cpp | 40 ++++++++++++++++++++ lib/structs/responses/common.cpp | 4 +- meson.build | 1 + tests/messages.cpp | 48 +++++++++++++++++++++++- 12 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 lib/structs/events/messages/location.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ee4f1158..381267d23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,6 +247,7 @@ target_sources(matrix_client lib/structs/events/messages/emote.cpp lib/structs/events/messages/file.cpp lib/structs/events/messages/image.cpp + lib/structs/events/messages/location.cpp lib/structs/events/messages/notice.cpp lib/structs/events/messages/text.cpp lib/structs/events/messages/video.cpp diff --git a/include/mtx.hpp b/include/mtx.hpp index 35024284c..5a2136347 100644 --- a/include/mtx.hpp +++ b/include/mtx.hpp @@ -28,6 +28,7 @@ #include "mtx/events/messages/emote.hpp" #include "mtx/events/messages/file.hpp" #include "mtx/events/messages/image.hpp" +#include "mtx/events/messages/location.hpp" #include "mtx/events/messages/notice.hpp" #include "mtx/events/messages/text.hpp" #include "mtx/events/messages/video.hpp" diff --git a/include/mtx/events/collections.hpp b/include/mtx/events/collections.hpp index f4fd41014..7cd6b15bd 100644 --- a/include/mtx/events/collections.hpp +++ b/include/mtx/events/collections.hpp @@ -43,6 +43,7 @@ #include "mtx/events/messages/emote.hpp" #include "mtx/events/messages/file.hpp" #include "mtx/events/messages/image.hpp" +#include "mtx/events/messages/location.hpp" #include "mtx/events/messages/notice.hpp" #include "mtx/events/messages/text.hpp" #include "mtx/events/messages/video.hpp" @@ -170,7 +171,7 @@ using TimelineEvents = mtx::events::RoomEvent, mtx::events::RoomEvent, mtx::events::RoomEvent, - // TODO: events::RoomEvent, + mtx::events::RoomEvent, mtx::events::RoomEvent, mtx::events::RoomEvent, mtx::events::RoomEvent, @@ -228,6 +229,9 @@ template<> constexpr inline EventType message_content_to_type = EventType::RoomMessage; template<> +constexpr inline EventType message_content_to_type = + EventType::RoomMessage; +template<> constexpr inline EventType message_content_to_type = EventType::RoomMessage; template<> diff --git a/include/mtx/events/common.hpp b/include/mtx/events/common.hpp index e9cb5e27c..980857658 100644 --- a/include/mtx/events/common.hpp +++ b/include/mtx/events/common.hpp @@ -146,10 +146,10 @@ struct LocationInfo std::string blurhash; //! Deserialization method needed by @p nlohmann::json. - friend void from_json(const nlohmann::json &obj, ThumbnailInfo &info); + friend void from_json(const nlohmann::json &obj, LocationInfo &info); //! Serialization method needed by @p nlohmann::json. - friend void to_json(nlohmann::json &obj, const ThumbnailInfo &info); + friend void to_json(nlohmann::json &obj, const LocationInfo &info); }; //! Definition of rel_type for relations. diff --git a/include/mtxclient/http/client.hpp b/include/mtxclient/http/client.hpp index 53ac0c978..1b1115848 100644 --- a/include/mtxclient/http/client.hpp +++ b/include/mtxclient/http/client.hpp @@ -894,6 +894,7 @@ MTXCLIENT_SEND_ROOM_MESSAGE_FWD(mtx::events::msg::Audio) MTXCLIENT_SEND_ROOM_MESSAGE_FWD(mtx::events::msg::Emote) MTXCLIENT_SEND_ROOM_MESSAGE_FWD(mtx::events::msg::File) MTXCLIENT_SEND_ROOM_MESSAGE_FWD(mtx::events::msg::Image) +MTXCLIENT_SEND_ROOM_MESSAGE_FWD(mtx::events::msg::Location) MTXCLIENT_SEND_ROOM_MESSAGE_FWD(mtx::events::msg::Notice) MTXCLIENT_SEND_ROOM_MESSAGE_FWD(mtx::events::msg::Text) MTXCLIENT_SEND_ROOM_MESSAGE_FWD(mtx::events::msg::Video) diff --git a/lib/http/client.cpp b/lib/http/client.cpp index ba0a02bf8..a13db4f93 100644 --- a/lib/http/client.cpp +++ b/lib/http/client.cpp @@ -1736,6 +1736,7 @@ MTXCLIENT_SEND_ROOM_MESSAGE(mtx::events::msg::Audio) MTXCLIENT_SEND_ROOM_MESSAGE(mtx::events::msg::Emote) MTXCLIENT_SEND_ROOM_MESSAGE(mtx::events::msg::File) MTXCLIENT_SEND_ROOM_MESSAGE(mtx::events::msg::Image) +MTXCLIENT_SEND_ROOM_MESSAGE(mtx::events::msg::Location) MTXCLIENT_SEND_ROOM_MESSAGE(mtx::events::msg::Notice) MTXCLIENT_SEND_ROOM_MESSAGE(mtx::events::msg::Text) MTXCLIENT_SEND_ROOM_MESSAGE(mtx::events::msg::Video) diff --git a/lib/structs/events/collections.cpp b/lib/structs/events/collections.cpp index 35519c591..045d0f027 100644 --- a/lib/structs/events/collections.cpp +++ b/lib/structs/events/collections.cpp @@ -45,6 +45,7 @@ MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::RoomEvent, mtx::events::msg::Confet MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::RoomEvent, mtx::events::msg::Emote) MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::RoomEvent, mtx::events::msg::File) MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::RoomEvent, mtx::events::msg::Image) +MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::RoomEvent, mtx::events::msg::Location) MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::RoomEvent, mtx::events::msg::Notice) MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::RoomEvent, mtx::events::msg::Text) MTXCLIENT_INSTANTIATE_JSON_FUNCTIONS(events::RoomEvent, mtx::events::msg::Video) @@ -309,8 +310,9 @@ from_json(const nlohmann::json &obj, TimelineEvent &e) break; } case MsgType::Location: { - /* events::RoomEvent location = e; */ - /* container.emplace_back(location); */ + // events::RoomEvent location = e; + // container.emplace_back(location); + e.data = events::RoomEvent(obj); break; } case MsgType::Notice: { diff --git a/lib/structs/events/common.cpp b/lib/structs/events/common.cpp index 779972129..80c40c151 100644 --- a/lib/structs/events/common.cpp +++ b/lib/structs/events/common.cpp @@ -169,6 +169,35 @@ to_json(json &obj, const VideoInfo &info) obj["xyz.amorgan.blurhash"] = info.blurhash; } +void +from_json(const json &obj, LocationInfo &info) +{ + if (obj.find("thumbnail_url") != obj.end()) + info.thumbnail_url = obj.at("thumbnail_url").get(); + + if (obj.find("thumbnail_info") != obj.end()) + info.thumbnail_info = obj.at("thumbnail_info").get(); + + if (obj.find("thumbnail_file") != obj.end()) + info.thumbnail_file = obj.at("thumbnail_file").get(); + + if (obj.find("xyz.amorgan.blurhash") != obj.end()) + info.blurhash = obj.at("xyz.amorgan.blurhash").get(); +} + +void +to_json(json &obj, const LocationInfo &info) +{ + if (!info.thumbnail_url.empty()) { + obj["thumbnail_url"] = info.thumbnail_url; + obj["thumbnail_info"] = info.thumbnail_info; + } + if (info.thumbnail_file) + obj["thumbnail_file"] = info.thumbnail_file.value(); + if (!info.blurhash.empty()) + obj["xyz.amorgan.blurhash"] = info.blurhash; +} + void to_json(json &obj, const RelationType &type) { diff --git a/lib/structs/events/messages/location.cpp b/lib/structs/events/messages/location.cpp new file mode 100644 index 000000000..0c289ee53 --- /dev/null +++ b/lib/structs/events/messages/location.cpp @@ -0,0 +1,40 @@ +#include +#include + +#include "mtx/events/common.hpp" +#include "mtx/events/messages/location.hpp" + +using json = nlohmann::json; + +namespace mtx { +namespace events { +namespace msg { + +void +from_json(const json &obj, Location &content) +{ + content.body = obj.at("body").get(); + content.msgtype = obj.at("msgtype").get(); + if (obj.find("geo_uri") != obj.end()) + content.geo_uri = obj.at("geo_uri").get(); + + if (obj.find("info") != obj.end()) + content.info = obj.at("info").get(); + + content.relations = common::parse_relations(obj); +} + +void +to_json(json &obj, const Location &content) +{ + obj["msgtype"] = "m.location"; + obj["body"] = content.body; + + obj["geo_uri"] = content.geo_uri; + obj["info"] = content.info; + common::apply_relations(obj, content.relations); +} + +} // namespace msg +} // namespace events +} // namespace mtx diff --git a/lib/structs/responses/common.cpp b/lib/structs/responses/common.cpp index 39dae2e82..8faae1deb 100644 --- a/lib/structs/responses/common.cpp +++ b/lib/structs/responses/common.cpp @@ -410,8 +410,8 @@ parse_timeline_events(const json &events, break; } case MsgType::Location: { - /* events::RoomEvent location = e; */ - /* container.emplace_back(location); */ + events::RoomEvent location = e; + container.emplace_back(location); break; } case MsgType::Notice: { diff --git a/meson.build b/meson.build index 1718e4cfd..85f94401a 100644 --- a/meson.build +++ b/meson.build @@ -85,6 +85,7 @@ src = [ 'lib/structs/events/messages/emote.cpp', 'lib/structs/events/messages/file.cpp', 'lib/structs/events/messages/image.cpp', + 'lib/structs/events/messages/location.cpp', 'lib/structs/events/messages/notice.cpp', 'lib/structs/events/messages/text.cpp', 'lib/structs/events/messages/video.cpp', diff --git a/tests/messages.cpp b/tests/messages.cpp index 1a3a19653..5b7b7d9a8 100644 --- a/tests/messages.cpp +++ b/tests/messages.cpp @@ -473,7 +473,53 @@ TEST(RoomEvents, ImageMessage) mtx::common::RelationType::InReplyTo); } -TEST(RoomEvents, LocationMessage) {} +TEST(RoomEvents, LocationMessage) { + json data = R"({ + "content": { + "body": "Big Ben, London, UK", + "geo_uri": "geo:51.5008,0.1247", + "info": { + "thumbnail_info": { + "h": 300, + "mimetype": "image/jpeg", + "size": 46144, + "w": 300 + }, + "thumbnail_url": "mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe" + }, + "msgtype": "m.location", + "m.relates_to": { + "m.in_reply_to": { + "event_id": "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E" + } + } + }, + "event_id": "$143273582443PhrSn:example.org", + "origin_server_ts": 1432735824653, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "sender": "@example:example.org", + "type": "m.room.message", + "unsigned": { + "age": 69168455 + } + } + )"_json; + RoomEvent event = data.get>(); + + EXPECT_EQ(event.type, EventType::RoomMessage); + EXPECT_EQ(event.event_id, "$143273582443PhrSn:example.org"); + EXPECT_EQ(event.room_id, "!jEsUZKDJdhlrceRyVU:example.org"); + EXPECT_EQ(event.sender, "@example:example.org"); + EXPECT_EQ(event.origin_server_ts, 1432735824653); + EXPECT_EQ(event.unsigned_data.age, 69168455); + EXPECT_EQ(event.content.body, + "Big Ben, London, UK"); + EXPECT_EQ(event.content.msgtype, "m.location"); + EXPECT_EQ(event.content.relations.relations.at(0).event_id, + "$6GKhAfJOcwNd69lgSizdcTob8z2pWQgBOZPrnsWMA1E"); + EXPECT_EQ(event.content.relations.relations.at(0).rel_type, + mtx::common::RelationType::InReplyTo); +} TEST(RoomEvents, NoticeMessage) {