scroll up to load more messages

This commit is contained in:
ouwou
2020-08-28 18:21:08 -04:00
parent 8699a547f3
commit 5b7d36bb74
10 changed files with 121 additions and 8 deletions

View File

@@ -121,7 +121,7 @@ void Abaddon::ActionMoveGuildUp(Snowflake id) {
std::swap(*left, *target_iter); std::swap(*left, *target_iter);
std::vector<Snowflake> new_sort; std::vector<Snowflake> new_sort;
for (const auto& x : order) for (const auto &x : order)
new_sort.push_back(x.first); new_sort.push_back(x.first);
m_discord.UpdateSettingsGuildPositions(new_sort); m_discord.UpdateSettingsGuildPositions(new_sort);
@@ -156,14 +156,39 @@ void Abaddon::ActionListChannelItemClick(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<MessageData> &msgs) { m_discord.FetchMessagesInChannel(id, [this, id](const std::vector<MessageData> &msgs) {
m_channels_requested.insert(id); if (msgs.size() > 0) {
m_oldest_listed_message[id] = msgs.back().ID;
m_main_window->UpdateChatWindowContents(); m_main_window->UpdateChatWindowContents();
}
m_channels_requested.insert(id);
}); });
} else { } else {
m_main_window->UpdateChatWindowContents(); m_main_window->UpdateChatWindowContents();
} }
} }
void Abaddon::ActionChatLoadHistory(Snowflake id) {
if (m_channels_history_loaded.find(id) != m_channels_history_loaded.end())
return;
if (m_channels_history_loading.find(id) != m_channels_history_loading.end())
return;
m_channels_history_loading.insert(id);
m_discord.FetchMessagesInChannelBefore(id, m_oldest_listed_message[id], [this, id](const std::vector<MessageData> &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().ID;
m_main_window->UpdateChatPrependHistory(msgs);
}
});
}
void Abaddon::ActionChatInputSubmit(std::string msg, Snowflake channel) { void Abaddon::ActionChatInputSubmit(std::string msg, Snowflake channel) {
m_discord.SendChatMessage(msg, channel); m_discord.SendChatMessage(msg, channel);
} }

View File

@@ -26,6 +26,7 @@ public:
void ActionCopyGuildID(Snowflake id); void ActionCopyGuildID(Snowflake id);
void ActionListChannelItemClick(Snowflake id); void ActionListChannelItemClick(Snowflake id);
void ActionChatInputSubmit(std::string msg, Snowflake channel); void ActionChatInputSubmit(std::string msg, Snowflake channel);
void ActionChatLoadHistory(Snowflake id);
std::string GetDiscordToken() const; std::string GetDiscordToken() const;
bool IsDiscordActive() const; bool IsDiscordActive() const;
@@ -38,7 +39,11 @@ public:
private: 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_map<Snowflake, Snowflake> m_oldest_listed_message;
std::unordered_set<Snowflake> m_channels_history_loading;
mutable std::mutex m_mutex; mutable std::mutex m_mutex;
Glib::RefPtr<Gtk::Application> m_gtk_app; Glib::RefPtr<Gtk::Application> m_gtk_app;

View File

@@ -57,3 +57,8 @@ void ChatMessageTextItem::AppendNewContent(std::string content) {
auto buf = m_text->get_buffer(); auto buf = m_text->get_buffer();
buf->set_text(buf->get_text() + "\n" + content); buf->set_text(buf->get_text() + "\n" + content);
} }
void ChatMessageTextItem::PrependNewContent(std::string content) {
auto buf = m_text->get_buffer();
buf->set_text(content + "\n" + buf->get_text());
}

View File

