fix unchecked optionals

also discard exceptions in file cache futures
This commit is contained in:
ouwou
2021-10-08 17:52:38 -04:00
parent abc0a7931e
commit fa1a007dc1
7 changed files with 40 additions and 23 deletions

View File

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

View File

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

View File

@@ -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("<i>reply unavailable</i>");
@@ -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);
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())
md = "<span weight='bold' color='#" + IntToCSSColor(role->Color) + "'>" + user->GetEscapedName() + "</span>";
else
md = "<span weight='bold'>" + user->GetEscapedName() + "</span>";
m_author.set_markup(md);
std::string md;
if (role.has_value())
m_author.set_markup("<span weight='bold' color='#" + IntToCSSColor(role->Color) + "'>" + user->GetEscapedName() + "</span>");
else
m_author.set_markup("<span weight='bold'>" + user->GetEscapedName() + "</span>");
} else
m_author.set_markup("<span weight='bold'>" + user->GetEscapedName() + "</span>");
}
std::vector<Gtk::Widget *> ChatMessageHeader::GetChildContent() {

View File

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

View File

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

View File

@@ -152,7 +152,8 @@ void DiscordClient::FetchMessagesInChannel(Snowflake id, sigc::slot<void(const s
m_store.BeginTransaction();
for (auto &msg : msgs) {
StoreMessageData(msg);
AddUserToGuild(msg.Author.ID, *msg.GuildID);
if (msg.GuildID.has_value())
AddUserToGuild(msg.Author.ID, *msg.GuildID);
}
m_store.EndTransaction();
@@ -300,7 +301,7 @@ bool DiscordClient::HasGuildPermission(Snowflake user_id, Snowflake guild_id, Pe
bool DiscordClient::HasAnyChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const {
const auto channel = m_store.GetChannel(channel_id);
if (!channel.has_value()) return false;
if (!channel.has_value() || !channel->GuildID.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);
}

View File

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