0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-16 08:58:20 +02:00

ircd::room::auth: Improve make_refs and cleanup interface related.

This commit is contained in:
Jason Volk 2019-07-08 19:58:04 -07:00
parent aa78f6f85c
commit e0655229c5
7 changed files with 103 additions and 150 deletions

View file

@ -19,17 +19,11 @@ struct ircd::m::room::auth
using closure = std::function<void (const event::idx &)>;
using types = vector_view<const string_view>;
static bool for_each(const auth &, const closure_bool &);
static void make_refs(const auth &, json::stack::array &, const types &, const m::id::user & = {});
m::room room;
public:
bool for_each(const closure_bool &) const;
void for_each(const closure &) const;
void make_refs(json::stack::array &, const types &, const m::id::user & = {}) const;
json::array make_refs(const mutable_buffer &, const types &, const m::id::user & = {}) const;
bool make_refs(json::stack::array &, const m::event &) const;
json::array make_refs(const mutable_buffer &, const m::event &) const;
auth(const m::room &room)
:room{room}

View file

@ -2010,7 +2010,7 @@ ircd::m::event::auth::failed(const m::event &event,
// i. If the only previous event is an m.room.create and the
// state_key is the creator, allow.
if(refs.prev_events_count() == 1 && refs.auth_events_count() == 1)
if(auth_create && at<"event_id"_>(*auth_create) == refs.prev_event(0))
if(auth_create && auth_create->event_id == refs.prev_event(0))
return {};
// ii. If the sender does not match state_key, reject.

View file

@ -3817,91 +3817,101 @@ ircd::m::room::head::for_each(const head &head,
ircd::json::array
ircd::m::room::auth::make_refs(const mutable_buffer &buf,
const types &types,
const m::id::user &user)
const m::event &event)
const
{
json::stack out{buf};
json::stack::array array{out};
make_refs(array, types, user);
array.~array();
json::stack::checkpoint cp{out};
{
json::stack::array array{out};
if(!make_refs(array, event))
cp.decommit();
}
return json::array{out.completed()};
}
void
bool
ircd::m::room::auth::make_refs(json::stack::array &out,
const types &types,
const m::id::user &user)
const m::event &event)
const
{
make_refs(*this, out, types, user);
}
void
ircd::m::room::auth::for_each(const closure &c)
const
{
for_each(closure_bool{[this, &c]
(const auto &event_idx)
const m::event::id::closure &v1_ref{[&out]
(const auto &event_id)
{
c(event_idx);
return true;
}});
}
bool
ircd::m::room::auth::for_each(const closure_bool &c)
const
{
return for_each(*this, c);
}
bool
ircd::m::room::auth::for_each(const auth &a,
const closure_bool &closure)
{
const m::room &room{a.room};
const auto &event_id{room.event_id};
if(!event_id)
return false;
return true;
}
void
ircd::m::room::auth::make_refs(const auth &auth,
json::stack::array &out,
const types &types,
const user::id &user_id)
{
const m::room::state state
{
auth.room
};
const auto fetch{[&out, &state]
(const string_view &type, const string_view &state_key)
{
state.get(std::nothrow, type, state_key, m::event::id::closure{[&out]
(const auto &event_id)
json::stack::array auth{out};
auth.append(event_id);
{
json::stack::array auth{out};
auth.append(event_id);
json::stack::object nilly{auth};
json::stack::member willy
{
json::stack::object hash{auth};
json::stack::member will
{
hash, "", ""
};
}
}});
nilly, "", ""
};
}
}};
for(const auto &type : types)
fetch(type, "");
const m::event::id::closure &v3_ref{[&out]
(const auto &event_id)
{
json::stack::array auth{out};
auth.append(event_id);
}};
if(user_id)
fetch("m.room.member", user_id);
char versionbuf[64];
const auto version
{
m::version(versionbuf, room, std::nothrow)
};
assert(version);
const auto &fetch_append
{
version == "1" || version == "2"? v1_ref : v3_ref
};
const m::room::state state
{
room
};
const auto &type
{
json::get<"type"_>(event)
};
if(!type)
return false;
if(type == "m.room.create")
return false;
state.get(std::nothrow, "m.room.create", "", fetch_append);
state.get(std::nothrow, "m.room.power_levels", "", fetch_append);
if(type == "m.room.member")
if(!m::membership(event) || m::membership(event) == "join")
state.get(std::nothrow, "m.room.join_rules", "", fetch_append);
const string_view member_sender
{
defined(json::get<"sender"_>(event))?
m::user::id{at<"sender"_>(event)}:
m::user::id{}
};
if(member_sender)
state.get(std::nothrow, "m.room.member", member_sender, fetch_append);
m::user::id member_target;
if(json::get<"sender"_>(event) && json::get<"state_key"_>(event))
if(at<"sender"_>(event) != at<"state_key"_>(event))
if(valid(m::id::USER, at<"state_key"_>(event)))
member_target = at<"state_key"_>(event);
if(member_target)
state.get(std::nothrow, "m.room.member", member_target, fetch_append);
return true;
}
//

