Skip to content

Commit

Permalink
add ability to set image alt text (closes #253)
Browse files Browse the repository at this point in the history
  • Loading branch information
ouwou committed Dec 15, 2023
1 parent 23bf237 commit 4bce7b7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
21 changes: 20 additions & 1 deletion src/components/chatinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 10 additions & 4 deletions src/components/chatinput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ class ChatInputAttachmentItem : public Gtk::EventBox {
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:
Expand All @@ -21,6 +24,7 @@ class ChatInputAttachmentItem : public Gtk::EventBox {
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;
Expand All @@ -29,6 +33,8 @@ class ChatInputAttachmentItem : public Gtk::EventBox {
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>;
Expand Down
2 changes: 2 additions & 0 deletions src/discord/chatsubmitparams.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <optional>
#include <vector>
#include <string>
#include <glibmm/ustring.h>
Expand All @@ -15,6 +16,7 @@ struct ChatSubmitParams {
Glib::RefPtr<Gio::File> File;
AttachmentType Type;
std::string Filename;
std::optional<std::string> Description;
};

bool Silent = false;
Expand Down
10 changes: 8 additions & 2 deletions src/discord/discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ void DiscordClient::SendChatMessageAttachments(const ChatSubmitParams &params, c
CreateMessageObject obj;
obj.Content = params.Message;
obj.Nonce = nonce;
obj.Attachments.emplace();
if (params.Silent) {
obj.Flags |= MessageFlags::SUPPRESS_NOTIFICATIONS;
}
Expand All @@ -541,11 +542,16 @@ void DiscordClient::SendChatMessageAttachments(const ChatSubmitParams &params, 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) {
Expand Down
6 changes: 6 additions & 0 deletions src/discord/objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions src/discord/objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down

0 comments on commit 4bce7b7

Please sign in to comment.