diff options
author | jellybrick <shlee1503@naver.com> | 2020-03-14 23:21:30 +0900 |
---|---|---|
committer | jellybrick <shlee1503@naver.com> | 2020-03-14 23:21:42 +0900 |
commit | 64174491dac84c559db5c48ed850177fa3eb4da0 (patch) | |
tree | 5f7a754d57c2a7ec439f309df5e4b8d964d0834f /src | |
parent | 19f49b7a2f540a756ed9759b0fa5fb52bf73f20e (diff) |
Fix #127, #128 & Code quality improvements
It seems that the author of this code misunderstood 'Array of PollOption' as 'PollOption'.
Diffstat (limited to 'src')
-rw-r--r-- | src/Api.cpp | 50 | ||||
-rw-r--r-- | src/EventHandler.cpp | 4 | ||||
-rw-r--r-- | src/TgException.cpp | 2 | ||||
-rw-r--r-- | src/TgTypeParser.cpp | 12 | ||||
-rw-r--r-- | src/net/CurlHttpClient.cpp | 8 | ||||
-rw-r--r-- | src/net/HttpParser.cpp | 1 | ||||
-rw-r--r-- | src/net/TgLongPoll.cpp | 12 | ||||
-rw-r--r-- | src/net/Url.cpp | 2 | ||||
-rw-r--r-- | src/tools/FileTools.cpp | 2 | ||||
-rw-r--r-- | src/tools/StringTools.cpp | 11 | ||||
-rw-r--r-- | src/types/InputFile.cpp | 5 |
11 files changed, 53 insertions, 56 deletions
diff --git a/src/Api.cpp b/src/Api.cpp index 26e9ada..c1b112c 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -1,10 +1,11 @@ #include "tgbot/Api.h" #include <cstdint> +#include <tgbot/tools/StringTools.h> #include <utility> -#include "tgbot/TgTypeParser.h" #include "tgbot/TgException.h" +#include "tgbot/TgTypeParser.h" using namespace std; using namespace boost::property_tree; @@ -54,7 +55,7 @@ Message::Ptr Api::forwardMessage(std::int64_t chatId, std::int64_t fromChatId, s return _tgTypeParser.parseJsonAndGetMessage(sendRequest("forwardMessage", args)); } -Message::Ptr Api::sendPhoto(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> photo, const string& caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendPhoto(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> photo, const string& caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(7); args.emplace_back("chat_id", chatId); @@ -82,7 +83,7 @@ Message::Ptr Api::sendPhoto(std::int64_t chatId, const boost::variant<InputFile: return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPhoto", args)); } -Message::Ptr Api::sendAudio(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> audio, const string &caption, std::int32_t duration, const string& performer, const string& title, const boost::variant<InputFile::Ptr, std::string> thumb, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendAudio(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> audio, const string &caption, std::int32_t duration, const string& performer, const string& title, boost::variant<InputFile::Ptr, std::string> thumb, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(11); args.emplace_back("chat_id", chatId); @@ -128,7 +129,7 @@ Message::Ptr Api::sendAudio(std::int64_t chatId, const boost::variant<InputFile: return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAudio", args)); } -Message::Ptr Api::sendDocument(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> document, const boost::variant<InputFile::Ptr, std::string> thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendDocument(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> document, boost::variant<InputFile::Ptr, std::string> thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(8); args.emplace_back("chat_id", chatId); @@ -256,7 +257,7 @@ bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok, } -Message::Ptr Api::sendSticker(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> sticker, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { +Message::Ptr Api::sendSticker(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> sticker, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(5); args.emplace_back("chat_id", chatId); @@ -293,7 +294,7 @@ File::Ptr Api::uploadStickerFile(std::int32_t userId, const InputFile::Ptr pngSt return _tgTypeParser.parseJsonAndGetFile(sendRequest("uploadStickerFile", args)); } -bool Api::createNewStickerSet(std::int32_t userId, const string& name, const string& title, const boost::variant<InputFile::Ptr, std::string> pngSticker, const string& emojis, bool containsMasks, MaskPosition::Ptr maskPosition) const { +bool Api::createNewStickerSet(std::int32_t userId, const string& name, const string& title, boost::variant<InputFile::Ptr, std::string> pngSticker, const string& emojis, bool containsMasks, MaskPosition::Ptr maskPosition) const { vector<HttpReqArg> args; args.reserve(7); args.emplace_back("user_id", userId); @@ -315,7 +316,7 @@ bool Api::createNewStickerSet(std::int32_t userId, const string& name, const str return sendRequest("createNewStickerSet", args).get<bool>("", false); } -bool Api::addStickerToSet(std::int32_t userId, const string& name, const boost::variant<InputFile::Ptr, std::string> pngSticker, const string& emojis, MaskPosition::Ptr maskPosition) const { +bool Api::addStickerToSet(std::int32_t userId, const string& name, boost::variant<InputFile::Ptr, std::string> pngSticker, const string& emojis, MaskPosition::Ptr maskPosition) const { vector<HttpReqArg> args; args.reserve(6); args.emplace_back("user_id", userId); @@ -348,7 +349,7 @@ bool Api::deleteStickerFromSet(const string& sticker) const { return sendRequest("deleteStickerFromSet", args).get<bool>("", false); } -Message::Ptr Api::sendVideo(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> video, bool supportsStreaming, std::int32_t duration, std::int32_t width, std::int32_t height, const boost::variant<InputFile::Ptr, std::string> thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendVideo(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> video, bool supportsStreaming, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant<InputFile::Ptr, std::string> thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(12); args.emplace_back("chat_id", chatId); @@ -397,7 +398,7 @@ Message::Ptr Api::sendVideo(std::int64_t chatId, const boost::variant<InputFile: return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVideo", args)); } -Message::Ptr Api::sendAnimation(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> animation, std::int32_t duration, std::int32_t width, std::int32_t height, const boost::variant<InputFile::Ptr, std::string> thumb, const string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendAnimation(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> animation, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant<InputFile::Ptr, std::string> thumb, const string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(11); args.emplace_back("chat_id", chatId); @@ -443,7 +444,7 @@ Message::Ptr Api::sendAnimation(std::int64_t chatId, const boost::variant<InputF return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAnimation", args)); } -Message::Ptr Api::sendVideoNote(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> videoNote, std::int64_t replyToMessageId, bool disableNotification, std::int32_t duration, std::int32_t length, const boost::variant<InputFile::Ptr, std::string> thumb, const GenericReply::Ptr replyMarkup) const { +Message::Ptr Api::sendVideoNote(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> videoNote, std::int64_t replyToMessageId, bool disableNotification, std::int32_t duration, std::int32_t length, boost::variant<InputFile::Ptr, std::string> thumb, const GenericReply::Ptr replyMarkup) const { vector<HttpReqArg> args; args.reserve(8); args.emplace_back("chat_id", chatId); @@ -491,7 +492,7 @@ vector<Message::Ptr> Api::sendMediaGroup(std::int64_t chatId, const vector<Input return _tgTypeParser.parseJsonAndGetArray<Message>(&TgTypeParser::parseJsonAndGetMessage, sendRequest("sendMediaGroup", args)); } -Message::Ptr Api::sendVoice(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> voice, const string &caption, int duration, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendVoice(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> voice, const string &caption, int duration, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(8); args.emplace_back("chat_id", chatId); @@ -1119,28 +1120,23 @@ void Api::deleteMessage(std::int64_t chatId, std::int32_t messageId) const { sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); } -Message::Ptr Api::sendPoll(std::int64_t chat_id, std::string question, std::vector<std::string> options, bool disable_notification, std::int32_t reply_to_message_id,GenericReply::Ptr reply_markup) const { +Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector<std::string>& options, bool disableNotification, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup) const { vector<HttpReqArg> args; args.reserve(6); - args.emplace_back("chat_id", chat_id); + args.emplace_back("chat_id", chatId); args.emplace_back("question", question); - std::string json_array; - json_array = "["; - for(const auto &arr : options){ - json_array += "\"" + arr + "\"" + ","; - } - json_array.pop_back(); - json_array += "]"; - args.emplace_back("options", json_array); + args.emplace_back("options", _tgTypeParser.parseArray<std::string>([] (const std::string& option) -> std::string { + return StringTools::urlEncode(option); + }, options)); - if (disable_notification){ - args.emplace_back("disable_notification", disable_notification); + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); } - if (reply_to_message_id!=0){ - args.emplace_back("reply_to_message_id", reply_to_message_id); + if (replyToMessageId != 0){ + args.emplace_back("reply_to_message_id", replyToMessageId); } - if (reply_markup){ - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(reply_markup)); + if (replyMarkup){ + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args)); } diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp index cd67379..43f2eb1 100644 --- a/src/EventHandler.cpp +++ b/src/EventHandler.cpp @@ -7,7 +7,7 @@ using namespace std; namespace TgBot { -void EventHandler::handleUpdate(Update::Ptr update) const { +void EventHandler::handleUpdate(const Update::Ptr& update) const { if (update->inlineQuery != nullptr) { _broadcaster.broadcastInlineQuery(update->inlineQuery); } @@ -22,7 +22,7 @@ void EventHandler::handleUpdate(Update::Ptr update) const { } } -void EventHandler::handleMessage(Message::Ptr message) const { +void EventHandler::handleMessage(const Message::Ptr& message) const { _broadcaster.broadcastAnyMessage(message); if (StringTools::startsWith(message->text, "/")) { diff --git a/src/TgException.cpp b/src/TgException.cpp index 6ccd3d9..03a942c 100644 --- a/src/TgException.cpp +++ b/src/TgException.cpp @@ -2,7 +2,7 @@ namespace TgBot { -TgBot::TgException::TgException(const std::string description) : runtime_error(description) { +TgBot::TgException::TgException(const std::string& description) : runtime_error(description) { } } diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index e002c60..4d91d28 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -377,8 +377,8 @@ Poll::Ptr TgTypeParser::parseJsonAndGetPoll(const ptree& data) const { auto result(make_shared<Poll>()); result->id = data.get("id", 0); result->question = data.get("question", ""); - result->options = tryParseJson<PollOption>(&TgTypeParser::parseJsonAndGetPollOption, data, "options"); - result->is_closed = data.get<bool>("is_closed"); + result->options = parseJsonAndGetArray<PollOption>(&TgTypeParser::parseJsonAndGetPollOption, data, "options"); + result->isClosed = data.get<bool>("is_closed"); return result; } @@ -390,8 +390,8 @@ string TgTypeParser::parsePoll(const Poll::Ptr& object) const { result += '{'; appendToJson(result, "id", object->id); appendToJson(result, "question", object->question); - appendToJson(result, "options", parsePollOption(object->options)); - appendToJson(result, "is_closed", object->is_closed); + appendToJson(result, "options", parseArray(&TgTypeParser::parsePollOption, object->options)); + appendToJson(result, "is_closed", object->isClosed); removeLastComma(result); result += '}'; return result; @@ -400,7 +400,7 @@ string TgTypeParser::parsePoll(const Poll::Ptr& object) const { PollOption::Ptr TgTypeParser::parseJsonAndGetPollOption(const ptree& data) const { auto result(make_shared<PollOption>()); result->text = data.get("text", ""); - result->voter_count = data.get("voter_count", 0); + result->voterCount = data.get("voter_count", 0); return result; } @@ -411,7 +411,7 @@ string TgTypeParser::parsePollOption(const PollOption::Ptr& object) const { string result; result += '{'; appendToJson(result, "text", object->text); - appendToJson(result, "voter_count", object->voter_count); + appendToJson(result, "voter_count", object->voterCount); removeLastComma(result); result += '}'; return result; diff --git a/src/net/CurlHttpClient.cpp b/src/net/CurlHttpClient.cpp index 5bd9313..6255974 100644 --- a/src/net/CurlHttpClient.cpp +++ b/src/net/CurlHttpClient.cpp @@ -17,9 +17,9 @@ CurlHttpClient::~CurlHttpClient() { } static std::size_t curlWriteString(char* ptr, std::size_t size, std::size_t nmemb, void* userdata) { - static_cast<std::string *>(userdata)->append(ptr, size * nmemb); + static_cast<std::string*>(userdata)->append(ptr, size * nmemb); return size * nmemb; -}; +} std::string CurlHttpClient::makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const { // Copy settings for each call because we change CURLOPT_URL and other stuff. @@ -34,8 +34,8 @@ std::string CurlHttpClient::makeRequest(const Url& url, const std::vector<HttpRe headers = curl_slist_append(headers, "Connection: close"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - curl_mime *mime; - curl_mimepart *part; + curl_mime* mime; + curl_mimepart* part; mime = curl_mime_init(curl); if (!args.empty()) { for (const HttpReqArg& a : args) { diff --git a/src/net/HttpParser.cpp b/src/net/HttpParser.cpp index 8006c85..2d3085f 100644 --- a/src/net/HttpParser.cpp +++ b/src/net/HttpParser.cpp @@ -81,7 +81,6 @@ string HttpParser::generateMultipartFormData(const vector<HttpReqArg>& args, con string HttpParser::generateMultipartBoundary(const vector<HttpReqArg>& args) const { string result; - srand((std::uint32_t) time(nullptr)); for (const HttpReqArg& item : args) { if (item.isFile) { while (result.empty() || item.value.find(result) != string::npos) { diff --git a/src/net/TgLongPoll.cpp b/src/net/TgLongPoll.cpp index 0138620..6d266f8 100644 --- a/src/net/TgLongPoll.cpp +++ b/src/net/TgLongPoll.cpp @@ -1,19 +1,21 @@ #include "tgbot/net/TgLongPoll.h" #include <cstdint> +#include <utility> namespace TgBot { -TgLongPoll::TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowupdates) - : _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout), _allowupdates(allowupdates) { +TgLongPoll::TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates) + : _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout), + _allowUpdates(std::move(allowUpdates)) { } -TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowupdates) : - TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowupdates) { +TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowUpdates) : + TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowUpdates) { } void TgLongPoll::start() { - std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowupdates); + std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowUpdates); for (Update::Ptr& item : updates) { if (item->updateId >= _lastUpdateId) { _lastUpdateId = item->updateId + 1; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index 016a97f..95e31c7 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -2,8 +2,6 @@ #include <cstddef> -#include "tgbot/tools/StringTools.h" - using namespace std; namespace TgBot { diff --git a/src/tools/FileTools.cpp b/src/tools/FileTools.cpp index 408c710..d2c303c 100644 --- a/src/tools/FileTools.cpp +++ b/src/tools/FileTools.cpp @@ -23,4 +23,4 @@ void write(const string& content, const string& filePath) { out.close(); } -}; +} diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp index 1246c50..f49a78e 100644 --- a/src/tools/StringTools.cpp +++ b/src/tools/StringTools.cpp @@ -3,6 +3,7 @@ #include <cstdlib> #include <iomanip> #include <cstdio> +#include <random> using namespace std; @@ -58,8 +59,13 @@ string generateRandomString(std::size_t length) { static const string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=[]\\;',./!@#$%^&*()_+{}|:\"<>?`~"); static const std::size_t charsLen = chars.length(); string result; + + random_device randomDevice; + mt19937 randomSeed(randomDevice()); + uniform_int_distribution<int> generator(0, charsLen - 1); + for (std::size_t i = 0; i < length; ++i) { - result += chars[rand() % charsLen]; + result += chars[generator(randomSeed)]; } return result; } @@ -84,8 +90,7 @@ string urlDecode(const string& value) { for (std::size_t i = 0, count = value.length(); i < count; ++i) { const char c = value[i]; if (c == '%') { - int t = 0; - sscanf(value.substr(i + 1, 2).c_str(), "%x", &t); + int t = stoi(value.substr(i + 1, 2), nullptr, 16); result += (char) t; i += 2; } else { diff --git a/src/types/InputFile.cpp b/src/types/InputFile.cpp index e0885f0..0ca1991 100644 --- a/src/types/InputFile.cpp +++ b/src/types/InputFile.cpp @@ -1,7 +1,4 @@ #include "tgbot/types/InputFile.h" - -#include <fstream> - #include "tgbot/tools/StringTools.h" #include "tgbot/tools/FileTools.h" @@ -17,4 +14,4 @@ InputFile::Ptr InputFile::fromFile(const string& filePath, const string& mimeTyp return result; } -}; +} |