View file

@ -5837,38 +5837,6 @@ console_cmd__stage__make_auth(opt &out, const string_view &line)
stage.at(id)
};
static const string_view types_general[]
{
"m.room.create",
"m.room.power_levels",
};
static const string_view types_membership[]
{
"m.room.create",
"m.room.join_rules",
"m.room.power_levels",
};
const auto is_membership
{
at<"type"_>(event) == "m.room.member"
};
const auto &types
{
is_membership?
vector_view<const string_view>(types_membership):
vector_view<const string_view>(types_general)
};
const auto member
{
!is_membership?
at<"sender"_>(event):
string_view{}
};
const m::room room
{
at<"room_id"_>(event)
@ -5880,7 +5848,7 @@ console_cmd__stage__make_auth(opt &out, const string_view &line)
};
thread_local char buf[1024];
json::get<"auth_events"_>(event) = auth.make_refs(buf, types, member);
json::get<"auth_events"_>(event) = auth.make_refs(buf, event);
stage.at(id) = json::strung
{
@ -9829,15 +9797,6 @@ console_cmd__room__auth(opt &out, const string_view &line)
room
};
auth.for_each([&out]
(const m::event::idx &idx)
{
const m::event::fetch event{idx};
out << idx
<< " " << pretty_oneline(event)
<< std::endl;
});
return true;
}

View file

@ -105,20 +105,21 @@ get__make_join(client &client,
{
const m::room::auth auth{room};
json::stack::checkpoint cp{out};
json::stack::array auth_events
{
event, "auth_events"
};
static const string_view types[]
const json::members args
{
"m.room.create",
"m.room.join_rules",
"m.room.power_levels",
"m.room.member",
{ "type", "m.room.member" },
{ "state_key", user_id },
{ "sender", user_id },
};
auth.make_refs(auth_events, types, user_id);
if(!auth.make_refs(auth_events, m::event{args}))
cp.decommit();
}
json::stack::member

View file

@ -105,20 +105,25 @@ get__make_leave(client &client,
{
const m::room::auth auth{room};
json::stack::checkpoint cp{out};
json::stack::array auth_events
{
event, "auth_events"
};
static const string_view types[]
const json::members args
{
"m.room.create",
"m.room.join_rules",
"m.room.power_levels",
"m.room.member",
{ "type", "m.room.member" },
{ "state_key", user_id },
{ "sender", user_id },
{ "content", json::members
{
{ "membership", "leave" }
}}
};
auth.make_refs(auth_events, types, user_id);
if(!auth.make_refs(auth_events, m::event{args}))
cp.decommit();
}
json::stack::member

View file

@ -275,23 +275,7 @@ ircd::m::vm::inject(eval &eval,
char ae_buf[1024];
json::array auth_events{json::empty_array};
if(depth != -1 && event.at("type") != "m.room.create" && opts.add_auth_events)
{
static const string_view types[]
{
"m.room.create",
"m.room.join_rules",
"m.room.power_levels",
};
const m::user::id &member
{
event.at("type") != "m.room.member"?
m::user::id{event.at("sender")}:
m::user::id{}
};
auth_events = auth.make_refs(ae_buf, types, member);
}
auth_events = auth.make_refs(ae_buf, m::event{event});
const json::iov::add auth_events_
{