From a7dbc8e8823244955d91d08f64f5a5951d6acc08 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 14 Sep 2018 06:21:19 -0700 Subject: [PATCH] modules/client/sync: Add highlight count to sync; reintegrate notification count. --- modules/client/sync.cc | 97 +++++++++++++++++++++++++++++++++++------- modules/client/sync.h | 3 ++ 2 files changed, 85 insertions(+), 15 deletions(-) diff --git a/modules/client/sync.cc b/modules/client/sync.cc index ea8e4b225..5b0e9eff1 100644 --- a/modules/client/sync.cc +++ b/modules/client/sync.cc @@ -350,20 +350,41 @@ ircd::m::sync::longpoll::sync_room(client &client, m::receipt::read(last_read_buf, room, args.request.user_id) }; - const auto notes + const auto last_read_idx { last_read && json::get<"event_id"_>(event)? - m::count_since(room, last_read, at<"event_id"_>(event)): + index(last_read): 0UL }; + const auto current_idx + { + last_read_idx? + index(at<"event_id"_>(event)): + 0UL + }; + + const auto notes + { + last_read_idx? + notification_count(room, last_read_idx, current_idx): + json::undefined_number + }; + + const auto highlights + { + last_read_idx? + highlight_count(room, args.request.user_id, last_read_idx, current_idx): + json::undefined_number + }; + const json::members body { { "account_data", json::members{} }, { "unread_notifications", { - { "highlight_count", int64_t(0) }, - { "notification_count", long(notes) }, + { "highlight_count", highlights }, + { "notification_count", notes }, }}, { "ephemeral", { @@ -433,7 +454,7 @@ ircd::m::sync::linear::handle(client &client, for(auto &p : r) { - const auto &room_id{p.first}; + const m::room::id &room_id{p.first}; auto &vec{p.second}; std::vector timeline; @@ -458,13 +479,27 @@ ircd::m::sync::linear::handle(client &client, m::receipt::read(last_read_buf, room_id, sp.user) }; - const auto notes + const auto last_read_idx { last_read? - m::count_since(m::room::id{room_id}, index(last_read), sp.current): + index(last_read): 0UL }; + const auto notes + { + last_read_idx? + notification_count(room_id, last_read_idx, sp.current): + json::undefined_number + }; + + const auto highlights + { + last_read_idx? + highlight_count(room_id, sp.user, last_read_idx, sp.current): + json::undefined_number + }; + const string_view prev_batch { !timeline.empty()? @@ -490,8 +525,8 @@ ircd::m::sync::linear::handle(client &client, }}, { "unread_notifications", { - { "highlight_count", int64_t(0) }, - { "notification_count", long(notes) }, + { "highlight_count", highlights }, + { "notification_count", notes }, }}, }; @@ -1111,22 +1146,32 @@ ircd::m::sync::polylog::room_account_data(shortpoll &sp, }); } - void ircd::m::sync::polylog::room_unread_notifications(shortpoll &sp, json::stack::object &out, const m::room &room) { m::event::id::buf last_read_buf; - const m::event::id last_read + const auto last_read { m::receipt::read(last_read_buf, room, sp.user) }; + if(!last_read) + return; + + const auto last_read_idx + { + index(last_read) + }; + // highlight_count json::stack::member { - out, "highlight_count", json::value{0L} + out, "highlight_count", json::value + { + highlight_count(room, sp.user, last_read_idx, sp.current) + } }; // notification_count @@ -1134,9 +1179,31 @@ ircd::m::sync::polylog::room_unread_notifications(shortpoll &sp, { out, "notification_count", json::value { - last_read? - long(m::count_since(room, index(last_read), sp.current)): - 0L + notification_count(room, last_read_idx, sp.current) } }; } + +long +ircd::m::sync::highlight_count(const room &r, + const user &u, + const event::idx &a, + const event::idx &b) +{ + using proto = size_t (const user &, const room &, const event::idx &, const event::idx &); + + static mods::import count + { + "m_user", "highlighted_count__between" + }; + + return count(u, r, a, b); +} + +long +ircd::m::sync::notification_count(const room &room, + const event::idx &a, + const event::idx &b) +{ + return m::count_since(room, a, b); +} diff --git a/modules/client/sync.h b/modules/client/sync.h index 1b640332a..46190d432 100644 --- a/modules/client/sync.h +++ b/modules/client/sync.h @@ -14,6 +14,9 @@ namespace ircd::m::sync struct stats; struct shortpoll; + static long notification_count(const room &, const event::idx &a, const event::idx &b); + static long highlight_count(const room &, const user &, const event::idx &a, const event::idx &b); + static resource::response handle_get(client &, const resource::request &); extern resource::method method_get; extern const string_view description;