summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2021-07-01 02:03:41 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2021-07-01 02:03:41 -0400
commitcbc65bf766c1241980961bcca999adf69c80dba0 (patch)
tree5c383a6201258e33ac72fa8e9b9690bcf5f7c5e6
parent220aa6d13a9cb12687139305c6f68eb9cf6f73a1 (diff)
downloadabaddon-portaudio-cbc65bf766c1241980961bcca999adf69c80dba0.tar.gz
abaddon-portaudio-cbc65bf766c1241980961bcca999adf69c80dba0.zip
re-add suppport for static (a)png stickers
-rw-r--r--components/chatmessage.cpp31
-rw-r--r--components/chatmessage.hpp7
-rw-r--r--discord/message.cpp2
-rw-r--r--discord/message.hpp1
-rw-r--r--discord/sticker.cpp20
-rw-r--r--discord/sticker.hpp11
-rw-r--r--discord/store.cpp25
7 files changed, 86 insertions, 11 deletions
diff --git a/components/chatmessage.cpp b/components/chatmessage.cpp
index 727b64a..e61ba3f 100644
--- a/components/chatmessage.cpp
+++ b/components/chatmessage.cpp
@@ -9,6 +9,7 @@ constexpr static int AvatarSize = 32;
constexpr static int EmbedImageWidth = 400;
constexpr static int EmbedImageHeight = 300;
constexpr static int ThumbnailSize = 100;
+constexpr static int StickerComponentSize = 160;
ChatMessageItemContainer::ChatMessageItemContainer() {
m_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
@@ -68,6 +69,8 @@ ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(const Message &d
}
// only 1?
+ /*
+ DEPRECATED
if (data.Stickers.has_value()) {
const auto &sticker = data.Stickers.value()[0];
// todo: lottie, proper apng
@@ -75,6 +78,11 @@ ChatMessageItemContainer *ChatMessageItemContainer::FromMessage(const Message &d
auto *widget = container->CreateStickerComponent(sticker);
container->m_main->add(*widget);
}
+ }*/
+
+ if (data.StickerItems.has_value()) {
+ auto *widget = container->CreateStickersComponent(*data.StickerItems);
+ container->m_main->add(*widget);
}
if (data.Reactions.has_value() && data.Reactions->size() > 0) {
@@ -479,7 +487,7 @@ Gtk::Widget *ChatMessageItemContainer::CreateAttachmentComponent(const Attachmen
return ev;
}
-Gtk::Widget *ChatMessageItemContainer::CreateStickerComponent(const StickerData &data) {
+Gtk::Widget *ChatMessageItemContainer::CreateStickerComponentDeprecated(const StickerData &data) {
auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *imgw = Gtk::manage(new Gtk::Image);
box->add(*imgw);
@@ -496,6 +504,27 @@ Gtk::Widget *ChatMessageItemContainer::CreateStickerComponent(const StickerData
return box;
}
+Gtk::Widget *ChatMessageItemContainer::CreateStickersComponent(const std::vector<StickerItem> &data) {
+ auto *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
+
+ for (const auto &sticker : data) {
+ // no lottie
+ if (sticker.FormatType != StickerFormatType::PNG && sticker.FormatType != StickerFormatType::APNG) continue;
+ auto *ev = Gtk::manage(new Gtk::EventBox);
+ auto *img = Gtk::manage(new LazyImage(sticker.GetURL(), StickerComponentSize, StickerComponentSize, false));
+ img->set_size_request(StickerComponentSize, StickerComponentSize); // should this go in LazyImage ?
+ img->show();
+ ev->show();
+ ev->add(*img);
+ box->add(*ev);
+ }
+
+ box->show();
+
+ AttachEventHandlers(*box);
+ return box;
+}
+
Gtk::Widget *ChatMessageItemContainer::CreateReactionsComponent(const Message &data) {
auto *flow = Gtk::manage(new Gtk::FlowBox);
flow->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
diff --git a/components/chatmessage.hpp b/components/chatmessage.hpp
index 7bc050d..3868094 100644
--- a/components/chatmessage.hpp
+++ b/components/chatmessage.hpp
@@ -25,7 +25,8 @@ protected:
Gtk::Widget *CreateEmbedComponent(const EmbedData &data); // Message.Embeds[0]
Gtk::Widget *CreateImageComponent(const std::string &proxy_url, const std::string &url, int inw, int inh);
Gtk::Widget *CreateAttachmentComponent(const AttachmentData &data); // non-image attachments
- Gtk::Widget *CreateStickerComponent(const StickerData &data);
+ Gtk::Widget *CreateStickerComponentDeprecated(const StickerData &data);
+ Gtk::Widget *CreateStickersComponent(const std::vector<StickerItem> &data);
Gtk::Widget *CreateReactionsComponent(const Message &data);
Gtk::Widget *CreateReplyComponent(const Message &data);
@@ -89,14 +90,14 @@ public:
ChatMessageHeader(const Message &data);
void AddContent(Gtk::Widget *widget, bool prepend);
void UpdateNameColor();
- std::vector<Gtk::Widget*> GetChildContent();
+ std::vector<Gtk::Widget *> GetChildContent();
protected:
void AttachUserMenuHandler(Gtk::Widget &widget);
bool on_author_button_press(GdkEventButton *ev);
- std::vector<Gtk::Widget*> m_content_widgets;
+ std::vector<Gtk::Widget *> m_content_widgets;
Gtk::Box *m_main_box;
Gtk::Box *m_content_box;
diff --git a/discord/message.cpp b/discord/message.cpp
index b072ba8..70c557d 100644
--- a/discord/message.cpp
+++ b/discord/message.cpp
@@ -218,6 +218,7 @@ void from_json(const nlohmann::json &j, Message &m) {
m.ReferencedMessage = nullptr;
}
JS_O("interaction", m.Interaction);
+ JS_O("sticker_items", m.StickerItems);
}
void Message::from_json_edited(const nlohmann::json &j) {
@@ -244,6 +245,7 @@ void Message::from_json_edited(const nlohmann::json &j) {
JS_O("flags", Flags);
JS_O("stickers", Stickers);
JS_O("interaction", Interaction);
+ JS_O("sticker_items", StickerItems);
}
void Message::SetDeleted() {
diff --git a/discord/message.hpp b/discord/message.hpp
index e24f248..a6ea039 100644
--- a/discord/message.hpp
+++ b/discord/message.hpp
@@ -199,6 +199,7 @@ struct Message {
std::optional<std::vector<StickerData>> Stickers;
std::optional<std::shared_ptr<Message>> ReferencedMessage; // has_value && null means deleted
std::optional<MessageInteractionData> Interaction;
+ std::optional<std::vector<StickerItem>> StickerItems;
friend void from_json(const nlohmann::json &j, Message &m);
void from_json_edited(const nlohmann::json &j); // for MESSAGE_UPDATE
diff --git a/discord/sticker.cpp b/discord/sticker.cpp
index 8e50a47..b92d031 100644
--- a/discord/sticker.cpp
+++ b/discord/sticker.cpp
@@ -30,3 +30,23 @@ std::string StickerData::GetURL() const {
return "https://media.discordapp.net/stickers/" + std::to_string(ID) + "/" + *AssetHash + ".json";
return "";
}
+
+void to_json(nlohmann::json &j, const StickerItem &m) {
+ j["id"] = m.ID;
+ j["name"] = m.Name;
+ j["format_type"] = m.FormatType;
+}
+
+void from_json(const nlohmann::json &j, StickerItem &m) {
+ JS_D("id", m.ID);
+ JS_D("name", m.Name);
+ JS_D("format_type", m.FormatType);
+}
+
+std::string StickerItem::GetURL() const {
+ if (FormatType == StickerFormatType::PNG || FormatType == StickerFormatType::APNG)
+ return "https://media.discordapp.net/stickers/" + std::to_string(ID) + ".png?size=256";
+ else if (FormatType == StickerFormatType::LOTTIE)
+ return "https://media.discordapp.net/stickers/" + std::to_string(ID) + ".json";
+ return "";
+}
diff --git a/discord/sticker.hpp b/discord/sticker.hpp
index 89279b3..d23fe7b 100644
--- a/discord/sticker.hpp
+++ b/discord/sticker.hpp
@@ -27,3 +27,14 @@ struct StickerData {
std::string GetURL() const;
};
+
+struct StickerItem {
+ StickerFormatType FormatType;
+ Snowflake ID;
+ std::string Name;
+
+ friend void to_json(nlohmann::json &j, const StickerItem &m);
+ friend void from_json(const nlohmann::json &j, StickerItem &m);
+
+ std::string GetURL() const;
+};
diff --git a/discord/store.cpp b/discord/store.cpp
index fc40108..2439da5 100644
--- a/discord/store.cpp
+++ b/discord/store.cpp
@@ -264,6 +264,12 @@ void Store::SetMessage(Snowflake id, const Message &message) {
Bind(m_set_msg_stmt, 23, message.IsPending);
Bind(m_set_msg_stmt, 24, message.Nonce); // sorry
+ if (message.StickerItems.has_value()) {
+ std::string tmp = nlohmann::json(*message.StickerItems).dump();
+ Bind(m_set_msg_stmt, 25, tmp);
+ } else
+ Bind(m_set_msg_stmt, 25, nullptr);
+
if (!RunInsert(m_set_msg_stmt))
fprintf(stderr, "message insert failed: %s\n", sqlite3_errstr(m_db_err));
@@ -369,14 +375,18 @@ Message Store::GetMessageBound(sqlite3_stmt *stmt) const {
Get(stmt, 22, ret.IsPending);
Get(stmt, 23, ret.Nonce);
+ Get(stmt, 24, tmps);
+ if (tmps != "")
+ ret.StickerItems = nlohmann::json::parse(tmps).get<std::vector<StickerItem>>();
+
// interaction data from join
- if (!IsNull(stmt, 24)) {
+ if (!IsNull(stmt, 25)) {
auto &interaction = ret.Interaction.emplace();
- Get(stmt, 24, interaction.ID);
- Get(stmt, 25, interaction.Name);
- Get(stmt, 26, interaction.Type);
- Get(stmt, 27, interaction.User.ID);
+ Get(stmt, 25, interaction.ID);
+ Get(stmt, 26, interaction.Name);
+ Get(stmt, 27, interaction.Type);
+ Get(stmt, 28, interaction.User.ID);
}
Reset(stmt);
@@ -837,7 +847,8 @@ bool Store::CreateTables() {
deleted BOOL, /* extra */
edited BOOL, /* extra */
pending BOOL, /* extra */
- nonce TEXT
+ nonce TEXT,
+ sticker_items TEXT /* json */
)
)";
@@ -1056,7 +1067,7 @@ bool Store::CreateStatements() {
const char *set_msg = R"(
REPLACE INTO messages VALUES (
- ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
)";