From fa1a007dc12208a90c278cb426c37c9a24079636 Mon Sep 17 00:00:00 2001
From: ouwou <26526779+ouwou@users.noreply.github.com>
Date: Fri, 8 Oct 2021 17:52:38 -0400
Subject: fix unchecked optionals also discard exceptions in file cache futures
---
components/channels.cpp | 2 +-
components/chatlist.cpp | 14 +++++++++-----
components/chatmessage.cpp | 29 +++++++++++++++++------------
components/memberlist.cpp | 2 +-
components/ratelimitindicator.cpp | 1 +
discord/discord.cpp | 8 +++++---
filecache.cpp | 9 +++++++--
7 files changed, 41 insertions(+), 24 deletions(-)
diff --git a/components/channels.cpp b/components/channels.cpp
index 98604d7..ac11a0c 100644
--- a/components/channels.cpp
+++ b/components/channels.cpp
@@ -227,7 +227,7 @@ void ChannelList::UpdateChannel(Snowflake id) {
Gtk::TreeModel::iterator new_parent;
if (channel->ParentID.has_value())
new_parent = GetIteratorForChannelFromID(*channel->ParentID);
- else
+ else if (channel->GuildID.has_value())
new_parent = GetIteratorForGuildFromID(*channel->GuildID);
if (new_parent && iter->parent() != new_parent)
diff --git a/components/chatlist.cpp b/components/chatlist.cpp
index 01f1629..96d8de2 100644
--- a/components/chatlist.cpp
+++ b/components/chatlist.cpp
@@ -139,7 +139,10 @@ void ChatList::ProcessNewMessage(const Message &data, bool prepend) {
if (should_attach) {
header = last_row;
} else {
- const auto guild_id = *discord.GetChannel(m_active_channel)->GuildID;
+ const auto chan = discord.GetChannel(m_active_channel);
+ Snowflake guild_id;
+ if (chan.has_value() && chan->GuildID.has_value())
+ guild_id = *chan->GuildID;
const auto user_id = data.Author.ID;
const auto user = discord.GetUser(user_id);
if (!user.has_value()) return;
@@ -170,13 +173,14 @@ void ChatList::ProcessNewMessage(const Message &data, bool prepend) {
if (!data.has_value()) return false;
const auto channel = client.GetChannel(m_active_channel);
- bool is_dm = channel.has_value() && (channel->Type == ChannelType::DM || channel->Type == ChannelType::GROUP_DM);
- const bool has_manage = client.HasChannelPermission(client.GetUserData().ID, m_active_channel, Permission::MANAGE_MESSAGES);
+ bool has_manage = channel.has_value() && (channel->Type == ChannelType::DM || channel->Type == ChannelType::GROUP_DM);
+ if (!has_manage)
+ has_manage = client.HasChannelPermission(client.GetUserData().ID, m_active_channel, Permission::MANAGE_MESSAGES);
m_menu_edit_message->set_visible(!m_use_pinned_menu);
m_menu_reply_to->set_visible(!m_use_pinned_menu);
- m_menu_unpin->set_visible((is_dm || has_manage) && data->IsPinned);
- m_menu_pin->set_visible((is_dm || has_manage) && !data->IsPinned);
+ m_menu_unpin->set_visible(has_manage && data->IsPinned);
+ m_menu_pin->set_visible(has_manage && !data->IsPinned);
if (data->IsDeleted()) {
m_menu_delete_message->set_sensitive(false);
diff --git a/components/chatmessage.cpp b/components/chatmessage.cpp
index 88e3f2c..220ae89 100644
--- a/components/chatmessage.cpp
+++ b/components/chatmessage.cpp
@@ -692,7 +692,10 @@ Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data)
// which of course would not be an issue if i could figure out how to get fonts to work on this god-forsaken framework
// oh well
// but ill manually get colors for the user who is being replied to
- lbl->set_markup(get_author_markup(referenced.Author.ID, *referenced.GuildID) + ": " + text);
+ if (referenced.GuildID.has_value())
+ lbl->set_markup(get_author_markup(referenced.Author.ID, *referenced.GuildID) + ": " + text);
+ else
+ lbl->set_markup(get_author_markup(referenced.Author.ID) + ": " + text);
}
} else {
lbl->set_markup("reply unavailable");
@@ -1163,19 +1166,21 @@ ChatMessageHeader::ChatMessageHeader(const Message &data)
void ChatMessageHeader::UpdateNameColor() {
const auto &discord = Abaddon::Get().GetDiscordClient();
- const auto guild_id = discord.GetChannel(ChannelID)->GuildID;
- const auto role_id = discord.GetMemberHoistedRole(*guild_id, UserID, true);
const auto user = discord.GetUser(UserID);
if (!user.has_value()) return;
- const auto role = discord.GetRole(role_id);
-
- std::string md;
- if (role.has_value())
- md = "" + user->GetEscapedName() + "";
- else
- md = "" + user->GetEscapedName() + "";
-
- m_author.set_markup(md);
+ const auto chan = discord.GetChannel(ChannelID);
+ bool is_guild = chan.has_value() && chan->GuildID.has_value();
+ if (is_guild) {
+ const auto role_id = discord.GetMemberHoistedRole(*chan->GuildID, UserID, true);
+ const auto role = discord.GetRole(role_id);
+
+ std::string md;
+ if (role.has_value())
+ m_author.set_markup("" + user->GetEscapedName() + "");
+ else
+ m_author.set_markup("" + user->GetEscapedName() + "");
+ } else
+ m_author.set_markup("" + user->GetEscapedName() + "");
}
std::vector ChatMessageHeader::GetChildContent() {
diff --git a/components/memberlist.cpp b/components/memberlist.cpp
index 804f443..ffc210b 100644
--- a/components/memberlist.cpp
+++ b/components/memberlist.cpp
@@ -94,7 +94,7 @@ void MemberList::SetActiveChannel(Snowflake id) {
m_guild_id = Snowflake::Invalid;
if (m_chan_id.IsValid()) {
const auto chan = Abaddon::Get().GetDiscordClient().GetChannel(id);
- if (chan.has_value()) m_guild_id = *chan->GuildID;
+ if (chan.has_value() && chan->GuildID.has_value()) m_guild_id = *chan->GuildID;
}
}
diff --git a/components/ratelimitindicator.cpp b/components/ratelimitindicator.cpp
index 708ab34..fe187db 100644
--- a/components/ratelimitindicator.cpp
+++ b/components/ratelimitindicator.cpp
@@ -106,6 +106,7 @@ bool RateLimitIndicator::UpdateIndicator() {
void RateLimitIndicator::OnMessageCreate(const Message &message) {
auto &discord = Abaddon::Get().GetDiscordClient();
if (message.Author.ID != discord.GetUserData().ID) return;
+ if (!message.GuildID.has_value()) return;
const bool can_bypass = discord.HasAnyChannelPermission(discord.GetUserData().ID, m_active_channel, Permission::MANAGE_MESSAGES | Permission::MANAGE_CHANNELS);
const auto rate_limit = GetRateLimit();
if (rate_limit > 0 && !can_bypass) {
diff --git a/discord/discord.cpp b/discord/discord.cpp
index 10bd322..257df5c 100644
--- a/discord/discord.cpp
+++ b/discord/discord.cpp
@@ -152,7 +152,8 @@ void DiscordClient::FetchMessagesInChannel(Snowflake id, sigc::slotGuildID.has_value()) return false;
const auto base = ComputePermissions(user_id, *channel->GuildID);
const auto overwrites = ComputeOverwrites(base, user_id, channel_id);
return (overwrites & perm) != Permission::NONE;
@@ -1400,7 +1401,8 @@ void DiscordClient::HandleGatewayReady(const GatewayMessage &msg) {
void DiscordClient::HandleGatewayMessageCreate(const GatewayMessage &msg) {
Message data = msg.Data;
StoreMessageData(data);
- AddUserToGuild(data.Author.ID, *data.GuildID);
+ if (data.GuildID.has_value())
+ AddUserToGuild(data.Author.ID, *data.GuildID);
m_signal_message_create.emit(data);
}
diff --git a/filecache.cpp b/filecache.cpp
index 125f0ec..05572f0 100644
--- a/filecache.cpp
+++ b/filecache.cpp
@@ -17,8 +17,13 @@ Cache::Cache() {
Cache::~Cache() {
m_worker.stop();
- for (auto &future : m_futures)
- if (future.valid()) future.get();
+ for (auto &future : m_futures) {
+ if (future.valid()) {
+ try { // dont care about stored exceptions
+ future.get();
+ } catch (...) {}
+ }
+ }
std::error_code err;
if (!std::filesystem::remove_all(m_tmp_path, err))
--
cgit v1.2.3