Skip to content

Commit

Permalink
Created get_url methods for message, channel and user. (brainboxdotcc…
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslan-ilesik authored and Mishura4 committed Sep 17, 2023
1 parent e46ca56 commit 5dca7eb
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/dpp/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,13 @@ class DPP_EXPORT channel : public managed, public json_interface<channel> {
*/
std::string get_icon_url(uint16_t size = 0, const image_type format = i_png) const;

/**
* @brief Returns string of URL to channel
*
* @return string of URL to channel
*/
std::string get_url() const;

/**
* @brief Returns true if the channel is NSFW gated
*
Expand Down
7 changes: 7 additions & 0 deletions include/dpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,13 @@ struct DPP_EXPORT message : public managed {
* @return true if message has remixed attachment
*/
bool has_remix_attachment() const;

/**
* @brief Returns URL to message
*
* @return string of URL to message
*/
std::string get_url() const;
};

/** A group of messages */
Expand Down
7 changes: 7 additions & 0 deletions include/dpp/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ class DPP_EXPORT user : public managed, public json_interface<user> {
*/
std::string get_mention() const;

/**
* @brief Returns URL to user
*
* @return string of URL to user
*/
std::string get_url() const;

/**
* @brief Return true if user has the active Developer badge
*
Expand Down
39 changes: 39 additions & 0 deletions include/dpp/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ namespace dpp {
*/
inline const std::string cdn_host = "https://cdn.discordapp.com";

/**
* @brief The base URL for message/user/channel links.
*/
inline const std::string url_host = "https://discord.com";

/**
* @brief Callback for the results of a command executed via dpp::utility::exec
*/
Expand Down Expand Up @@ -531,6 +536,40 @@ namespace dpp {
*/
std::string DPP_EXPORT role_mention(const snowflake& id);

/**
* @brief Create a URL for message.
* @param guild_id The ID of the guild where message is written.
* @param channel_id The ID of the channel where message is written.
* @param message_id The ID of the message.
* @return std::string The URL to message or empty string if any of ids is 0.
*/
std::string DPP_EXPORT message_url(const snowflake& guild_id, const snowflake& channel_id, const snowflake& message_id);

/**
* @brief Create a URL for message.
* @param guild_id The ID of the guild where channel is located.
* @param channel_id The ID of the channel.
* @return std::string The URL to message or empty string if any of ids is 0.
*/
std::string DPP_EXPORT channel_url(const snowflake& guild_id, const snowflake& channel_id);

/**
* @brief Create a URL for message.
* @param guild_id The ID of the guild where thread is located.
* @param thread_id The ID of the thread.
* @return std::string The URL to message or empty string if any of ids is 0.
*/
std::string DPP_EXPORT thread_url(const snowflake& guild_id, const snowflake& thread_id);

/**
* @brief Create a URL for message.
* @param user_id The ID of the guild where thread is located.
* @return std::string The URL to message or empty string if id is 0.
*/
std::string DPP_EXPORT user_url(const snowflake& user_id);



#ifdef _DOXYGEN_
/**
* @brief Get the mime type for an image type.
Expand Down
4 changes: 4 additions & 0 deletions src/dpp/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ std::string channel::get_icon_url(uint16_t size, const image_type format) const
}
}

std::string channel::get_url() const{
return utility::channel_url(guild_id, id);
}

channel_type channel::get_type() const {
return static_cast<channel_type>(flags & CHANNEL_TYPE_MASK);
}
Expand Down
4 changes: 4 additions & 0 deletions src/dpp/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,10 @@ bool message::has_remix_attachment() const {
});
}

std::string message::get_url() const {
return utility::message_url(guild_id, channel_id, id);
}

sticker::sticker() : managed(0), pack_id(0), type(st_standard), format_type(sf_png), available(true), guild_id(0), sort_value(0) {
}

Expand Down
4 changes: 4 additions & 0 deletions src/dpp/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ std::string user::get_mention() const {
return utility::user_mention(id);
}

std::string user::get_url() const{
return utility::user_url(id);
}

bool user::is_active_developer() const {
return this->flags & u_active_developer;
}
Expand Down
25 changes: 25 additions & 0 deletions src/dpp/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,31 @@ namespace dpp {
return "<@&" + std::to_string(id) + ">";
}

std::string message_url(const snowflake& guild_id, const snowflake& channel_id, const snowflake& message_id){
if (guild_id.empty() || channel_id.empty() || message_id.empty()) {
return "";
}
return url_host + "/channels/" + std::to_string(guild_id) + "/" + std::to_string(channel_id) + "/" + std::to_string(message_id);
}

std::string channel_url(const snowflake& guild_id, const snowflake& channel_id){
if (guild_id.empty() || channel_id.empty()) {
return "";
}
return url_host + "/channels/" + std::to_string(guild_id) + "/" + std::to_string(channel_id);
}

std::string thread_url(const snowflake& guild_id, const snowflake& thread_id){
return channel_url(guild_id, thread_id);
};

std::string user_url(const snowflake& user_id){
if (user_id.empty()) {
return "";
}
return url_host + "/users/" + std::to_string(user_id);
};

template <typename T>
std::enable_if_t<std::is_same_v<T, image_type>, std::string> mime_type(T type) {
static constexpr auto get_image_mime = [](image_type t) constexpr noexcept {
Expand Down
106 changes: 106 additions & 0 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b

set_test(USER_GET_CREATION_TIME, false);
set_test(USER_GET_CREATION_TIME, (uint64_t)user1.get_creation_time() == 1465312605);

set_test(USER_GET_URL, false);

dpp::user user2;
set_test(USER_GET_URL,
user1.get_url() == dpp::utility::url_host + "/users/189759562910400512" &&
user2.get_url() == ""
);
}

{ // avatar size function
Expand Down Expand Up @@ -422,6 +430,48 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
set_test(EMOJI_GET_URL, emoji.get_url() == dpp::utility::cdn_host + "/emojis/825407338755653641.png");
}

{ // message methods
dpp::message m;
m.guild_id = 825407338755653642;
m.channel_id = 956230231277072415;
m.id = 1151617986541666386;

dpp::message m2;
m2.guild_id = 825407338755653642;
m2.channel_id = 956230231277072415;

dpp::message m3;
m3.guild_id = 825407338755653642;
m3.id = 1151617986541666386;

dpp::message m4;
m4.channel_id = 956230231277072415;
m4.id = 1151617986541666386;

dpp::message m5;
m5.guild_id = 825407338755653642;

dpp::message m6;
m6.channel_id = 956230231277072415;

dpp::message m7;
m7.id = 1151617986541666386;

dpp::message m8;

set_test(MESSAGE_GET_URL, false);
set_test(MESSAGE_GET_URL,
m.get_url() == dpp::utility::url_host + "/channels/825407338755653642/956230231277072415/1151617986541666386" &&
m2.get_url() == "" &&
m3.get_url() == "" &&
m4.get_url() == "" &&
m5.get_url() == "" &&
m6.get_url() == "" &&
m7.get_url() == "" &&
m8.get_url() == ""
);
}

{ // channel methods
set_test(CHANNEL_SET_TYPE, false);
dpp::channel c;
Expand All @@ -435,6 +485,24 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
set_test(CHANNEL_GET_MENTION, false);
c.id = 825411707521728511;
set_test(CHANNEL_GET_MENTION, c.get_mention() == "<#825411707521728511>");

set_test(CHANNEL_GET_URL, false);
c.guild_id = 825407338755653642;

dpp::channel c2;
c2.id = 825411707521728511;

dpp::channel c3;
c3.guild_id = 825407338755653642;

dpp::channel c4;

set_test(CHANNEL_GET_URL,
c.get_url() == dpp::utility::url_host + "/channels/825407338755653642/825411707521728511" &&
c2.get_url() == "" &&
c3.get_url() == "" &&
c4.get_url() == ""
);
}

{ // utility methods
Expand Down Expand Up @@ -507,6 +575,44 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
emoji_mention3 == ":white_check_mark:" &&
emoji_mention4 == ":white_check_mark:"
);

set_test(UTILITY_USER_URL, false);
auto user_url = dpp::utility::user_url(123);
set_test(UTILITY_USER_URL,
user_url == dpp::utility::url_host + "/users/123" &&
dpp::utility::user_url(0) == ""
);

set_test(UTILITY_MESSAGE_URL, false);
auto message_url = dpp::utility::message_url(1,2,3);
set_test(UTILITY_MESSAGE_URL,
message_url == dpp::utility::url_host+ "/channels/1/2/3" &&
dpp::utility::message_url(0,2,3) == "" &&
dpp::utility::message_url(1,0,3) == "" &&
dpp::utility::message_url(1,2,0) == "" &&
dpp::utility::message_url(0,0,3) == "" &&
dpp::utility::message_url(0,2,0) == "" &&
dpp::utility::message_url(1,0,0) == "" &&
dpp::utility::message_url(0,0,0) == ""
);

set_test(UTILITY_CHANNEL_URL, false);
auto channel_url = dpp::utility::channel_url(1,2);
set_test(UTILITY_CHANNEL_URL,
channel_url == dpp::utility::url_host+ "/channels/1/2" &&
dpp::utility::channel_url(0,2) == "" &&
dpp::utility::channel_url(1,0) == "" &&
dpp::utility::channel_url(0,0) == ""
);

set_test(UTILITY_THREAD_URL, false);
auto thread_url = dpp::utility::thread_url(1,2);
set_test(UTILITY_THREAD_URL,
thread_url == dpp::utility::url_host+ "/channels/1/2" &&
dpp::utility::thread_url(0,2) == "" &&
dpp::utility::thread_url(1,0) == "" &&
dpp::utility::thread_url(0,0) == ""
);
}

#ifndef _WIN32
Expand Down
7 changes: 7 additions & 0 deletions src/unittest/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,14 @@ DPP_TEST(USER_GET, "cluster::user_get", tf_online);
DPP_TEST(USER_GET_FLAGS, "cluster::user_get flag parsing", tf_online);
DPP_TEST(MEMBER_GET, "cluster::guild_get_member", tf_online);
DPP_TEST(USER_GET_MENTION, "user::get_mention", tf_offline);
DPP_TEST(USER_GET_URL, "user::get_url", tf_offline);
DPP_TEST(USER_FORMAT_USERNAME, "user::format_username", tf_offline);
DPP_TEST(USER_GET_CREATION_TIME, "user::get_creation_time", tf_offline);
DPP_TEST(USER_GET_AVATAR_URL, "user::get_avatar_url", tf_offline);
DPP_TEST(CHANNEL_SET_TYPE, "channel::set_type", tf_offline);
DPP_TEST(CHANNEL_GET_MENTION, "channel::get_mention", tf_offline);
DPP_TEST(CHANNEL_GET_URL, "channel::get_url", tf_offline);
DPP_TEST(MESSAGE_GET_URL,"message::get_url",tf_offline);
DPP_TEST(UTILITY_GUILD_NAVIGATION, "utility::guild_navigation", tf_offline);
DPP_TEST(UTILITY_ICONHASH, "utility::iconhash", tf_offline);
DPP_TEST(UTILITY_MAKE_URL_PARAMETERS, "utility::make_url_parameters", tf_offline);
Expand All @@ -185,6 +188,10 @@ DPP_TEST(UTILITY_CHANNEL_MENTION, "utility::channel_mention", tf_offline);
DPP_TEST(UTILITY_USER_MENTION, "utility::user_mention", tf_offline);
DPP_TEST(UTILITY_ROLE_MENTION, "utility::role_mention", tf_offline);
DPP_TEST(UTILITY_EMOJI_MENTION, "utility::emoji_mention", tf_offline);
DPP_TEST(UTILITY_USER_URL, "utility::user_url", tf_offline);
DPP_TEST(UTILITY_MESSAGE_URL, "utility::message_url", tf_offline);
DPP_TEST(UTILITY_CHANNEL_URL, "utility::channel_url", tf_offline);
DPP_TEST(UTILITY_THREAD_URL, "utility::thread_url", tf_offline);
DPP_TEST(UTILITY_AVATAR_SIZE, "utility::avatar_size", tf_offline);
DPP_TEST(UTILITY_CDN_ENDPOINT_URL_HASH, "utility::cdn_endpoint_url_hash", tf_offline);
DPP_TEST(STICKER_GET_URL, "sticker::get_url aka utility::cdn_endpoint_url_sticker", tf_offline);
Expand Down

0 comments on commit 5dca7eb

Please sign in to comment.