add ability to edit messages

This commit is contained in:
ouwou
2020-08-30 22:55:36 -04:00
parent 44b7989f50
commit 9b1bc8f145
12 changed files with 120 additions and 1 deletions

View File

@@ -148,6 +148,7 @@
<ClCompile Include="components\chatmessage.cpp" /> <ClCompile Include="components\chatmessage.cpp" />
<ClCompile Include="components\chatwindow.cpp" /> <ClCompile Include="components\chatwindow.cpp" />
<ClCompile Include="components\memberlist.cpp" /> <ClCompile Include="components\memberlist.cpp" />
<ClCompile Include="dialogs\editmessage.cpp" />
<ClCompile Include="dialogs\token.cpp" /> <ClCompile Include="dialogs\token.cpp" />
<ClCompile Include="discord\discord.cpp" /> <ClCompile Include="discord\discord.cpp" />
<ClCompile Include="discord\http.cpp" /> <ClCompile Include="discord\http.cpp" />
@@ -162,6 +163,7 @@
<ClInclude Include="components\chatmessage.hpp" /> <ClInclude Include="components\chatmessage.hpp" />
<ClInclude Include="components\chatwindow.hpp" /> <ClInclude Include="components\chatwindow.hpp" />
<ClInclude Include="components\memberlist.hpp" /> <ClInclude Include="components\memberlist.hpp" />
<ClInclude Include="dialogs\editmessage.hpp" />
<ClInclude Include="dialogs\token.hpp" /> <ClInclude Include="dialogs\token.hpp" />
<ClInclude Include="discord\discord.hpp" /> <ClInclude Include="discord\discord.hpp" />
<ClInclude Include="discord\http.hpp" /> <ClInclude Include="discord\http.hpp" />

View File

@@ -51,6 +51,9 @@
<ClCompile Include="discord\objects.cpp"> <ClCompile Include="discord\objects.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="dialogs\editmessage.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="windows\mainwindow.hpp"> <ClInclude Include="windows\mainwindow.hpp">
@@ -89,5 +92,8 @@
<ClInclude Include="discord\objects.hpp"> <ClInclude Include="discord\objects.hpp">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="dialogs\editmessage.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -4,6 +4,7 @@
#include <algorithm> #include <algorithm>
#include "discord/discord.hpp" #include "discord/discord.hpp"
#include "dialogs/token.hpp" #include "dialogs/token.hpp"
#include "dialogs/editmessage.hpp"
#include "abaddon.hpp" #include "abaddon.hpp"
#ifdef _WIN32 #ifdef _WIN32
@@ -205,6 +206,15 @@ void Abaddon::ActionChatDeleteMessage(Snowflake channel_id, Snowflake id) {
m_discord.DeleteMessage(channel_id, id); m_discord.DeleteMessage(channel_id, id);
} }
void Abaddon::ActionChatEditMessage(Snowflake channel_id, Snowflake id) {
EditMessageDialog dlg(*m_main_window);
auto response = dlg.run();
if (response == Gtk::RESPONSE_OK) {
auto new_content = dlg.GetContent();
m_discord.EditMessage(channel_id, id, new_content);
}
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
Gtk::Main::init_gtkmm_internals(); // why??? Gtk::Main::init_gtkmm_internals(); // why???
Abaddon abaddon; Abaddon abaddon;

View File

@@ -28,6 +28,7 @@ public:
void ActionChatInputSubmit(std::string msg, Snowflake channel); void ActionChatInputSubmit(std::string msg, Snowflake channel);
void ActionChatLoadHistory(Snowflake id); void ActionChatLoadHistory(Snowflake id);
void ActionChatDeleteMessage(Snowflake channel_id, Snowflake id); void ActionChatDeleteMessage(Snowflake channel_id, Snowflake id);
void ActionChatEditMessage(Snowflake channel_id, Snowflake id);
std::string GetDiscordToken() const; std::string GetDiscordToken() const;
bool IsDiscordActive() const; bool IsDiscordActive() const;

View File

@@ -62,6 +62,10 @@ ChatMessageItem::ChatMessageItem() {
m_menu_delete_message->signal_activate().connect(sigc::mem_fun(*this, &ChatMessageItem::on_menu_message_delete)); m_menu_delete_message->signal_activate().connect(sigc::mem_fun(*this, &ChatMessageItem::on_menu_message_delete));
m_menu.append(*m_menu_delete_message); m_menu.append(*m_menu_delete_message);
m_menu_edit_message = Gtk::manage(new Gtk::MenuItem("_Edit Message", true));
m_menu_edit_message->signal_activate().connect(sigc::mem_fun(*this, &ChatMessageItem::on_menu_message_edit));
m_menu.append(*m_menu_edit_message);
m_menu.show_all(); m_menu.show_all();
} }
@@ -73,6 +77,10 @@ void ChatMessageItem::on_menu_message_delete() {
m_abaddon->ActionChatDeleteMessage(ChannelID, ID); m_abaddon->ActionChatDeleteMessage(ChannelID, ID);
} }
void ChatMessageItem::on_menu_message_edit() {
m_abaddon->ActionChatEditMessage(ChannelID, ID);
}
void ChatMessageItem::on_menu_copy_id() { void ChatMessageItem::on_menu_copy_id() {
Gtk::Clipboard::get()->set_text(std::to_string(ID)); Gtk::Clipboard::get()->set_text(std::to_string(ID));
} }
@@ -94,7 +102,9 @@ void ChatMessageItem::AttachMenuHandler(Gtk::Widget *widget) {
void ChatMessageItem::ShowMenu(const GdkEvent *event) { void ChatMessageItem::ShowMenu(const GdkEvent *event) {
auto &client = m_abaddon->GetDiscordClient(); auto &client = m_abaddon->GetDiscordClient();
auto *data = client.GetMessage(ID); auto *data = client.GetMessage(ID);
m_menu_delete_message->set_sensitive(client.GetUserData().ID == data->Author.ID); bool can_manage = client.GetUserData().ID == data->Author.ID;
m_menu_delete_message->set_sensitive(can_manage);
m_menu_edit_message->set_sensitive(can_manage);
m_menu.popup_at_pointer(event); m_menu.popup_at_pointer(event);
} }

View File

@@ -43,10 +43,12 @@ protected:
void AttachMenuHandler(Gtk::Widget *widget); void AttachMenuHandler(Gtk::Widget *widget);
void on_menu_copy_id(); void on_menu_copy_id();
void on_menu_message_delete(); void on_menu_message_delete();
void on_menu_message_edit();
Gtk::Menu m_menu; Gtk::Menu m_menu;
Gtk::MenuItem *m_menu_copy_id; Gtk::MenuItem *m_menu_copy_id;
Gtk::MenuItem *m_menu_delete_message; Gtk::MenuItem *m_menu_delete_message;
Gtk::MenuItem *m_menu_edit_message;
Abaddon *m_abaddon = nullptr; Abaddon *m_abaddon = nullptr;
}; };

