From f0598bb27ae05191c1e20a2c365b5b669866f587 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 5 Mar 2019 12:04:09 -0800 Subject: [PATCH] modules/client/createroom: Set initial room state from supplied request data. modules/client/createroom: Set the room name and topic if supplied in request. modules/client/createroom: Set any state events from supplied initial_state. modules/client/createroom: Invite any users in the supplied invite array. modules/client/createroom: Set the room's initial guest_access if supplied by user. --- modules/client/createroom.cc | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/modules/client/createroom.cc b/modules/client/createroom.cc index 8fb4a39f8..f1c2025fe 100644 --- a/modules/client/createroom.cc +++ b/modules/client/createroom.cc @@ -72,6 +72,60 @@ try join(room, sender_id) }; + // Takes precedence over events set by preset, but gets overriden by name + // and topic keys. + for(const json::object &event : json::get<"initial_state"_>(request)) + { + const json::string &type(event["type"]); + if(empty(type)) + continue; + + const json::string &state_key(event["state_key"]); + const json::object &content(event["content"]); + send(room, sender_id, type, state_key, content); + } + + if(json::get<"guest_can_join"_>(request)) + { + send(room, sender_id, "m.room.guest_access", "", + { + { "guest_access", "can_join" } + }); + } + + if(json::get<"name"_>(request)) + { + static const size_t name_max_len + { + // 14.2.1.3: The name of the room. This MUST NOT exceed 255 bytes. + 255 + }; + + const auto name + { + trunc(json::get<"name"_>(request), name_max_len) + }; + + send(room, sender_id, "m.room.name", "", + { + { "name", name } + }); + } + + if(json::get<"topic"_>(request)) + { + send(room, sender_id, "m.room.topic", "", + { + { "topic", json::get<"topic"_>(request) } + }); + } + + for(const json::string &_user_id : json::get<"invite"_>(request)) + { + const m::user::id &user_id{_user_id}; + invite(room, user_id, sender_id); + } + return resource::response { client, http::CREATED,