summaryrefslogtreecommitdiff
path: root/src/components/chatmessage.cpp
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2023-03-15 20:19:54 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2023-03-15 20:19:54 -0400
commit358f68d2679640d502d0a13b1d09ee2b3490b4e1 (patch)
tree685b851243092e1345e70da4e7794f1d282e055c /src/components/chatmessage.cpp
parent823ba090f05a3b464f93721fc8ce2a6969e3bb67 (diff)
downloadabaddon-portaudio-358f68d2679640d502d0a13b1d09ee2b3490b4e1.tar.gz
abaddon-portaudio-358f68d2679640d502d0a13b1d09ee2b3490b4e1.zip
cleanup and escape notification bodies
Diffstat (limited to 'src/components/chatmessage.cpp')
-rw-r--r--src/components/chatmessage.cpp225
1 files changed, 12 insertions, 213 deletions
diff --git a/src/components/chatmessage.cpp b/src/components/chatmessage.cpp
index 8c16922..5f4c9a5 100644
--- a/src/components/chatmessage.cpp
+++ b/src/components/chatmessage.cpp
@@ -1,14 +1,9 @@
#include "chatmessage.hpp"
+#include "constants.hpp"
#include "lazyimage.hpp"
+#include "misc/chatutil.hpp"
#include <unordered_map>
-constexpr static int EmojiSize = 24; // settings eventually
-constexpr static int AvatarSize = 32;
-constexpr static int EmbedImageWidth = 400;
-constexpr static int EmbedImageHeight = 300;
-constexpr static int ThumbnailSize = 100;
-constexpr static int StickerComponentSize = 160;
-
ChatMessageItemContainer::ChatMessageItemContainer()
: m_main(Gtk::ORIENTATION_VERTICAL) {
add(m_main);
@@ -190,11 +185,11 @@ void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) {
case MessageType::DEFAULT:
case MessageType::INLINE_REPLY:
b->insert(s, data->Content);
- HandleRoleMentions(b);
- HandleUserMentions(b);
+ ChatUtil::HandleRoleMentions(b);
+ ChatUtil::HandleUserMentions(b, ChannelID, false);
HandleLinks(*tv);
HandleChannelMentions(tv);
- HandleEmojis(*tv);
+ ChatUtil::HandleEmojis(*tv);
break;
case MessageType::USER_PREMIUM_GUILD_SUBSCRIPTION:
b->insert_markup(s, "<span color='#999999'><i>[boosted server]</i></span>");
@@ -216,10 +211,10 @@ void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) {
}
} else {
b->insert(s, data->Content);
- HandleUserMentions(b);
+ ChatUtil::HandleUserMentions(b, ChannelID, false);
HandleLinks(*tv);
HandleChannelMentions(tv);
- HandleEmojis(*tv);
+ ChatUtil::HandleEmojis(*tv);
}
} break;
case MessageType::RECIPIENT_ADD: {
@@ -711,8 +706,8 @@ Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data)
Gtk::TextBuffer::iterator start, end;
buf->get_bounds(start, end);
buf->set_text(referenced.Content);
- CleanupEmojis(buf);
- HandleUserMentions(buf);
+ ChatUtil::CleanupEmojis(buf);
+ ChatUtil::HandleUserMentions(buf, ChannelID, false);
HandleChannelMentions(buf);
text = Glib::Markup::escape_text(buf->get_text());
}
@@ -734,13 +729,6 @@ Gtk::Widget *ChatMessageItemContainer::CreateReplyComponent(const Message &data)
return box;
}
-Glib::ustring ChatMessageItemContainer::GetText(const Glib::RefPtr<Gtk::TextBuffer> &buf) {
- Gtk::TextBuffer::iterator a, b;
- buf->get_bounds(a, b);
- auto slice = buf->get_slice(a, b, true);
- return slice;
-}
-
bool ChatMessageItemContainer::IsEmbedImageOnly(const EmbedData &data) {
if (!data.Thumbnail.has_value()) return false;
if (data.Author.has_value()) return false;
@@ -752,199 +740,10 @@ bool ChatMessageItemContainer::IsEmbedImageOnly(const EmbedData &data) {
return data.Thumbnail->ProxyURL.has_value() && data.Thumbnail->URL.has_value() && data.Thumbnail->Width.has_value() && data.Thumbnail->Height.has_value();
}
-void ChatMessageItemContainer::HandleRoleMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf) {
- constexpr static const auto mentions_regex = R"(<@&(\d+)>)";
-
- static auto rgx = Glib::Regex::create(mentions_regex);
-
- Glib::ustring text = GetText(buf);
- const auto &discord = Abaddon::Get().GetDiscordClient();
-
- int startpos = 0;
- Glib::MatchInfo match;
- while (rgx->match(text, startpos, match)) {
- int mstart, mend;
- if (!match.fetch_pos(0, mstart, mend)) break;
- const Glib::ustring role_id = match.fetch(1);
- const auto role = discord.GetRole(role_id);
- if (!role.has_value()) {
- startpos = mend;
- continue;
- }
-
- Glib::ustring replacement;
- if (role->HasColor()) {
- replacement = "<b><span color=\"#" + IntToCSSColor(role->Color) + "\">@" + role->GetEscapedName() + "</span></b>";
- } else {
- replacement = "<b>@" + role->GetEscapedName() + "</b>";
- }
-
- const auto chars_start = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mstart);
- const auto chars_end = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mend);
- const auto start_it = buf->get_iter_at_offset(chars_start);
- const auto end_it = buf->get_iter_at_offset(chars_end);
-
- auto it = buf->erase(start_it, end_it);
- buf->insert_markup(it, replacement);
-
- text = GetText(buf);
- startpos = 0;
- }
-}
-
-void ChatMessageItemContainer::HandleUserMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf) const {
- constexpr static const auto mentions_regex = R"(<@!?(\d+)>)";
-
- static auto rgx = Glib::Regex::create(mentions_regex);
-
- Glib::ustring text = GetText(buf);
- const auto &discord = Abaddon::Get().GetDiscordClient();
-
- int startpos = 0;
- Glib::MatchInfo match;
- while (rgx->match(text, startpos, match)) {
- int mstart, mend;
- if (!match.fetch_pos(0, mstart, mend)) break;
- const Glib::ustring user_id = match.fetch(1);
- const auto user = discord.GetUser(user_id);
- const auto channel = discord.GetChannel(ChannelID);
- if (!user.has_value() || !channel.has_value()) {
- startpos = mend;
- continue;
- }
-
- Glib::ustring replacement;
-
- if (channel->Type == ChannelType::DM || channel->Type == ChannelType::GROUP_DM)
- replacement = user->GetEscapedBoldString<true>();
- else {
- const auto role_id = user->GetHoistedRole(*channel->GuildID, true);
- const auto role = discord.GetRole(role_id);
- if (!role.has_value())
- replacement = user->GetEscapedBoldString<true>();
- else
- replacement = "<span color=\"#" + IntToCSSColor(role->Color) + "\">" + user->GetEscapedBoldString<true>() + "</span>";
- }
-
- // regex returns byte positions and theres no straightforward way in the c++ bindings to deal with that :(
- const auto chars_start = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mstart);
- const auto chars_end = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mend);
- const auto start_it = buf->get_iter_at_offset(chars_start);
- const auto end_it = buf->get_iter_at_offset(chars_end);
-
- auto it = buf->erase(start_it, end_it);
- buf->insert_markup(it, replacement);
-
- text = GetText(buf);
- startpos = 0;
- }
-}
-
-void ChatMessageItemContainer::HandleStockEmojis(Gtk::TextView &tv) {
- Abaddon::Get().GetEmojis().ReplaceEmojis(tv.get_buffer(), EmojiSize);
-}
-
-void ChatMessageItemContainer::HandleCustomEmojis(Gtk::TextView &tv) {
- static auto rgx = Glib::Regex::create(R"(<a?:([\w\d_]+):(\d+)>)");
-
- auto &img = Abaddon::Get().GetImageManager();
-
- auto buf = tv.get_buffer();
- auto text = GetText(buf);
-
- Glib::MatchInfo match;
- int startpos = 0;
- while (rgx->match(text, startpos, match)) {
- int mstart, mend;
- if (!match.fetch_pos(0, mstart, mend)) break;
- const bool is_animated = match.fetch(0)[1] == 'a';
-
- const auto chars_start = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mstart);
- const auto chars_end = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mend);
- auto start_it = buf->get_iter_at_offset(chars_start);
- auto end_it = buf->get_iter_at_offset(chars_end);
-
- startpos = mend;
- if (is_animated && Abaddon::Get().GetSettings().ShowAnimations) {
- const auto mark_start = buf->create_mark(start_it, false);
- end_it.backward_char();
- const auto mark_end = buf->create_mark(end_it, false);
- const auto cb = [&tv, buf, mark_start, mark_end](const Glib::RefPtr<Gdk::PixbufAnimation> &pixbuf) {
- auto start_it = mark_start->get_iter();
- auto end_it = mark_end->get_iter();
- end_it.forward_char();
- buf->delete_mark(mark_start);
- buf->delete_mark(mark_end);
- auto it = buf->erase(start_it, end_it);
- const auto anchor = buf->create_child_anchor(it);
- auto img = Gtk::manage(new Gtk::Image(pixbuf));
- img->show();
- tv.add_child_at_anchor(*img, anchor);
- };
- img.LoadAnimationFromURL(EmojiData::URLFromID(match.fetch(2), "gif"), EmojiSize, EmojiSize, sigc::track_obj(cb, tv));
- } else {
- // can't erase before pixbuf is ready or else marks that are in the same pos get mixed up
- const auto mark_start = buf->create_mark(start_it, false);
- end_it.backward_char();
- const auto mark_end = buf->create_mark(end_it, false);
- const auto cb = [buf, mark_start, mark_end](const Glib::RefPtr<Gdk::Pixbuf> &pixbuf) {
- auto start_it = mark_start->get_iter();
- auto end_it = mark_end->get_iter();
- end_it.forward_char();
- buf->delete_mark(mark_start);
- buf->delete_mark(mark_end);
- auto it = buf->erase(start_it, end_it);
- int width, height;
- GetImageDimensions(pixbuf->get_width(), pixbuf->get_height(), width, height, EmojiSize, EmojiSize);
- buf->insert_pixbuf(it, pixbuf->scale_simple(width, height, Gdk::INTERP_BILINEAR));
- };
- img.LoadFromURL(EmojiData::URLFromID(match.fetch(2)), sigc::track_obj(cb, tv));
- }
-
- text = GetText(buf);
- }
-}
-
-void ChatMessageItemContainer::HandleEmojis(Gtk::TextView &tv) {
- if (Abaddon::Get().GetSettings().ShowStockEmojis) HandleStockEmojis(tv);
- if (Abaddon::Get().GetSettings().ShowCustomEmojis) HandleCustomEmojis(tv);
-}
-
-void ChatMessageItemContainer::CleanupEmojis(const Glib::RefPtr<Gtk::TextBuffer> &buf) {
- static auto rgx = Glib::Regex::create(R"(<a?:([\w\d_]+):(\d+)>)");
-
- auto text = GetText(buf);
-
- Glib::MatchInfo match;
- int startpos = 0;
- while (rgx->match(text, startpos, match)) {
- int mstart, mend;
- if (!match.fetch_pos(0, mstart, mend)) break;
-
- const auto new_term = ":" + match.fetch(1) + ":";
-
- const auto chars_start = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mstart);
- const auto chars_end = g_utf8_pointer_to_offset(text.c_str(), text.c_str() + mend);
- auto start_it = buf->get_iter_at_offset(chars_start);
- auto end_it = buf->get_iter_at_offset(chars_end);
-
- startpos = mend;
- const auto it = buf->erase(start_it, end_it);
- const int alen = static_cast<int>(text.size());
- text = GetText(buf);
- const int blen = static_cast<int>(text.size());
- startpos -= (alen - blen);
-
- buf->insert(it, new_term);
-
- text = GetText(buf);
- }
-}
-
void ChatMessageItemContainer::HandleChannelMentions(const Glib::RefPtr<Gtk::TextBuffer> &buf) {
static auto rgx = Glib::Regex::create(R"(<#(\d+)>)");
- Glib::ustring text = GetText(buf);
+ Glib::ustring text = ChatUtil::GetText(buf);
const auto &discord = Abaddon::Get().GetDiscordClient();
@@ -975,7 +774,7 @@ void ChatMessageItemContainer::HandleChannelMentions(const Glib::RefPtr<Gtk::Tex
it = buf->insert_with_tag(it, "#" + *chan->Name, tag);
// rescan the whole thing so i dont have to deal with fixing match positions
- text = GetText(buf);
+ text = ChatUtil::GetText(buf);
startpos = 0;
}
}
@@ -1036,7 +835,7 @@ void ChatMessageItemContainer::HandleLinks(Gtk::TextView &tv) {
const auto rgx = Glib::Regex::create(R"(\bhttps?:\/\/[^\s]+\.[^\s]+\b)");
auto buf = tv.get_buffer();
- Glib::ustring text = GetText(buf);
+ Glib::ustring text = ChatUtil::GetText(buf);
int startpos = 0;
Glib::MatchInfo match;