From c416be83989b2602405f9be3212b4ad8c093c959 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 11 Mar 2019 16:02:48 -0700 Subject: [PATCH] modules: Replace individual client event appending with m::append(). --- modules/client/rooms/context.cc | 58 ++++++--------------- modules/client/rooms/initialsync.cc | 44 +++------------- modules/client/rooms/messages.cc | 44 ++-------------- modules/client/sync/rooms/state.cc | 24 ++------- modules/client/sync/rooms/timeline.cc | 75 +++------------------------ 5 files changed, 40 insertions(+), 205 deletions(-) diff --git a/modules/client/rooms/context.cc b/modules/client/rooms/context.cc index dd7f61af6..4b4165f28 100644 --- a/modules/client/rooms/context.cc +++ b/modules/client/rooms/context.cc @@ -201,13 +201,21 @@ get__context(client &client, room, &default_fetch_opts }; - state.for_each([&array, &request] - (const m::event &event) + state.for_each([&array, &request, &user_room] + (const m::event::idx &event_idx) { + const m::event::fetch event + { + event_idx, std::nothrow + }; + + if(!event.valid) + return; + if(!visible(event, request.user_id)) return; - array.append(event); + _append(array, event, event_idx, user_room); }); } @@ -220,43 +228,9 @@ _append(json::stack::array &chunk, const m::event::idx &event_idx, const m::user::room &user_room) { - json::stack::object object - { - chunk - }; - - object.append(event); - - json::stack::object unsigned_ - { - object, "unsigned" - }; - - json::stack::member - { - unsigned_, "age", json::value - { - long(m::vm::current_sequence - event_idx) - } - }; - - if(at<"sender"_>(event) != user_room.user.user_id) - return; - - const auto txnid_idx - { - user_room.get(std::nothrow, "ircd.client.txnid", at<"event_id"_>(event)) - }; - - if(!txnid_idx) - return; - - m::get(std::nothrow, txnid_idx, "content", [&unsigned_] - (const json::object &content) - { - json::stack::member - { - unsigned_, "transaction_id", unquote(content.get("transaction_id")) - }; - }); + m::event_append_opts opts; + opts.event_idx = &event_idx; + opts.user_id = &user_room.user.user_id; + opts.user_room = &user_room; + m::append(chunk, event, opts); } diff --git a/modules/client/rooms/initialsync.cc b/modules/client/rooms/initialsync.cc index 1d48ada15..765c705bd 100644 --- a/modules/client/rooms/initialsync.cc +++ b/modules/client/rooms/initialsync.cc @@ -193,25 +193,9 @@ get__initialsync_local(client &client, if(!event.valid) return true; - json::stack::object room_event - { - state - }; - - room_event.append(event); - json::stack::object unsigned_ - { - room_event, "unsigned" - }; - - json::stack::member - { - unsigned_, "age", json::value - { - long(m::vm::current_sequence - event.event_idx) - } - }; - + m::event_append_opts opts; + opts.event_idx = &event.event_idx; + m::append(state, event, opts); return true; }}); state.~array(); @@ -247,24 +231,12 @@ get__initialsync_local(client &client, if(!visible(event_id, user.user_id)) continue; - json::stack::object room_event - { - chunk - }; + const m::event &event(*it); + const auto &event_idx(it.event_idx()); - room_event.append(*it); - json::stack::object unsigned_ - { - room_event, "unsigned" - }; - - json::stack::member - { - unsigned_, "age", json::value - { - long(m::vm::current_sequence - it.event_idx()) - } - }; + m::event_append_opts opts; + opts.event_idx = &event_idx; + m::append(chunk, event, opts); } } diff --git a/modules/client/rooms/messages.cc b/modules/client/rooms/messages.cc index 6d33ecf03..ccc023fab 100644 --- a/modules/client/rooms/messages.cc +++ b/modules/client/rooms/messages.cc @@ -186,45 +186,11 @@ _append(json::stack::array &chunk, const m::event::idx &event_idx, const m::user::room &user_room) { - json::stack::object object - { - chunk - }; - - object.append(event); - - json::stack::object unsigned_ - { - object, "unsigned" - }; - - json::stack::member - { - unsigned_, "age", json::value - { - long(m::vm::current_sequence - event_idx) - } - }; - - if(at<"sender"_>(event) != user_room.user.user_id) - return; - - const auto txnid_idx - { - user_room.get(std::nothrow, "ircd.client.txnid", at<"event_id"_>(event)) - }; - - if(!txnid_idx) - return; - - m::get(std::nothrow, txnid_idx, "content", [&unsigned_] - (const json::object &content) - { - json::stack::member - { - unsigned_, "transaction_id", unquote(content.get("transaction_id")) - }; - }); + m::event_append_opts opts; + opts.event_idx = &event_idx; + opts.user_id = &user_room.user.user_id; + opts.user_room = &user_room; + m::append(chunk, event, opts); } // Client-Server 6.3.6 query parameters diff --git a/modules/client/sync/rooms/state.cc b/modules/client/sync/rooms/state.cc index a4569757e..0dcef2c3e 100644 --- a/modules/client/sync/rooms/state.cc +++ b/modules/client/sync/rooms/state.cc @@ -202,23 +202,9 @@ ircd::m::sync::room_state_append(data &data, const m::event &event, const m::event::idx &event_idx) { - json::stack::object object - { - events - }; - - object.append(event); - - json::stack::object unsigned_ - { - object, "unsigned" - }; - - json::stack::member - { - unsigned_, "age", json::value - { - long(vm::current_sequence - event_idx) - } - }; + m::event_append_opts opts; + opts.event_idx = &event_idx; + opts.client_txnid = &data.client_txnid; + opts.user_id = &data.user.user_id; + opts.user_room = &data.user_room; } diff --git a/modules/client/sync/rooms/timeline.cc b/modules/client/sync/rooms/timeline.cc index 095f5a2dc..fb73ac571 100644 --- a/modules/client/sync/rooms/timeline.cc +++ b/modules/client/sync/rooms/timeline.cc @@ -16,7 +16,6 @@ IRCD_MODULE namespace ircd::m::sync { - static void _room_timeline_append_txnid(data &, json::stack::object &, const m::event &); static void _room_timeline_append(data &, json::stack::array &, const m::event::idx &, const m::event &); static event::id::buf _room_timeline_polylog_events(data &, const m::room &, bool &, bool &); static bool room_timeline_polylog(data &); @@ -187,72 +186,10 @@ ircd::m::sync::_room_timeline_append(data &data, const m::event::idx &event_idx, const m::event &event) { - json::stack::object object - { - events - }; - - object.append(event); - - json::stack::object unsigned_ - { - object, "unsigned" - }; - - json::stack::member - { - unsigned_, "age", json::value - { - long(vm::current_sequence - event_idx) - } - }; - - _room_timeline_append_txnid(data, unsigned_, event); -} - - -void -ircd::m::sync::_room_timeline_append_txnid(data &data, - json::stack::object &unsigned_, - const m::event &event) -{ - if(data.client_txnid) - { - json::stack::member - { - unsigned_, "transaction_id", data.client_txnid - }; - - return; - } - - if(json::get<"sender"_>(event) != data.user.user_id) - return; - - const auto txnid_idx - { - data.user_room.get(std::nothrow, "ircd.client.txnid", at<"event_id"_>(event)) - }; - - if(!txnid_idx) - { - log::dwarning - { - log, "Could not find transaction_id for %s from %s in %s", - json::get<"event_id"_>(event), - json::get<"sender"_>(event), - json::get<"room_id"_>(event) - }; - - return; - } - - m::get(std::nothrow, txnid_idx, "content", [&unsigned_] - (const json::object &content) - { - json::stack::member - { - unsigned_, "transaction_id", unquote(content.get("transaction_id")) - }; - }); + m::event_append_opts opts; + opts.event_idx = &event_idx; + opts.client_txnid = &data.client_txnid; + opts.user_id = &data.user.user_id; + opts.user_room = &data.user_room; + m::append(events, event, opts); }