From e0df4984157527f3fcc4e212663645faecbd633e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 8 Feb 2018 23:18:42 -0800 Subject: [PATCH] ircd::m::room: Plug more stuff back in. --- include/ircd/m/room.h | 8 +-- ircd/m/room.cc | 151 +++++++++++++++++++++++++++++++++++------- ircd/m/vm.cc | 15 ----- 3 files changed, 130 insertions(+), 44 deletions(-) diff --git a/include/ircd/m/room.h b/include/ircd/m/room.h index 939f076c2..193b0951a 100644 --- a/include/ircd/m/room.h +++ b/include/ircd/m/room.h @@ -32,24 +32,24 @@ namespace ircd::m // [SET] Lowest-level event::id::buf commit(const room &, json::iov &event, const json::iov &content); - // Send state to room + // [SET] Send state to room event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const string_view &state_key, const json::iov &content); event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const string_view &state_key, const json::members &content); event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const string_view &state_key, const json::object &content); - // Send non-state to room + // [SET] Send non-state to room event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const json::iov &content); event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const json::members &content); event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const json::object &content); - // Convenience sends + // [SET] Convenience sends event::id::buf message(const room &, const m::id::user &sender, const json::members &content); event::id::buf message(const room &, const m::id::user &sender, const string_view &body, const string_view &msgtype = "m.text"); event::id::buf membership(const room &, const m::id::user &, const string_view &membership); event::id::buf leave(const room &, const m::id::user &); event::id::buf join(const room &, const m::id::user &); - // Create new room + // [SET] Create new room room create(const id::room &, const id::user &creator, const id::room &parent, const string_view &type); room create(const id::room &, const id::user &creator, const string_view &type = {}); } diff --git a/ircd/m/room.cc b/ircd/m/room.cc index a3e4b8e7e..dd0569340 100644 --- a/ircd/m/room.cc +++ b/ircd/m/room.cc @@ -212,9 +212,45 @@ ircd::m::commit(const room &room, json::iov &event, const json::iov &contents) { - const json::iov::set set[] + const json::iov::push room_id { - { event, { "room_id", room.room_id }}, + event, { "room_id", room.room_id } + }; + + int64_t depth; + const auto prev_event_id + { + head(room.room_id, depth) + }; + + //TODO: LCOCK + const json::iov::set_if depth_ + { + event, !event.has("depth"), + { + "depth", depth + 1 + } + }; + + const string_view auth_events{}; + const string_view prev_state{}; + + json::value prev_event0[] + { + prev_event_id + }; + + json::value prev_events[] + { + { prev_event0, 1 } + }; + + //TODO: LOLCK + const json::iov::push prevs[] + { + { event, { "auth_events", auth_events }}, + { event, { "prev_state", prev_state }}, + { event, { "prev_events", { prev_events, 1 } } }, }; return m::vm::commit(event, contents); @@ -327,21 +363,39 @@ bool ircd::m::room::prev(const event::closure &closure) const { - const vm::query query + auto it { - { "room_id", room_id }, + dbs::room_events.begin(room_id) }; - vm::cursor cursor - { - "prev_event_id for event_id in room_id", &query - }; - - auto it(cursor.begin(room_id)); if(!it) return false; - closure(*it); + const auto &key{it->first}; + const auto part + { + dbs::room_events_key(key) + }; + + const string_view event_id + { + std::get<1>(part) + }; + + std::array cell; + static const event _dummy_; + db::row row + { + *dbs::events, event_id, _dummy_, cell + }; + + seek(row, event_id); + if(!row.valid(event_id)) + return false; + + m::event event; + assign(event, row, event_id); + closure(event); return true; } @@ -349,21 +403,41 @@ bool ircd::m::room::get(const event::closure &closure) const { - const vm::query query + std::array cell; + static const event _dummy_; + db::row row { - { "room_id", room_id }, + *dbs::events, string_view{}, _dummy_, cell }; - vm::cursor cursor + auto it { - "event_id in room_id", &query + dbs::room_events.begin(room_id) }; - auto it(cursor.begin(room_id)); - if(!it) - return false; + if(it) do + { + const auto &key{it->first}; + const auto part + { + dbs::room_events_key(key) + }; + + const string_view event_id + { + std::get<1>(part) + }; + + seek(row, event_id); + if(!row.valid(event_id)) + return false; + + m::event event; + assign(event, row, event_id); + closure(event); + } + while(++it); else return false; - closure(*it); return true; } @@ -499,16 +573,43 @@ ircd::m::room::members::until(const string_view &membership, const event::closure_bool &view) const { - const vm::query query + const event::id::buf event_id_buf { - { "room_id", room.room_id }, - { "type", "m.room.member" }, - { "membership", membership }, + !room.event_id? head(room.room_id) : string_view{} }; - return m::vm::until(query, [&view] - (const auto &event) + const event::id event_id { + room.event_id? room.event_id : event_id_buf + }; + + m::state::id_buffer state_root_buf; + const auto state_root + { + dbs::state_root(state_root_buf, room.room_id, event_id) + }; + + std::array cell; + static const event _dummy_; + db::row row + { + *dbs::events, string_view{}, _dummy_, cell + }; + + return m::state::each(state_root, "m.room.member", [&cell, &row, &view] + (const json::array &key, const string_view &val) + { + const string_view event_id + { + unquote(val) + }; + + seek(row, event_id); + if(!row.valid(event_id)) + return false; + + m::event event; + assign(event, row, event_id); return view(event); }); } diff --git a/ircd/m/vm.cc b/ircd/m/vm.cc index a7799ffef..b8d70cc28 100644 --- a/ircd/m/vm.cc +++ b/ircd/m/vm.cc @@ -53,25 +53,10 @@ ircd::m::vm::commit(json::iov &event, contents }; - //TODO: XXX - const int64_t depth - { - -1 - }; - - //TODO: XXX - const string_view auth_events {}; - - //TODO: XXX - const string_view prev_events {}; - const json::iov::set set[] { - { event, { "auth_events", auth_events }}, - { event, { "depth", depth }}, { event, { "origin_server_ts", ircd::time() }}, { event, { "origin", my_host() }}, - { event, { "prev_events", prev_events }}, }; thread_local char preimage_buf[64_KiB];