0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-26 18:38:52 +02:00

ircd:Ⓜ️:device: Add a proper device interface (sans device::set()).

This commit is contained in:
Jason Volk 2019-02-19 17:39:24 -08:00
parent b4ee0a9bcf
commit b3baebd395
4 changed files with 211 additions and 13 deletions

View file

@ -80,5 +80,16 @@ struct ircd::m::device
json::property<name::device_display_name, json::string>
>
{
using closure = std::function<void (const device &)>;
using closure_bool = std::function<bool (const device &)>;
using id_closure_bool = std::function<bool (const string_view &)>;
static bool for_each(const user &, const id_closure_bool &);
static bool for_each(const user &, const closure_bool &);
static bool get(std::nothrow_t, const user &, const string_view &id, const closure &);
static bool get(const user &, const string_view &id, const closure &);
static bool del(const user &, const string_view &id);
static bool set(const user &, const device &);
using super_type::tuple;
};

View file

@ -1572,6 +1572,104 @@ ircd::m::presence::valid_state(const string_view &state)
return function(state);
}
///////////////////////////////////////////////////////////////////////////////
//
// m/device.h
//
bool
ircd::m::device::set(const m::user &user,
const device &device_)
{
using prototype = bool (const m::user &, const device &);
static mods::import<prototype> function
{
"m_user", "ircd::m::device::set"
};
return function(user, device_);
}
bool
ircd::m::device::del(const m::user &user,
const string_view &id)
{
using prototype = bool (const m::user &, const string_view &);
static mods::import<prototype> function
{
"m_user", "ircd::m::device::del"
};
return function(user, id);
}
bool
ircd::m::device::get(const m::user &user,
const string_view &id,
const closure &c)
{
const bool ret
{
get(std::nothrow, user, id, c)
};
if(!ret)
throw m::NOT_FOUND
{
"Device '%s' for user %s not found",
id,
string_view{user.user_id}
};
return ret;
}
bool
ircd::m::device::get(std::nothrow_t,
const m::user &user,
const string_view &id,
const closure &c)
{
using prototype = bool (std::nothrow_t, const m::user &, const string_view &, const closure &);
static mods::import<prototype> function
{
"m_user", "ircd::m::device::get"
};
return function(std::nothrow, user, id, c);
}
bool
ircd::m::device::for_each(const m::user &user,
const id_closure_bool &c)
{
using prototype = bool (const m::user &, const id_closure_bool &);
static mods::import<prototype> function
{
"m_user", "ircd::m::device::for_each"
};
return function(user, c);
}
bool
ircd::m::device::for_each(const m::user &user,
const closure_bool &c)
{
using prototype = bool (const m::user &, const closure_bool &);
static mods::import<prototype> function
{
"m_user", "ircd::m::device::for_each"
};
return function(user, c);
}
///////////////////////////////////////////////////////////////////////////////
//
// m/node.h

View file

@ -8943,27 +8943,25 @@ console_cmd__user__device(opt &out, const string_view &line)
param.at("device_id", string_view{})
};
const m::user::room user_room
{
user_id
};
const m::room::state state
{
user_room
};
if(!device_id)
{
state.for_each("ircd.device", [&out]
(const m::event &event)
m::device::for_each(user_id, [&out]
(const string_view &device_id)
{
out << at<"state_key"_>(event) << std::endl;
out << device_id << std::endl;
return true;
});
return true;
}
m::device::get(user_id, device_id, [&out]
(const m::device &device)
{
out << device << std::endl;
return true;
});
return true;
}

View file

@ -17,6 +17,97 @@ IRCD_MODULE
"Matrix user library; modular components."
};
bool
IRCD_MODULE_EXPORT
ircd::m::device::set(const m::user &user,
const device &device)
{
return true;
}
bool
IRCD_MODULE_EXPORT
ircd::m::device::del(const m::user &user,
const string_view &id)
{
const m::user::room user_room{user};
const m::room::state state{user_room};
const m::event::idx event_idx
{
state.get(std::nothrow, "ircd.device", id)
};
if(!event_idx)
return false;
const m::event::id::buf event_id
{
m::event_id(event_idx, std::nothrow)
};
m::redact(user_room, user, event_id, "deleted");
return true;
}
bool
IRCD_MODULE_EXPORT
ircd::m::device::get(std::nothrow_t,
const m::user &user,
const string_view &id,
const closure &closure)
{
const m::user::room user_room{user};
const m::room::state state{user_room};
const m::event::idx event_idx
{
state.get(std::nothrow, "ircd.device", id)
};
if(!event_idx)
return false;
return m::get(std::nothrow, event_idx, "content", [&closure]
(const json::object &content)
{
closure(content);
});
}
bool
IRCD_MODULE_EXPORT
ircd::m::device::for_each(const m::user &user,
const closure_bool &closure)
{
return for_each(user, id_closure_bool{[&user, &closure]
(const string_view &device_id)
{
bool ret(true);
get(std::nothrow, user, device_id, [&closure, &ret]
(const device &device)
{
ret = closure(device);
});
return ret;
}});
}
bool
IRCD_MODULE_EXPORT
ircd::m::device::for_each(const m::user &user,
const id_closure_bool &closure)
{
const m::room::state::keys_bool state_key{[&closure]
(const string_view &state_key)
{
return closure(state_key);
}};
const m::user::room user_room{user};
const m::room::state state{user_room};
return state.for_each("ircd.device", state_key);
}
extern "C" m::user
user_create(const m::user::id &user_id,
const json::members &contents)