0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 07:23:53 +01:00

ircd:Ⓜ️:relates: Add interface around the rel_type for the relation ref type.

This commit is contained in:
Jason Volk 2022-08-13 16:45:43 -07:00
parent 31c1e834bd
commit fedf1e976a
4 changed files with 223 additions and 35 deletions

View file

@ -13,17 +13,54 @@
namespace ircd::m
{
struct relates;
struct relates_to;
}
struct ircd::m::relates_to
:json::tuple
<
/// Target event_id
json::property<name::event_id, json::string>,
/// Relation type
json::property<name::rel_type, json::string>,
/// m.in_reply_to object
json::property<name::m_in_reply_to, json::object>
>
{
using super_type::tuple;
using super_type::operator=;
};
/// Interface to the rel_type of the M_RELATES dbs::ref type
///
struct ircd::m::relates
{
event::refs refs;
public:
using closure = util::closure_bool
<
std::function,
const event::idx &, const json::object &, const m::relates_to &
>;
bool for_each(const string_view &type, const closure &) const;
bool for_each(const closure &) const;
event::idx get(const string_view &type, const uint at = 0) const;
event::idx latest(const string_view &type, uint *const at = nullptr) const;
bool has(const string_view &type, const event::idx &) const;
bool has(const string_view &type) const;
bool has(const event::idx &) const;
size_t count(const string_view &type = {}) const;
bool prefetch(const string_view &type = {}, const bool depth = false) const;
relates(const event::refs &refs)
:refs{refs}
{}
};

View file

@ -166,6 +166,7 @@ libircd_matrix_la_SOURCES += fetch.cc
libircd_matrix_la_SOURCES += fetch_check.cc
libircd_matrix_la_SOURCES += gossip.cc
libircd_matrix_la_SOURCES += groups.cc
libircd_matrix_la_SOURCES += relates.cc
libircd_matrix_la_SOURCES += request.cc
libircd_matrix_la_SOURCES += keys.cc
libircd_matrix_la_SOURCES += media.cc

175
matrix/relates.cc Normal file
View file

@ -0,0 +1,175 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2022 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.
bool
ircd::m::relates::prefetch(const string_view &type,
const bool depth)
const
{
// If the prefetch was launched, bail here without blocking below.
if(refs.prefetch(dbs::ref::M_RELATES))
return true;
// The iteration is cached so we can prefetch the content now
bool ret{false};
refs.for_each(dbs::ref::M_RELATES, [&ret, &depth]
(const auto &event_idx, const auto &)
{
if(depth)
ret |= m::prefetch(event_idx, "depth");
ret |= m::prefetch(event_idx, "content");
return true;
});
return ret;
}
size_t
ircd::m::relates::count(const string_view &type)
const
{
size_t ret(0);
for_each(type, [&ret]
(const event::idx &, const json::object &, const m::relates_to &)
noexcept
{
++ret;
return true;
});
return ret;
}
bool
ircd::m::relates::has(const event::idx &idx)
const
{
return has(string_view{}, idx);
}
bool
ircd::m::relates::has(const string_view &type)
const
{
return !for_each(type, []
(const event::idx &, const json::object &, const m::relates_to &)
noexcept
{
return false;
});
}
bool
ircd::m::relates::has(const string_view &type,
const event::idx &idx)
const
{
return !for_each(type, [&idx]
(const event::idx &ref_idx, const json::object &, const m::relates_to &)
noexcept
{
return ref_idx != idx; // true to continue, false to break
});
}
bool
ircd::m::relates::for_each(const closure &closure)
const
{
return for_each(string_view{}, closure);
}
ircd::m::event::idx
ircd::m::relates::latest(const string_view &type,
uint *const at)
const
{
if(at)
*at = -1;
uint i{0};
int64_t best{0};
event::idx ret{0};
for_each(type, [&]
(const event::idx &event_idx, const json::object &, const m::relates_to &)
noexcept
{
int64_t depth{0};
if((depth = m::get(std::nothrow, event_idx, "depth", depth)) > best)
{
best = depth;
ret = event_idx;
if(at)
*at = i;
}
++i;
return true;
});
return ret;
}
ircd::m::event::idx
ircd::m::relates::get(const string_view &type,
const uint at)
const
{
uint ctr{0};
event::idx ret{0};
for_each(type, [&at, &ctr, &ret]
(const event::idx &event_idx, const json::object &, const m::relates_to &)
noexcept
{
if(ctr++ < at)
return true;
ret = event_idx;
return false;
});
return ret;
}
bool
ircd::m::relates::for_each(const string_view &rel_type,
const closure &closure)
const
{
return refs.for_each(dbs::ref::M_RELATES, [&rel_type, &closure]
(const auto &event_idx, const auto &type)
{
thread_local char buf[event::MAX_SIZE];
const json::object content
{
m::get(std::nothrow, event_idx, "content", buf)
};
if(!content)
return true;
m::relates_to relates
{
content["m.relates_to"]
};
if(!json::get<"rel_type"_>(relates))
if(json::get<"m.in_reply_to"_>(relates))
json::get<"rel_type"_>(relates) = "m.in_reply_to";
if(rel_type)
if(json::get<"rel_type"_>(relates) != rel_type)
return true;
return closure(event_idx, content, relates);
});
}

View file

@ -8703,59 +8703,34 @@ console_cmd__event__relates(opt &out, const string_view &line)
param.at("event_id")
};
const string_view &rel_type
const string_view &type
{
param["rel_type"]
};
const m::event::refs refs
const auto event_idx
{
index(event_id)
};
const m::dbs::ref type
const m::relates rels
{
m::dbs::ref::M_RELATES
event_idx
};
refs.for_each(type, [&out, &refs, &event_id, &rel_type]
(const auto &ref, const auto &type)
rels.for_each(type, [&out, &event_id, &event_idx]
(const m::event::idx &ref_idx, const json::object &content, const m::relates_to &relates)
{
const auto ref_id
{
m::event_id(std::nothrow, ref)
m::event_id(std::nothrow, ref_idx)
};
const auto content
{
m::get(std::nothrow, ref, "content")
};
if(!content || !ref_id)
return true;
const m::relates_to relates
{
json::object(content)["m.relates_to"]
};
const auto ref_rel_type
{
json::get<"m.in_reply_to"_>(relates)?
"<m.in_reply_to>"_sv:
json::get<"rel_type"_>(relates)?
string_view(json::get<"rel_type"_>(relates)):
"<none>"_sv
};
if(rel_type && ref_rel_type != rel_type)
return true;
out
<< " " << std::right << std::setw(10) << refs.idx
<< " " << std::right << std::setw(10) << event_idx
<< " " << std::left << std::setw(45) << trunc(event_id, 45)
<< " <- " << std::left << std::setw(36) << ref_rel_type
<< " " << std::right << std::setw(10) << ref
<< " <- " << std::left << std::setw(36) << json::get<"rel_type"_>(relates)
<< " " << std::right << std::setw(10) << ref_idx
<< " " << std::left << std::setw(45) << trunc(ref_id? string_view{ref_id}: "<index error>"_sv, 45)
<< std::endl
;