Skip to content

Commit

Permalink
* Save some memory for non-reply messages by using a shared_ptr<Refer…
Browse files Browse the repository at this point in the history
…enceMessage>.
  • Loading branch information
iProgramMC committed Sep 11, 2024
1 parent 8be040c commit 8c40ec5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/discord/DiscordInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,8 @@ bool DiscordInstance::EditMessageInCurrentChannel(const std::string& msg_, Snowf
return false;

Json j;
if (!pMsg->m_referencedMessage.m_bMentionsAuthor) {
if (pMsg->m_pReferencedMessage &&
!pMsg->m_pReferencedMessage->m_bMentionsAuthor) {
Json alm, prs;
prs.push_back("users");
prs.push_back("roles");
Expand Down
14 changes: 8 additions & 6 deletions src/discord/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ void Message::Load(Json& data, Snowflake guild)

if (!msgRef.is_null())
{
m_bHaveReferencedMessage = true;
m_pReferencedMessage = std::make_shared<ReferenceMessage>();
ReferenceMessage& rMsg = *m_pReferencedMessage;

if (msgRef["guild_id"].is_null())
m_refMessageGuild = guild;
Expand All @@ -304,11 +305,11 @@ void Message::Load(Json& data, Snowflake guild)
m_refMessageSnowflake = GetSnowflake(msgRef, "message_id");

if (!refdMsg.is_null()) {
m_referencedMessage.Load(refdMsg, guild);
rMsg.Load(refdMsg, guild);
}
else {
m_referencedMessage.m_author = "Original message was deleted.";
m_referencedMessage.m_author_snowflake = 0;
rMsg.m_author = "Original message was deleted.";
rMsg.m_author_snowflake = 0;
}
}

Expand All @@ -325,8 +326,9 @@ void Message::Load(Json& data, Snowflake guild)

m_userMentions.insert(id);

if (m_bHaveReferencedMessage && m_referencedMessage.m_author_snowflake == id)
m_referencedMessage.m_bMentionsAuthor = true;
if (m_pReferencedMessage != nullptr &&
m_pReferencedMessage->m_author_snowflake == id)
m_pReferencedMessage->m_bMentionsAuthor = true;
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/discord/Message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,15 @@ class Message
std::set<Snowflake> m_userMentions;
std::set<Snowflake> m_roleMentions;
bool m_bMentionedEveryone = false;
bool m_bHaveReferencedMessage = false;
bool m_bIsAuthorBot = false;
bool m_bRead = false; // valid only for the notification viewer messages
Snowflake m_refMessageGuild = 0;
Snowflake m_refMessageChannel = 0;
Snowflake m_refMessageSnowflake = 0;
ReferenceMessage m_referencedMessage;
std::vector<RichEmbed> m_embeds;
Snowflake m_webhookId = 0;
std::shared_ptr<MessagePoll> m_pMessagePoll;
std::shared_ptr<ReferenceMessage> m_pReferencedMessage;

public:
Message() {}
Expand Down
22 changes: 12 additions & 10 deletions src/windows/MessageList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,14 @@ void MessageItem::Update(Snowflake guildID)
m_date = ConvertCppStringToTString(isCompact ? m_msg.m_dateCompact : m_msg.m_dateFull);
m_dateEdited = ConvertCppStringToTString(isCompact ? m_msg.m_editedTextCompact : m_msg.m_editedText);

if (m_msg.m_bHaveReferencedMessage)
if (m_msg.m_pReferencedMessage)
{
std::string authorStr = m_msg.m_referencedMessage.m_author;
if (m_msg.m_referencedMessage.m_bMentionsAuthor) {
std::string authorStr = m_msg.m_pReferencedMessage->m_author;
if (m_msg.m_pReferencedMessage->m_bMentionsAuthor) {
authorStr = "@" + authorStr;
}

m_replyMsg = ConvertCppStringToTString(m_msg.m_referencedMessage.m_message);
m_replyMsg = ConvertCppStringToTString(m_msg.m_pReferencedMessage->m_message);
m_replyAuth = ConvertCppStringToTString(authorStr);
}

Expand Down Expand Up @@ -1885,7 +1885,7 @@ int MessageList::DrawMessageReply(HDC hdc, MessageItem& item, RECT& rc)
RECT rcReply = rc;
rcReply.bottom = rcReply.top + item.m_replyHeight;

auto& refMsg = item.m_msg.m_referencedMessage;
auto& refMsg = *item.m_msg.m_pReferencedMessage;
const bool isActionMessage = IsActionMessage(refMsg.m_type);
const bool isCompact = IsCompact();

Expand Down Expand Up @@ -2391,7 +2391,7 @@ void MessageList::DrawMessage(HDC hdc, MessageItem& item, RECT& msgRect, RECT& c
// draw the reply if needed
int replyOffset = 0;

if (item.m_msg.m_bHaveReferencedMessage && !isActionMessage && isChainBegin)
if (item.m_msg.m_pReferencedMessage != nullptr && !isActionMessage && isChainBegin)
replyOffset = DrawMessageReply(hdc, item, rc);

rc.top += replyOffset;
Expand Down Expand Up @@ -3463,8 +3463,9 @@ LRESULT CALLBACK MessageList::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
break;

std::string action = " said ";
if (pMsg->m_msg.m_type == MessageType::REPLY)
action = " replied to " + pMsg->m_msg.m_referencedMessage.m_author + " ";
if (pMsg->m_msg.m_type == MessageType::REPLY &&
pMsg->m_msg.m_pReferencedMessage != nullptr)
action = " replied to " + pMsg->m_msg.m_pReferencedMessage->m_author + " ";

TextToSpeech::Speak(pMsg->m_msg.m_author + action + GetDiscordInstance()->ReverseMentions(pMsg->m_msg.m_message, pThis->m_guildID, true));
break;
Expand Down Expand Up @@ -4151,7 +4152,7 @@ bool MessageList::ShouldStartNewChain(Snowflake prevAuthor, time_t prevTime, int
prevAuthor != item.m_msg.m_author_snowflake ||
prevTime + 15 * 60 < item.m_msg.m_dateTime ||
item.m_msg.IsLoadGap() ||
item.m_msg.m_bHaveReferencedMessage ||
item.m_msg.m_pReferencedMessage != nullptr ||
item.m_msg.m_type == MessageType::REPLY ||
IsActionMessage(prevType) ||
IsActionMessage(item.m_msg.m_type) ||
Expand Down Expand Up @@ -4306,7 +4307,8 @@ void MessageList::OnUpdateAvatar(Snowflake sf)
if (msg.m_msg.m_author_snowflake == sf && !IsCompact())
InvalidateRect(m_hwnd, &msg.m_avatarRect, false);

if (msg.m_msg.m_bHaveReferencedMessage && msg.m_msg.m_referencedMessage.m_author_snowflake == sf && !IsCompact())
if (msg.m_msg.m_pReferencedMessage != nullptr &&
msg.m_msg.m_pReferencedMessage->m_author_snowflake == sf && !IsCompact())
InvalidateRect(m_hwnd, &msg.m_refAvatarRect, false);
}
}
Expand Down

0 comments on commit 8c40ec5

Please sign in to comment.