better join/leave thread logic

This commit is contained in:
ouwou 2021-08-05 04:02:47 -04:00
parent 41a63df1b1
commit 856674506c
5 changed files with 55 additions and 27 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);
}

View File

@ -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;
}

View File

@ -266,7 +266,7 @@ private:
std::map<Snowflake, GuildApplicationData> m_guild_join_requests;
std::map<Snowflake, PresenceStatus> m_user_to_status;
std::map<Snowflake, RelationshipType> m_user_relationships;
std::vector<Snowflake> m_joined_threads;
std::set<Snowflake> m_joined_threads;
UserData m_user_data;
UserSettings m_user_settings;
@ -338,9 +338,14 @@ public:
typedef sigc::signal<void, ThreadDeleteData> type_signal_thread_delete;
typedef sigc::signal<void, ThreadListSyncData> type_signal_thread_list_sync;
typedef sigc::signal<void, ThreadMembersUpdateData> type_signal_thread_members_update;
typedef sigc::signal<void, Message> type_signal_message_unpinned; // not a real event
typedef sigc::signal<void, Message> type_signal_message_pinned; // not a real event either
// not discord dispatch events
typedef sigc::signal<void, Snowflake> type_signal_added_to_thread;
typedef sigc::signal<void, Snowflake> type_signal_removed_from_thread;
typedef sigc::signal<void, Message> type_signal_message_unpinned;
typedef sigc::signal<void, Message> type_signal_message_pinned;
typedef sigc::signal<void, Message> type_signal_message_sent;
typedef sigc::signal<void, std::string /* nonce */, float /* retry_after */> type_signal_message_send_fail; // retry after param will be 0 if it failed for a reason that isnt slowmode
typedef sigc::signal<void, bool, GatewayCloseCode> type_signal_disconnected; // bool true if reconnecting
typedef sigc::signal<void> 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;