0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-10-03 06:08:52 +02:00

modules/m_room: Add interface to count events in a room between two arguments.

This commit is contained in:
Jason Volk 2018-09-07 06:20:32 -07:00
parent 632278e607
commit f4831c863a
3 changed files with 83 additions and 0 deletions

View file

@ -51,6 +51,12 @@ namespace ircd::m
int64_t depth(std::nothrow_t, const id::room &);
int64_t depth(const id::room &);
// [GET] Count the events in the room between two (note always >=1 if a!=b)
size_t count_since(const room &, const m::event::idx &, const m::event::idx &);
size_t count_since(const room &, const m::event::id &, const m::event::id &);
size_t count_since(const m::event::idx &, const m::event::idx &);
size_t count_since(const m::event::id &, const m::event::id &);
// [SET] Lowest-level
event::id::buf commit(const room &, json::iov &event, const json::iov &content);

View file

@ -2702,6 +2702,52 @@ ircd::m::commit(const room &room,
return eval.event_id;
}
size_t
ircd::m::count_since(const m::event::id &a,
const m::event::id &b)
{
return count_since(index(a), index(b));
}
size_t
ircd::m::count_since(const m::event::idx &a,
const m::event::idx &b)
{
// Get the room_id from b here; a might not be in the same room but downstream
// the counter seeks to a in the given room and will properly fail there.
room::id::buf room_id
{
m::get(std::max(a, b), "room_id", room_id)
};
return count_since(room_id, a, b);
}
size_t
ircd::m::count_since(const room &room,
const m::event::id &a,
const m::event::id &b)
{
return count_since(room, index(a), index(b));
}
size_t
ircd::m::count_since(const room &r,
const event::idx &a,
const event::idx &b)
{
using prototype = bool (const room &,
const event::idx &,
const event::idx &);
static m::import<prototype> _count_since
{
"m_room", "count_since"
};
return _count_since(r, std::min(a, b), std::max(a, b));
}
ircd::m::id::room::buf
ircd::m::room_id(const id::room_alias &room_alias)
{

View file

@ -16,6 +16,37 @@ IRCD_MODULE
"Matrix state library; modular components."
};
extern "C" size_t
count_since(const m::room &room,
const m::event::idx &a,
const m::event::idx &b)
{
assert(a <= b);
m::room::messages it{room};
it.seek_idx(a);
if(!it && !m::exists(room))
throw m::NOT_FOUND
{
"Cannot find room '%s' to count events in",
string_view{room.room_id}
};
else if(!it)
throw m::NOT_FOUND
{
"Event @ idx:%lu or idx:%lu not found in room '%s' or at all",
a,
b,
string_view{room.room_id}
};
size_t ret{0};
// Hit the iterator once first otherwise the count will always increment
// to `1` erroneously when it ought to show `0`.
for(++it; it && it.event_idx() < b; ++it, ++ret);
return ret;
}
extern "C" bool
random_origin(const m::room &room,
const m::room::origins::closure &view,