0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-14 00:34:18 +01:00

ircd:Ⓜ️:room::timeline: Checkpoint initial interface.

This commit is contained in:
Jason Volk 2019-05-07 15:27:50 -07:00
parent 180c83634f
commit 47f496a155
4 changed files with 243 additions and 0 deletions

View file

@ -100,6 +100,7 @@ namespace ircd::m
struct ircd::m::room
{
struct messages;
struct timeline;
struct state;
struct members;
struct origins;
@ -184,6 +185,7 @@ struct ircd::m::room
};
#include "messages.h"
#include "timeline.h"
#include "state.h"
#include "members.h"
#include "origins.h"

View file

@ -0,0 +1,46 @@
// 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_ROOM_TIMELINE_H
namespace ircd::m
{
uint64_t latency(const room::timeline &, const room::timeline &);
}
/// Interface to room timeline
///
struct ircd::m::room::timeline
{
struct coord;
using closure = std::function<bool (const coord &, const event::idx &)>;
m::room room;
public:
bool has_past(const event::id &) const;
bool has_future(const event::id &) const;
bool for_each(const closure &, const coord &branch) const;
timeline(const m::room &);
timeline() = default;
timeline(const timeline &) = delete;
timeline &operator=(const timeline &) = delete;
static void rebuild(const m::room &);
};
struct ircd::m::room::timeline::coord
{
int64_t x {0};
int64_t y {0};
};

View file

@ -1685,6 +1685,117 @@ const
return true;
}
//
// room::timeline
//
uint64_t
ircd::m::latency(const room::timeline &a,
const room::timeline &b)
{
return 0UL;
}
//
// room::timeline::timeline
//
ircd::m::room::timeline::timeline(const m::room &room)
:room{room}
{
}
bool
ircd::m::room::timeline::for_each(const closure &closure,
const coord &branch)
const
{
m::event::refs refs
{
room.event_id?
index(room.event_id):
room::index(room)
};
if(!refs.idx)
return true;
timeline::coord coord;
if(!closure(coord, refs.idx))
return false;
for(++coord.y; coord.y <= branch.y; ++coord.y, coord.x = 0)
{
auto idx(0);
refs.for_each(dbs::ref::PREV, [&coord, &branch, &idx]
(const auto &event_idx, const auto &)
{
if(coord.x <= branch.x)
idx = event_idx;
if(coord.x < branch.x)
{
++coord.x;
return true;
}
else return false;
});
if(!idx)
return true;
if(!closure(coord, idx))
return false;
refs.idx = idx;
}
return true;
}
bool
ircd::m::room::timeline::has_future(const event::id &event_id)
const
{
return true;
}
bool
ircd::m::room::timeline::has_past(const event::id &event_id)
const
{
return true;
}
void
ircd::m::room::timeline::rebuild(const m::room &room)
{
m::room::messages it
{
room, 0UL
};
if(!it)
return;
db::txn txn
{
*m::dbs::events
};
for(; it; ++it)
{
const m::event &event{*it};
m::dbs::write_opts opts;
opts.event_idx = it.event_idx();
m::dbs::_index_event_refs_prev(txn, event, opts);
}
txn();
}
//
// room::messages
//

View file

@ -8775,6 +8775,90 @@ console_cmd__room__messages(opt &out, const string_view &line)
return true;
}
bool
console_cmd__room__timeline(opt &out, const string_view &line)
{
const params param{line, " ",
{
"room_id", "x", "y"
}};
const auto &room_id
{
m::room_id(param.at("room_id"))
};
const m::room room
{
room_id
};
const m::room::timeline timeline
{
room
};
int64_t last_depth(0);
const auto closure{[&out, &last_depth]
(const auto &coord, const auto &event_idx)
{
const m::event::fetch event
{
event_idx, std::nothrow
};
if(json::get<"depth"_>(event) > last_depth + 1)
out << std::endl;
last_depth = json::get<"depth"_>(event);
out << "("
<< std::left
<< std::setw(2) << coord.x
<< " "
<< std::right
<< std::setw(3) << coord.y
<< ") "
<< std::left
<< std::setw(11) << event_idx
<< " "
<< pretty_oneline(event, false)
<< std::endl;
return true;
}};
timeline.for_each(closure,
{
param.at<int64_t>("x", 0),
param.at<int64_t>("y", 0)
});
return true;
}
bool
console_cmd__room__timeline__rebuild(opt &out, const string_view &line)
{
const params param{line, " ",
{
"room_id"
}};
const auto &room_id
{
m::room_id(param.at("room_id"))
};
const m::room room
{
room_id
};
m::room::timeline::rebuild(room);
out << "done" << std::endl;
return true;
}
bool
console_cmd__room__roots(opt &out, const string_view &line)
{