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

Add support for uploading files from memory + bug fixes #168

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ if(AUTO_DOWNLOAD_LIBRARY)
endif()

if(USE_ASIO)
# Note websocketpp doesn't work with the latest version of asio
# Remember to go back to the master version after they are fixed
download_project(
PROJ asio
GIT_REPOSITORY https://github.com/chriskohlhoff/asio.git
GIT_TAG 22afb86
GIT_TAG master
SOURCE_DIR ${PROJECT_SOURCE_DIR}/deps/asio
UPDATE_DISCONNECTED 1
)
Expand Down Expand Up @@ -167,6 +165,7 @@ if(USE_CPR)
set(CMAKE_USE_OPENSSL OFF CACHE BOOL "")
endif()
endif()
set(CMAKE_USE_OPENSSL ON)
if(NOT DEFINED USE_SYSTEM_CURL AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
#using cmake to configure curl on linux has issues
#so use system curl
Expand All @@ -189,19 +188,19 @@ endif()
# Get Version Info
if(Git_FOUND)
execute_process(
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD
OUTPUT_VARIABLE SLEEPY_DISCORD_VERSION_BUILD
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
OUTPUT_VARIABLE SLEEPY_DISCORD_VERSION_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
OUTPUT_VARIABLE SLEEPY_DISCORD_VERSION_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
Expand All @@ -212,7 +211,7 @@ if(Git_FOUND)
set(SLEEPY_DISCORD_VERSION_IS_MASTER "0")
endif()
execute_process(
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND ${GIT_EXECUTABLE} describe --always --long --dirty
OUTPUT_VARIABLE SLEEPY_DISCORD_VERSION_DESCRIPTION
OUTPUT_STRIP_TRAILING_WHITESPACE
Expand Down
8 changes: 4 additions & 4 deletions examples/wasm_example/wasm_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ class WebSession : public SleepyDiscord::GenericSession {
}
inline void setUrl(const std::string& _url) { url = &_url; }
inline void setBody(const std::string* jsonParamters) { body = jsonParamters; }
inline void setHeader(const std::vector<SleepyDiscord::HeaderPair>& _header) {
inline void setHeader(const std::vector<SleepyDiscord::HeaderPair>& _header) {
header.reserve(_header.size() * 2);
for (unsigned int i = 0; i < _header.size(); ++i) {
header.push_back(_header[i].name);
header.push_back(_header[i].value.data());
}
}
inline void setMultipart(const std::initializer_list<SleepyDiscord::Part>& parts) {}
inline void setMultipart(const std::vector<SleepyDiscord::Part>& parts) {}
inline SleepyDiscord::Response Post () { return request(SleepyDiscord::Post ); }
inline SleepyDiscord::Response Patch () { return request(SleepyDiscord::Patch ); }
inline SleepyDiscord::Response Delete() { return request(SleepyDiscord::Delete); }
Expand All @@ -79,5 +79,5 @@ class WebSession : public SleepyDiscord::GenericSession {
};

//init the custom session
SleepyDiscord::CustomInit SleepyDiscord::Session::init =
[]()->SleepyDiscord::GenericSession* { return new WebSession; };
SleepyDiscord::CustomInit SleepyDiscord::Session::init =
[]()->SleepyDiscord::GenericSession* { return new WebSession; };
1 change: 1 addition & 0 deletions include/sleepy_discord/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ namespace SleepyDiscord {
ObjectResponse<Message > sendMessage (Snowflake<Channel> channelID, std::string message, Embed embed = Embed::Flag::INVALID_EMBED, bool tts = false, RequestSettings<ObjectResponse<Message>> settings = {});
ObjectResponse<Message > sendMessage (SendMessageParams params , RequestSettings<ObjectResponse<Message>> settings = {});
ObjectResponse<Message > uploadFile (Snowflake<Channel> channelID, std::string fileLocation, std::string message , RequestSettings<ObjectResponse<Message>> settings = {});
ObjectResponse<Message > uploadFile (Snowflake<Channel> channelID, Buffer buffer, std::string message, std::string filename , RequestSettings<ObjectResponse<Message>> settings = {});
BoolResponse addReaction (Snowflake<Channel> channelID, Snowflake<Message> messageID, std::string emoji , RequestSettings<BoolResponse > settings = {});
BoolResponse removeReaction (Snowflake<Channel> channelID, Snowflake<Message> messageID, std::string emoji, Snowflake<User> userID = "@me");
ArrayResponse <Reaction > getReactions (Snowflake<Channel> channelID, Snowflake<Message> messageID, std::string emoji , RequestSettings<ArrayResponse<Reaction>> settings = {});
Expand Down
9 changes: 9 additions & 0 deletions include/sleepy_discord/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <utility>
#include <map>
#include <vector>
#include <memory>
#include <functional>
#include "error.h"

Expand Down Expand Up @@ -42,14 +43,22 @@ namespace SleepyDiscord {
const std::string filePath;
};

struct Buffer {
std::shared_ptr<uint8_t> data = nullptr;
size_t length = 0;
};

struct Part {
Part(const std::string _name, const std::string _value) :
name(_name), value(_value), isFile(false) {}
Part(const std::string _name, const filePathPart _file) :
name(_name), value(_file.filePath), isFile(true) {}
Part(const std::string _name, Buffer buffer) :
name(_name), value{}, isFile(false), buffer(buffer) {}
const std::string name;
const std::string value;
const bool isFile; //if isFile is true then value is the filepath
const Buffer buffer;
};

typedef std::initializer_list<Part> Multipart;
Expand Down
7 changes: 4 additions & 3 deletions include/sleepy_discord/json_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include <list>
#include <utility>
#include <vector>
#include <array>
#include <vector>
#include <tuple>
//for errrors
#include <iostream>
Expand All @@ -12,6 +12,7 @@ typedef std::size_t SizeType;
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/istreamwrapper.h"
#include "nonstd/string_view.hpp"
//#include "json.h"

Expand Down Expand Up @@ -229,7 +230,7 @@ namespace SleepyDiscord {
for (const typename Container::value_type& value : values)
arr.PushBack(TypeHelper<typename Container::value_type>::fromType(value, allocator), allocator);
return arr;
}
}
};

template<class Container, template<class...> class TypeHelper>
Expand Down Expand Up @@ -316,7 +317,7 @@ namespace SleepyDiscord {
object.*(field.member) = Helper::toType(iterator->value);
} else if (field.type == REQUIRIED_FIELD) {
//error
std::cout <<
std::cout <<
"JSON Parse Error: "
"variable #" << i << ": \"" << field.name << "\" not found. "
"Please look at call stack from your debugger for more details.";
Expand Down
6 changes: 3 additions & 3 deletions include/sleepy_discord/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace SleepyDiscord {
);
JSONStructEnd
};

struct Reaction : public DiscordObject {
public:
Reaction() = default;
Expand Down Expand Up @@ -141,9 +141,9 @@ namespace SleepyDiscord {
assert(outOfDateMessage.ID == messageID);
json::fromJSON(outOfDateMessage, RevisionsJSON);
}
const json::Value& RevisionsJSON;
Snowflake<Message> messageID;
Snowflake<Channel> channelID;
const json::Value& RevisionsJSON;
};

struct SendMessageParams : public DiscordObject {
Expand All @@ -160,4 +160,4 @@ namespace SleepyDiscord {
);
JSONStructEnd
};
}
}
37 changes: 31 additions & 6 deletions include/sleepy_discord/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace SleepyDiscord {
enum Permission : int64_t;
struct Role;

/*Guild Member Structure
Field Type Description
user object user object
Expand Down Expand Up @@ -49,6 +49,28 @@ namespace SleepyDiscord {
JSONStructEnd
};

enum class Unavailable {
NOT_PRESENT = -1,
FALSE = 0,
TRUE = 1,
};

namespace json {
template<>
struct EnumTypeHelper<Unavailable> {
static inline Unavailable toType(const json::Value& value) {
Unavailable unavailable = static_cast<Unavailable>(json::toBool(value));
return unavailable;
}

static inline json::Value fromType(
const Unavailable& value, json::Value::AllocatorType&
) {
return json::Value(value != Unavailable::FALSE);
}
};
}

struct Server : public IdentifiableDiscordObject<Server> {
//~Server();
Server() = default;
Expand All @@ -72,12 +94,12 @@ namespace SleepyDiscord {
//voice_states
//emojis
//features
bool unavailable;
Unavailable unavailable = Unavailable::NOT_PRESENT;

//presences
int MFALevel;
std::string joinedAt;

//those are only filled in from the onServer event
bool large;

Expand Down Expand Up @@ -105,7 +127,7 @@ namespace SleepyDiscord {
json::pair (&Server::verificationLevel , "verification_level" , json::OPTIONAL_FIELD ),
json::pair (&Server::defaultMessageNotifications, "default_message_notifications", json::OPTIONAL_FIELD ),
json::pair<json::ContainerTypeHelper>(&Server::roles , "roles" , json::OPTIONAL_FIELD ),
json::pair (&Server::unavailable , "unavailable" , json::OPTIONAL_FIELD ),
json::pair<json::EnumTypeHelper >(&Server::unavailable , "unavailable" , json::OPTIONAL_FIELD ),
json::pair (&Server::MFALevel , "mfa_level" , json::OPTIONAL_FIELD ),
json::pair (&Server::joinedAt , "joined_at" , json::OPTIONAL_FIELD ),
json::pair (&Server::large , "large" , json::OPTIONAL_FIELD ),
Expand All @@ -122,10 +144,13 @@ namespace SleepyDiscord {
UnavailableServer(const json::Value& json);
//UnavailableServer(const json::Values values);

Unavailable unavailable = Unavailable::NOT_PRESENT;

//const static std::initializer_list<const char*const> fields;
JSONStructStart
std::make_tuple(
json::pair(&UnavailableServer::ID, "id", json::REQUIRIED_FIELD)
json::pair (&UnavailableServer::ID, "id" , json::REQUIRIED_FIELD),
json::pair<json::EnumTypeHelper>(&UnavailableServer::unavailable, "unavailable", json::OPTIONAL_FIELD )
);
JSONStructEnd
};
Expand Down Expand Up @@ -179,4 +204,4 @@ namespace SleepyDiscord {
);
JSONStructEnd
};
}
}
4 changes: 2 additions & 2 deletions sleepy_discord/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ if (NOT ONLY_SLEEPY_DISCORD)
set(libsodium-make_BINARY_DIR ${BINARY_DIR})
message(WARNING ${libsodium-make_BINARY_DIR})
set(
LIB_SODIUM_BUILDS
LIB_SODIUM_BUILDS
${libsodium-make_BINARY_DIR}/src/libsodium/.libs/${CMAKE_STATIC_LIBRARY_PREFIX}sodium${CMAKE_STATIC_LIBRARY_SUFFIX}
)
target_include_directories(
Expand All @@ -145,4 +145,4 @@ endif()

if(ENABLE_VOICE)
target_compile_definitions(sleepy-discord PUBLIC SLEEPY_VOICE_ENABLED)
endif()
endif()
Loading