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

ircd:Ⓜ️ Support query for non-membership with empty string to interface.

This commit is contained in:
Jason Volk 2019-09-18 10:53:51 -07:00
parent 11699baff0
commit 0d25e68c50
2 changed files with 26 additions and 11 deletions

View file

@ -20,13 +20,16 @@ namespace ircd::m
// is not checked here, only content.membership is sought. // is not checked here, only content.membership is sought.
string_view membership(const mutable_buffer &out, const event::idx &); string_view membership(const mutable_buffer &out, const event::idx &);
// Query and compare membership string to argument string. Returns
// true on equal; false on not equal; false on not found.
bool membership(const event::idx &, const string_view &);
// Query and copy membership string to buffer; queries room state. (also room.h) // Query and copy membership string to buffer; queries room state. (also room.h)
string_view membership(const mutable_buffer &out, const room &, const id::user &); string_view membership(const mutable_buffer &out, const room &, const id::user &);
// Query and compare membership string to argument string. Returns true on
// equal; false on not equal; false on not found. Note in addition to this
// we allow the user to pass an empty membership string which will test for
// non-membership as well and return true.
bool membership(const event::idx &, const string_view &);
// Query and compare membership string; queries room state. (also room.h) // Query and compare membership string; queries room state. (also room.h)
// also see overload notes.
bool membership(const room &, const id::user &, const string_view &); bool membership(const room &, const id::user &, const string_view &);
} }

View file

@ -2363,16 +2363,28 @@ bool
ircd::m::membership(const event::idx &event_idx, ircd::m::membership(const event::idx &event_idx,
const string_view &membership) const string_view &membership)
{ {
return m::query(std::nothrow, event_idx, "content", [&membership] bool ret; // not initialized unless fetched=true below
(const json::object &content) const auto closure
{ {
const json::string &content_membership [&membership, &ret](const json::object &content)
{ {
content["membership"] const json::string &content_membership
}; {
content["membership"]
};
return content_membership == membership; ret = membership && content_membership == membership;
}); }
};
const bool fetched
{
m::get(std::nothrow, event_idx, "content", closure)
};
// In addition to the intuitive behavior of this function, we allow the
// user to pass an empty membership string to test non-membership as well.
return (fetched && ret) || (!fetched && !membership);
} }
ircd::string_view ircd::string_view