0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-07 12:38:56 +02:00

ircd:Ⓜ️:events::annotates: Interface to scan the key of the m.annotation rel_type.

This commit is contained in:
Jason Volk 2022-09-27 18:40:23 -07:00
parent e2d70f249f
commit 5f758aaa3b
3 changed files with 107 additions and 0 deletions

View file

@ -108,6 +108,20 @@ namespace ircd::m::events::relates
bool for_each(const range &, const closure &);
}
/// Interface to scan the keys of the m.annotation rel_type of the M_RELATES ref type
namespace ircd::m::events::annotates
{
// (source, key, target)
using closure = util::closure_bool
<
std::function, const event::idx &, const string_view &, const event::idx &
>;
// Iterate events in range
bool for_each(const range &, const string_view &key, const closure &);
bool for_each(const range &, const closure &);
}
/// Interface to scan the content of events
namespace ircd::m::events::content
{
@ -143,6 +157,13 @@ struct ircd::m::events::range
{}
};
inline bool
ircd::m::events::annotates::for_each(const range &range,
const closure &closure)
{
return for_each(range, string_view{}, closure);
}
inline bool
ircd::m::events::relates::for_each(const range &range,
const closure &closure)

View file

@ -407,6 +407,33 @@ ircd::m::events::content::for_each(const closure &closure)
return true;
}
//
// events::annotates
//
bool
ircd::m::events::annotates::for_each(const range &range,
const string_view &key,
const closure &closure)
{
return relates::for_each(range, [&key, &closure]
(const event::idx &src, const m::relates_to &relates, const event::idx &tgt)
{
if(json::get<"rel_type"_>(relates) != "m.annotation")
return true;
const json::string _key
{
relates.source["key"]
};
if(key && key != _key)
return true;
return closure(src, _key, tgt);
});
}
//
// events::relates
//

View file

@ -8036,6 +8036,65 @@ console_cmd__events__relates(opt &out, const string_view &line)
return true;
}
bool
console_cmd__events__annotates(opt &out, const string_view &line)
{
const params param{line, " ",
{
"start", "stop", "key"
}};
const m::event::idx start
{
param.at("start", 0UL)
};
const m::event::idx stop
{
param.at("stop", m::vm::sequence::retired + 1)
};
const string_view &keystr
{
param.at("key", "*"_sv)
};
const string_view &key
{
keystr == "*"?
string_view{}:
keystr
};
m::events::annotates::for_each({start, stop}, key, [&out]
(const m::event::idx &src, const string_view &key, const m::event::idx &tgt)
{
const auto src_id
{
m::event_id(std::nothrow, src)
};
const auto tgt_id
{
m::event_id(std::nothrow, tgt)
};
out
<< ' ' << std::right << std::setw(10) << src
<< ' ' << std::left << std::setw(45) << trunc(src_id? string_view{src_id}: "<index error>"_sv, 45)
<< " <-" << std::right << std::setw(10) << tgt
<< ' ' << std::left << std::setw(45) << trunc(tgt_id? string_view{tgt_id}: "<index error>"_sv, 45)
<< " (" << key.size() << ')'
<< " :" << key
<< std::endl
;
return true;
});
return true;
}
bool
console_cmd__events__dump(opt &out, const string_view &line)
{