mirror of
https://github.com/uowuo/abaddon.git
synced 2025-03-04 03:03:16 -05:00
remove old messages when new ones come in to save resources
This commit is contained in:
parent
f4366fc0af
commit
599a768459
@ -410,7 +410,6 @@ void Abaddon::ActionDisconnect() {
|
|||||||
m_channels_history_loaded.clear();
|
m_channels_history_loaded.clear();
|
||||||
m_channels_history_loading.clear();
|
m_channels_history_loading.clear();
|
||||||
m_channels_requested.clear();
|
m_channels_requested.clear();
|
||||||
m_oldest_listed_message.clear();
|
|
||||||
m_main_window->set_title(APP_TITLE);
|
m_main_window->set_title(APP_TITLE);
|
||||||
m_main_window->UpdateComponents();
|
m_main_window->UpdateComponents();
|
||||||
}
|
}
|
||||||
@ -455,9 +454,6 @@ void Abaddon::ActionChannelOpened(Snowflake id) {
|
|||||||
m_main_window->UpdateChatActiveChannel(id);
|
m_main_window->UpdateChatActiveChannel(id);
|
||||||
if (m_channels_requested.find(id) == m_channels_requested.end()) {
|
if (m_channels_requested.find(id) == m_channels_requested.end()) {
|
||||||
m_discord.FetchMessagesInChannel(id, [this, id](const std::vector<Snowflake> &msgs) {
|
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_main_window->UpdateChatWindowContents();
|
||||||
m_channels_requested.insert(id);
|
m_channels_requested.insert(id);
|
||||||
});
|
});
|
||||||
@ -495,13 +491,12 @@ void Abaddon::ActionChatLoadHistory(Snowflake id) {
|
|||||||
|
|
||||||
m_channels_history_loading.insert(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);
|
m_channels_history_loading.erase(id);
|
||||||
|
|
||||||
if (msgs.size() == 0) {
|
if (msgs.size() == 0) {
|
||||||
m_channels_history_loaded.insert(id);
|
m_channels_history_loaded.insert(id);
|
||||||
} else {
|
} else {
|
||||||
m_oldest_listed_message[id] = msgs.back();
|
|
||||||
m_main_window->UpdateChatPrependHistory(msgs);
|
m_main_window->UpdateChatPrependHistory(msgs);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -116,10 +116,9 @@ private:
|
|||||||
|
|
||||||
DiscordClient m_discord;
|
DiscordClient m_discord;
|
||||||
std::string m_discord_token;
|
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_requested;
|
||||||
std::unordered_set<Snowflake> m_channels_history_loaded;
|
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;
|
std::unordered_set<Snowflake> m_channels_history_loading;
|
||||||
|
|
||||||
ImageManager m_img_mgr;
|
ImageManager m_img_mgr;
|
||||||
|
@ -1148,6 +1148,10 @@ void ChatMessageHeader::UpdateNameColor() {
|
|||||||
m_author->set_markup(md);
|
m_author->set_markup(md);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<Gtk::Widget *> ChatMessageHeader::GetChildContent() {
|
||||||
|
return m_content_widgets;
|
||||||
|
}
|
||||||
|
|
||||||
void ChatMessageHeader::AttachUserMenuHandler(Gtk::Widget &widget) {
|
void ChatMessageHeader::AttachUserMenuHandler(Gtk::Widget &widget) {
|
||||||
widget.signal_button_press_event().connect([this](GdkEventButton *ev) -> bool {
|
widget.signal_button_press_event().connect([this](GdkEventButton *ev) -> bool {
|
||||||
if (ev->type == GDK_BUTTON_PRESS && ev->button == GDK_BUTTON_SECONDARY) {
|
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) {
|
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);
|
m_content_box->add(*widget);
|
||||||
if (prepend)
|
if (prepend)
|
||||||
m_content_box->reorder_child(*widget, 1);
|
m_content_box->reorder_child(*widget, 1);
|
||||||
|
@ -110,12 +110,15 @@ public:
|
|||||||
ChatMessageHeader(const Message *data);
|
ChatMessageHeader(const Message *data);
|
||||||
void AddContent(Gtk::Widget *widget, bool prepend);
|
void AddContent(Gtk::Widget *widget, bool prepend);
|
||||||
void UpdateNameColor();
|
void UpdateNameColor();
|
||||||
|
std::vector<Gtk::Widget*> GetChildContent();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void AttachUserMenuHandler(Gtk::Widget &widget);
|
void AttachUserMenuHandler(Gtk::Widget &widget);
|
||||||
|
|
||||||
bool on_author_button_press(GdkEventButton *ev);
|
bool on_author_button_press(GdkEventButton *ev);
|
||||||
|
|
||||||
|
std::vector<Gtk::Widget*> m_content_widgets;
|
||||||
|
|
||||||
Gtk::Box *m_main_box;
|
Gtk::Box *m_main_box;
|
||||||
Gtk::Box *m_content_box;
|
Gtk::Box *m_content_box;
|
||||||
Gtk::EventBox *m_content_box_ev;
|
Gtk::EventBox *m_content_box_ev;
|
||||||
|
@ -110,6 +110,7 @@ void ChatWindow::SetMessages(const std::set<Snowflake> &msgs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_num_rows = 0;
|
m_num_rows = 0;
|
||||||
|
m_num_messages = 0;
|
||||||
m_id_to_widget.clear();
|
m_id_to_widget.clear();
|
||||||
|
|
||||||
for (const auto &id : msgs) {
|
for (const auto &id : msgs) {
|
||||||
@ -159,14 +160,7 @@ void ChatWindow::InsertChatInput(std::string text) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Snowflake ChatWindow::GetOldestListedMessage() {
|
Snowflake ChatWindow::GetOldestListedMessage() {
|
||||||
Snowflake m;
|
return m_id_to_widget.begin()->first;
|
||||||
|
|
||||||
for (const auto &[id, widget] : m_id_to_widget) {
|
|
||||||
if (id < m)
|
|
||||||
m = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChatWindow::UpdateReactions(Snowflake id) {
|
void ChatWindow::UpdateReactions(Snowflake id) {
|
||||||
@ -203,6 +197,7 @@ ChatMessageItemContainer *ChatWindow::CreateMessageComponent(Snowflake id) {
|
|||||||
return container;
|
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) {
|
void ChatWindow::ProcessNewMessage(Snowflake id, bool prepend) {
|
||||||
const auto &client = Abaddon::Get().GetDiscordClient();
|
const auto &client = Abaddon::Get().GetDiscordClient();
|
||||||
if (!client.IsStarted()) return; // e.g. load channel and then dc
|
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;
|
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;
|
ChatMessageHeader *header;
|
||||||
if (should_attach) {
|
if (should_attach) {
|
||||||
header = last_row;
|
header = last_row;
|
||||||
|
@ -36,8 +36,9 @@ protected:
|
|||||||
void StartReplying(Snowflake message_id);
|
void StartReplying(Snowflake message_id);
|
||||||
void StopReplying();
|
void StopReplying();
|
||||||
|
|
||||||
|
int m_num_messages = 0;
|
||||||
int m_num_rows = 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;
|
Snowflake m_active_channel;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user