diff options
author | ouwou <26526779+ouwou@users.noreply.github.com> | 2020-09-04 00:48:38 -0400 |
---|---|---|
committer | ouwou <26526779+ouwou@users.noreply.github.com> | 2020-09-04 00:48:38 -0400 |
commit | f69b0e61558680d9fdcaafd5c184b2c45fc06fce (patch) | |
tree | 87499f456700f365eb04cb19772f75f08eed614b | |
parent | 6c04a0a86d832217dc6d08652430cd84b09ff1db (diff) | |
download | abaddon-portaudio-f69b0e61558680d9fdcaafd5c184b2c45fc06fce.tar.gz abaddon-portaudio-f69b0e61558680d9fdcaafd5c184b2c45fc06fce.zip |
pass around snowflake instead of MessageData
-rw-r--r-- | abaddon.cpp | 8 | ||||
-rw-r--r-- | components/chatwindow.cpp | 17 | ||||
-rw-r--r-- | components/chatwindow.hpp | 6 | ||||
-rw-r--r-- | discord/discord.cpp | 31 | ||||
-rw-r--r-- | discord/discord.hpp | 6 | ||||
-rw-r--r-- | windows/mainwindow.cpp | 2 | ||||
-rw-r--r-- | windows/mainwindow.hpp | 2 |
7 files changed, 41 insertions, 31 deletions
diff --git a/abaddon.cpp b/abaddon.cpp index ef0580d..56a211f 100644 --- a/abaddon.cpp +++ b/abaddon.cpp @@ -172,9 +172,9 @@ void Abaddon::ActionListChannelItemClick(Snowflake id) { m_main_window->set_title(std::string(APP_TITLE) + " - #" + channel->Name); 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) { + m_discord.FetchMessagesInChannel(id, [this, id](const std::vector<Snowflake> &msgs) { if (msgs.size() > 0) { - m_oldest_listed_message[id] = msgs.back().ID; + m_oldest_listed_message[id] = msgs.back(); m_main_window->UpdateChatWindowContents(); } @@ -194,13 +194,13 @@ 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<MessageData> &msgs) { + m_discord.FetchMessagesInChannelBefore(id, m_oldest_listed_message[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().ID; + m_oldest_listed_message[id] = msgs.back(); m_main_window->UpdateChatPrependHistory(msgs); } }); diff --git a/components/chatwindow.cpp b/components/chatwindow.cpp index 2005554..2c0c4a0 100644 --- a/components/chatwindow.cpp +++ b/components/chatwindow.cpp @@ -166,7 +166,7 @@ void ChatWindow::on_scroll_edge_overshot(Gtk::PositionType pos) { m_abaddon->ActionChatLoadHistory(m_active_channel); } -void ChatWindow::SetMessages(std::unordered_set<const MessageData *> msgs) { +void ChatWindow::SetMessages(std::set<Snowflake> msgs) { std::scoped_lock<std::mutex> guard(m_update_mutex); m_message_set_queue.push(msgs); m_message_set_dispatch.emit(); @@ -178,12 +178,9 @@ void ChatWindow::AddNewMessage(Snowflake id) { m_new_message_dispatch.emit(); } -void ChatWindow::AddNewHistory(const std::vector<MessageData> &msgs) { +void ChatWindow::AddNewHistory(const std::vector<Snowflake> &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_queue.push(msgs); m_new_history_dispatch.emit(); } @@ -201,7 +198,7 @@ void ChatWindow::UpdateMessageContent(Snowflake id) { void ChatWindow::ClearMessages() { 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::set<Snowflake>()); m_message_set_dispatch.emit(); } @@ -289,7 +286,7 @@ void ChatWindow::SetMessagesInternal() { m_num_rows = 0; m_id_to_widget.clear(); - std::unordered_set<const MessageData *> *msgs; + std::set<Snowflake> *msgs; { std::scoped_lock<std::mutex> guard(m_update_mutex); msgs = &m_message_set_queue.front(); @@ -297,8 +294,8 @@ void ChatWindow::SetMessagesInternal() { // sort std::map<Snowflake, const MessageData *> sorted_messages; - for (const auto msg : *msgs) - sorted_messages[msg->ID] = msg; + for (const auto id : *msgs) + sorted_messages[id] = m_abaddon->GetDiscordClient().GetMessage(id); for (const auto &[id, msg] : sorted_messages) { ProcessMessage(msg); diff --git a/components/chatwindow.hpp b/components/chatwindow.hpp index f5342e9..777ef22 100644 --- a/components/chatwindow.hpp +++ b/components/chatwindow.hpp @@ -15,9 +15,9 @@ public: Gtk::Widget *GetRoot() const; void SetActiveChannel(Snowflake id); Snowflake GetActiveChannel() const; - void SetMessages(std::unordered_set<const MessageData *> msgs); + void SetMessages(std::set<Snowflake> msgs); void AddNewMessage(Snowflake id); - void AddNewHistory(const std::vector<MessageData> &msgs); + void AddNewHistory(const std::vector<Snowflake> &msgs); void DeleteMessage(Snowflake id); void UpdateMessageContent(Snowflake id); void ClearMessages(); @@ -40,7 +40,7 @@ protected: 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; + std::queue<std::set<Snowflake>> m_message_set_queue; Glib::Dispatcher m_new_message_dispatch; std::queue<Snowflake> m_new_message_queue; Glib::Dispatcher m_new_history_dispatch; diff --git a/discord/discord.cpp b/discord/discord.cpp index f04c136..ed919df 100644 --- a/discord/discord.cpp +++ b/discord/discord.cpp @@ -110,11 +110,16 @@ std::vector<std::pair<Snowflake, GuildData>> DiscordClient::GetUserSortedGuilds( return sorted_guilds; } -std::unordered_set<const MessageData *> DiscordClient::GetMessagesForChannel(Snowflake id) const { +std::set<Snowflake> DiscordClient::GetMessagesForChannel(Snowflake id) const { auto it = m_chan_to_message_map.find(id); if (it == m_chan_to_message_map.end()) - return std::unordered_set<const MessageData *>(); - return it->second; + return std::set<Snowflake>(); + + std::set<Snowflake> ret; + for (const auto &msg : it->second) + ret.insert(msg->ID); + + return ret; } void DiscordClient::UpdateSettingsGuildPositions(const std::vector<Snowflake> &pos) { @@ -127,27 +132,35 @@ void DiscordClient::UpdateSettingsGuildPositions(const std::vector<Snowflake> &p }); } -void DiscordClient::FetchMessagesInChannel(Snowflake id, std::function<void(const std::vector<MessageData> &)> cb) { +void DiscordClient::FetchMessagesInChannel(Snowflake id, std::function<void(const std::vector<Snowflake> &)> cb) { std::string path = "/channels/" + std::to_string(id) + "/messages?limit=50"; m_http.MakeGET(path, [this, id, cb](cpr::Response r) { std::vector<MessageData> msgs; + std::vector<Snowflake> ids; + nlohmann::json::parse(r.text).get_to(msgs); - for (const auto &msg : msgs) + for (const auto &msg : msgs) { StoreMessage(msg.ID, msg); + ids.push_back(msg.ID); + } - cb(msgs); + cb(ids); }); } -void DiscordClient::FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, std::function<void(const std::vector<MessageData> &)> cb) { +void DiscordClient::FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, std::function<void(const std::vector<Snowflake> &)> 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; + std::vector<Snowflake> ids; + nlohmann::json::parse(r.text).get_to(msgs); - for (const auto &msg : msgs) + for (const auto &msg : msgs) { StoreMessage(msg.ID, msg); + ids.push_back(msg.ID); + } - cb(msgs); + cb(ids); }); } diff --git a/discord/discord.hpp b/discord/discord.hpp index 707120e..1264d1c 100644 --- a/discord/discord.hpp +++ b/discord/discord.hpp @@ -59,12 +59,12 @@ public: const UserData &GetUserData() const; const UserSettingsData &GetUserSettings() const; std::vector<std::pair<Snowflake, GuildData>> GetUserSortedGuilds() const; - std::unordered_set<const MessageData *> GetMessagesForChannel(Snowflake id) const; + std::set<Snowflake> GetMessagesForChannel(Snowflake id) const; std::set<Snowflake> GetPrivateChannels() const; 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); + void FetchMessagesInChannel(Snowflake id, std::function<void(const std::vector<Snowflake> &)> cb); + void FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake before_id, std::function<void(const std::vector<Snowflake> &)> cb); const MessageData *GetMessage(Snowflake id) const; const ChannelData *GetChannel(Snowflake id) const; diff --git a/windows/mainwindow.cpp b/windows/mainwindow.cpp index 67e33fb..a714c1a 100644 --- a/windows/mainwindow.cpp +++ b/windows/mainwindow.cpp @@ -131,7 +131,7 @@ void MainWindow::UpdateChatMessageEditContent(Snowflake id, Snowflake channel_id m_chat.UpdateMessageContent(id); } -void MainWindow::UpdateChatPrependHistory(const std::vector<MessageData> &msgs) { +void MainWindow::UpdateChatPrependHistory(const std::vector<Snowflake> &msgs) { m_chat.AddNewHistory(msgs); } diff --git a/windows/mainwindow.hpp b/windows/mainwindow.hpp index b4db516..9cd329f 100644 --- a/windows/mainwindow.hpp +++ b/windows/mainwindow.hpp @@ -18,7 +18,7 @@ public: void UpdateChatNewMessage(Snowflake id); void UpdateChatMessageDeleted(Snowflake id, Snowflake channel_id); void UpdateChatMessageEditContent(Snowflake id, Snowflake channel_id); - void UpdateChatPrependHistory(const std::vector<MessageData> &msgs); + void UpdateChatPrependHistory(const std::vector<Snowflake> &msgs); protected: Gtk::Box m_main_box; |