From 4cc8ba0b79673d845e6e1c25223e2a8bef7f5ba8 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 22 May 2018 02:58:49 -0700 Subject: [PATCH] modules/m_room: Add head reset w/ console cmd. --- modules/console.cc | 33 +++++++++++++++++++++++++++++++++ modules/m_room.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/modules/console.cc b/modules/console.cc index 5b7e93f64..6c98aaeb4 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -3123,6 +3123,39 @@ console_cmd__room__head__rebuild(opt &out, const string_view &line) return true; } +bool +console_cmd__room__head__reset(opt &out, const string_view &line) +{ + const params param{line, " ", + { + "room_id", + }}; + + const auto &room_id + { + m::room_id(param.at(0)) + }; + + const m::room room + { + room_id + }; + + using prototype = size_t (const m::room &); + static m::import head__reset + { + "m_room", "head__reset" + }; + + const size_t count + { + head__reset(room) + }; + + out << "done " << count << std::endl; + return true; +} + bool console_cmd__room__depth(opt &out, const string_view &line) { diff --git a/modules/m_room.cc b/modules/m_room.cc index 517526b53..06e3dd572 100644 --- a/modules/m_room.cc +++ b/modules/m_room.cc @@ -180,3 +180,47 @@ head__rebuild(const m::room &room) txn(); return ret; } + +extern "C" size_t +head__reset(const m::room &room) +{ + size_t ret{0}; + const m::room::state state{room}; + const auto create_id + { + state.get("m.room.create") + }; + + m::room::messages it + { + room, create_id + }; + + if(!it) + return ret; + + db::txn txn + { + *m::dbs::events + }; + + m::dbs::write_opts opts; + opts.op = db::op::DELETE; + opts.head = true; + for(; it; ++it) + { + const m::event &event{*it}; + opts.event_idx = it.event_idx(); + m::dbs::_index__room_head(txn, event, opts); + ++ret; + } + + { + opts.op = db::op::SET; + const m::event::fetch event{opts.event_idx}; + m::dbs::_index__room_head(txn, event, opts); + } + + txn(); + return ret; +}