Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: changed the way messages holds files #999

Merged
merged 6 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions include/dpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,26 @@ namespace cache_policy {

};

/**
* @brief The data for a file.
*/
struct message_file_data {
/**
* @brief Name of file to upload (for use server-side in discord's url).
*/
std::string name{};

/**
* @brief File content to upload (raw binary)
*/
std::string content{};

/**
* @brief Mime type of files to upload.
*/
std::string mimetype{};
};

/**
* @brief Represents messages sent and received on Discord
*/
Expand Down Expand Up @@ -1828,19 +1848,9 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
std::vector<sticker> stickers;

/**
* @brief Name of file to upload (for use server-side in discord's url).
*/
std::vector<std::string> filename;

/**
* @brief File content to upload (raw binary)
*/
std::vector<std::string> filecontent;

/**
* @brief Mime type of files to upload.
* @brief An array of file data.
*/
std::vector<std::string> filemimetype;
std::vector<message_file_data> file_data;

/**
* @brief Reference to another message, e.g. a reply
Expand Down
60 changes: 55 additions & 5 deletions src/dpp/cluster/appcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,51 +136,101 @@ void cluster::guild_commands_get(snowflake guild_id, command_completion_event_t
}

void cluster::interaction_response_create(snowflake interaction_id, const std::string &token, const interaction_response &r, command_completion_event_t callback) {
std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : r.msg.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/interactions", std::to_string(interaction_id), utility::url_encode(token) + "/callback", m_post, r.build_json(), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
callback(confirmation_callback_t(this, confirmation(), http));
}
}, r.msg.filename, r.msg.filecontent, r.msg.filemimetype);
}, file_names, file_contents, file_mimetypes);
}

void cluster::interaction_response_edit(const std::string &token, const message &m, command_completion_event_t callback) {
std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
Jaskowicz1 marked this conversation as resolved.
Show resolved Hide resolved
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/webhooks", std::to_string(me.id), utility::url_encode(token) + "/messages/@original", m_patch, m.build_json(), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
callback(confirmation_callback_t(this, confirmation(), http));
}
}, m.filename, m.filecontent, m.filemimetype);
}, file_names, file_contents, file_mimetypes);
}

void cluster::interaction_response_get_original(const std::string &token, command_completion_event_t callback) {
rest_request<message>(this, API_PATH "/webhooks",std::to_string(me.id), utility::url_encode(token) + "/messages/@original", m_get, "", callback);
}

void cluster::interaction_followup_create(const std::string &token, const message &m, command_completion_event_t callback) {
std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/webhooks", std::to_string(me.id), utility::url_encode(token), m_post, m.build_json(), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
callback(confirmation_callback_t(this, confirmation(), http));
}
}, m.filename, m.filecontent, m.filemimetype);
}, file_names, file_contents, file_mimetypes);
}

void cluster::interaction_followup_edit_original(const std::string &token, const message &m, command_completion_event_t callback) {
std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/webhooks", std::to_string(me.id), utility::url_encode(token) + "/messages/@original", m_patch, m.build_json(), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
callback(confirmation_callback_t(this, confirmation(), http));
}
}, m.filename, m.filecontent, m.filemimetype);
}, file_names, file_contents, file_mimetypes);
}

void cluster::interaction_followup_delete(const std::string &token, command_completion_event_t callback) {
rest_request<confirmation>(this, API_PATH "/webhooks",std::to_string(me.id), utility::url_encode(token) + "/messages/@original", m_delete, "", callback);
}

void cluster::interaction_followup_edit(const std::string &token, const message &m, command_completion_event_t callback) {
std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/webhooks", std::to_string(me.id), utility::url_encode(token) + "/messages/" + std::to_string(m.id), m_patch, m.build_json(), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
callback(confirmation_callback_t(this, confirmation(), http));
}
}, m.filename, m.filecontent, m.filemimetype);
}, file_names, file_contents, file_mimetypes);
}

void cluster::interaction_followup_get(const std::string &token, snowflake message_id, command_completion_event_t callback) {
Expand Down
24 changes: 22 additions & 2 deletions src/dpp/cluster/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ void cluster::message_add_reaction(snowflake message_id, snowflake channel_id, c


void cluster::message_create(const message &m, command_completion_event_t callback) {
std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/channels", std::to_string(m.channel_id), "messages", m_post, m.build_json(), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
callback(confirmation_callback_t(this, message(this).fill_from_json(&j), http));
}
}, m.filename, m.filecontent, m.filemimetype);
}, file_names, file_contents, file_mimetypes);
}


Expand Down Expand Up @@ -112,11 +122,21 @@ void cluster::message_delete_reaction_emoji(snowflake message_id, snowflake chan


void cluster::message_edit(const message &m, command_completion_event_t callback) {
std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/channels", std::to_string(m.channel_id), "messages/" + std::to_string(m.id), m_patch, m.build_json(true), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
callback(confirmation_callback_t(this, message(this).fill_from_json(&j), http));
}
}, m.filename, m.filecontent, m.filemimetype);
}, file_names, file_contents, file_mimetypes);
}


Expand Down
13 changes: 12 additions & 1 deletion src/dpp/cluster/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ void cluster::thread_create_in_forum(const std::string& thread_name, snowflake c
j["auto_archive_duration"] = 10080;
break;
}

std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : msg.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/channels", std::to_string(channel_id), "threads", m_post, j.dump(), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
auto t = thread().fill_from_json(&j);
Expand All @@ -122,7 +133,7 @@ void cluster::thread_create_in_forum(const std::string& thread_name, snowflake c
}
callback(confirmation_callback_t(this, t, http));
}
}, msg.filename, msg.filecontent, msg.filemimetype);
}, file_names, file_contents, file_mimetypes);
}

void cluster::thread_create(const std::string& thread_name, snowflake channel_id, uint16_t auto_archive_duration, channel_type thread_type, bool invitable, uint16_t rate_limit_per_user, command_completion_event_t callback)
Expand Down
26 changes: 24 additions & 2 deletions src/dpp/cluster/webhook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,22 @@ void cluster::edit_webhook_message(const class webhook &wh, const struct message
std::string parameters = utility::make_url_parameters({
{"thread_id", thread_id},
});

std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/webhooks", std::to_string(wh.id), utility::url_encode(!wh.token.empty() ? wh.token: token) + "/messages/" + std::to_string(m.id) + parameters, m_patch, m.build_json(false), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
callback(confirmation_callback_t(this, message(this).fill_from_json(&j), http));
}
}, m.filename, m.filecontent, m.filemimetype);
}, file_names, file_contents, file_mimetypes);
}

void cluster::edit_webhook_with_token(const class webhook& wh, command_completion_event_t callback) {
Expand Down Expand Up @@ -84,11 +95,22 @@ void cluster::execute_webhook(const class webhook &wh, const struct message& m,
}
body = j.dump();
}

std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

this->post_rest_multipart(API_PATH "/webhooks", std::to_string(wh.id), utility::url_encode(!wh.token.empty() ? wh.token : token) + parameters, m_post, !body.empty() ? body : m.build_json(false), [this, callback](json &j, const http_request_completion_t& http) {
if (callback) {
callback(confirmation_callback_t(this, message(this).fill_from_json(&j), http));
}
}, m.filename, m.filecontent, m.filemimetype);
}, file_names, file_contents, file_mimetypes);
}

void cluster::get_channel_webhooks(snowflake channel_id, command_completion_event_t callback) {
Expand Down
12 changes: 11 additions & 1 deletion src/dpp/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,21 @@ void interaction_create_t::get_original_response(command_completion_event_t call
}

void interaction_create_t::edit_original_response(const message& m, command_completion_event_t callback) const {
std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

from->creator->post_rest_multipart(API_PATH "/webhooks", std::to_string(command.application_id), command.token + "/messages/@original", m_patch, m.build_json(), [creator = this->from->creator, cb = std::move(callback)](json& j, const http_request_completion_t& http) {
if (cb) {
cb(confirmation_callback_t(creator, message().fill_from_json(&j), http));
}
}, m.filename, m.filecontent, m.filemimetype);
}, file_names, file_contents, file_mimetypes);
}

void interaction_create_t::delete_original_response(command_completion_event_t callback) const {
Expand Down
35 changes: 22 additions & 13 deletions src/dpp/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,30 +559,39 @@ message& message::set_type(message_type t)
return *this;
}

message& message::set_filename(const std::string &fn)
{
if (filename.empty()) {
filename.push_back(fn);
message& message::set_filename(const std::string &fn) {
if (file_data.empty()) {
message_file_data data;
data.name = fn;

file_data.push_back(data);
} else {
filename[filename.size() - 1] = fn;
file_data[file_data.size() - 1].name = fn;
}

return *this;
}

message& message::set_file_content(const std::string &fc)
{
if (filecontent.empty()) {
filecontent.push_back(fc);
message& message::set_file_content(const std::string &fc) {
if (file_data.empty()) {
message_file_data data;
data.content = fc;

file_data.push_back(data);
} else {
filecontent[filecontent.size() - 1] = fc;
file_data[file_data.size() - 1].content = fc;
}

return *this;
}

message& message::add_file(const std::string &fn, const std::string &fc, const std::string &fm) {
filename.push_back(fn);
filecontent.push_back(fc);
filemimetype.push_back(fm);
message_file_data data;
data.name = fn;
data.content = fc;
data.mimetype = fm;

file_data.push_back(data);
return *this;
}

Expand Down