0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 10:12:39 +01:00

ircd:Ⓜ️ Add exists(room_alias) with option for remote query; translate not_found exception.

This commit is contained in:
Jason Volk 2018-03-08 11:39:09 -08:00
parent dc59cb8880
commit be2a7007fa
3 changed files with 48 additions and 0 deletions

View file

@ -27,6 +27,7 @@ namespace ircd::m
// [GET] Util
bool exists(const room &);
bool exists(const id::room &);
bool exists(const id::room_alias &, const bool &remote = false);
id::room room_id(const mutable_buffer &, const id::room_alias &);
id::room::buf room_id(const id::room_alias &);

View file

@ -1139,6 +1139,20 @@ ircd::m::room_id(const mutable_buffer &out,
return function(out, room_alias);
}
bool
ircd::m::exists(const id::room_alias &room_alias,
const bool &remote_query)
{
using prototype = bool (const id::room_alias, const bool &);
static import<prototype> function
{
"client_directory_room", "room_alias_exists"
};
return function(room_alias, remote_query);
}
///////////////////////////////////////////////////////////////////////////////
//
// m/hook.h

View file

@ -36,6 +36,10 @@ extern "C" m::id::room
room_id__room_alias(const mutable_buffer &out,
const m::id::room_alias &);
extern "C" bool
room_alias_exists(const m::id::room_alias &,
const bool &remote_query);
resource::response
get__directory_room(client &client,
const resource::request &request)
@ -104,12 +108,34 @@ static json::object
room_alias_fetch(const mutable_buffer &out,
const m::id::room_alias &alias);
bool
room_alias_exists(const m::id::room_alias &alias,
const bool &remote_query)
try
{
const m::room alias_room{alias_room_id};
if(alias_room.has("ircd.alias", alias))
return true;
if(!remote_query)
return false;
char buf[256];
room_id__room_alias(buf, alias);
return true;
}
catch(const m::NOT_FOUND &)
{
return false;
}
/// Translate a room alias into a room_id. This function first checks the
/// local cache. A cache miss will then cause in a query to the remote, the
/// result of which will be added to cache.
m::id::room
room_id__room_alias(const mutable_buffer &out,
const m::id::room_alias &alias)
try
{
m::id::room ret;
const auto cache_closure{[&out, &ret]
@ -168,6 +194,13 @@ room_id__room_alias(const mutable_buffer &out,
}
};
}
catch(const http::error &e)
{
if(e.code == http::NOT_FOUND)
throw m::NOT_FOUND{};
throw;
}
/// This function makes a room alias request to a remote. The alias
/// room cache is not checked or updated from here, this is only the