fix potential crash when displaying audit log

This commit is contained in:
ouwou
2021-05-14 02:44:07 -04:00
parent 09cb0b07bb
commit cd97c55465
3 changed files with 45 additions and 41 deletions

View File

@@ -20,7 +20,7 @@ void from_json(const nlohmann::json &j, AuditLogOptions &m) {
void from_json(const nlohmann::json &j, AuditLogEntry &m) { void from_json(const nlohmann::json &j, AuditLogEntry &m) {
JS_N("target_id", m.TargetID); JS_N("target_id", m.TargetID);
JS_O("changes", m.Changes); JS_O("changes", m.Changes);
JS_D("user_id", m.UserID); JS_N("user_id", m.UserID);
JS_D("id", m.ID); JS_D("id", m.ID);
JS_D("action_type", m.Type); JS_D("action_type", m.Type);
JS_O("options", m.Options); JS_O("options", m.Options);

View File

@@ -66,7 +66,7 @@ struct AuditLogOptions {
struct AuditLogEntry { struct AuditLogEntry {
Snowflake ID; Snowflake ID;
std::string TargetID; // null std::string TargetID; // null
Snowflake UserID; std::optional<Snowflake> UserID;
AuditLogActionType Type; AuditLogActionType Type;
std::optional<std::string> Reason; std::optional<std::string> Reason;
std::optional<std::vector<AuditLogChange>> Changes; std::optional<std::vector<AuditLogChange>> Changes;

View File

@@ -33,7 +33,11 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
auto label = Gtk::manage(new Gtk::Label); auto label = Gtk::manage(new Gtk::Label);
label->set_ellipsize(Pango::ELLIPSIZE_END); label->set_ellipsize(Pango::ELLIPSIZE_END);
auto user = *discord.GetUser(entry.UserID); Glib::ustring user_markup = "<b>Unknown User</b>";
if (entry.UserID.has_value()) {
if (auto user = discord.GetUser(*entry.UserID); user.has_value())
user_markup = discord.GetUser(*entry.UserID)->GetEscapedBoldString<false>();
}
// spaghetti moment // spaghetti moment
Glib::ustring markup; Glib::ustring markup;
@@ -41,7 +45,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
switch (entry.Type) { switch (entry.Type) {
case AuditLogActionType::GUILD_UPDATE: { case AuditLogActionType::GUILD_UPDATE: {
markup = markup =
user.GetEscapedBoldString<false>() + user_markup +
" made changes to <b>" + " made changes to <b>" +
Glib::Markup::escape_text(guild.Name) + Glib::Markup::escape_text(guild.Name) +
"</b>"; "</b>";
@@ -63,7 +67,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
} break; } break;
case AuditLogActionType::CHANNEL_CREATE: { case AuditLogActionType::CHANNEL_CREATE: {
const auto type = *entry.GetNewFromKey<ChannelType>("type"); const auto type = *entry.GetNewFromKey<ChannelType>("type");
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" created a " + (type == ChannelType::GUILD_VOICE ? "voice" : "text") + " created a " + (type == ChannelType::GUILD_VOICE ? "voice" : "text") +
" channel <b>#" + " channel <b>#" +
Glib::Markup::escape_text(*entry.GetNewFromKey<std::string>("name")) + Glib::Markup::escape_text(*entry.GetNewFromKey<std::string>("name")) +
@@ -83,12 +87,12 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
case AuditLogActionType::CHANNEL_UPDATE: { case AuditLogActionType::CHANNEL_UPDATE: {
const auto target_channel = discord.GetChannel(entry.TargetID); const auto target_channel = discord.GetChannel(entry.TargetID);
if (target_channel.has_value()) { if (target_channel.has_value()) {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" made changes to <b>#" + " made changes to <b>#" +
Glib::Markup::escape_text(*target_channel->Name) + Glib::Markup::escape_text(*target_channel->Name) +
"</b>"; "</b>";
} else { } else {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" made changes to <b>&lt;#" + " made changes to <b>&lt;#" +
entry.TargetID + entry.TargetID +
"&gt;</b>"; "&gt;</b>";
@@ -126,7 +130,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
} }
} break; } break;
case AuditLogActionType::CHANNEL_DELETE: { case AuditLogActionType::CHANNEL_DELETE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" removed <b>#" + " removed <b>#" +
Glib::Markup::escape_text(*entry.GetOldFromKey<std::string>("name")) + Glib::Markup::escape_text(*entry.GetOldFromKey<std::string>("name")) +
"</b>"; "</b>";
@@ -134,11 +138,11 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
case AuditLogActionType::CHANNEL_OVERWRITE_CREATE: { case AuditLogActionType::CHANNEL_OVERWRITE_CREATE: {
const auto channel = discord.GetChannel(entry.TargetID); const auto channel = discord.GetChannel(entry.TargetID);
if (channel.has_value()) { if (channel.has_value()) {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" created channel overrides for <b>#" + " created channel overrides for <b>#" +
Glib::Markup::escape_text(*channel->Name) + "</b>"; Glib::Markup::escape_text(*channel->Name) + "</b>";
} else { } else {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" created channel overrides for <b>&lt;#" + " created channel overrides for <b>&lt;#" +
entry.TargetID + "&gt;</b>"; entry.TargetID + "&gt;</b>";
} }
@@ -146,11 +150,11 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
case AuditLogActionType::CHANNEL_OVERWRITE_UPDATE: { case AuditLogActionType::CHANNEL_OVERWRITE_UPDATE: {
const auto channel = discord.GetChannel(entry.TargetID); const auto channel = discord.GetChannel(entry.TargetID);
if (channel.has_value()) { if (channel.has_value()) {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" updated channel overrides for <b>#" + " updated channel overrides for <b>#" +
Glib::Markup::escape_text(*channel->Name) + "</b>"; Glib::Markup::escape_text(*channel->Name) + "</b>";
} else { } else {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" updated channel overrides for <b>&lt;#" + " updated channel overrides for <b>&lt;#" +
entry.TargetID + "&gt;</b>"; entry.TargetID + "&gt;</b>";
} }
@@ -158,24 +162,24 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
case AuditLogActionType::CHANNEL_OVERWRITE_DELETE: { case AuditLogActionType::CHANNEL_OVERWRITE_DELETE: {
const auto channel = discord.GetChannel(entry.TargetID); const auto channel = discord.GetChannel(entry.TargetID);
if (channel.has_value()) { if (channel.has_value()) {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" removed channel overrides for <b>#" + " removed channel overrides for <b>#" +
Glib::Markup::escape_text(*channel->Name) + "</b>"; Glib::Markup::escape_text(*channel->Name) + "</b>";
} else { } else {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" removed channel overrides for <b>&lt;#" + " removed channel overrides for <b>&lt;#" +
entry.TargetID + "&gt;</b>"; entry.TargetID + "&gt;</b>";
} }
} break; } break;
case AuditLogActionType::MEMBER_KICK: { case AuditLogActionType::MEMBER_KICK: {
const auto target_user = discord.GetUser(entry.TargetID); const auto target_user = discord.GetUser(entry.TargetID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" kicked <b>" + " kicked <b>" +
target_user->GetEscapedString() + target_user->GetEscapedString() +
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::MEMBER_PRUNE: { case AuditLogActionType::MEMBER_PRUNE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" pruned <b>" + " pruned <b>" +
*entry.Options->MembersRemoved + *entry.Options->MembersRemoved +
"</b> members"; "</b> members";
@@ -185,21 +189,21 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
} break; } break;
case AuditLogActionType::MEMBER_BAN_ADD: { case AuditLogActionType::MEMBER_BAN_ADD: {
const auto target_user = discord.GetUser(entry.TargetID); const auto target_user = discord.GetUser(entry.TargetID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" banned <b>" + " banned <b>" +
target_user->GetEscapedString() + target_user->GetEscapedString() +
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::MEMBER_BAN_REMOVE: { case AuditLogActionType::MEMBER_BAN_REMOVE: {
const auto target_user = discord.GetUser(entry.TargetID); const auto target_user = discord.GetUser(entry.TargetID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" removed the ban for <b>" + " removed the ban for <b>" +
target_user->GetEscapedString() + target_user->GetEscapedString() +
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::MEMBER_UPDATE: { case AuditLogActionType::MEMBER_UPDATE: {
const auto target_user = discord.GetUser(entry.TargetID); const auto target_user = discord.GetUser(entry.TargetID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" updated <b>" + " updated <b>" +
target_user->GetEscapedString() + target_user->GetEscapedString() +
"</b>"; "</b>";
@@ -221,7 +225,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
} break; } break;
case AuditLogActionType::MEMBER_ROLE_UPDATE: { case AuditLogActionType::MEMBER_ROLE_UPDATE: {
const auto target_user = discord.GetUser(entry.TargetID); const auto target_user = discord.GetUser(entry.TargetID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" updated roles for <b>" + " updated roles for <b>" +
target_user->GetEscapedString() + "</b>"; target_user->GetEscapedString() + "</b>";
if (entry.Changes.has_value()) if (entry.Changes.has_value())
@@ -239,7 +243,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
} break; } break;
case AuditLogActionType::MEMBER_MOVE: { case AuditLogActionType::MEMBER_MOVE: {
const auto channel = discord.GetChannel(*entry.Options->ChannelID); const auto channel = discord.GetChannel(*entry.Options->ChannelID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" moved <b>" + " moved <b>" +
*entry.Options->Count + *entry.Options->Count +
" user" + " user" +
@@ -249,27 +253,27 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::MEMBER_DISCONNECT: { case AuditLogActionType::MEMBER_DISCONNECT: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" disconnected <b>" + " disconnected <b>" +
*entry.Options->Count + *entry.Options->Count +
"</b> users from voice"; "</b> users from voice";
} break; } break;
case AuditLogActionType::BOT_ADD: { case AuditLogActionType::BOT_ADD: {
const auto target_user = discord.GetUser(entry.TargetID); const auto target_user = discord.GetUser(entry.TargetID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" added <b>" + " added <b>" +
target_user->GetEscapedString() + target_user->GetEscapedString() +
"</b> to the server"; "</b> to the server";
} break; } break;
case AuditLogActionType::ROLE_CREATE: { case AuditLogActionType::ROLE_CREATE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" created the role <b>" + " created the role <b>" +
*entry.GetNewFromKey<std::string>("name") + *entry.GetNewFromKey<std::string>("name") +
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::ROLE_UPDATE: { case AuditLogActionType::ROLE_UPDATE: {
const auto role = discord.GetRole(entry.TargetID); const auto role = discord.GetRole(entry.TargetID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" updated the role <b>" + " updated the role <b>" +
(role.has_value() ? Glib::Markup::escape_text(role->Name) : Glib::ustring(entry.TargetID)) + (role.has_value() ? Glib::Markup::escape_text(role->Name) : Glib::ustring(entry.TargetID)) +
"</b>"; "</b>";
@@ -297,14 +301,14 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
} }
} break; } break;
case AuditLogActionType::ROLE_DELETE: { case AuditLogActionType::ROLE_DELETE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" deleted the role <b>" + " deleted the role <b>" +
*entry.GetOldFromKey<std::string>("name") + *entry.GetOldFromKey<std::string>("name") +
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::INVITE_CREATE: { case AuditLogActionType::INVITE_CREATE: {
const auto code = *entry.GetNewFromKey<std::string>("code"); const auto code = *entry.GetNewFromKey<std::string>("code");
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" created an invite <b>" + code + "</b>"; " created an invite <b>" + code + "</b>";
if (entry.Changes.has_value()) if (entry.Changes.has_value())
for (const auto &change : *entry.Changes) { for (const auto &change : *entry.Changes) {
@@ -328,13 +332,13 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
} }
} break; } break;
case AuditLogActionType::INVITE_DELETE: { case AuditLogActionType::INVITE_DELETE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" deleted an invite <b>" + " deleted an invite <b>" +
*entry.GetOldFromKey<std::string>("code") + *entry.GetOldFromKey<std::string>("code") +
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::WEBHOOK_CREATE: { case AuditLogActionType::WEBHOOK_CREATE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" created the webhook <b>" + " created the webhook <b>" +
Glib::Markup::escape_text(*entry.GetNewFromKey<std::string>("name")) + Glib::Markup::escape_text(*entry.GetNewFromKey<std::string>("name")) +
"</b>"; "</b>";
@@ -356,12 +360,12 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
webhookptr = &webhook; webhookptr = &webhook;
} }
if (webhookptr != nullptr) { if (webhookptr != nullptr) {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" updated the webhook <b>" + " updated the webhook <b>" +
Glib::Markup::escape_text(webhookptr->Name) + Glib::Markup::escape_text(webhookptr->Name) +
"</b>"; "</b>";
} else { } else {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" updated a webhook"; " updated a webhook";
} }
if (entry.Changes.has_value()) if (entry.Changes.has_value())
@@ -385,19 +389,19 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
} }
} break; } break;
case AuditLogActionType::WEBHOOK_DELETE: { case AuditLogActionType::WEBHOOK_DELETE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" deleted the webhook <b>" + " deleted the webhook <b>" +
Glib::Markup::escape_text(*entry.GetOldFromKey<std::string>("name")) + Glib::Markup::escape_text(*entry.GetOldFromKey<std::string>("name")) +
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::EMOJI_CREATE: { case AuditLogActionType::EMOJI_CREATE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" created the emoji <b>" + " created the emoji <b>" +
Glib::Markup::escape_text(*entry.GetNewFromKey<std::string>("name")) + Glib::Markup::escape_text(*entry.GetNewFromKey<std::string>("name")) +
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::EMOJI_UPDATE: { case AuditLogActionType::EMOJI_UPDATE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" updated the emoji <b>" + " updated the emoji <b>" +
Glib::Markup::escape_text(*entry.GetOldFromKey<std::string>("name")) + Glib::Markup::escape_text(*entry.GetOldFromKey<std::string>("name")) +
"</b>"; "</b>";
@@ -408,7 +412,7 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
"</b>"); "</b>");
} break; } break;
case AuditLogActionType::EMOJI_DELETE: { case AuditLogActionType::EMOJI_DELETE: {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" deleted the emoji <b>" + " deleted the emoji <b>" +
Glib::Markup::escape_text(*entry.GetOldFromKey<std::string>("name")) + Glib::Markup::escape_text(*entry.GetOldFromKey<std::string>("name")) +
"</b>"; "</b>";
@@ -417,26 +421,26 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
const auto channel = discord.GetChannel(*entry.Options->ChannelID); const auto channel = discord.GetChannel(*entry.Options->ChannelID);
const auto count = *entry.Options->Count; const auto count = *entry.Options->Count;
if (channel.has_value()) { if (channel.has_value()) {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" deleted <b>" + count + "</b> messages in <b>#" + " deleted <b>" + count + "</b> messages in <b>#" +
Glib::Markup::escape_text(*channel->Name) + Glib::Markup::escape_text(*channel->Name) +
"</b>"; "</b>";
} else { } else {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" deleted <b>" + count + "</b> messages"; " deleted <b>" + count + "</b> messages";
} }
} break; } break;
case AuditLogActionType::MESSAGE_BULK_DELETE: { case AuditLogActionType::MESSAGE_BULK_DELETE: {
const auto channel = discord.GetChannel(entry.TargetID); const auto channel = discord.GetChannel(entry.TargetID);
if (channel.has_value()) { if (channel.has_value()) {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" deleted <b>" + " deleted <b>" +
*entry.Options->Count + *entry.Options->Count +
"</b> messages in <b>#" + "</b> messages in <b>#" +
Glib::Markup::escape_text(*channel->Name) + Glib::Markup::escape_text(*channel->Name) +
"</b>"; "</b>";
} else { } else {
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" deleted <b>" + " deleted <b>" +
*entry.Options->Count + *entry.Options->Count +
"</b> messages"; "</b> messages";
@@ -444,14 +448,14 @@ void GuildSettingsAuditLogPane::OnAuditLogFetch(const AuditLogData &data) {
} break; } break;
case AuditLogActionType::MESSAGE_PIN: { case AuditLogActionType::MESSAGE_PIN: {
const auto target_user = discord.GetUser(entry.TargetID); const auto target_user = discord.GetUser(entry.TargetID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" pinned a message by <b>" + " pinned a message by <b>" +
target_user->GetEscapedString() + target_user->GetEscapedString() +
"</b>"; "</b>";
} break; } break;
case AuditLogActionType::MESSAGE_UNPIN: { case AuditLogActionType::MESSAGE_UNPIN: {
const auto target_user = discord.GetUser(entry.TargetID); const auto target_user = discord.GetUser(entry.TargetID);
markup = user.GetEscapedBoldString<false>() + markup = user_markup +
" unpinned a message by <b>" + " unpinned a message by <b>" +
target_user->GetEscapedString() + target_user->GetEscapedString() +
"</b>"; "</b>";