@@ -17,6 +17,7 @@ class ChatMessageTextItem : public ChatMessageItem {
public: public:
ChatMessageTextItem(const MessageData *data); ChatMessageTextItem(const MessageData *data);
void AppendNewContent(std::string content); void AppendNewContent(std::string content);
void PrependNewContent(std::string content);
protected: protected:
Gtk::Box *m_main_box; Gtk::Box *m_main_box;

View File

@@ -5,6 +5,7 @@
ChatWindow::ChatWindow() { ChatWindow::ChatWindow() {
m_message_set_dispatch.connect(sigc::mem_fun(*this, &ChatWindow::SetMessagesInternal)); m_message_set_dispatch.connect(sigc::mem_fun(*this, &ChatWindow::SetMessagesInternal));
m_new_message_dispatch.connect(sigc::mem_fun(*this, &ChatWindow::AddNewMessageInternal)); m_new_message_dispatch.connect(sigc::mem_fun(*this, &ChatWindow::AddNewMessageInternal));
m_new_history_dispatch.connect(sigc::mem_fun(*this, &ChatWindow::AddNewHistoryInternal));
m_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL)); m_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
m_listbox = Gtk::manage(new Gtk::ListBox); m_listbox = Gtk::manage(new Gtk::ListBox);
@@ -19,11 +20,19 @@ ChatWindow::ChatWindow() {
m_main->set_vexpand(true); m_main->set_vexpand(true);
m_main->show(); m_main->show();
m_scroll->signal_edge_reached().connect(sigc::mem_fun(*this, &ChatWindow::on_scroll_edge_overshot));
auto vadj = m_scroll->get_vadjustment();
vadj->signal_value_changed().connect([&, vadj]() {
m_scroll_to_bottom = vadj->get_upper() - vadj->get_page_size() <= vadj->get_value();
});
m_scroll->set_can_focus(false); m_scroll->set_can_focus(false);
m_scroll->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS); m_scroll->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
m_scroll->show(); m_scroll->show();
m_listbox->signal_size_allocate().connect([this](Gtk::Allocation &) { m_listbox->signal_size_allocate().connect([this](Gtk::Allocation &) {
if (m_scroll_to_bottom)
ScrollToBottom(); ScrollToBottom();
}); });
@@ -96,10 +105,13 @@ ChatMessageItem *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
return item; return item;
} }
void ChatWindow::ProcessMessage(const MessageData *data) { void ChatWindow::ProcessMessage(const MessageData *data, bool prepend) {
auto create_new_row = [&]() { auto create_new_row = [&]() {
auto *item = CreateChatEntryComponent(data); auto *item = CreateChatEntryComponent(data);
if (item != nullptr) { if (item != nullptr) {
if (prepend)
m_listbox->prepend(*item);
else
m_listbox->add(*item); m_listbox->add(*item);
m_num_rows++; m_num_rows++;
} }
@@ -107,7 +119,11 @@ void ChatWindow::ProcessMessage(const MessageData *data) {
// if the last row's message's author is the same as the new one's, then append the new message content to the last row // if the last row's message's author is the same as the new one's, then append the new message content to the last row
if (m_num_rows > 0) { if (m_num_rows > 0) {
auto *item = dynamic_cast<ChatMessageItem *>(m_listbox->get_row_at_index(m_num_rows - 1)); ChatMessageItem *item;
if (prepend)
item = dynamic_cast<ChatMessageItem *>(m_listbox->get_row_at_index(0));
else
item = dynamic_cast<ChatMessageItem *>(m_listbox->get_row_at_index(m_num_rows - 1));
assert(item != nullptr); assert(item != nullptr);
auto *previous_data = m_abaddon->GetDiscordClient().GetMessage(item->ID); auto *previous_data = m_abaddon->GetDiscordClient().GetMessage(item->ID);
@@ -116,6 +132,9 @@ void ChatWindow::ProcessMessage(const MessageData *data) {
if ((data->Author.ID == previous_data->Author.ID) && (new_type == old_type && new_type == ChatDisplayType::Text)) { if ((data->Author.ID == previous_data->Author.ID) && (new_type == old_type && new_type == ChatDisplayType::Text)) {
auto *text_item = dynamic_cast<ChatMessageTextItem *>(item); auto *text_item = dynamic_cast<ChatMessageTextItem *>(item);
if (prepend)
text_item->PrependNewContent(data->Content);
else
text_item->AppendNewContent(data->Content); text_item->AppendNewContent(data->Content);
} else { } else {
create_new_row(); create_new_row();
@@ -143,6 +162,11 @@ bool ChatWindow::on_key_press_event(GdkEventKey *e) {
return false; return false;
} }
void ChatWindow::on_scroll_edge_overshot(Gtk::PositionType pos) {
if (pos == Gtk::POS_TOP)
m_abaddon->ActionChatLoadHistory(m_active_channel);
}
void ChatWindow::SetMessages(std::unordered_set<const MessageData *> msgs) { void ChatWindow::SetMessages(std::unordered_set<const MessageData *> msgs) {
std::scoped_lock<std::mutex> guard(m_update_mutex); std::scoped_lock<std::mutex> guard(m_update_mutex);
m_message_set_queue.push(msgs); m_message_set_queue.push(msgs);
@@ -155,6 +179,15 @@ void ChatWindow::AddNewMessage(Snowflake id) {
m_new_message_dispatch.emit(); m_new_message_dispatch.emit();
} }
void ChatWindow::AddNewHistory(const std::vector<MessageData> &msgs) {
std::scoped_lock<std::mutex> guard(m_update_mutex);
std::vector<Snowflake> x;
for (const auto &msg : msgs)
x.push_back(msg.ID);
m_new_history_queue.push(x);
m_new_history_dispatch.emit();
}
void ChatWindow::ClearMessages() { void ChatWindow::ClearMessages() {
std::scoped_lock<std::mutex> guard(m_update_mutex); std::scoped_lock<std::mutex> guard(m_update_mutex);
m_message_set_queue.push(std::unordered_set<const MessageData *>()); m_message_set_queue.push(std::unordered_set<const MessageData *>());
@@ -178,6 +211,25 @@ void ChatWindow::AddNewMessageInternal() {
ProcessMessage(data); ProcessMessage(data);
} }
// todo this keeps the scrollbar at the top
void ChatWindow::AddNewHistoryInternal() {
std::set<Snowflake> msgs;
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
auto vec = m_new_history_queue.front();
msgs = std::set<Snowflake>(vec.begin(), vec.end());
}
for (auto it = msgs.rbegin(); it != msgs.rend(); it++) {
ProcessMessage(m_abaddon->GetDiscordClient().GetMessage(*it), true);
}
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_new_history_queue.pop();
}
}
void ChatWindow::SetMessagesInternal() { void ChatWindow::SetMessagesInternal() {
auto children = m_listbox->get_children(); auto children = m_listbox->get_children();
auto it = children.begin(); auto it = children.begin();

View File

@@ -16,24 +16,31 @@ public:
Snowflake GetActiveChannel() const; Snowflake GetActiveChannel() const;
void SetMessages(std::unordered_set<const MessageData *> msgs); void SetMessages(std::unordered_set<const MessageData *> msgs);
void AddNewMessage(Snowflake id); void AddNewMessage(Snowflake id);
void AddNewHistory(const std::vector<MessageData> &msgs);
void ClearMessages(); void ClearMessages();
protected: protected:
void ScrollToBottom(); void ScrollToBottom();
void SetMessagesInternal(); void SetMessagesInternal();
void AddNewMessageInternal(); void AddNewMessageInternal();
void AddNewHistoryInternal();
ChatDisplayType GetMessageDisplayType(const MessageData *data); ChatDisplayType GetMessageDisplayType(const MessageData *data);
ChatMessageItem *CreateChatEntryComponentText(const MessageData *data); ChatMessageItem *CreateChatEntryComponentText(const MessageData *data);
ChatMessageItem *CreateChatEntryComponent(const MessageData *data); ChatMessageItem *CreateChatEntryComponent(const MessageData *data);
void ProcessMessage(const MessageData *data); void ProcessMessage(const MessageData *data, bool prepend = false);
int m_num_rows = 0; // youd think thered be a Gtk::ListBox::get_row_count or something but nope int m_num_rows = 0; // youd think thered be a Gtk::ListBox::get_row_count or something but nope
bool m_scroll_to_bottom = true;
bool on_key_press_event(GdkEventKey *e); bool on_key_press_event(GdkEventKey *e);
void on_scroll_edge_overshot(Gtk::PositionType pos);
Glib::Dispatcher m_message_set_dispatch; Glib::Dispatcher m_message_set_dispatch;
std::queue<std::unordered_set<const MessageData *>> m_message_set_queue; std::queue<std::unordered_set<const MessageData *>> m_message_set_queue;
Glib::Dispatcher m_new_message_dispatch; Glib::Dispatcher m_new_message_dispatch;
std::queue<Snowflake> m_new_message_queue; std::queue<Snowflake> m_new_message_queue;
Glib::Dispatcher m_new_history_dispatch;
std::queue<std::vector<Snowflake>> m_new_history_queue;
std::mutex m_update_mutex; std::mutex m_update_mutex;
Snowflake m_active_channel; Snowflake m_active_channel;

View File

@@ -129,6 +129,18 @@ void DiscordClient::FetchMessagesInChannel(Snowflake id, std::function<void(cons
}); });
} }
void DiscordClient::FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, std::function<void(const std::vector<MessageData> &)> cb) {
std::string path = "/channels/" + std::to_string(channel_id) + "/messages?limit=50&before=" + std::to_string(before_id);
m_http.MakeGET(path, [this, channel_id, cb](cpr::Response r) {
std::vector<MessageData> msgs;
nlohmann::json::parse(r.text).get_to(msgs);
for (const auto &msg : msgs)
StoreMessage(msg.ID, msg);
cb(msgs);
});
}
const MessageData *DiscordClient::GetMessage(Snowflake id) const { const MessageData *DiscordClient::GetMessage(Snowflake id) const {
return &m_messages.at(id); return &m_messages.at(id);
} }

View File

@@ -476,6 +476,7 @@ public:
void UpdateSettingsGuildPositions(const std::vector<Snowflake> &pos); void UpdateSettingsGuildPositions(const std::vector<Snowflake> &pos);
void FetchMessagesInChannel(Snowflake id, std::function<void(const std::vector<MessageData> &)> cb); void FetchMessagesInChannel(Snowflake id, std::function<void(const std::vector<MessageData> &)> cb);
void FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, std::function<void(const std::vector<MessageData> &)> cb);
const MessageData *GetMessage(Snowflake id) const; const MessageData *GetMessage(Snowflake id) const;
void SendChatMessage(std::string content, Snowflake channel); void SendChatMessage(std::string content, Snowflake channel);

View File

@@ -110,6 +110,10 @@ void MainWindow::UpdateChatNewMessage(Snowflake id) {
m_chat.AddNewMessage(id); m_chat.AddNewMessage(id);
} }
void MainWindow::UpdateChatPrependHistory(const std::vector<MessageData> &msgs) {
m_chat.AddNewHistory(msgs);
}
void MainWindow::SetAbaddon(Abaddon *ptr) { void MainWindow::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr; m_abaddon = ptr;
m_channel_list.SetAbaddon(ptr); m_channel_list.SetAbaddon(ptr);

View File

@@ -16,6 +16,7 @@ public:
void UpdateChatActiveChannel(Snowflake id); void UpdateChatActiveChannel(Snowflake id);
Snowflake GetChatActiveChannel() const; Snowflake GetChatActiveChannel() const;
void UpdateChatNewMessage(Snowflake id); void UpdateChatNewMessage(Snowflake id);
void UpdateChatPrependHistory(const std::vector<MessageData> &msgs);
protected: protected:
Gtk::Box m_main_box; Gtk::Box m_main_box;