From 856674506c064ba94396752664aede5167c3ec4b Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Thu, 5 Aug 2021 04:02:47 -0400 Subject: [PATCH] better join/leave thread logic --- components/channels.cpp | 36 ++++++++++++++++++------------------ components/channels.hpp | 6 +++--- discord/channel.cpp | 2 +- discord/discord.cpp | 23 +++++++++++++++++++++-- discord/discord.hpp | 15 ++++++++++++--- 5 files changed, 55 insertions(+), 27 deletions(-) diff --git a/components/channels.cpp b/components/channels.cpp index b96b2e6..71a4cd9 100644 --- a/components/channels.cpp +++ b/components/channels.cpp @@ -141,9 +141,9 @@ ChannelList::ChannelList() discord.signal_channel_delete().connect(sigc::mem_fun(*this, &ChannelList::UpdateRemoveChannel)); discord.signal_channel_update().connect(sigc::mem_fun(*this, &ChannelList::UpdateChannel)); discord.signal_channel_create().connect(sigc::mem_fun(*this, &ChannelList::UpdateCreateChannel)); - discord.signal_thread_create().connect(sigc::mem_fun(*this, &ChannelList::UpdateCreateThread)); discord.signal_thread_delete().connect(sigc::mem_fun(*this, &ChannelList::OnThreadDelete)); - discord.signal_thread_members_update().connect(sigc::mem_fun(*this, &ChannelList::OnThreadMembersUpdate)); + discord.signal_added_to_thread().connect(sigc::mem_fun(*this, &ChannelList::OnThreadJoined)); + discord.signal_removed_from_thread().connect(sigc::mem_fun(*this, &ChannelList::OnThreadRemoved)); discord.signal_guild_update().connect(sigc::mem_fun(*this, &ChannelList::UpdateGuild)); } @@ -280,25 +280,25 @@ void ChannelList::UpdateGuild(Snowflake id) { } } +void ChannelList::OnThreadJoined(Snowflake id) { + if (GetIteratorForChannelFromID(id)) return; + const auto channel = Abaddon::Get().GetDiscordClient().GetChannel(id); + if (!channel.has_value()) return; + const auto parent = GetIteratorForChannelFromID(*channel->ParentID); + if (parent) + CreateThreadRow(parent->children(), *channel); +} + +void ChannelList::OnThreadRemoved(Snowflake id) { + if (GetIteratorForChannelFromID(id)) + DeleteThreadRow(id); +} + void ChannelList::OnThreadDelete(const ThreadDeleteData &data) { - UpdateDeleteThread(data.ID); + DeleteThreadRow(data.ID); } -void ChannelList::OnThreadMembersUpdate(const ThreadMembersUpdateData &data) { - auto &r = data.RemovedMemberIDs; - if (r.has_value() && std::find(r->begin(), r->end(), Abaddon::Get().GetDiscordClient().GetUserData().ID) != r->end()) { - UpdateDeleteThread(data.ID); - } -} - -void ChannelList::UpdateCreateThread(const ChannelData &channel) { - if (GetIteratorForChannelFromID(channel.ID)) return; // dont do anything if already exists - auto parent_row = GetIteratorForChannelFromID(*channel.ParentID); - if (parent_row) - CreateThreadRow(parent_row->children(), channel); -} - -void ChannelList::UpdateDeleteThread(Snowflake id) { +void ChannelList::DeleteThreadRow(Snowflake id) { auto iter = GetIteratorForChannelFromID(id); if (iter) m_model->erase(iter); diff --git a/components/channels.hpp b/components/channels.hpp index fce3546..c1c74a4 100644 --- a/components/channels.hpp +++ b/components/channels.hpp @@ -140,12 +140,12 @@ protected: void UpdateRemoveChannel(Snowflake id); void UpdateChannel(Snowflake id); void UpdateCreateChannel(const ChannelData &channel); - void UpdateCreateThread(const ChannelData &channel); - void UpdateDeleteThread(Snowflake id); void UpdateGuild(Snowflake id); + void DeleteThreadRow(Snowflake id); + void OnThreadJoined(Snowflake id); + void OnThreadRemoved(Snowflake id); void OnThreadDelete(const ThreadDeleteData &data); - void OnThreadMembersUpdate(const ThreadMembersUpdateData &data); Gtk::TreeView m_view; diff --git a/discord/channel.cpp b/discord/channel.cpp index f78e0cd..60f481b 100644 --- a/discord/channel.cpp +++ b/discord/channel.cpp @@ -10,7 +10,7 @@ void from_json(const nlohmann::json &j, ThreadMetadataData &m) { void from_json(const nlohmann::json &j, ThreadMemberObject &m) { JS_O("id", m.ThreadID); - JS_O("user_id", m.ThreadID); + JS_O("user_id", m.UserID); JS_D("join_timestamp", m.JoinTimestamp); JS_D("flags", m.Flags); } diff --git a/discord/discord.cpp b/discord/discord.cpp index 5a7c07e..aa363f5 100644 --- a/discord/discord.cpp +++ b/discord/discord.cpp @@ -1254,7 +1254,7 @@ void DiscordClient::ProcessNewGuild(GuildData &guild) { if (guild.Threads.has_value()) { for (auto &c : *guild.Threads) { - m_joined_threads.push_back(c.ID); + m_joined_threads.insert(c.ID); c.GuildID = guild.ID; m_store.SetChannel(c.ID, c); } @@ -1670,10 +1670,10 @@ void DiscordClient::HandleGatewayRelationshipAdd(const GatewayMessage &msg) { // remarkably this doesnt actually mean a thread was created // it can also mean you gained access to a thread. yay ... +// except sometimes it doesnt??? i dont know whats going on void DiscordClient::HandleGatewayThreadCreate(const GatewayMessage &msg) { ThreadCreateData data = msg.Data; m_store.SetChannel(data.Channel.ID, data.Channel); - m_joined_threads.push_back(data.Channel.ID); m_signal_thread_create.emit(data.Channel); } @@ -1695,6 +1695,17 @@ void DiscordClient::HandleGatewayThreadListSync(const GatewayMessage &msg) { void DiscordClient::HandleGatewayThreadMembersUpdate(const GatewayMessage &msg) { ThreadMembersUpdateData data = msg.Data; + if (data.AddedMembers.has_value() && + std::find_if(data.AddedMembers->begin(), data.AddedMembers->end(), [this](const auto &x) { + return *x.UserID == m_user_data.ID; // safe to assume UserID is present here + }) != data.AddedMembers->end()) { + m_joined_threads.insert(data.ID); + m_signal_added_to_thread.emit(data.ID); + } else if (data.RemovedMemberIDs.has_value() && + std::find(data.RemovedMemberIDs->begin(), data.RemovedMemberIDs->end(), m_user_data.ID) != data.RemovedMemberIDs->end()) { + m_joined_threads.erase(data.ID); + m_signal_removed_from_thread.emit(data.ID); + } m_signal_thread_members_update.emit(data); } @@ -2218,6 +2229,14 @@ DiscordClient::type_signal_thread_members_update DiscordClient::signal_thread_me return m_signal_thread_members_update; } +DiscordClient::type_signal_added_to_thread DiscordClient::signal_added_to_thread() { + return m_signal_added_to_thread; +} + +DiscordClient::type_signal_removed_from_thread DiscordClient::signal_removed_from_thread() { + return m_signal_removed_from_thread; +} + DiscordClient::type_signal_message_sent DiscordClient::signal_message_sent() { return m_signal_message_sent; } diff --git a/discord/discord.hpp b/discord/discord.hpp index a589702..11b8cbd 100644 --- a/discord/discord.hpp +++ b/discord/discord.hpp @@ -266,7 +266,7 @@ private: std::map m_guild_join_requests; std::map m_user_to_status; std::map m_user_relationships; - std::vector m_joined_threads; + std::set m_joined_threads; UserData m_user_data; UserSettings m_user_settings; @@ -338,9 +338,14 @@ public: typedef sigc::signal type_signal_thread_delete; typedef sigc::signal type_signal_thread_list_sync; typedef sigc::signal type_signal_thread_members_update; - typedef sigc::signal type_signal_message_unpinned; // not a real event - typedef sigc::signal type_signal_message_pinned; // not a real event either + + // not discord dispatch events + typedef sigc::signal type_signal_added_to_thread; + typedef sigc::signal type_signal_removed_from_thread; + typedef sigc::signal type_signal_message_unpinned; + typedef sigc::signal type_signal_message_pinned; typedef sigc::signal type_signal_message_sent; + typedef sigc::signal type_signal_message_send_fail; // retry after param will be 0 if it failed for a reason that isnt slowmode typedef sigc::signal type_signal_disconnected; // bool true if reconnecting typedef sigc::signal type_signal_connected; @@ -381,6 +386,8 @@ public: type_signal_thread_delete signal_thread_delete(); type_signal_thread_list_sync signal_thread_list_sync(); type_signal_thread_members_update signal_thread_members_update(); + type_signal_added_to_thread signal_added_to_thread(); + type_signal_removed_from_thread signal_removed_from_thread(); type_signal_message_sent signal_message_sent(); type_signal_message_send_fail signal_message_send_fail(); type_signal_disconnected signal_disconnected(); @@ -423,6 +430,8 @@ protected: type_signal_thread_delete m_signal_thread_delete; type_signal_thread_list_sync m_signal_thread_list_sync; type_signal_thread_members_update m_signal_thread_members_update; + type_signal_removed_from_thread m_signal_removed_from_thread; + type_signal_added_to_thread m_signal_added_to_thread; type_signal_message_sent m_signal_message_sent; type_signal_message_send_fail m_signal_message_send_fail; type_signal_disconnected m_signal_disconnected;