shitty MESSAGE_CREATE handling

This commit is contained in:
ouwou 2020-08-21 00:42:46 -04:00
parent a201d5905a
commit 6f11aa4dae
9 changed files with 110 additions and 10 deletions

View File

@ -79,6 +79,10 @@ void Abaddon::DiscordNotifyChannelListFullRefresh() {
m_main_window->UpdateChannelListing();
}
void Abaddon::DiscordNotifyMessageCreate(Snowflake id) {
m_main_window->UpdateChatNewMessage(id);
}
void Abaddon::ActionConnect() {
if (!m_discord.IsStarted())
StartDiscord();

View File

@ -31,6 +31,7 @@ public:
const DiscordClient &GetDiscordClient() const;
void DiscordNotifyReady();
void DiscordNotifyChannelListFullRefresh();
void DiscordNotifyMessageCreate(Snowflake id);
private:
DiscordClient m_discord;

View File

@ -1,8 +1,10 @@
#include "chatwindow.hpp"
#include "../abaddon.hpp"
#include <map>
ChatWindow::ChatWindow() {
m_update_dispatcher.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_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
m_listbox = Gtk::manage(new Gtk::ListBox);
@ -11,6 +13,8 @@ ChatWindow::ChatWindow() {
m_input = Gtk::manage(new Gtk::TextView);
m_entry_scroll = Gtk::manage(new Gtk::ScrolledWindow);
m_input->signal_key_press_event().connect(sigc::mem_fun(*this, &ChatWindow::on_key_press_event), false);
m_main->set_hexpand(true);
m_main->set_vexpand(true);
m_main->show();
@ -51,6 +55,10 @@ ChatWindow::ChatWindow() {
m_main->add(*m_entry_scroll);
}
void ChatWindow::SetAbaddon(Abaddon* ptr) {
m_abaddon = ptr;
}
Gtk::Widget *ChatWindow::GetRoot() const {
return m_main;
}
@ -120,10 +128,33 @@ Gtk::ListBoxRow *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
return nullptr;
}
bool ChatWindow::on_key_press_event(GdkEventKey *e) {
if (e->keyval == GDK_KEY_Return) {
auto buffer = m_input->get_buffer();
if (e->state & GDK_SHIFT_MASK)
return false;
auto text = buffer->get_text();
buffer->set_text("");
return true;
}
return false;
}
void ChatWindow::SetMessages(std::unordered_set<const MessageData *> msgs) {
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_update_queue.push(msgs);
m_update_dispatcher.emit();
m_message_set_queue.push(msgs);
m_message_set_dispatch.emit();
}
void ChatWindow::AddNewMessage(Snowflake id) {
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_new_message_queue.push(id);
m_new_message_dispatch.emit();
}
void ChatWindow::ScrollToBottom() {
@ -131,6 +162,20 @@ void ChatWindow::ScrollToBottom() {
x->set_value(x->get_upper());
}
void ChatWindow::AddNewMessageInternal() {
Snowflake id;
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
id = m_new_message_queue.front();
m_new_message_queue.pop();
}
auto data = m_abaddon->GetDiscordClient().GetMessage(id);
auto *row = CreateChatEntryComponent(data);
if (row != nullptr)
m_listbox->add(*row);
}
void ChatWindow::SetMessagesInternal() {
auto children = m_listbox->get_children();
auto it = children.begin();
@ -143,7 +188,7 @@ void ChatWindow::SetMessagesInternal() {
std::unordered_set<const MessageData *> *msgs;
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
msgs = &m_update_queue.front();
msgs = &m_message_set_queue.front();
}
// sort
@ -159,6 +204,6 @@ void ChatWindow::SetMessagesInternal() {
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_update_queue.pop();
m_message_set_queue.pop();
}
}

View File

@ -4,22 +4,31 @@
#include <mutex>
#include "../discord/discord.hpp"
class Abaddon;
class ChatWindow {
public:
ChatWindow();
void SetAbaddon(Abaddon *ptr);
Gtk::Widget *GetRoot() const;
void SetActiveChannel(Snowflake id);
Snowflake GetActiveChannel() const;
void SetMessages(std::unordered_set<const MessageData *> msgs);
void AddNewMessage(Snowflake id);
protected:
void ScrollToBottom();
void SetMessagesInternal();
void AddNewMessageInternal();
Gtk::ListBoxRow *CreateChatEntryComponentText(const MessageData *data);
Gtk::ListBoxRow *CreateChatEntryComponent(const MessageData *data);
Glib::Dispatcher m_update_dispatcher;
std::queue<std::unordered_set<const MessageData *>> m_update_queue;
bool on_key_press_event(GdkEventKey *e);
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;
std::mutex m_update_mutex;
Snowflake m_active_channel;
@ -30,4 +39,6 @@ protected:
Gtk::ScrolledWindow *m_scroll;
Gtk::ScrolledWindow *m_entry_scroll;
Gtk::TextView *m_input;
Abaddon *m_abaddon = nullptr;
};

View File

@ -113,6 +113,10 @@ void DiscordClient::FetchMessagesInChannel(Snowflake id, std::function<void(cons
});
}
const MessageData *DiscordClient::GetMessage(Snowflake id) const {
return &m_messages.at(id);
}
void DiscordClient::UpdateToken(std::string token) {
m_token = token;
m_http.SetAuth(token);
@ -147,7 +151,10 @@ void DiscordClient::HandleGatewayMessage(nlohmann::json j) {
switch (iter->second) {
case GatewayEvent::READY: {
HandleGatewayReady(m);
}
} break;
case GatewayEvent::MESSAGE_CREATE: {
HandleGatewayMessageCreate(m);
} break;
}
} break;
default:
@ -164,7 +171,12 @@ void DiscordClient::HandleGatewayReady(const GatewayMessage &msg) {
}
m_abaddon->DiscordNotifyReady();
m_user_settings = data.UserSettings;
return;
}
void DiscordClient::HandleGatewayMessageCreate(const GatewayMessage& msg) {
MessageData data = msg.Data;
StoreMessage(data.ID, data);
m_abaddon->DiscordNotifyMessageCreate(data.ID);
}
void DiscordClient::StoreGuild(Snowflake id, const GuildData &g) {
@ -211,6 +223,7 @@ void DiscordClient::SendIdentify() {
void DiscordClient::LoadEventMap() {
m_event_map["READY"] = GatewayEvent::READY;
m_event_map["MESSAGE_CREATE"] = GatewayEvent::MESSAGE_CREATE;
}
#define JS_D(k, t) \

View File

@ -7,6 +7,11 @@
#include <unordered_set>
#include <mutex>
// bruh
#ifdef GetMessage
#undef GetMessage
#endif
struct Snowflake {
Snowflake();
Snowflake(const Snowflake &s);
@ -64,6 +69,7 @@ enum class GatewayOp : int {
enum class GatewayEvent : int {
READY,
MESSAGE_CREATE,
};
struct GatewayMessage {
@ -381,12 +387,14 @@ public:
void UpdateSettingsGuildPositions(const std::vector<Snowflake> &pos);
void FetchMessagesInChannel(Snowflake id, std::function<void(const std::vector<MessageData> &)> cb);
const MessageData *GetMessage(Snowflake id) const;
void UpdateToken(std::string token);
private:
void HandleGatewayMessage(nlohmann::json msg);
void HandleGatewayReady(const GatewayMessage &msg);
void HandleGatewayMessageCreate(const GatewayMessage &msg);
void HeartbeatThread();
void SendIdentify();

View File

@ -39,7 +39,13 @@ void Websocket::OnMessage(const ix::WebSocketMessagePtr &msg) {
// printf("%s\n", msg->str.substr(0, 1000).c_str());
//else
// printf("%s\n", msg->str.c_str());
auto obj = nlohmann::json::parse(msg->str);
nlohmann::json obj;
try {
obj = nlohmann::json::parse(msg->str);
} catch (std::exception &e) {
printf("Error decoding JSON. Discarding message: %s\n", e.what());
return;
}
if (m_json_callback)
m_json_callback(obj);
} break;

View File

@ -88,7 +88,17 @@ void MainWindow::UpdateChatActiveChannel(Snowflake id) {
m_chat.SetActiveChannel(id);
}
Snowflake MainWindow::GetChatActiveChannel() const {
return m_chat.GetActiveChannel();
}
void MainWindow::UpdateChatNewMessage(Snowflake id) {
if (m_abaddon->GetDiscordClient().GetMessage(id)->ChannelID == GetChatActiveChannel())
m_chat.AddNewMessage(id);
}
void MainWindow::SetAbaddon(Abaddon *ptr) {
m_abaddon = ptr;
m_channel_list.SetAbaddon(ptr);
m_chat.SetAbaddon(ptr);
}

View File

@ -13,6 +13,8 @@ public:
void UpdateChannelListing();
void UpdateChatWindowContents();
void UpdateChatActiveChannel(Snowflake id);
Snowflake GetChatActiveChannel() const;
void UpdateChatNewMessage(Snowflake id);
protected:
Gtk::Box m_main_box;