diff --git a/examples/crypto_bot.cpp b/examples/crypto_bot.cpp index f0f6064e985af3f30d79edf16686e9402babde9a..e641ef997c70f5148a8a5d7a8130524ab0ff2ade 100644 --- a/examples/crypto_bot.cpp +++ b/examples/crypto_bot.cpp @@ -365,7 +365,7 @@ send_group_message(OlmOutboundGroupSession *session, data.device_id = client->device_id(); client->send_room_message<msg::Encrypted, EventType::RoomEncrypted>( - parse<Room>(room_id), data, [](const mtx::responses::EventId &res, RequestErr err) { + room_id, data, [](const mtx::responses::EventId &res, RequestErr err) { if (err) { print_errors(err); return; @@ -663,13 +663,13 @@ void parse_messages(const mtx::responses::Sync &res) { for (const auto &room : res.rooms.invite) { - auto room_id = parse<Room>(room.first); + auto room_id = room.first; - console->info("joining room {}", room_id.to_string()); + console->info("joining room {}", room_id); client->join_room(room_id, [room_id](const nlohmann::json &, RequestErr e) { if (e) { print_errors(e); - console->error("failed to join room {}", room_id.to_string()); + console->error("failed to join room {}", room_id); return; } }); diff --git a/examples/simple_bot.cpp b/examples/simple_bot.cpp index a7725227f1b9e400875ae396bfd12bb741d02ff7..5d4796a5d48843c7ee889e514549926aaf151a75 100644 --- a/examples/simple_bot.cpp +++ b/examples/simple_bot.cpp @@ -85,13 +85,13 @@ void parse_messages(const mtx::responses::Sync &res, bool parse_repeat_cmd = false) { for (const auto room : res.rooms.invite) { - auto room_id = parse<Room>(room.first); + auto room_id = room.first; - printf("joining room %s\n", room_id.to_string().c_str()); + printf("joining room %s\n", room_id.c_str()); client->join_room(room_id, [room_id](const nlohmann::json &obj, RequestErr e) { if (e) { print_errors(e); - printf("failed to join room %s\n", room_id.to_string().c_str()); + printf("failed to join room %s\n", room_id.c_str()); return; } @@ -108,7 +108,7 @@ parse_messages(const mtx::responses::Sync &res, bool parse_repeat_cmd = false) return; } - printf("sent message to %s\n", room_id.to_string().c_str()); + printf("sent message to %s\n", room_id.c_str()); }); }); } @@ -136,9 +136,7 @@ parse_messages(const mtx::responses::Sync &res, bool parse_repeat_cmd = false) client->send_room_message<mtx::events::msg::Text, mtx::events::EventType::RoomMessage>( - parse<Room>(room_id), - text, - [room_id](const mtx::responses::EventId &, RequestErr e) { + room_id, text, [room_id](const mtx::responses::EventId &, RequestErr e) { if (e) { print_errors(e); return; diff --git a/include/mtxclient/http/client.hpp b/include/mtxclient/http/client.hpp index e747de76ec69f8d16828ea4c4f71203e185b2f3f..22402d8823d21c38bb2ba636915fd507a11b32cd 100644 --- a/include/mtxclient/http/client.hpp +++ b/include/mtxclient/http/client.hpp @@ -132,14 +132,12 @@ public: //! Create a room with the given options. void create_room(const mtx::requests::CreateRoom &room_options, Callback<mtx::responses::CreateRoom> cb); - //! Join a room by its room_id. - void join_room(const mtx::identifiers::Room &room_id, Callback<nlohmann::json> cb); //! Join a room by an alias or a room_id. void join_room(const std::string &room, Callback<nlohmann::json> cb); //! Leave a room by its room_id. - void leave_room(const mtx::identifiers::Room &room_id, Callback<nlohmann::json> cb); + void leave_room(const std::string &room_id, Callback<nlohmann::json> cb); //! Invite a user to a room. - void invite_user(const mtx::identifiers::Room &room_id, + void invite_user(const std::string &room_id, const std::string &user_id, Callback<mtx::responses::RoomInvite> cb); @@ -147,7 +145,7 @@ public: void sync(const SyncOpts &opts, Callback<mtx::responses::Sync> cb); //! Paginate through room messages. - void messages(const mtx::identifiers::Room &room_id, + void messages(const std::string &room_id, const std::string &from, const std::string &to, PaginationDirection dir, @@ -159,9 +157,7 @@ public: void versions(Callback<mtx::responses::Versions> cb); //! Mark an event as read. - void read_event(const mtx::identifiers::Room &room_id, - const mtx::identifiers::Event &event_id, - ErrCallback cb); + void read_event(const std::string &room_id, const std::string &event_id, ErrCallback cb); //! Upload a filter void upload_filter(const nlohmann::json &j, Callback<mtx::responses::FilterId> cb); @@ -184,29 +180,29 @@ public: const std::string &original_filename, RequestErr err)> cb); //! Send typing notifications to the room. - void start_typing(const mtx::identifiers::Room &room_id, uint64_t timeout, ErrCallback cb); + void start_typing(const std::string &room_id, uint64_t timeout, ErrCallback cb); //! Remove typing notifications from the room. - void stop_typing(const mtx::identifiers::Room &room_id, ErrCallback cb); + void stop_typing(const std::string &room_id, ErrCallback cb); //! Send a room message with auto-generated transaction id. template<class Payload, mtx::events::EventType Event> - void send_room_message(const mtx::identifiers::Room &room_id, + void send_room_message(const std::string &room_id, const Payload &payload, Callback<mtx::responses::EventId> cb); //! Send a room message by providing transaction id. template<class Payload, mtx::events::EventType Event> - void send_room_message(const mtx::identifiers::Room &room_id, + void send_room_message(const std::string &room_id, const std::string &txn_id, const Payload &payload, Callback<mtx::responses::EventId> cb); //! Send a state event by providing the state key. template<class Payload, mtx::events::EventType Event> - void send_state_event(const mtx::identifiers::Room &room_id, + void send_state_event(const std::string &room_id, const std::string &state_key, const Payload &payload, Callback<mtx::responses::EventId> cb); //! Send a state event with an empty state key. template<class Payload, mtx::events::EventType Event> - void send_state_event(const mtx::identifiers::Room &room_id, + void send_state_event(const std::string &room_id, const Payload &payload, Callback<mtx::responses::EventId> cb); @@ -248,8 +244,7 @@ public: Callback<mtx::responses::KeyChanges> cb); //! Enable encryption in a room by sending a `m.room.encryption` state event. - void enable_encryption(const mtx::identifiers::Room &room, - Callback<mtx::responses::EventId> cb); + void enable_encryption(const std::string &room, Callback<mtx::responses::EventId> cb); private: template<class Request, class Response> @@ -463,7 +458,7 @@ mtx::http::Client::create_session(HeadersCallback<Response> callback) template<class Payload, mtx::events::EventType Event> void -mtx::http::Client::send_room_message(const mtx::identifiers::Room &room_id, +mtx::http::Client::send_room_message(const std::string &room_id, const Payload &payload, Callback<mtx::responses::EventId> callback) { @@ -472,25 +467,25 @@ mtx::http::Client::send_room_message(const mtx::identifiers::Room &room_id, template<class Payload, mtx::events::EventType Event> void -mtx::http::Client::send_room_message(const mtx::identifiers::Room &room_id, +mtx::http::Client::send_room_message(const std::string &room_id, const std::string &txn_id, const Payload &payload, Callback<mtx::responses::EventId> callback) { - const auto api_path = "/client/r0/rooms/" + room_id.to_string() + "/send/" + - mtx::events::to_string(Event) + "/" + txn_id; + const auto api_path = + "/client/r0/rooms/" + room_id + "/send/" + mtx::events::to_string(Event) + "/" + txn_id; put<Payload, mtx::responses::EventId>(api_path, payload, callback); } template<class Payload, mtx::events::EventType Event> void -mtx::http::Client::send_state_event(const mtx::identifiers::Room &room_id, +mtx::http::Client::send_state_event(const std::string &room_id, const std::string &state_key, const Payload &payload, Callback<mtx::responses::EventId> callback) { - const auto api_path = "/client/r0/rooms/" + room_id.to_string() + "/state/" + + const auto api_path = "/client/r0/rooms/" + room_id + "/state/" + mtx::events::to_string(Event) + "/" + state_key; put<Payload, mtx::responses::EventId>(api_path, payload, callback); @@ -498,7 +493,7 @@ mtx::http::Client::send_state_event(const mtx::identifiers::Room &room_id, template<class Payload, mtx::events::EventType Event> void -mtx::http::Client::send_state_event(const mtx::identifiers::Room &room_id, +mtx::http::Client::send_state_event(const std::string &room_id, const Payload &payload, Callback<mtx::responses::EventId> callback) { diff --git a/lib/http/client.cpp b/lib/http/client.cpp index 71e7a3ae02636f51409597e959e3feb5806c1699..922598899b19082cc2ad399aec12aad2ed9f3b2f 100644 --- a/lib/http/client.cpp +++ b/lib/http/client.cpp @@ -35,7 +35,7 @@ Client::set_server(const std::string &server) } else { server_ = server; } -}; +} void Client::close() @@ -151,14 +151,6 @@ Client::create_room(const mtx::requests::CreateRoom &room_options, "/client/r0/createRoom", room_options, callback); } -void -Client::join_room(const mtx::identifiers::Room &room_id, Callback<nlohmann::json> callback) -{ - auto api_path = "/client/r0/rooms/" + room_id.to_string() + "/join"; - - post<std::string, nlohmann::json>(api_path, "", callback); -} - void Client::join_room(const std::string &room, Callback<nlohmann::json> callback) { @@ -168,22 +160,22 @@ Client::join_room(const std::string &room, Callback<nlohmann::json> callback) } void -Client::leave_room(const mtx::identifiers::Room &room_id, Callback<nlohmann::json> callback) +Client::leave_room(const std::string &room_id, Callback<nlohmann::json> callback) { - auto api_path = "/client/r0/rooms/" + room_id.to_string() + "/leave"; + auto api_path = "/client/r0/rooms/" + room_id + "/leave"; post<std::string, nlohmann::json>(api_path, "", callback); } void -Client::invite_user(const mtx::identifiers::Room &room_id, +Client::invite_user(const std::string &room_id, const std::string &user_id, Callback<mtx::responses::RoomInvite> callback) { mtx::requests::RoomInvite req; req.user_id = user_id; - auto api_path = "/client/r0/rooms/" + room_id.to_string() + "/invite"; + auto api_path = "/client/r0/rooms/" + room_id + "/invite"; post<mtx::requests::RoomInvite, mtx::responses::RoomInvite>(api_path, req, callback); } @@ -273,10 +265,9 @@ Client::download(const std::string &server, } void -Client::start_typing(const mtx::identifiers::Room &room_id, uint64_t timeout, ErrCallback callback) +Client::start_typing(const std::string &room_id, uint64_t timeout, ErrCallback callback) { - const auto api_path = - "/client/r0/rooms/" + room_id.to_string() + "/typing/" + user_id_.to_string(); + const auto api_path = "/client/r0/rooms/" + room_id + "/typing/" + user_id_.to_string(); mtx::requests::TypingNotification req; req.typing = true; @@ -286,10 +277,9 @@ Client::start_typing(const mtx::identifiers::Room &room_id, uint64_t timeout, Er } void -Client::stop_typing(const mtx::identifiers::Room &room_id, ErrCallback callback) +Client::stop_typing(const std::string &room_id, ErrCallback callback) { - const auto api_path = - "/client/r0/rooms/" + room_id.to_string() + "/typing/" + user_id_.to_string(); + const auto api_path = "/client/r0/rooms/" + room_id + "/typing/" + user_id_.to_string(); mtx::requests::TypingNotification req; req.typing = false; @@ -298,7 +288,7 @@ Client::stop_typing(const mtx::identifiers::Room &room_id, ErrCallback callback) } void -Client::messages(const mtx::identifiers::Room &room_id, +Client::messages(const std::string &room_id, const std::string &from, const std::string &to, PaginationDirection dir, @@ -318,8 +308,8 @@ Client::messages(const mtx::identifiers::Room &room_id, if (!filter.empty()) params.emplace("filter", filter); - const auto api_path = "/client/r0/rooms/" + room_id.to_string() + "/messages?" + - client::utils::query_params(params); + const auto api_path = + "/client/r0/rooms/" + room_id + "/messages?" + client::utils::query_params(params); get<mtx::responses::Messages>( api_path, [callback](const mtx::responses::Messages &res, HeaderFields, RequestErr err) { @@ -336,14 +326,11 @@ Client::upload_filter(const nlohmann::json &j, Callback<mtx::responses::FilterId } void -Client::read_event(const mtx::identifiers::Room &room_id, - const mtx::identifiers::Event &event_id, - ErrCallback callback) +Client::read_event(const std::string &room_id, const std::string &event_id, ErrCallback callback) { - const auto api_path = "/client/r0/rooms/" + room_id.to_string() + "/read_markers"; + const auto api_path = "/client/r0/rooms/" + room_id + "/read_markers"; - nlohmann::json body = {{"m.fully_read", event_id.to_string()}, - {"m.read", event_id.to_string()}}; + nlohmann::json body = {{"m.fully_read", event_id}, {"m.read", event_id}}; post<nlohmann::json, mtx::responses::Empty>( api_path, body, [callback](const mtx::responses::Empty, RequestErr err) { @@ -455,8 +442,7 @@ Client::key_changes(const std::string &from, } void -Client::enable_encryption(const mtx::identifiers::Room &room, - Callback<mtx::responses::EventId> callback) +Client::enable_encryption(const std::string &room, Callback<mtx::responses::EventId> callback) { using namespace mtx::events; state::Encryption event; diff --git a/tests/client_api.cpp b/tests/client_api.cpp index 432c6c75772953f7154739290cc8dd191a8fcfdb..9955c855090ae32c7abe5a264c6182ce51f5e385 100644 --- a/tests/client_api.cpp +++ b/tests/client_api.cpp @@ -370,12 +370,12 @@ TEST(ClientAPI, CreateRoomInvites) req.invite = {"@bob:localhost", "@carl:localhost"}; alice->create_room(req, [bob, carl](const mtx::responses::CreateRoom &res, RequestErr err) { check_error(err); - auto room_id = res.room_id; + auto room_id = res.room_id.to_string(); - bob->join_room(res.room_id, + bob->join_room(room_id, [](const nlohmann::json &, RequestErr err) { check_error(err); }); - carl->join_room(res.room_id, + carl->join_room(room_id, [](const nlohmann::json &, RequestErr err) { check_error(err); }); }); @@ -412,19 +412,18 @@ TEST(ClientAPI, JoinRoom) alice->create_room( req, [bob, alias](const mtx::responses::CreateRoom &res, RequestErr err) { check_error(err); - auto room_id = res.room_id; + auto room_id = res.room_id.to_string(); - bob->join_room(res.room_id, + bob->join_room(room_id, [](const nlohmann::json &, RequestErr err) { check_error(err); }); using namespace mtx::identifiers; - bob->join_room(parse<Room>("!random_room_id:localhost"), - [](const nlohmann::json &, RequestErr err) { - ASSERT_TRUE(err); - EXPECT_EQ( - mtx::errors::to_string(err->matrix_error.errcode), - "M_UNRECOGNIZED"); - }); + bob->join_room( + "!random_room_id:localhost", [](const nlohmann::json &, RequestErr err) { + ASSERT_TRUE(err); + EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), + "M_UNRECOGNIZED"); + }); // Join the room using an alias. bob->join_room("#" + alias + ":localhost", @@ -459,22 +458,23 @@ TEST(ClientAPI, LeaveRoom) check_error(err); auto room_id = res.room_id; - bob->join_room(res.room_id, [room_id, bob](const nlohmann::json &, RequestErr err) { - check_error(err); + bob->join_room(res.room_id.to_string(), + [room_id, bob](const nlohmann::json &, RequestErr err) { + check_error(err); - bob->leave_room(room_id, [](const nlohmann::json &, RequestErr err) { - check_error(err); - }); - }); + bob->leave_room(room_id.to_string(), + [](const nlohmann::json &, RequestErr err) { + check_error(err); + }); + }); }); // Trying to leave a non-existent room should fail. - bob->leave_room( - parse<Room>("!random_room_id:localhost"), [](const nlohmann::json &, RequestErr err) { - ASSERT_TRUE(err); - EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), "M_UNRECOGNIZED"); - EXPECT_EQ(err->matrix_error.error, "Not a known room"); - }); + bob->leave_room("!random_room_id:localhost", [](const nlohmann::json &, RequestErr err) { + ASSERT_TRUE(err); + EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), "M_UNRECOGNIZED"); + EXPECT_EQ(err->matrix_error.error, "Not a known room"); + }); alice->close(); bob->close(); @@ -503,7 +503,7 @@ TEST(ClientAPI, InviteRoom) alice->create_room( req, [alice, bob](const mtx::responses::CreateRoom &res, RequestErr err) { check_error(err); - auto room_id = res.room_id; + auto room_id = res.room_id.to_string(); alice->invite_user(room_id, "@bob:localhost", @@ -544,7 +544,7 @@ TEST(ClientAPI, InvalidInvite) alice->create_room( req, [alice, bob](const mtx::responses::CreateRoom &res, RequestErr err) { check_error(err); - auto room_id = res.room_id; + auto room_id = res.room_id.to_string(); bob->invite_user(room_id, "@carl:localhost", @@ -623,7 +623,7 @@ TEST(ClientAPI, Typing) alice->create_room(req, [alice](const mtx::responses::CreateRoom &res, RequestErr err) { check_error(err); - alice->start_typing(res.room_id, 10000, [alice, res](RequestErr err) { + alice->start_typing(res.room_id.to_string(), 10000, [alice, res](RequestErr err) { check_error(err); const auto room_id = res.room_id.to_string(); @@ -648,18 +648,20 @@ TEST(ClientAPI, Typing) while (!can_continue) sleep(); - alice->stop_typing(res.room_id, [alice, room_id](RequestErr err) { - check_error(err); - - SyncOpts opts; - opts.timeout = 0; - alice->sync( - opts, [room_id](const mtx::responses::Sync &res, RequestErr err) { - check_error(err); - auto room = res.rooms.join.at(room_id); - EXPECT_EQ(room.ephemeral.typing.size(), 0); - }); - }); + alice->stop_typing( + res.room_id.to_string(), [alice, room_id](RequestErr err) { + check_error(err); + + SyncOpts opts; + opts.timeout = 0; + alice->sync( + opts, + [room_id](const mtx::responses::Sync &res, RequestErr err) { + check_error(err); + auto room = res.rooms.join.at(room_id); + EXPECT_EQ(room.ephemeral.typing.size(), 0); + }); + }); }); }); @@ -687,10 +689,10 @@ TEST(ClientAPI, SendMessages) alice->create_room( req, [alice, bob](const mtx::responses::CreateRoom &res, RequestErr err) { check_error(err); - auto room_id = res.room_id; + auto room_id = res.room_id.to_string(); bob->join_room( - res.room_id, [alice, bob, room_id](const nlohmann::json &, RequestErr err) { + room_id, [alice, bob, room_id](const nlohmann::json &, RequestErr err) { check_error(err); // Flag to indicate when those messages would be ready to be read by @@ -726,21 +728,22 @@ TEST(ClientAPI, SendMessages) SyncOpts opts; opts.timeout = 0; - alice->sync( - opts, - [room_id, event_ids](const mtx::responses::Sync &res, - RequestErr err) { - check_error(err); - - auto ids = get_event_ids<TimelineEvents>( - res.rooms.join.at(room_id.to_string()).timeline.events); - - // The sent event ids should be visible in the timeline. - for (const auto &event_id : event_ids) - ASSERT_TRUE(std::find(ids.begin(), - ids.end(), - event_id) != std::end(ids)); - }); + alice->sync(opts, + [room_id, event_ids](const mtx::responses::Sync &res, + RequestErr err) { + check_error(err); + + auto ids = get_event_ids<TimelineEvents>( + res.rooms.join.at(room_id).timeline.events); + + // The sent event ids should be visible in the + // timeline. + for (const auto &event_id : event_ids) + ASSERT_TRUE(std::find(ids.begin(), + ids.end(), + event_id) != + std::end(ids)); + }); }); }); @@ -779,7 +782,9 @@ TEST(ClientAPI, SendStateEvents) event.name = "Bob's room"; bob->send_state_event<mtx::events::state::Name, mtx::events::EventType::RoomName>( - room_id, event, [](const mtx::responses::EventId &, RequestErr err) { + room_id.to_string(), + event, + [](const mtx::responses::EventId &, RequestErr err) { ASSERT_TRUE(err); ASSERT_EQ("M_FORBIDDEN", mtx::errors::to_string(err->matrix_error.errcode)); @@ -789,7 +794,7 @@ TEST(ClientAPI, SendStateEvents) name_event.name = "Alice's room"; alice ->send_state_event<mtx::events::state::Name, mtx::events::EventType::RoomName>( - room_id, + room_id.to_string(), name_event, [&event_ids](const mtx::responses::EventId &res, RequestErr err) { check_error(err); @@ -800,7 +805,7 @@ TEST(ClientAPI, SendStateEvents) avatar.url = "mxc://localhost/random"; alice->send_state_event<mtx::events::state::Avatar, mtx::events::EventType::RoomAvatar>( - room_id, + room_id.to_string(), avatar, [&event_ids](const mtx::responses::EventId &res, RequestErr err) { check_error(err); @@ -847,7 +852,7 @@ TEST(ClientAPI, Pagination) auto room_id = res.room_id; alice->messages( - res.room_id, + res.room_id.to_string(), "", // from "", // to PaginationDirection::Backwards, @@ -860,7 +865,7 @@ TEST(ClientAPI, Pagination) ASSERT_TRUE(res.chunk.size() > 5); ASSERT_NE(res.start, res.end); - alice->messages(room_id, + alice->messages(room_id.to_string(), res.end, "", PaginationDirection::Backwards, @@ -927,17 +932,18 @@ TEST(ClientAPI, ReadMarkers) alice ->send_room_message<mtx::events::msg::Text, mtx::events::EventType::RoomMessage>( - room_id, + room_id.to_string(), text, [alice, &event_id, room_id](const mtx::responses::EventId &res, RequestErr err) { check_error(err); - alice->read_event( - room_id, res.event_id, [&event_id, res](RequestErr err) { - check_error(err); - event_id = res.event_id.to_string(); - }); + alice->read_event(room_id.to_string(), + res.event_id.to_string(), + [&event_id, res](RequestErr err) { + check_error(err); + event_id = res.event_id.to_string(); + }); }); while (event_id.size() == 0) diff --git a/tests/e2ee.cpp b/tests/e2ee.cpp index e3641474115f389bcf31123e29d14d9377b1bd8e..5fc2722ff7e0b173124dfdde5a0a9ff61100a270 100644 --- a/tests/e2ee.cpp +++ b/tests/e2ee.cpp @@ -494,12 +494,13 @@ TEST(Encryption, EnableEncryption) joined_room = res.room_id; bob->enable_encryption( - res.room_id, [&responses](const mtx::responses::EventId &, RequestErr err) { + res.room_id.to_string(), + [&responses](const mtx::responses::EventId &, RequestErr err) { check_error(err); responses += 1; }); - carl->join_room(res.room_id, + carl->join_room(res.room_id.to_string(), [&responses](const nlohmann::json &, RequestErr err) { check_error(err); responses += 1;