diff --git a/include/dpp/utility.h b/include/dpp/utility.h index b7778f25f3..487a90d1de 100644 --- a/include/dpp/utility.h +++ b/include/dpp/utility.h @@ -541,7 +541,7 @@ namespace dpp { * @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. + * @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); @@ -549,7 +549,7 @@ namespace dpp { * @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. + * @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); @@ -557,14 +557,14 @@ namespace dpp { * @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. + * @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. + * @return std::string The URL to message or empty string if id is 0. */ std::string DPP_EXPORT user_url(const snowflake& user_id); diff --git a/src/dpp/utility.cpp b/src/dpp/utility.cpp index 7f0850b7b8..0cea7bd544 100644 --- a/src/dpp/utility.cpp +++ b/src/dpp/utility.cpp @@ -551,10 +551,16 @@ namespace dpp { } std::string message_url(const snowflake& guild_id, const snowflake& channel_id, const snowflake& message_id){ + if (guild_id == snowflake(0) || channel_id == snowflake(0) || message_id == snowflake(0)) { + 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 == snowflake(0) || channel_id == snowflake(0)) { + return ""; + } return url_host + "/channels/" + std::to_string(guild_id) + "/" + std::to_string(channel_id); } @@ -563,6 +569,9 @@ namespace dpp { }; std::string user_url(const snowflake& user_id){ + if (user_id == snowflake(0)) { + return ""; + } return url_host + "/users/" + std::to_string(user_id); }; diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index 9da69d3575..f3adb662f9 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -345,7 +345,12 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b set_test(USER_GET_CREATION_TIME, (uint64_t)user1.get_creation_time() == 1465312605); set_test(USER_GET_URL, false); - set_test(USER_GET_URL, user1.get_url() == dpp::utility::url_host + "/users/189759562910400512"); + + dpp::user user2; + set_test(USER_GET_URL, + user1.get_url() == dpp::utility::url_host + "/users/189759562910400512" && + user2.get_url() == "" + ); } { // avatar size function @@ -431,8 +436,40 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b 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"); + 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 @@ -451,7 +488,21 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b set_test(CHANNEL_GET_URL, false); c.guild_id = 825407338755653642; - set_test(CHANNEL_GET_URL, c.get_url() == dpp::utility::url_host + "/channels/825407338755653642/825411707521728511"); + + 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 @@ -527,19 +578,41 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b 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"); + 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"); + 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::thread_url(1,2); - set_test(UTILITY_CHANNEL_URL, channel_url == dpp::utility::url_host+ "/channels/1/2"); + 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"); + 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