0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 15:33:54 +01:00

ircd:Ⓜ️:device: Develop interface; use individual state events for properties.

This commit is contained in:
Jason Volk 2019-02-20 13:09:32 -08:00
parent 7c09123a3c
commit 0efd723edb
8 changed files with 271 additions and 99 deletions

View file

@ -52,6 +52,7 @@ struct ircd::m::device_keys
>
{
using super_type::tuple;
using super_type::operator=;
};
struct ircd::m::device
@ -74,22 +75,23 @@ struct ircd::m::device
json::property<name::last_seen_ts, time_t>,
/// (s2s) Required. Identity keys for the device.
json::property<name::keys, device_keys>,
json::property<name::keys, json::object>,
/// (s2s) Optional display name for the 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 &)>;
using closure = std::function<void (const string_view &)>;
using 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 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 &);
static bool get(const user &, const string_view &id, const string_view &prop, const closure &);
static bool has(const user &, const string_view &id);
static bool del(const user &, const string_view &id);
static bool set(const user &, const device &);
using super_type::tuple;
using super_type::operator=;
};

View file

@ -1605,21 +1605,37 @@ ircd::m::device::del(const m::user &user,
return function(user, id);
}
bool
ircd::m::device::has(const m::user &user,
const string_view &id)
{
using prototype = bool (const m::user &, const string_view &id);
static mods::import<prototype> function
{
"m_device", "ircd::m::device::has"
};
return function(user, id);
}
bool
ircd::m::device::get(const m::user &user,
const string_view &id,
const string_view &prop,
const closure &c)
{
const bool ret
{
get(std::nothrow, user, id, c)
get(std::nothrow, user, id, prop, c)
};
if(!ret)
throw m::NOT_FOUND
{
"Device '%s' for user %s not found",
"Property '%s' for device '%s' for user %s not found",
id,
prop,
string_view{user.user_id}
};
@ -1630,30 +1646,36 @@ bool
ircd::m::device::get(std::nothrow_t,
const m::user &user,
const string_view &id,
const string_view &prop,
const closure &c)
{
using prototype = bool (std::nothrow_t, const m::user &, const string_view &, const closure &);
using prototype = bool (std::nothrow_t,
const m::user &,
const string_view &,
const string_view &,
const closure &);
static mods::import<prototype> function
{
"m_device", "ircd::m::device::get"
};
return function(std::nothrow, user, id, c);
return function(std::nothrow, user, id, prop, c);
}
bool
ircd::m::device::for_each(const m::user &user,
const id_closure_bool &c)
const string_view &id,
const closure_bool &c)
{
using prototype = bool (const m::user &, const id_closure_bool &);
using prototype = bool (const m::user &, const string_view &id, const closure_bool &);
static mods::import<prototype> function
{
"m_device", "ircd::m::device::for_each"
};
return function(user, c);
return function(user, id, c);
}
bool

View file

