0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-26 00:32:35 +01:00

ircd:Ⓜ️:device: Add unconditional direct putter; setter to check for duplicate value.

This commit is contained in:
Jason Volk 2020-04-01 12:17:26 -07:00
parent 135798c8ab
commit 9bca61313e
2 changed files with 31 additions and 6 deletions

View file

@ -132,6 +132,7 @@ struct ircd::m::device
static bool has(const user &, const string_view &id, const string_view &prop);
static bool has(const user &, const string_view &id);
static bool del(const user &, const string_view &id);
static bool put(const user &, const string_view &id, const string_view &prop, const string_view &val);
static bool set(const user &, const string_view &id, const string_view &prop, const string_view &val);
static bool set(const user &, const device &);

View file

@ -55,16 +55,18 @@ ircd::m::device::set(const m::user &user,
json::at<"device_id"_>(device)
};
json::for_each(device, [&user, &device_id]
bool ret {false};
json::for_each(device, [&user, &device_id, &ret]
(const auto &prop, auto &&val)
{
if(!json::defined(json::value(val)))
return;
set(user, device_id, prop, val);
if constexpr(std::is_assignable<string_view, decltype(val)>())
{
if(json::defined(json::value(val)))
ret |= set(user, device_id, prop, val);
}
});
return true;
return ret;
}
bool
@ -72,6 +74,28 @@ ircd::m::device::set(const m::user &user,
const string_view &id,
const string_view &prop,
const string_view &val)
{
bool dup {false};
const bool got
{
get(std::nothrow, user, id, prop, [&val, &dup]
(const json::string &existing)
{
dup = val == existing;
})
};
assert(!dup || got);
return !dup?
put(user, id, prop, val):
false;
}
bool
ircd::m::device::put(const m::user &user,
const string_view &id,
const string_view &prop,
const string_view &val)
{
char buf[m::event::TYPE_MAX_SIZE];
const string_view type{fmt::sprintf