diff options
author | ouwou <26526779+ouwou@users.noreply.github.com> | 2023-12-15 01:16:11 -0500 |
---|---|---|
committer | ouwou <26526779+ouwou@users.noreply.github.com> | 2023-12-15 01:16:11 -0500 |
commit | 4bce7b7523caa26023b1a2f5e02e6646ed29f5e0 (patch) | |
tree | 1c029a16d83ba64209511fd05b9276251821e051 | |
parent | 23bf237e4e7dd008df8ff32da31878d1b990f5bf (diff) | |
download | abaddon-portaudio-4bce7b7523caa26023b1a2f5e02e6646ed29f5e0.tar.gz abaddon-portaudio-4bce7b7523caa26023b1a2f5e02e6646ed29f5e0.zip |
add ability to set image alt text (closes #253)
-rw-r--r-- | src/components/chatinput.cpp | 21 | ||||
-rw-r--r-- | src/components/chatinput.hpp | 14 | ||||
-rw-r--r-- | src/discord/chatsubmitparams.hpp | 2 | ||||
-rw-r--r-- | src/discord/discord.cpp | 10 | ||||
-rw-r--r-- | src/discord/objects.cpp | 6 | ||||
-rw-r--r-- | src/discord/objects.hpp | 8 |
6 files changed, 54 insertions, 7 deletions
diff --git a/src/components/chatinput.cpp b/src/components/chatinput.cpp index c802413..10896fb 100644 --- a/src/components/chatinput.cpp +++ b/src/components/chatinput.cpp @@ -295,7 +295,7 @@ std::vector<ChatSubmitParams::Attachment> ChatInputAttachmentContainer::GetAttac for (auto *x : m_attachments) { if (!x->GetFile()->query_exists()) puts("bad!"); - ret.push_back({ x->GetFile(), x->GetType(), x->GetFilename() }); + ret.push_back({ x->GetFile(), x->GetType(), x->GetFilename(), x->GetDescription() }); } return ret; } @@ -343,6 +343,7 @@ ChatInputAttachmentItem::ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> & , m_img(Gtk::make_managed<Gtk::Image>()) , m_type(is_extant ? ChatSubmitParams::ExtantFile : ChatSubmitParams::PastedImage) , m_filename("unknown.png") + , m_is_image(true) , m_label("unknown.png") , m_box(Gtk::ORIENTATION_VERTICAL) { get_style_context()->add_class("attachment-item"); @@ -389,10 +390,18 @@ std::string ChatInputAttachmentItem::GetFilename() const { return m_filename; } +std::optional<std::string> ChatInputAttachmentItem::GetDescription() const { + return m_description.empty() ? std::nullopt : std::optional<std::string>(m_description); +} + bool ChatInputAttachmentItem::IsTemp() const noexcept { return m_type == ChatSubmitParams::PastedImage; } +bool ChatInputAttachmentItem::IsImage() const noexcept { + return m_is_image; +} + void ChatInputAttachmentItem::RemoveIfTemp() { if (IsTemp()) m_file->remove(); @@ -420,12 +429,22 @@ void ChatInputAttachmentItem::SetupMenu() { } }); + m_menu_set_alt_text.set_label("Change Alt-Text"); + m_menu_set_alt_text.signal_activate().connect([this]() { + const auto description = Abaddon::Get().ShowTextPrompt("Enter description (alt-text) for attachment", "Enter alt-text", m_description); + if (description.has_value()) { + m_description = *description; + } + }); + m_menu.add(m_menu_set_filename); + m_menu.add(m_menu_set_alt_text); m_menu.add(m_menu_remove); m_menu.show_all(); signal_button_press_event().connect([this](GdkEventButton *ev) -> bool { if (ev->button == GDK_BUTTON_SECONDARY) { + m_menu_set_alt_text.set_visible(IsImage()); m_menu.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev)); return true; } diff --git a/src/components/chatinput.hpp b/src/components/chatinput.hpp index a3c9742..be8c141 100644 --- a/src/components/chatinput.hpp +++ b/src/components/chatinput.hpp @@ -7,10 +7,13 @@ public: ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> &file); ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> &file, const Glib::RefPtr<Gdk::Pixbuf> &pb, bool is_extant = false); - [[nodiscard]] Glib::RefPtr<Gio::File> GetFile() const; - [[nodiscard]] ChatSubmitParams::AttachmentType GetType() const; - [[nodiscard]] std::string GetFilename() const; - [[nodiscard]] bool IsTemp() const noexcept; + Glib::RefPtr<Gio::File> GetFile() const; + ChatSubmitParams::AttachmentType GetType() const; + std::string GetFilename() const; + std::optional<std::string> GetDescription() const; + bool IsTemp() const noexcept; + bool IsImage() const noexcept; + void RemoveIfTemp(); private: @@ -21,6 +24,7 @@ private: Gtk::Menu m_menu; Gtk::MenuItem m_menu_remove; Gtk::MenuItem m_menu_set_filename; + Gtk::MenuItem m_menu_set_alt_text; Gtk::Box m_box; Gtk::Label m_label; @@ -29,6 +33,8 @@ private: Glib::RefPtr<Gio::File> m_file; ChatSubmitParams::AttachmentType m_type; std::string m_filename; + std::string m_description; + bool m_is_image = false; private: using type_signal_item_removed = sigc::signal<void>; diff --git a/src/discord/chatsubmitparams.hpp b/src/discord/chatsubmitparams.hpp index e195189..45fbb2a 100644 --- a/src/discord/chatsubmitparams.hpp +++ b/src/discord/chatsubmitparams.hpp @@ -1,4 +1,5 @@ #pragma once +#include <optional> #include <vector> #include <string> #include <glibmm/ustring.h> @@ -15,6 +16,7 @@ struct ChatSubmitParams { Glib::RefPtr<Gio::File> File; AttachmentType Type; std::string Filename; + std::optional<std::string> Description; }; bool Silent = false; diff --git a/src/discord/discord.cpp b/src/discord/discord.cpp index 0618e72..ac7e2f2 100644 --- a/src/discord/discord.cpp +++ b/src/discord/discord.cpp @@ -518,6 +518,7 @@ void DiscordClient::SendChatMessageAttachments(const ChatSubmitParams ¶ms, c CreateMessageObject obj; obj.Content = params.Message; obj.Nonce = nonce; + obj.Attachments.emplace(); if (params.Silent) { obj.Flags |= MessageFlags::SUPPRESS_NOTIFICATIONS; } @@ -541,11 +542,16 @@ void DiscordClient::SendChatMessageAttachments(const ChatSubmitParams ¶ms, c m_generic_dispatch.emit(); }); req.make_form(); - req.add_field("payload_json", nlohmann::json(obj).dump().c_str(), CURL_ZERO_TERMINATED); + for (size_t i = 0; i < params.Attachments.size(); i++) { + auto &attachment = params.Attachments.at(i); const auto field_name = "files[" + std::to_string(i) + "]"; - req.add_file(field_name, params.Attachments.at(i).File, params.Attachments.at(i).Filename); + req.add_file(field_name, attachment.File, attachment.Filename); + obj.Attachments->push_back({ static_cast<int>(i), attachment.Description }); } + + req.add_field("payload_json", nlohmann::json(obj).dump().c_str(), CURL_ZERO_TERMINATED); + m_http.Execute(std::move(req), [this, params, nonce, callback](const http::response_type &res) { for (const auto &attachment : params.Attachments) { if (attachment.Type == ChatSubmitParams::AttachmentType::PastedImage) { diff --git a/src/discord/objects.cpp b/src/discord/objects.cpp index 4ad17c3..804f10d 100644 --- a/src/discord/objects.cpp +++ b/src/discord/objects.cpp @@ -306,9 +306,15 @@ void to_json(nlohmann::json &j, const HeartbeatMessage &m) { j["d"] = m.Sequence; } +void to_json(nlohmann::json &j, const CreateMessageAttachmentObject &m) { + j["id"] = m.ID; + JS_IF("description", m.Description); +} + void to_json(nlohmann::json &j, const CreateMessageObject &m) { j["content"] = m.Content; j["flags"] = m.Flags; + JS_IF("attachments", m.Attachments); JS_IF("message_reference", m.MessageReference); JS_IF("nonce", m.Nonce); } diff --git a/src/discord/objects.hpp b/src/discord/objects.hpp index 603a9c7..dfe99f0 100644 --- a/src/discord/objects.hpp +++ b/src/discord/objects.hpp @@ -433,11 +433,19 @@ struct HeartbeatMessage : GatewayMessage { friend void to_json(nlohmann::json &j, const HeartbeatMessage &m); }; +struct CreateMessageAttachmentObject { + int ID; + std::optional<std::string> Description; + + friend void to_json(nlohmann::json &j, const CreateMessageAttachmentObject &m); +}; + struct CreateMessageObject { std::string Content; MessageFlags Flags = MessageFlags::NONE; std::optional<MessageReferenceData> MessageReference; std::optional<std::string> Nonce; + std::optional<std::vector<CreateMessageAttachmentObject>> Attachments; friend void to_json(nlohmann::json &j, const CreateMessageObject &m); }; |