mirror of
https://github.com/matrix-construct/construct
synced 2024-11-12 13:01:07 +01:00
ircd:Ⓜ️ Consolidate m::membership() suite to header; minor cleanup.
This commit is contained in:
parent
9a446d6c42
commit
11699baff0
10 changed files with 158 additions and 127 deletions
|
@ -18,10 +18,6 @@ namespace ircd::m
|
|||
// General util
|
||||
bool my(const id::event &);
|
||||
bool my(const event &);
|
||||
size_t degree(const event &);
|
||||
string_view membership(const event &);
|
||||
|
||||
// [GET]
|
||||
bool exists(const id::event &);
|
||||
bool exists(const id::event &, const bool &good);
|
||||
bool cached(const id::event &);
|
||||
|
@ -38,6 +34,7 @@ namespace ircd::m
|
|||
bool operator>=(const event &, const event &);
|
||||
|
||||
// Topological comparison
|
||||
size_t degree(const event &);
|
||||
bool before(const event &a, const event &b);
|
||||
|
||||
bool check_id(const event &) noexcept;
|
||||
|
|
|
@ -57,6 +57,7 @@ namespace ircd::m
|
|||
#include "users.h"
|
||||
#include "rooms.h"
|
||||
#include "rooms_summary.h"
|
||||
#include "membership.h"
|
||||
#include "filter.h"
|
||||
#include "events.h"
|
||||
#include "node.h"
|
||||
|
|
32
include/ircd/m/membership.h
Normal file
32
include/ircd/m/membership.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Matrix Construct
|
||||
//
|
||||
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2019 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_MEMBERSHIP_H
|
||||
|
||||
namespace ircd::m
|
||||
{
|
||||
// Extract membership string from event data.
|
||||
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.
|
||||
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)
|
||||
string_view membership(const mutable_buffer &out, const room &, const id::user &);
|
||||
|
||||
// Query and compare membership string; queries room state. (also room.h)
|
||||
bool membership(const room &, const id::user &, const string_view &);
|
||||
}
|
|
@ -21,9 +21,6 @@ struct ircd::m::room::members
|
|||
using closure_idx = std::function<bool (const id::user &, const event::idx &)>;
|
||||
using closure = std::function<bool (const id::user &)>;
|
||||
|
||||
static string_view membership(const mutable_buffer &out, const event::idx &);
|
||||
static bool membership(const event::idx &, const string_view &);
|
||||
|
||||
m::room room;
|
||||
|
||||
bool for_each_join_present(const string_view &host, const closure &) const;
|
||||
|
|
100
ircd/m.cc
100
ircd/m.cc
|
@ -2318,6 +2318,106 @@ ircd::m::event_filter::event_filter(const mutable_buffer &buf,
|
|||
{
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// m/membership.h
|
||||
//
|
||||
|
||||
bool
|
||||
ircd::m::membership(const room &room,
|
||||
const user::id &user_id,
|
||||
const string_view &membership)
|
||||
{
|
||||
const room::state state
|
||||
{
|
||||
room
|
||||
};
|
||||
|
||||
const auto event_idx
|
||||
{
|
||||
state.get(std::nothrow, "m.room.member", user_id)
|
||||
};
|
||||
|
||||
return m::membership(event_idx, membership);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::m::membership(const mutable_buffer &out,
|
||||
const room &room,
|
||||
const user::id &user_id)
|
||||
{
|
||||
const room::state state
|
||||
{
|
||||
room
|
||||
};
|
||||
|
||||
const auto event_idx
|
||||
{
|
||||
state.get(std::nothrow, "m.room.member", user_id)
|
||||
};
|
||||
|
||||
return m::membership(out, event_idx);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::membership(const event::idx &event_idx,
|
||||
const string_view &membership)
|
||||
{
|
||||
return m::query(std::nothrow, event_idx, "content", [&membership]
|
||||
(const json::object &content)
|
||||
{
|
||||
const json::string &content_membership
|
||||
{
|
||||
content["membership"]
|
||||
};
|
||||
|
||||
return content_membership == membership;
|
||||
});
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::m::membership(const mutable_buffer &out,
|
||||
const event::idx &event_idx)
|
||||
{
|
||||
return m::query(std::nothrow, event_idx, "content", [&out]
|
||||
(const json::object &content) -> string_view
|
||||
{
|
||||
const json::string &content_membership
|
||||
{
|
||||
content["membership"]
|
||||
};
|
||||
|
||||
return strlcpy
|
||||
{
|
||||
out, content_membership
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::m::membership(const event &event)
|
||||
{
|
||||
const json::object &content
|
||||
{
|
||||
json::get<"content"_>(event)
|
||||
};
|
||||
|
||||
const string_view &membership
|
||||
{
|
||||
json::get<"membership"_>(event)
|
||||
};
|
||||
|
||||
if(membership)
|
||||
return membership;
|
||||
|
||||
const json::string &content_membership
|
||||
{
|
||||
content.get("membership")
|
||||
};
|
||||
|
||||
return content_membership;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// m/user.h
|
||||
|
|
|
@ -3099,6 +3099,25 @@ ircd::m::before(const event &a,
|
|||
return prev.prev_events_has(a.event_id);
|
||||
}
|
||||
|
||||
size_t
|
||||
ircd::m::degree(const event &event)
|
||||
{
|
||||
return degree(event::prev{event});
|
||||
}
|
||||
|
||||
size_t
|
||||
ircd::m::degree(const event::prev &prev)
|
||||
{
|
||||
size_t ret{0};
|
||||
json::for_each(prev, [&ret]
|
||||
(const auto &, const json::array &prevs)
|
||||
{
|
||||
ret += prevs.count();
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::operator>=(const event &a, const event &b)
|
||||
{
|
||||
|
@ -3186,49 +3205,6 @@ ircd::m::exists(const id::event &event_id)
|
|||
return bool(event_id) && has(column, event_id);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::m::membership(const event &event)
|
||||
{
|
||||
const json::object &content
|
||||
{
|
||||
json::get<"content"_>(event)
|
||||
};
|
||||
|
||||
const string_view &membership
|
||||
{
|
||||
json::get<"membership"_>(event)
|
||||
};
|
||||
|
||||
if(membership)
|
||||
return membership;
|
||||
|
||||
const json::string &content_membership
|
||||
{
|
||||
content.get("membership")
|
||||
};
|
||||
|
||||
return content_membership;
|
||||
}
|
||||
|
||||
size_t
|
||||
ircd::m::degree(const event &event)
|
||||
{
|
||||
return degree(event::prev{event});
|
||||
}
|
||||
|
||||
size_t
|
||||
ircd::m::degree(const event::prev &prev)
|
||||
{
|
||||
size_t ret{0};
|
||||
json::for_each(prev, [&ret]
|
||||
(const auto &, const json::array &prevs)
|
||||
{
|
||||
ret += prevs.count();
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::my(const event &event)
|
||||
{
|
||||
|
|
|
@ -891,24 +891,6 @@ ircd::m::any_user(const room &room,
|
|||
return ret;
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::m::membership(const mutable_buffer &out,
|
||||
const room &room,
|
||||
const user::id &user_id)
|
||||
{
|
||||
const room::state state
|
||||
{
|
||||
room
|
||||
};
|
||||
|
||||
const auto event_idx
|
||||
{
|
||||
state.get(std::nothrow, "m.room.member", user_id)
|
||||
};
|
||||
|
||||
return room::members::membership(out, event_idx);
|
||||
}
|
||||
|
||||
/// Receive the join_rule of the room into buffer of sufficient size.
|
||||
/// The protocol does not specify a join_rule string longer than 7
|
||||
/// characters but do be considerate of the future. This function
|
||||
|
@ -1161,24 +1143,6 @@ ircd::m::join_rule(const room &room,
|
|||
return join_rule(buf, room) == rule;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::membership(const room &room,
|
||||
const user::id &user_id,
|
||||
const string_view &membership)
|
||||
{
|
||||
const room::state state
|
||||
{
|
||||
room
|
||||
};
|
||||
|
||||
const auto event_idx
|
||||
{
|
||||
state.get(std::nothrow, "m.room.member", user_id)
|
||||
};
|
||||
|
||||
return room::members::membership(event_idx, membership);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::creator(const room::id &room_id,
|
||||
const user::id &user_id)
|
||||
|
@ -3188,7 +3152,7 @@ const
|
|||
if(host && user_id.host() != host)
|
||||
return true;
|
||||
|
||||
return !membership || this->membership(event_idx, membership)?
|
||||
return !membership || m::membership(event_idx, membership)?
|
||||
closure(user_id, event_idx):
|
||||
true;
|
||||
});
|
||||
|
@ -3232,41 +3196,6 @@ const
|
|||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::room::members::membership(const event::idx &event_idx,
|
||||
const string_view &membership)
|
||||
{
|
||||
return m::query(std::nothrow, event_idx, "content", [&membership]
|
||||
(const json::object &content)
|
||||
{
|
||||
const json::string &content_membership
|
||||
{
|
||||
content["membership"]
|
||||
};
|
||||
|
||||
return content_membership && content_membership == membership;
|
||||
});
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::m::room::members::membership(const mutable_buffer &out,
|
||||
const event::idx &event_idx)
|
||||
{
|
||||
return m::query(std::nothrow, event_idx, "content", [&out]
|
||||
(const json::object &content) -> string_view
|
||||
{
|
||||
const json::string &content_membership
|
||||
{
|
||||
content["membership"]
|
||||
};
|
||||
|
||||
return strlcpy
|
||||
{
|
||||
out, content_membership
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// room::origins
|
||||
//
|
||||
|
|
|
@ -102,7 +102,7 @@ get__members(client &client,
|
|||
members.for_each(membership, [¬_membership]
|
||||
(const m::user::id &member, const m::event::idx &event_idx)
|
||||
{
|
||||
if(m::room::members::membership(event_idx, not_membership))
|
||||
if(m::membership(event_idx, not_membership))
|
||||
return true;
|
||||
|
||||
m::prefetch(event_idx);
|
||||
|
@ -112,7 +112,7 @@ get__members(client &client,
|
|||
members.for_each(membership, [&request, &chunk, ¬_membership]
|
||||
(const m::user::id &member, const m::event::idx &event_idx)
|
||||
{
|
||||
if(m::room::members::membership(event_idx, not_membership))
|
||||
if(m::membership(event_idx, not_membership))
|
||||
return true;
|
||||
|
||||
const m::event::fetch event
|
||||
|
|
|
@ -8701,7 +8701,7 @@ console_cmd__room__members(opt &out, const string_view &line)
|
|||
(const m::user::id &user_id, const m::event::idx &event_idx)
|
||||
{
|
||||
char buf[32];
|
||||
out << std::setw(8) << std::left << m::room::members::membership(buf, event_idx)
|
||||
out << std::setw(8) << std::left << m::membership(buf, event_idx)
|
||||
<< " " << user_id << std::endl;
|
||||
|
||||
return true;
|
||||
|
|
|
@ -108,8 +108,7 @@ ircd::m::room::bootstrap::bootstrap(m::event::id::buf &event_id_buf,
|
|||
|
||||
const bool existing_join
|
||||
{
|
||||
member_event_idx &&
|
||||
m::room::members::membership(member_event_idx, "join")
|
||||
m::membership(member_event_idx, "join")
|
||||
};
|
||||
|
||||
if(existing_join)
|
||||
|
|
Loading…
Reference in a new issue