ircd:Ⓜ️ Add m::member(event) convenience inline interface.

ircd:Ⓜ️:membership: Assertions for event type; optimize branch.
This commit is contained in:
Jason Volk 2023-02-18 10:53:26 -08:00
parent 1a8889503f
commit 0fd5514d49
4 changed files with 67 additions and 14 deletions

View File

@ -73,6 +73,7 @@ namespace ircd::m
#include "rooms_summary.h"
#include "groups.h"
#include "membership.h"
#include "member.h"
#include "filter.h"
#include "events.h"
#include "node.h"

42
include/ircd/m/member.h Normal file
View File

@ -0,0 +1,42 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2023 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_M_MEMBER_H
namespace ircd::m
{
bool member(const event &, const string_view &membership);
bool member(const event &, const vector_view<const string_view> &);
}
/// Events that are not type=m.room.member will return false; the rest will be
/// passed to the analogous m::membership function which does not check type.
inline bool
ircd::m::member(const event &event,
const vector_view<const string_view> &membership)
{
if(json::get<"type"_>(event) != "m.room.member")
return false;
return m::membership(event, membership);
}
/// Events that are not type=m.room.member will return false; the rest will be
/// passed to the analogous m::membership function which does not check type.
inline bool
ircd::m::member(const event &event,
const string_view &membership)
{
if(json::get<"type"_>(event) != "m.room.member")
return false;
return m::membership(event) == membership;
}

View File

@ -14,7 +14,7 @@
namespace ircd::m
{
// Extract membership string from event data.
string_view membership(const event &);
[[gnu::pure]] string_view membership(const event &);
// Query and copy membership string to buffer. Note that the event type
// is not checked here, only content.membership is sought.

View File

@ -82,9 +82,14 @@ bool
ircd::m::membership(const m::event &event,
const vector_view<const string_view> &membership)
{
const auto matching
{
m::membership(event)
};
const auto it
{
std::find(begin(membership), end(membership), m::membership(event))
std::find(begin(membership), end(membership), matching)
};
return it != end(membership);
@ -125,23 +130,28 @@ ircd::m::membership(const mutable_buffer &out,
ircd::string_view
ircd::m::membership(const event &event)
{
const json::object &content
{
json::get<"content"_>(event)
};
// The caller should check event type; empty is allowed if they only have content.
assert(!json::get<"type"_>(event) || json::get<"type"_>(event) == "m.room.member");
const string_view &membership
const auto &membership
{
json::get<"membership"_>(event)
};
if(membership)
return membership;
const json::string &content_membership
if(likely(!membership))
{
content.get("membership")
};
const auto &content
{
json::get<"content"_>(event)
};
return content_membership;
const json::string &membership
{
content.get("membership")
};
return membership;
}
return membership;
}