0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-28 19:58:53 +02:00

ircd:Ⓜ️:device: Add access_token_to_id() lookup suite.

This commit is contained in:
Jason Volk 2019-08-06 18:28:57 -07:00
parent 14f16f2b0d
commit f3ada0ed9c
2 changed files with 53 additions and 0 deletions

View file

@ -120,6 +120,9 @@ struct ircd::m::device
using closure = std::function<void (const string_view &)>;
using closure_bool = std::function<bool (const string_view &)>;
static bool access_token_to_id(const string_view &token, const closure &); //nothrow
static id::buf access_token_to_id(const string_view &token);
static bool for_each(const user &, const closure_bool &); // each device_id
static bool for_each(const user &, const string_view &id, const closure_bool &); // each property
static bool get(std::nothrow_t, const user &, const string_view &id, const string_view &prop, const closure &);

View file

@ -265,3 +265,53 @@ ircd::m::device::for_each(const m::user &user,
const m::room::state state{user_room};
return state.for_each("ircd.device.device_id", state_key);
}
ircd::m::device::id::buf
IRCD_MODULE_EXPORT
ircd::m::device::access_token_to_id(const string_view &token)
{
id::buf ret;
access_token_to_id(token, [&ret]
(const string_view &device_id)
{
ret = device_id;
});
return ret;
}
bool
IRCD_MODULE_EXPORT
ircd::m::device::access_token_to_id(const string_view &token,
const closure &closure)
{
const m::room::state &state{m::user::tokens};
const m::event::idx &event_idx
{
state.get(std::nothrow, "ircd.access_token", token)
};
bool ret{false};
const auto device_id{[&closure, &ret]
(const json::object &content)
{
const json::string &device_id
{
content["device_id"]
};
if(likely(device_id))
{
closure(device_id);
ret = true;
}
}};
if(!event_idx)
return ret;
if(!m::get(std::nothrow, event_idx, "content", device_id))
return ret;
return ret;
}