start attachments (image paste and upload)

This commit is contained in:
ouwou
2022-06-05 21:41:57 -04:00
parent 4ec5c1dfcc
commit 270730d9b3
15 changed files with 418 additions and 54 deletions

View File

@@ -424,12 +424,32 @@ void DiscordClient::ChatMessageCallback(const std::string &nonce, const http::re
}
}
void DiscordClient::SendChatMessage(const std::string &content, Snowflake channel) {
void DiscordClient::SendChatMessageAttachments(const std::string &content, const std::vector<std::string> &attachment_paths, Snowflake channel, Snowflake referenced_message) {
const auto nonce = std::to_string(Snowflake::FromNow());
CreateMessageObject obj;
obj.Content = content;
obj.Nonce = nonce;
if (referenced_message.IsValid())
obj.MessageReference.emplace().MessageID = referenced_message;
auto req = m_http.CreateRequest(http::REQUEST_POST, "/channels/" + std::to_string(channel) + "/messages");
req.make_form();
req.add_field("payload_json", nlohmann::json(obj).dump().c_str(), CURL_ZERO_TERMINATED);
for (size_t i = 0; i < attachment_paths.size(); i++) {
const auto field_name = "files[" + std::to_string(i) + "]";
req.add_file(field_name, attachment_paths.at(i), "unknown.png");
}
m_http.Execute(std::move(req), sigc::bind<0>(sigc::mem_fun(*this, &DiscordClient::ChatMessageCallback), nonce));
}
void DiscordClient::SendChatMessageText(const std::string &content, Snowflake channel, Snowflake referenced_message) {
// @([^@#]{1,32})#(\\d{4})
const auto nonce = std::to_string(Snowflake::FromNow());
CreateMessageObject obj;
obj.Content = content;
obj.Nonce = nonce;
if (referenced_message.IsValid())
obj.MessageReference.emplace().MessageID = referenced_message;
m_http.MakePOST("/channels/" + std::to_string(channel) + "/messages", nlohmann::json(obj).dump(), sigc::bind<0>(sigc::mem_fun(*this, &DiscordClient::ChatMessageCallback), nonce));
// dummy data so the content can be shown while waiting for MESSAGE_CREATE
Message tmp;
@@ -448,27 +468,22 @@ void DiscordClient::SendChatMessage(const std::string &content, Snowflake channe
m_signal_message_sent.emit(tmp);
}
void DiscordClient::SendChatMessage(const std::string &content, Snowflake channel, Snowflake referenced_message) {
const auto nonce = std::to_string(Snowflake::FromNow());
CreateMessageObject obj;
obj.Content = content;
obj.Nonce = nonce;
obj.MessageReference.emplace().MessageID = referenced_message;
m_http.MakePOST("/channels/" + std::to_string(channel) + "/messages", nlohmann::json(obj).dump(), sigc::bind<0>(sigc::mem_fun(*this, &DiscordClient::ChatMessageCallback), nonce));
Message tmp;
tmp.Content = content;
tmp.ID = nonce;
tmp.ChannelID = channel;
tmp.Author = GetUserData();
tmp.IsTTS = false;
tmp.DoesMentionEveryone = false;
tmp.Type = MessageType::DEFAULT;
tmp.IsPinned = false;
tmp.Timestamp = "2000-01-01T00:00:00.000000+00:00";
tmp.Nonce = obj.Nonce;
tmp.IsPending = true;
m_store.SetMessage(tmp.ID, tmp);
m_signal_message_sent.emit(tmp);
void DiscordClient::SendChatMessage(const std::string &content, const std::vector<std::string> &attachment_paths, Snowflake channel) {
if (attachment_paths.empty())
SendChatMessageText(content, channel);
else {
puts("attach");
SendChatMessageAttachments(content, attachment_paths, channel, Snowflake::Invalid);
}
}
void DiscordClient::SendChatMessage(const std::string &content, const std::vector<std::string> &attachment_paths, Snowflake channel, Snowflake referenced_message) {
if (attachment_paths.empty())
SendChatMessageText(content, channel, referenced_message);
else {
puts("attach");
SendChatMessageAttachments(content, attachment_paths, channel, referenced_message);
}
}
void DiscordClient::DeleteMessage(Snowflake channel_id, Snowflake id) {

View File

@@ -104,8 +104,8 @@ public:
void ChatMessageCallback(const std::string &nonce, const http::response_type &response);
void SendChatMessage(const std::string &content, Snowflake channel);
void SendChatMessage(const std::string &content, Snowflake channel, Snowflake referenced_message);
void SendChatMessage(const std::string &content, const std::vector<std::string> &attachment_paths, Snowflake channel);
void SendChatMessage(const std::string &content, const std::vector<std::string> &attachment_paths, Snowflake channel, Snowflake referenced_message);
void DeleteMessage(Snowflake channel_id, Snowflake id);
void EditMessage(Snowflake channel_id, Snowflake id, std::string content);
void SendLazyLoad(Snowflake id);
@@ -223,6 +223,9 @@ private:
std::vector<uint8_t> m_decompress_buf;
z_stream m_zstream;
void SendChatMessageAttachments(const std::string &content, const std::vector<std::string> &attachment_paths, Snowflake channel, Snowflake referenced_message = Snowflake::Invalid);
void SendChatMessageText(const std::string &content, Snowflake channel, Snowflake referenced_message = Snowflake::Invalid);
static std::string GetAPIURL();
static std::string GetGatewayURL();

View File

@@ -112,6 +112,25 @@ void HTTPClient::MakeGET(const std::string &path, const std::function<void(http:
}));
}
http::request HTTPClient::CreateRequest(http::EMethod method, std::string path) {
http::request req(method, m_api_base + path);
req.set_header("Authorization", m_authorization);
req.set_user_agent(!m_agent.empty() ? m_agent : "Abaddon");
#ifdef USE_LOCAL_PROXY
req.set_proxy("http://127.0.0.1:8888");
req.set_verify_ssl(false);
#endif
return req;
}
void HTTPClient::Execute(http::request &&req, const std::function<void(http::response_type r)> &cb) {
printf("%s %s\n", req.get_method(), req.get_url().c_str());
m_futures.push_back(std::async(std::launch::async, [this, cb, req = std::move(req)]() mutable {
auto res = req.execute();
OnResponse(res, cb);
}));
}
void HTTPClient::CleanupFutures() {
for (auto it = m_futures.begin(); it != m_futures.end();) {
if (it->wait_for(std::chrono::seconds(0)) == std::future_status::ready)

View File

@@ -23,6 +23,9 @@ public:
void MakePOST(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb);
void MakePUT(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb);
[[nodiscard]] http::request CreateRequest(http::EMethod method, std::string path);
void Execute(http::request &&req, const std::function<void(http::response_type r)> &cb);
private:
void OnResponse(const http::response_type &r, const std::function<void(http::response_type r)> &cb);
void CleanupFutures();