diff --git a/modules/client/directory/room.cc b/modules/client/directory/room.cc index 5c989affc..4a8192c29 100644 --- a/modules/client/directory/room.cc +++ b/modules/client/directory/room.cc @@ -73,6 +73,12 @@ resource::response put__directory_room(client &client, const resource::request &request) { + if(request.parv.size() < 1) + throw m::NEED_MORE_PARAMS + { + "Room alias path parameter missing" + }; + m::room::alias::buf room_alias { url::decode(room_alias, request.parv[0]) @@ -83,7 +89,23 @@ put__directory_room(client &client, unquote(request.at("room_id")) }; - if(false) + if(!exists(room_id)) + throw m::NOT_FOUND + { + "Room %s is not found here.", + string_view{room_id} + }; + + const m::room::power power{room_id}; + if(!power(request.user_id, "", "m.room.aliases", room_alias.host())) + throw m::ACCESS_DENIED + { + "Insufficient power in %s to set alias %s", + string_view{room_id}, + string_view{room_alias} + }; + + if(m::room::aliases::cache::has(room_alias)) throw m::error { http::CONFLICT, "M_EXISTS", @@ -91,8 +113,43 @@ put__directory_room(client &client, room_alias }; + const unique_buffer buf + { + 4_KiB //TODO: conf + }; + + json::stack out{buf}; + json::stack::object content{out}; + json::stack::array array + { + content, "aliases" + }; + + const m::room::aliases aliases{room_id}; + aliases.for_each(room_alias.host(), [&array] + (const m::room::alias &alias) + { + array.append(alias); + return true; + }); + + array.append(room_alias); + array.~array(); + content.~object(); + + const auto eid + { + m::send(room_id, request.user_id, "m.room.aliases", room_alias.host(), json::object + { + out.completed() + }) + }; + return resource::response { - client, http::OK + client, json::members + { + { "event_id", eid } + } }; }