39
dialogs/editmessage.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include "editmessage.hpp"
EditMessageDialog::EditMessageDialog(Gtk::Window &parent)
: Gtk::Dialog("Edit Message", parent, true)
, m_layout(Gtk::ORIENTATION_VERTICAL)
, m_bbox(Gtk::ORIENTATION_HORIZONTAL)
, m_ok("OK")
, m_cancel("Cancel") {
set_default_size(300, 50);
m_ok.signal_clicked().connect([&]() {
m_content = m_text.get_buffer()->get_text();
response(Gtk::RESPONSE_OK);
});
m_cancel.signal_clicked().connect([&]() {
response(Gtk::RESPONSE_CANCEL);
});
m_bbox.pack_start(m_ok, Gtk::PACK_SHRINK);
m_bbox.pack_start(m_cancel, Gtk::PACK_SHRINK);
m_bbox.set_layout(Gtk::BUTTONBOX_END);
m_text.set_hexpand(true);
m_scroll.set_hexpand(true);
m_scroll.set_vexpand(true);
m_scroll.add(m_text);
m_layout.add(m_scroll);
m_layout.add(m_bbox);
get_content_area()->add(m_layout);
show_all_children();
}
std::string EditMessageDialog::GetContent() {
return m_content;
}

20
dialogs/editmessage.hpp Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <gtkmm.h>
#include <string>
class EditMessageDialog : public Gtk::Dialog {
public:
EditMessageDialog(Gtk::Window &parent);
std::string GetContent();
protected:
Gtk::Box m_layout;
Gtk::Button m_ok;
Gtk::Button m_cancel;
Gtk::ButtonBox m_bbox;
Gtk::ScrolledWindow m_scroll;
Gtk::TextView m_text;
private:
std::string m_content;
};

View File

@@ -172,6 +172,14 @@ void DiscordClient::DeleteMessage(Snowflake channel_id, Snowflake id) {
m_http.MakeDELETE(path, [](auto) {}); m_http.MakeDELETE(path, [](auto) {});
} }
void DiscordClient::EditMessage(Snowflake channel_id, Snowflake id, std::string content) {
std::string path = "/channels/" + std::to_string(channel_id) + "/messages/" + std::to_string(id);
MessageEditObject obj;
obj.Content = content;
nlohmann::json j = obj;
m_http.MakePATCH(path, j.dump(), [](auto) {});
}
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);

View File

@@ -70,6 +70,7 @@ public:
void SendChatMessage(std::string content, Snowflake channel); void SendChatMessage(std::string content, Snowflake channel);
void DeleteMessage(Snowflake channel_id, Snowflake id); void DeleteMessage(Snowflake channel_id, Snowflake id);
void EditMessage(Snowflake channel_id, Snowflake id, std::string content);
void UpdateToken(std::string token); void UpdateToken(std::string token);

View File

@@ -295,6 +295,18 @@ void to_json(nlohmann::json &j, const CreateMessageObject &m) {
j["content"] = m.Content; j["content"] = m.Content;
} }
void to_json(nlohmann::json &j, const MessageEditObject &m) {
if (m.Content.size() > 0)
j["content"] = m.Content;
// todo EmbedData to_json
// if (m.Embeds.size() > 0)
// j["embeds"] = m.Embeds;
if (m.Flags != -1)
j["flags"] = m.Flags;
}
Snowflake::Snowflake() Snowflake::Snowflake()
: m_num(Invalid) {} : m_num(Invalid) {}

View File

@@ -424,3 +424,11 @@ struct CreateMessageObject {
friend void to_json(nlohmann::json &j, const CreateMessageObject &m); friend void to_json(nlohmann::json &j, const CreateMessageObject &m);
}; };
struct MessageEditObject {
std::string Content; // opt, null
std::vector<EmbedData> Embeds; // opt, null
int Flags = -1; // opt, null
friend void to_json(nlohmann::json &j, const MessageEditObject &m);
};