diff --git a/include/ircd/m/device.h b/include/ircd/m/device.h index 350b8d7c4..5d097d942 100644 --- a/include/ircd/m/device.h +++ b/include/ircd/m/device.h @@ -120,6 +120,9 @@ struct ircd::m::device using closure = std::function; using closure_bool = std::function; + 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 &); diff --git a/modules/m_device.cc b/modules/m_device.cc index dd5af39c2..859f0e58b 100644 --- a/modules/m_device.cc +++ b/modules/m_device.cc @@ -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; +}