pass around snowflake instead of MessageData

This commit is contained in:
ouwou
2020-09-04 00:48:38 -04:00
parent 6c04a0a86d
commit f69b0e6155
7 changed files with 45 additions and 35 deletions

View File

@@ -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);
}
});

View File

@@ -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);

View File

@@ -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;

View File

@@ -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;
nlohmann::json::parse(r.text).get_to(msgs);
for (const auto &msg : msgs)
StoreMessage(msg.ID, msg);
std::vector<Snowflake> ids;
cb(msgs);
nlohmann::json::parse(r.text).get_to(msgs);
for (const auto &msg : msgs) {
StoreMessage(msg.ID, msg);
ids.push_back(msg.ID);
}
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;
nlohmann::json::parse(r.text).get_to(msgs);
for (const auto &msg : msgs)
StoreMessage(msg.ID, msg);
std::vector<Snowflake> ids;
cb(msgs);
nlohmann::json::parse(r.text).get_to(msgs);
for (const auto &msg : msgs) {
StoreMessage(msg.ID, msg);
ids.push_back(msg.ID);
}
cb(ids);
});
}

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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;