From d288989386acbade608fd02da7f078a99efa8578 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Mon, 6 Dec 2021 03:04:22 -0500 Subject: [PATCH] mark guild as read --- src/components/channels.cpp | 9 +++++++-- src/components/channels.hpp | 1 + src/discord/discord.cpp | 23 ++++++++++++++++++++++- src/discord/discord.hpp | 3 ++- src/discord/objects.cpp | 9 +++++++++ src/discord/objects.hpp | 7 +++++++ 6 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/components/channels.cpp b/src/components/channels.cpp index 764fb43..418781a 100644 --- a/src/components/channels.cpp +++ b/src/components/channels.cpp @@ -13,6 +13,7 @@ ChannelList::ChannelList() , m_menu_guild_copy_id("_Copy ID", true) , m_menu_guild_settings("View _Settings", true) , m_menu_guild_leave("_Leave", true) + , m_menu_guild_mark_as_read("Mark as _Read", true) , m_menu_category_copy_id("_Copy ID", true) , m_menu_channel_copy_id("_Copy ID", true) , m_menu_channel_mark_as_read("Mark as _Read", true) @@ -43,7 +44,7 @@ ChannelList::ChannelList() if (type == RenderType::TextChannel || type == RenderType::DM || type == RenderType::Thread) { const auto id = static_cast(row[m_columns.m_id]); m_signal_action_channel_item_select.emit(id); - Abaddon::Get().GetDiscordClient().MarkAsRead(id, [](...) {}); + Abaddon::Get().GetDiscordClient().MarkChannelAsRead(id, [](...) {}); } }; m_view.signal_row_activated().connect(cb, false); @@ -94,9 +95,13 @@ ChannelList::ChannelList() m_menu_guild_leave.signal_activate().connect([this] { m_signal_action_guild_leave.emit(static_cast((*m_model->get_iter(m_path_for_menu))[m_columns.m_id])); }); + m_menu_guild_mark_as_read.signal_activate().connect([this] { + Abaddon::Get().GetDiscordClient().MarkGuildAsRead(static_cast((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]), [](...) {}); + }); m_menu_guild.append(m_menu_guild_copy_id); m_menu_guild.append(m_menu_guild_settings); m_menu_guild.append(m_menu_guild_leave); + m_menu_guild.append(m_menu_guild_mark_as_read); m_menu_guild.show_all(); m_menu_category_copy_id.signal_activate().connect([this] { @@ -110,7 +115,7 @@ ChannelList::ChannelList() Gtk::Clipboard::get()->set_text(std::to_string((*m_model->get_iter(m_path_for_menu))[m_columns.m_id])); }); m_menu_channel_mark_as_read.signal_activate().connect([this] { - Abaddon::Get().GetDiscordClient().MarkAsRead(static_cast((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]), [](...) {}); + Abaddon::Get().GetDiscordClient().MarkChannelAsRead(static_cast((*m_model->get_iter(m_path_for_menu))[m_columns.m_id]), [](...) {}); }); m_menu_channel.append(m_menu_channel_copy_id); m_menu_channel.append(m_menu_channel_mark_as_read); diff --git a/src/components/channels.hpp b/src/components/channels.hpp index 99eff5f..d279907 100644 --- a/src/components/channels.hpp +++ b/src/components/channels.hpp @@ -101,6 +101,7 @@ protected: Gtk::MenuItem m_menu_guild_copy_id; Gtk::MenuItem m_menu_guild_settings; Gtk::MenuItem m_menu_guild_leave; + Gtk::MenuItem m_menu_guild_mark_as_read; Gtk::Menu m_menu_category; Gtk::MenuItem m_menu_category_copy_id; diff --git a/src/discord/discord.cpp b/src/discord/discord.cpp index 27d162c..09463b8 100644 --- a/src/discord/discord.cpp +++ b/src/discord/discord.cpp @@ -874,7 +874,7 @@ void DiscordClient::UnArchiveThread(Snowflake channel_id, sigc::slot callback) { +void DiscordClient::MarkChannelAsRead(Snowflake channel_id, sigc::slot callback) { if (m_unread.find(channel_id) == m_unread.end()) return; const auto iter = m_last_message_id.find(channel_id); if (iter == m_last_message_id.end()) return; @@ -886,6 +886,27 @@ void DiscordClient::MarkAsRead(Snowflake channel_id, sigc::slot callback) { + AckBulkData data; + const auto channels = GetChannelsInGuild(guild_id); + for (const auto &[unread, mention_count] : m_unread) { + const auto iter = m_last_message_id.find(unread); + if (iter == m_last_message_id.end()) continue; + auto &e = data.ReadStates.emplace_back(); + e.ID = unread; + e.LastMessageID = iter->second; + } + + if (data.ReadStates.empty()) return; + + m_http.MakePOST("/read-states/ack-bulk", nlohmann::json(data).dump(), [this, callback](const http::response_type &response) { + if (CheckCode(response)) + callback(DiscordError::NONE); + else + callback(GetCodeFromResponse(response)); + }); +} + void DiscordClient::FetchPinned(Snowflake id, sigc::slot, DiscordError code)> callback) { // return from db if we know the pins have already been requested if (m_channels_pinned_requested.find(id) != m_channels_pinned_requested.end()) { diff --git a/src/discord/discord.hpp b/src/discord/discord.hpp index 06aa698..465866a 100644 --- a/src/discord/discord.hpp +++ b/src/discord/discord.hpp @@ -138,7 +138,8 @@ public: void LeaveThread(Snowflake channel_id, const std::string &location, sigc::slot callback); void ArchiveThread(Snowflake channel_id, sigc::slot callback); void UnArchiveThread(Snowflake channel_id, sigc::slot callback); - void MarkAsRead(Snowflake channel_id, sigc::slot callback); + void MarkChannelAsRead(Snowflake channel_id, sigc::slot callback); + void MarkGuildAsRead(Snowflake guild_id, sigc::slot callback); bool CanModifyRole(Snowflake guild_id, Snowflake role_id) const; bool CanModifyRole(Snowflake guild_id, Snowflake role_id, Snowflake user_id) const; diff --git a/src/discord/objects.cpp b/src/discord/objects.cpp index 88d3f30..9fbc7ce 100644 --- a/src/discord/objects.cpp +++ b/src/discord/objects.cpp @@ -125,6 +125,11 @@ void from_json(const nlohmann::json &j, ReadStateEntry &m) { JS_D("id", m.ID); } +void to_json(nlohmann::json &j, const ReadStateEntry &m) { + j["channel_id"] = m.ID; + j["message_id"] = m.LastMessageID; +} + void from_json(const nlohmann::json &j, ReadStateData &m) { JS_ON("version", m.Version); JS_ON("partial", m.IsPartial); @@ -551,3 +556,7 @@ void from_json(const nlohmann::json &j, MessageAckData &m) { JS_D("message_id", m.MessageID); JS_D("channel_id", m.ChannelID); } + +void to_json(nlohmann::json &j, const AckBulkData &m) { + j["read_states"] = m.ReadStates; +} diff --git a/src/discord/objects.hpp b/src/discord/objects.hpp index 008fe98..28331c3 100644 --- a/src/discord/objects.hpp +++ b/src/discord/objects.hpp @@ -232,6 +232,7 @@ struct ReadStateEntry { // std::string LastPinTimestamp; iso friend void from_json(const nlohmann::json &j, ReadStateEntry &m); + friend void to_json(nlohmann::json &j, const ReadStateEntry &m); }; struct ReadStateData { @@ -772,3 +773,9 @@ struct MessageAckData { friend void from_json(const nlohmann::json &j, MessageAckData &m); }; + +struct AckBulkData { + std::vector ReadStates; + + friend void to_json(nlohmann::json &j, const AckBulkData &m); +};