shitty MESSAGE_CREATE handling
This commit is contained in:
@@ -79,6 +79,10 @@ void Abaddon::DiscordNotifyChannelListFullRefresh() {
|
|||||||
m_main_window->UpdateChannelListing();
|
m_main_window->UpdateChannelListing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Abaddon::DiscordNotifyMessageCreate(Snowflake id) {
|
||||||
|
m_main_window->UpdateChatNewMessage(id);
|
||||||
|
}
|
||||||
|
|
||||||
void Abaddon::ActionConnect() {
|
void Abaddon::ActionConnect() {
|
||||||
if (!m_discord.IsStarted())
|
if (!m_discord.IsStarted())
|
||||||
StartDiscord();
|
StartDiscord();
|
||||||
|
@@ -31,6 +31,7 @@ public:
|
|||||||
const DiscordClient &GetDiscordClient() const;
|
const DiscordClient &GetDiscordClient() const;
|
||||||
void DiscordNotifyReady();
|
void DiscordNotifyReady();
|
||||||
void DiscordNotifyChannelListFullRefresh();
|
void DiscordNotifyChannelListFullRefresh();
|
||||||
|
void DiscordNotifyMessageCreate(Snowflake id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DiscordClient m_discord;
|
DiscordClient m_discord;
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
#include "chatwindow.hpp"
|
#include "chatwindow.hpp"
|
||||||
|
#include "../abaddon.hpp"
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
ChatWindow::ChatWindow() {
|
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_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
||||||
m_listbox = Gtk::manage(new Gtk::ListBox);
|
m_listbox = Gtk::manage(new Gtk::ListBox);
|
||||||
@@ -11,6 +13,8 @@ ChatWindow::ChatWindow() {
|
|||||||
m_input = Gtk::manage(new Gtk::TextView);
|
m_input = Gtk::manage(new Gtk::TextView);
|
||||||
m_entry_scroll = Gtk::manage(new Gtk::ScrolledWindow);
|
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_hexpand(true);
|
||||||
m_main->set_vexpand(true);
|
m_main->set_vexpand(true);
|
||||||
m_main->show();
|
m_main->show();
|
||||||
@@ -51,6 +55,10 @@ ChatWindow::ChatWindow() {
|
|||||||
m_main->add(*m_entry_scroll);
|
m_main->add(*m_entry_scroll);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ChatWindow::SetAbaddon(Abaddon* ptr) {
|
||||||
|
m_abaddon = ptr;
|
||||||
|
}
|
||||||
|
|
||||||
Gtk::Widget *ChatWindow::GetRoot() const {
|
Gtk::Widget *ChatWindow::GetRoot() const {
|
||||||
return m_main;
|
return m_main;
|
||||||
}
|
}
|
||||||
@@ -120,10 +128,33 @@ Gtk::ListBoxRow *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
|
|||||||
return nullptr;
|
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) {
|
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_update_queue.push(msgs);
|
m_message_set_queue.push(msgs);
|
||||||
m_update_dispatcher.emit();
|
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() {
|
void ChatWindow::ScrollToBottom() {
|
||||||
@@ -131,6 +162,20 @@ void ChatWindow::ScrollToBottom() {
|
|||||||
x->set_value(x->get_upper());
|
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() {
|
void ChatWindow::SetMessagesInternal() {
|
||||||
auto children = m_listbox->get_children();
|
auto children = m_listbox->get_children();
|
||||||
auto it = children.begin();
|
auto it = children.begin();
|
||||||
@@ -143,7 +188,7 @@ void ChatWindow::SetMessagesInternal() {
|
|||||||
std::unordered_set<const MessageData *> *msgs;
|
std::unordered_set<const MessageData *> *msgs;
|
||||||
{
|
{
|
||||||
std::scoped_lock<std::mutex> guard(m_update_mutex);
|
std::scoped_lock<std::mutex> guard(m_update_mutex);
|
||||||
msgs = &m_update_queue.front();
|
msgs = &m_message_set_queue.front();
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort
|
// sort
|
||||||
@@ -159,6 +204,6 @@ void ChatWindow::SetMessagesInternal() {
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::scoped_lock<std::mutex> guard(m_update_mutex);
|
std::scoped_lock<std::mutex> guard(m_update_mutex);
|
||||||
m_update_queue.pop();
|
m_message_set_queue.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,22 +4,31 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "../discord/discord.hpp"
|
#include "../discord/discord.hpp"
|
||||||
|
|
||||||
|
class Abaddon;
|
||||||
class ChatWindow {
|
class ChatWindow {
|
||||||
public:
|
public:
|
||||||
ChatWindow();
|
ChatWindow();
|
||||||
|
void SetAbaddon(Abaddon *ptr);
|
||||||
|
|
||||||
Gtk::Widget *GetRoot() const;
|
Gtk::Widget *GetRoot() const;
|
||||||
void SetActiveChannel(Snowflake id);
|
void SetActiveChannel(Snowflake id);
|
||||||
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);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void ScrollToBottom();
|
void ScrollToBottom();
|
||||||
void SetMessagesInternal();
|
void SetMessagesInternal();
|
||||||
|
void AddNewMessageInternal();
|
||||||
Gtk::ListBoxRow *CreateChatEntryComponentText(const MessageData *data);
|
Gtk::ListBoxRow *CreateChatEntryComponentText(const MessageData *data);
|
||||||
Gtk::ListBoxRow *CreateChatEntryComponent(const MessageData *data);
|
Gtk::ListBoxRow *CreateChatEntryComponent(const MessageData *data);
|
||||||
|
|
||||||
Glib::Dispatcher m_update_dispatcher;
|
bool on_key_press_event(GdkEventKey *e);
|
||||||
std::queue<std::unordered_set<const MessageData *>> m_update_queue;
|
|
||||||
|
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;
|
std::mutex m_update_mutex;
|
||||||
|
|
||||||
Snowflake m_active_channel;
|
Snowflake m_active_channel;
|
||||||
@@ -30,4 +39,6 @@ protected:
|
|||||||
Gtk::ScrolledWindow *m_scroll;
|
Gtk::ScrolledWindow *m_scroll;
|
||||||
Gtk::ScrolledWindow *m_entry_scroll;
|
Gtk::ScrolledWindow *m_entry_scroll;
|
||||||
Gtk::TextView *m_input;
|
Gtk::TextView *m_input;
|
||||||
|
|
||||||
|
Abaddon *m_abaddon = nullptr;
|
||||||
};
|
};
|
||||||
|
@@ -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) {
|
void DiscordClient::UpdateToken(std::string token) {
|
||||||
m_token = token;
|
m_token = token;
|
||||||
m_http.SetAuth(token);
|
m_http.SetAuth(token);
|
||||||
@@ -147,7 +151,10 @@ void DiscordClient::HandleGatewayMessage(nlohmann::json j) {
|
|||||||
switch (iter->second) {
|
switch (iter->second) {
|
||||||
case GatewayEvent::READY: {
|
case GatewayEvent::READY: {
|
||||||
HandleGatewayReady(m);
|
HandleGatewayReady(m);
|
||||||
}
|
} break;
|
||||||
|
case GatewayEvent::MESSAGE_CREATE: {
|
||||||
|
HandleGatewayMessageCreate(m);
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
default:
|
default:
|
||||||
@@ -164,7 +171,12 @@ void DiscordClient::HandleGatewayReady(const GatewayMessage &msg) {
|
|||||||
}
|
}
|
||||||
m_abaddon->DiscordNotifyReady();
|
m_abaddon->DiscordNotifyReady();
|
||||||
m_user_settings = data.UserSettings;
|
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) {
|
void DiscordClient::StoreGuild(Snowflake id, const GuildData &g) {
|
||||||
@@ -211,6 +223,7 @@ void DiscordClient::SendIdentify() {
|
|||||||
|
|
||||||
void DiscordClient::LoadEventMap() {
|
void DiscordClient::LoadEventMap() {
|
||||||
m_event_map["READY"] = GatewayEvent::READY;
|
m_event_map["READY"] = GatewayEvent::READY;
|
||||||
|
m_event_map["MESSAGE_CREATE"] = GatewayEvent::MESSAGE_CREATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define JS_D(k, t) \
|
#define JS_D(k, t) \
|
||||||
|
@@ -7,6 +7,11 @@
|
|||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
|
// bruh
|
||||||
|
#ifdef GetMessage
|
||||||
|
#undef GetMessage
|
||||||
|
#endif
|
||||||
|
|
||||||
struct Snowflake {
|
struct Snowflake {
|
||||||
Snowflake();
|
Snowflake();
|
||||||
Snowflake(const Snowflake &s);
|
Snowflake(const Snowflake &s);
|
||||||
@@ -64,6 +69,7 @@ enum class GatewayOp : int {
|
|||||||
|
|
||||||
enum class GatewayEvent : int {
|
enum class GatewayEvent : int {
|
||||||
READY,
|
READY,
|
||||||
|
MESSAGE_CREATE,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GatewayMessage {
|
struct GatewayMessage {
|
||||||
@@ -381,12 +387,14 @@ 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);
|
||||||
|
const MessageData *GetMessage(Snowflake id) const;
|
||||||
|
|
||||||
void UpdateToken(std::string token);
|
void UpdateToken(std::string token);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void HandleGatewayMessage(nlohmann::json msg);
|
void HandleGatewayMessage(nlohmann::json msg);
|
||||||
void HandleGatewayReady(const GatewayMessage &msg);
|
void HandleGatewayReady(const GatewayMessage &msg);
|
||||||
|
void HandleGatewayMessageCreate(const GatewayMessage &msg);
|
||||||
void HeartbeatThread();
|
void HeartbeatThread();
|
||||||
void SendIdentify();
|
void SendIdentify();
|
||||||
|
|
||||||
|
@@ -39,7 +39,13 @@ void Websocket::OnMessage(const ix::WebSocketMessagePtr &msg) {
|
|||||||
// printf("%s\n", msg->str.substr(0, 1000).c_str());
|
// printf("%s\n", msg->str.substr(0, 1000).c_str());
|
||||||
//else
|
//else
|
||||||
// printf("%s\n", msg->str.c_str());
|
// 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)
|
if (m_json_callback)
|
||||||
m_json_callback(obj);
|
m_json_callback(obj);
|
||||||
} break;
|
} break;
|
||||||
|
@@ -88,7 +88,17 @@ void MainWindow::UpdateChatActiveChannel(Snowflake id) {
|
|||||||
m_chat.SetActiveChannel(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) {
|
void MainWindow::SetAbaddon(Abaddon *ptr) {
|
||||||
m_abaddon = ptr;
|
m_abaddon = ptr;
|
||||||
m_channel_list.SetAbaddon(ptr);
|
m_channel_list.SetAbaddon(ptr);
|
||||||
|
m_chat.SetAbaddon(ptr);
|
||||||
}
|
}
|
||||||
|
@@ -13,6 +13,8 @@ public:
|
|||||||
void UpdateChannelListing();
|
void UpdateChannelListing();
|
||||||
void UpdateChatWindowContents();
|
void UpdateChatWindowContents();
|
||||||
void UpdateChatActiveChannel(Snowflake id);
|
void UpdateChatActiveChannel(Snowflake id);
|
||||||
|
Snowflake GetChatActiveChannel() const;
|
||||||
|
void UpdateChatNewMessage(Snowflake id);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Gtk::Box m_main_box;
|
Gtk::Box m_main_box;
|
||||||
|
Reference in New Issue
Block a user