summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2020-08-28 18:21:08 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2020-08-28 18:21:08 -0400
commit5b7d36bb7438f32b56d6ebdf043ee291332060f1 (patch)
treef243bab9cd2d845e9958353c2f89d9bc0aa42eec
parent8699a547f3d51504cd0aa695f580315b8fe27678 (diff)
downloadabaddon-portaudio-5b7d36bb7438f32b56d6ebdf043ee291332060f1.tar.gz
abaddon-portaudio-5b7d36bb7438f32b56d6ebdf043ee291332060f1.zip
scroll up to load more messages
-rw-r--r--abaddon.cpp29
-rw-r--r--abaddon.hpp5
-rw-r--r--components/chatmessage.cpp5
-rw-r--r--components/chatmessage.hpp1
-rw-r--r--components/chatwindow.cpp62
-rw-r--r--components/chatwindow.hpp9
-rw-r--r--discord/discord.cpp12
-rw-r--r--discord/discord.hpp1
-rw-r--r--windows/mainwindow.cpp4
-rw-r--r--windows/mainwindow.hpp1
10 files changed, 121 insertions, 8 deletions
diff --git a/abaddon.cpp b/abaddon.cpp
index 04d80ac..48ba1fd 100644
--- a/abaddon.cpp
+++ b/abaddon.cpp
@@ -121,7 +121,7 @@ void Abaddon::ActionMoveGuildUp(Snowflake id) {
std::swap(*left, *target_iter);
std::vector<Snowflake> new_sort;
- for (const auto& x : order)
+ for (const auto &x : order)
new_sort.push_back(x.first);
m_discord.UpdateSettingsGuildPositions(new_sort);
@@ -156,14 +156,39 @@ void Abaddon::ActionListChannelItemClick(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<MessageData> &msgs) {
+ if (msgs.size() > 0) {
+ m_oldest_listed_message[id] = msgs.back().ID;
+ m_main_window->UpdateChatWindowContents();
+ }
+
m_channels_requested.insert(id);
- m_main_window->UpdateChatWindowContents();
});
} else {
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) {
m_discord.SendChatMessage(msg, channel);
}
diff --git a/abaddon.hpp b/abaddon.hpp
index 87aebcf..5886944 100644
--- a/abaddon.hpp
+++ b/abaddon.hpp
@@ -26,6 +26,7 @@ public:
void ActionCopyGuildID(Snowflake id);
void ActionListChannelItemClick(Snowflake id);
void ActionChatInputSubmit(std::string msg, Snowflake channel);
+ void ActionChatLoadHistory(Snowflake id);
std::string GetDiscordToken() const;
bool IsDiscordActive() const;
@@ -38,7 +39,11 @@ public:
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;
mutable std::mutex m_mutex;
Glib::RefPtr<Gtk::Application> m_gtk_app;
diff --git a/components/chatmessage.cpp b/components/chatmessage.cpp
index b03c6d6..893a464 100644
--- a/components/chatmessage.cpp
+++ b/components/chatmessage.cpp
@@ -57,3 +57,8 @@ void ChatMessageTextItem::AppendNewContent(std::string content) {
auto buf = m_text->get_buffer();
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());
+}
diff --git a/components/chatmessage.hpp b/components/chatmessage.hpp
index e9d58a4..227e272 100644
--- a/components/chatmessage.hpp
+++ b/components/chatmessage.hpp
@@ -17,6 +17,7 @@ class ChatMessageTextItem : public ChatMessageItem {
public:
ChatMessageTextItem(const MessageData *data);
void AppendNewContent(std::string content);
+ void PrependNewContent(std::string content);
protected:
Gtk::Box *m_main_box;
diff --git a/components/chatwindow.cpp b/components/chatwindow.cpp
index 36df514..8e2b85c 100644
--- a/components/chatwindow.cpp
+++ b/components/chatwindow.cpp
@@ -5,6 +5,7 @@
ChatWindow::ChatWindow() {
m_message_set_dispatch.connect(sigc::mem_fun(*this, &ChatWindow::SetMessagesInternal));
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_listbox = Gtk::manage(new Gtk::ListBox);
@@ -19,12 +20,20 @@ ChatWindow::ChatWindow() {
m_main->set_vexpand(true);
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_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
m_scroll->show();
m_listbox->signal_size_allocate().connect([this](Gtk::Allocation &) {
- ScrollToBottom();
+ if (m_scroll_to_bottom)
+ ScrollToBottom();
});
m_listbox->set_selection_mode(Gtk::SELECTION_NONE);
@@ -96,18 +105,25 @@ ChatMessageItem *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
return item;
}
-void ChatWindow::ProcessMessage(const MessageData *data) {
+void ChatWindow::ProcessMessage(const MessageData *data, bool prepend) {
auto create_new_row = [&]() {
auto *item = CreateChatEntryComponent(data);
if (item != nullptr) {
- m_listbox->add(*item);
+ if (prepend)
+ m_listbox->prepend(*item);
+ else
+ m_listbox->add(*item);
m_num_rows++;
}
};
// 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) {
- 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);
auto *previous_data = m_abaddon->GetDiscordClient().GetMessage(item->ID);
@@ -116,7 +132,10 @@ void ChatWindow::ProcessMessage(const MessageData *data) {
if ((data->Author.ID == previous_data->Author.ID) && (new_type == old_type && new_type == ChatDisplayType::Text)) {
auto *text_item = dynamic_cast<ChatMessageTextItem *>(item);
- text_item->AppendNewContent(data->Content);
+ if (prepend)
+ text_item->PrependNewContent(data->Content);
+ else
+ text_item->AppendNewContent(data->Content);
} else {
create_new_row();
}
@@ -143,6 +162,11 @@ bool ChatWindow::on_key_press_event(GdkEventKey *e) {
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) {
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_message_set_queue.push(msgs);
@@ -155,6 +179,15 @@ void ChatWindow::AddNewMessage(Snowflake id) {
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() {
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_message_set_queue.push(std::unordered_set<const MessageData *>());
@@ -178,6 +211,25 @@ void ChatWindow::AddNewMessageInternal() {
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() {
auto children = m_listbox->get_children();
auto it = children.begin();
diff --git a/components/chatwindow.hpp b/components/chatwindow.hpp
index a758de9..83f5676 100644
--- a/components/chatwindow.hpp
+++ b/components/chatwindow.hpp
@@ -16,24 +16,31 @@ public:
Snowflake GetActiveChannel() const;
void SetMessages(std::unordered_set<const MessageData *> msgs);
void AddNewMessage(Snowflake id);
+ void AddNewHistory(const std::vector<MessageData> &msgs);
void ClearMessages();
protected:
void ScrollToBottom();
void SetMessagesInternal();
void AddNewMessageInternal();
+ void AddNewHistoryInternal();
ChatDisplayType GetMessageDisplayType(const MessageData *data);
ChatMessageItem *CreateChatEntryComponentText(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
+ bool m_scroll_to_bottom = true;
+
bool on_key_press_event(GdkEventKey *e);
+ void on_scroll_edge_overshot(Gtk::PositionType pos);
Glib::Dispatcher m_message_set_dispatch;
std::queue<std::unordered_set<const MessageData *>> m_message_set_queue;
Glib::Dispatcher m_new_message_dispatch;
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;
Snowflake m_active_channel;
diff --git a/discord/discord.cpp b/discord/discord.cpp
index 66e449c..7a75b95 100644
--- a/discord/discord.cpp
+++ b/discord/discord.cpp
@@ -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 {
return &m_messages.at(id);
}
diff --git a/discord/discord.hpp b/discord/discord.hpp
index 81f8b42..6c7127d 100644
--- a/discord/discord.hpp
+++ b/discord/discord.hpp
@@ -476,6 +476,7 @@ public:
void UpdateSettingsGuildPositions(const std::vector<Snowflake> &pos);
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;
void SendChatMessage(std::string content, Snowflake channel);
diff --git a/windows/mainwindow.cpp b/windows/mainwindow.cpp
index 2594abc..743b2a2 100644
--- a/windows/mainwindow.cpp
+++ b/windows/mainwindow.cpp
@@ -110,6 +110,10 @@ void MainWindow::UpdateChatNewMessage(Snowflake id) {
m_chat.AddNewMessage(id);
}
+void MainWindow::UpdateChatPrependHistory(const std::vector<MessageData> &msgs) {
+ m_chat.AddNewHistory(msgs);
+}
+
void MainWindow::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
m_channel_list.SetAbaddon(ptr);
diff --git a/windows/mainwindow.hpp b/windows/mainwindow.hpp
index f3085f5..256e930 100644
--- a/windows/mainwindow.hpp
+++ b/windows/mainwindow.hpp
@@ -16,6 +16,7 @@ public:
void UpdateChatActiveChannel(Snowflake id);
Snowflake GetChatActiveChannel() const;
void UpdateChatNewMessage(Snowflake id);
+ void UpdateChatPrependHistory(const std::vector<MessageData> &msgs);
protected:
Gtk::Box m_main_box;