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

Support location message type #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/mtx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion include/mtx/events/collections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -170,7 +171,7 @@ using TimelineEvents =
mtx::events::RoomEvent<mtx::events::msg::Emote>,
mtx::events::RoomEvent<mtx::events::msg::File>,
mtx::events::RoomEvent<mtx::events::msg::Image>,
// TODO: events::RoomEvent<mtx::events::msg::Location>,
mtx::events::RoomEvent<mtx::events::msg::Location>,
mtx::events::RoomEvent<mtx::events::msg::Notice>,
mtx::events::RoomEvent<mtx::events::msg::Text>,
mtx::events::RoomEvent<mtx::events::msg::Video>,
Expand Down Expand Up @@ -228,6 +229,9 @@ template<>
constexpr inline EventType message_content_to_type<mtx::events::msg::Image> =
EventType::RoomMessage;
template<>
constexpr inline EventType message_content_to_type<mtx::events::msg::Location> =
EventType::RoomMessage;
template<>
constexpr inline EventType message_content_to_type<mtx::events::msg::Notice> =
EventType::RoomMessage;
template<>
Expand Down
4 changes: 2 additions & 2 deletions include/mtx/events/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions include/mtxclient/http/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/http/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/structs/events/collections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -309,8 +310,9 @@ from_json(const nlohmann::json &obj, TimelineEvent &e)
break;
}
case MsgType::Location: {
/* events::RoomEvent<events::msg::Location> location = e; */
/* container.emplace_back(location); */
// events::RoomEvent<events::msg::Location> location = e;
// container.emplace_back(location);
Comment on lines +313 to +314
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can delete these.

e.data = events::RoomEvent<events::msg::Location>(obj);
break;
}
case MsgType::Notice: {
Expand Down
29 changes: 29 additions & 0 deletions lib/structs/events/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>();

if (obj.find("thumbnail_info") != obj.end())
info.thumbnail_info = obj.at("thumbnail_info").get<ThumbnailInfo>();

if (obj.find("thumbnail_file") != obj.end())
info.thumbnail_file = obj.at("thumbnail_file").get<crypto::EncryptedFile>();

if (obj.find("xyz.amorgan.blurhash") != obj.end())
info.blurhash = obj.at("xyz.amorgan.blurhash").get<std::string>();
Comment on lines +184 to +185
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is wrong. I think only the thumbnail has a blurhash.

}

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be serialized when a thumbnail_url or a thumbnail_file is present.

}
if (info.thumbnail_file)
obj["thumbnail_file"] = info.thumbnail_file.value();
if (!info.blurhash.empty())
obj["xyz.amorgan.blurhash"] = info.blurhash;
Comment on lines +197 to +198
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Also not existing, afaik)

}

void
to_json(json &obj, const RelationType &type)
{
Expand Down
40 changes: 40 additions & 0 deletions lib/structs/events/messages/location.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <nlohmann/json.hpp>
#include <string>

#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<std::string>();
content.msgtype = obj.at("msgtype").get<std::string>();
if (obj.find("geo_uri") != obj.end())
content.geo_uri = obj.at("geo_uri").get<std::string>();

if (obj.find("info") != obj.end())
content.info = obj.at("info").get<common::LocationInfo>();

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
4 changes: 2 additions & 2 deletions lib/structs/responses/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ parse_timeline_events(const json &events,
break;
}
case MsgType::Location: {
/* events::RoomEvent<events::msg::Location> location = e; */
/* container.emplace_back(location); */
events::RoomEvent<events::msg::Location> location = e;
container.emplace_back(location);
Comment on lines +413 to +414
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write that in the style of the lines above, i.e.:

Suggested change
events::RoomEvent<events::msg::Location> location = e;
container.emplace_back(location);
container.emplace_back(events::RoomEvent<events::msg::Location>(e));

break;
}
case MsgType::Notice: {
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
48 changes: 47 additions & 1 deletion tests/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<msg::Location> event = data.get<RoomEvent<msg::Location>>();

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)
{
Expand Down