@ -37,6 +37,44 @@ devices_resource__unstable
}
};
static void
_get_device(json::stack::object &obj,
const m::user &user,
const string_view &device_id)
{
json::stack::member
{
obj, "device_id", device_id
};
m::device::get(std::nothrow, user, device_id, "display_name", [&obj]
(const string_view &value)
{
json::stack::member
{
obj, "display_name", unquote(value)
};
});
m::device::get(std::nothrow, user, device_id, "last_seen_ip", [&obj]
(const string_view &value)
{
json::stack::member
{
obj, "last_seen_ip", unquote(value)
};
});
m::device::get(std::nothrow, user, device_id, "last_seen_ts", [&obj]
(const string_view &value)
{
json::stack::member
{
obj, "last_seen_ts", value
};
});
}
static resource::response
get__devices_all(client &client,
const resource::request &request,
@ -62,10 +100,11 @@ get__devices_all(client &client,
top, "devices"
};
m::device::for_each(request.user_id, [&devices]
(const m::device &device)
m::device::for_each(request.user_id, [&request, &devices]
(const string_view &device_id)
{
devices.append(device);
json::stack::object obj{devices};
_get_device(obj, request.user_id, device_id);
return true;
});
@ -86,20 +125,32 @@ get__devices(client &client,
m::id::device::buf device_id
{
url::decode(device_id, request.parv[1])
url::decode(device_id, request.parv[0])
};
std::string buf;
m::device::get(request.user_id, device_id, [&buf]
(const m::device &device)
if(!m::device::has(request.user_id, device_id))
throw m::NOT_FOUND
{
buf = json::strung{device};
});
return resource::response
{
client, http::OK, json::object{buf}
"Device ID '%s' not found", device_id
};
resource::response::chunked response
{
client, http::OK
};
json::stack out
{
response.buf, response.flusher()
};
json::stack::object top
{
out
};
_get_device(top, request.user_id, device_id);
return {};
}
resource::method
@ -121,29 +172,15 @@ put__devices(client &client,
"device_id required"
};
const m::user::room user_room
{
request.user_id
};
m::id::device::buf device_id
{
url::decode(device_id, request.parv[1])
};
user_room.get("ircd.device", device_id, [&]
(const m::event &event)
{
const json::object &content
{
at<"content"_>(event)
};
m::device data{request.content};
json::get<"device_id"_>(data) = device_id;
//TODO: where is your Object.update()??
throw m::UNSUPPORTED{};
});
send(user_room, request.user_id, "ircd.device", device_id, request.content);
m::device::set(request.user_id, data);
return resource::response
{

View file

@ -90,9 +90,7 @@ post__login_password(client &client,
{ "device_id", device_id },
});
const m::user::room user_room{user};
if(!user_room.has("ircd.device", device_id))
m::send(user_room, user_id, "ircd.device", device_id, json::members
m::device::set(user_id,
{
{ "device_id", device_id },
{ "display_name", initial_device_display_name },

View file

@ -318,8 +318,8 @@ register_user(const m::registar &request,
{ "device", device_id },
});
if(gen_token && !user_room.has("ircd.device", device_id))
m::send(user_room, user_id, "ircd.device", device_id, json::members
if(gen_token)
m::device::set(user_id,
{
{ "device_id", device_id },
{ "display_name", initial_device_display_name },

View file

@ -8955,10 +8955,17 @@ console_cmd__user__device(opt &out, const string_view &line)
return true;
}
m::device::get(user_id, device_id, [&out]
(const m::device &device)
m::device::for_each(user_id, device_id, [&out, &user_id, &device_id]
(const string_view &prop)
{
out << device << std::endl;
m::device::get(std::nothrow, user_id, device_id, prop, [&out, &prop]
(const json::object &value)
{
out << prop << ": "
<< value
<< std::endl;
});
return true;
});

View file

@ -84,10 +84,39 @@ get__user_devices(client &client,
top, "devices"
};
m::device::for_each(user_id, [&devices]
(const m::device &device)
m::device::for_each(user_id, [&devices, &user_id]
(const string_view &device_id)
{
devices.append(device);
json::stack::object device
{
devices
};
json::stack::member
{
device, "device_id", device_id
};
// The property name difference here is on purpose, probably one of
// those so-called spec "thinkos"
m::device::get(std::nothrow, user_id, device_id, "display_name", [&device]
(const string_view &value)
{
json::stack::member
{
device, "device_display_name", unquote(value)
};
});
m::device::get(std::nothrow, user_id, device_id, "keys", [&device]
(const json::object &value)
{
json::stack::member
{
device, "keys", value
};
});
return true;
});

View file

@ -19,31 +19,91 @@ IRCD_MODULE_EXPORT
ircd::m::device::set(const m::user &user,
const device &device)
{
const user::room user_room{user};
const string_view &device_id
{
at<"device_id"_>(device)
};
json::for_each(device, [&user, &user_room, &device_id]
(const auto &prop, auto &&val)
{
if(!json::defined(json::value(val)))
return;
char buf[m::event::TYPE_MAX_SIZE];
const string_view type{fmt::sprintf
{
buf, "ircd.device.%s", prop
}};
m::send(user_room, user, type, device_id,
{
{ "", val }
});
});
return true;
}
/// To delete a device we iterate the user's room state for all types matching
/// ircd.device.* (and ircd.device) which have a state_key of the device_id.
/// Those events are redacted which removes them from appearing in the state.
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
const user::room user_room{user};
const room::state state{user_room};
const room::state::type_prefix type
{
state.get(std::nothrow, "ircd.device", id)
"ircd.device."
};
if(!event_idx)
return false;
state.for_each(type, [&user, &id, &user_room, &state]
(const string_view &type)
{
const auto event_idx
{
state.get(std::nothrow, type, id)
};
const m::event::id::buf event_id
const auto event_id
{
m::event_id(event_idx, std::nothrow)
};
if(event_id)
m::redact(user_room, user, event_id, "deleted");
return true;
});
return true;
}
bool
IRCD_MODULE_EXPORT
ircd::m::device::has(const m::user &user,
const string_view &id)
{
const user::room user_room{user};
const room::state state{user_room};
const room::state::type_prefix type
{
"ircd.device."
};
bool ret(false);
state.for_each(type, [&state, &id, &ret]
(const string_view &type)
{
ret = state.has(type, id);
return !ret;
});
return ret;
}
bool
@ -51,22 +111,58 @@ IRCD_MODULE_EXPORT
ircd::m::device::get(std::nothrow_t,
const m::user &user,
const string_view &id,
const string_view &prop,
const closure &closure)
{
char buf[m::event::TYPE_MAX_SIZE];
const string_view type{fmt::sprintf
{
buf, "ircd.device.%s", prop
}};
const m::user::room user_room{user};
const m::room::state state{user_room};
const m::event::idx event_idx
const auto event_idx
{
state.get(std::nothrow, "ircd.device", id)
state.get(std::nothrow, type, id)
};
if(!event_idx)
return false;
return m::get(std::nothrow, event_idx, "content", [&closure]
(const json::object &content)
{
closure(content);
const string_view &value
{
content.get("")
};
closure(value);
});
}
bool
IRCD_MODULE_EXPORT
ircd::m::device::for_each(const m::user &user,
const string_view &device_id,
const closure_bool &closure)
{
const m::user::room user_room{user};
const m::room::state state{user_room};
const room::state::type_prefix type
{
"ircd.device."
};
return state.for_each(type, [&state, &device_id, &closure]
(const string_view &type)
{
const string_view &prop
{
lstrip(type, "ircd.device.")
};
return state.has(type, device_id)?
closure(prop):
true;
});
}
@ -74,25 +170,6 @@ 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)
@ -102,5 +179,5 @@ ircd::m::device::for_each(const m::user &user,
const m::user::room user_room{user};
const m::room::state state{user_room};
return state.for_each("ircd.device", state_key);
return state.for_each("ircd.device.device_id", state_key);
}