remove old messages when new ones come in to save resources

This commit is contained in:
ouwou 2021-03-30 22:31:13 -04:00
parent f4366fc0af
commit 599a768459
6 changed files with 36 additions and 17 deletions

View File

@ -410,7 +410,6 @@ void Abaddon::ActionDisconnect() {
m_channels_history_loaded.clear();
m_channels_history_loading.clear();
m_channels_requested.clear();
m_oldest_listed_message.clear();
m_main_window->set_title(APP_TITLE);
m_main_window->UpdateComponents();
}
@ -455,9 +454,6 @@ void Abaddon::ActionChannelOpened(Snowflake id) {
m_main_window->UpdateChatActiveChannel(id);
if (m_channels_requested.find(id) == m_channels_requested.end()) {
m_discord.FetchMessagesInChannel(id, [this, id](const std::vector<Snowflake> &msgs) {
if (msgs.size() > 0)
m_oldest_listed_message[id] = msgs.back();
m_main_window->UpdateChatWindowContents();
m_channels_requested.insert(id);
});
@ -495,13 +491,12 @@ void Abaddon::ActionChatLoadHistory(Snowflake id) {
m_channels_history_loading.insert(id);
m_discord.FetchMessagesInChannelBefore(id, m_oldest_listed_message[id], [this, id](const std::vector<Snowflake> &msgs) {
m_discord.FetchMessagesInChannelBefore(id, before_id, [this, id](const std::vector<Snowflake> &msgs) {
m_channels_history_loading.erase(id);
if (msgs.size() == 0) {
m_channels_history_loaded.insert(id);
} else {
m_oldest_listed_message[id] = msgs.back();
m_main_window->UpdateChatPrependHistory(msgs);
}
});

View File

@ -116,10 +116,9 @@ private:
DiscordClient m_discord;
std::string m_discord_token;
// todo make these map snowflake to attribs
std::unordered_set<Snowflake> m_channels_requested;
std::unordered_set<Snowflake> m_channels_history_loaded;
std::unordered_map<Snowflake, Snowflake> m_oldest_listed_message;
std::unordered_set<Snowflake> m_channels_history_loading;
ImageManager m_img_mgr;

View File

@ -1148,6 +1148,10 @@ void ChatMessageHeader::UpdateNameColor() {
m_author->set_markup(md);
}
std::vector<Gtk::Widget *> ChatMessageHeader::GetChildContent() {
return m_content_widgets;
}
void ChatMessageHeader::AttachUserMenuHandler(Gtk::Widget &widget) {
widget.signal_button_press_event().connect([this](GdkEventButton *ev) -> bool {
if (ev->type == GDK_BUTTON_PRESS && ev->button == GDK_BUTTON_SECONDARY) {
@ -1177,6 +1181,10 @@ ChatMessageHeader::type_signal_action_open_user_menu ChatMessageHeader::signal_a
}
void ChatMessageHeader::AddContent(Gtk::Widget *widget, bool prepend) {
m_content_widgets.push_back(widget);
widget->signal_unmap().connect([this, widget]() {
m_content_widgets.erase(std::remove(m_content_widgets.begin(), m_content_widgets.end(), widget), m_content_widgets.end());
});
m_content_box->add(*widget);
if (prepend)
m_content_box->reorder_child(*widget, 1);

View File

@ -110,12 +110,15 @@ public:
ChatMessageHeader(const Message *data);
void AddContent(Gtk::Widget *widget, bool prepend);
void UpdateNameColor();
std::vector<Gtk::Widget*> GetChildContent();
protected:
void AttachUserMenuHandler(Gtk::Widget &widget);
bool on_author_button_press(GdkEventButton *ev);
std::vector<Gtk::Widget*> m_content_widgets;
Gtk::Box *m_main_box;
Gtk::Box *m_content_box;
Gtk::EventBox *m_content_box_ev;

View File

@ -110,6 +110,7 @@ void ChatWindow::SetMessages(const std::set<Snowflake> &msgs) {
}
m_num_rows = 0;
m_num_messages = 0;
m_id_to_widget.clear();
for (const auto &id : msgs) {
@ -159,14 +160,7 @@ void ChatWindow::InsertChatInput(std::string text) {
}
Snowflake ChatWindow::GetOldestListedMessage() {
Snowflake m;
for (const auto &[id, widget] : m_id_to_widget) {
if (id < m)
m = id;
}
return m;
return m_id_to_widget.begin()->first;
}
void ChatWindow::UpdateReactions(Snowflake id) {
@ -203,6 +197,7 @@ ChatMessageItemContainer *ChatWindow::CreateMessageComponent(Snowflake id) {
return container;
}
constexpr static int MaxMessagesForCull = 50; // this has to be 50 cuz that magic number is used in a couple other places and i dont feel like replacing them
void ChatWindow::ProcessNewMessage(Snowflake id, bool prepend) {
const auto &client = Abaddon::Get().GetDiscordClient();
if (!client.IsStarted()) return; // e.g. load channel and then dc
@ -222,6 +217,24 @@ void ChatWindow::ProcessNewMessage(Snowflake id, bool prepend) {
should_attach = true;
}
m_num_messages++;
if (m_should_scroll_to_bottom && !prepend)
while (m_num_messages > MaxMessagesForCull) {
auto first_it = m_id_to_widget.begin();
ChatMessageHeader *header = dynamic_cast<ChatMessageHeader *>(first_it->second->get_ancestor(Gtk::ListBoxRow::get_type()));
if (header != nullptr) {
if (header->GetChildContent().size() == 1)
delete header;
else
delete first_it->second;
} else
delete first_it->second;
m_id_to_widget.erase(first_it);
m_num_rows--;
m_num_messages--;
}
ChatMessageHeader *header;
if (should_attach) {
header = last_row;

View File

@ -36,8 +36,9 @@ protected:
void StartReplying(Snowflake message_id);
void StopReplying();
int m_num_messages = 0;
int m_num_rows = 0;
std::unordered_map<Snowflake, Gtk::Widget *> m_id_to_widget;
std::map<Snowflake, Gtk::Widget *> m_id_to_widget;
Snowflake m_active_channel;