From 930a2f0fdb4ef5ba7cadab2e493bcd24e5b44809 Mon Sep 17 00:00:00 2001 From: llnulldisk <48621230+llnulldisk@users.noreply.github.com> Date: Tue, 20 Sep 2022 14:42:45 +0200 Subject: Sort TgTypeParser functions --- src/TgTypeParser.cpp | 4129 +++++++++++++++++++++++++------------------------- 1 file changed, 2052 insertions(+), 2077 deletions(-) (limited to 'src') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index d251da6..f6ae9f0 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -1,10 +1,133 @@ #include "tgbot/TgTypeParser.h" -#include -#include - namespace TgBot { +Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const boost::property_tree::ptree& data) const { + auto result(std::make_shared()); + result->updateId = data.get("update_id", 0); + result->message = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "message"); + result->editedMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "edited_message"); + result->channelPost = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "channel_post"); + result->editedChannelPost = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "edited_channel_post"); + result->inlineQuery = tryParseJson(&TgTypeParser::parseJsonAndGetInlineQuery, data, "inline_query"); + result->chosenInlineResult = tryParseJson(&TgTypeParser::parseJsonAndGetChosenInlineResult, data, "chosen_inline_result"); + result->callbackQuery = tryParseJson(&TgTypeParser::parseJsonAndGetCallbackQuery, data, "callback_query"); + result->shippingQuery = tryParseJson(&TgTypeParser::parseJsonAndGetShippingQuery, data, "shipping_query"); + result->preCheckoutQuery = tryParseJson(&TgTypeParser::parseJsonAndGetPreCheckoutQuery, data, "pre_checkout_query"); + result->poll = tryParseJson(&TgTypeParser::parseJsonAndGetPoll, data, "poll"); + result->pollAnswer = tryParseJson(&TgTypeParser::parseJsonAndGetPollAnswer, data, "poll_answer"); + result->myChatMember = tryParseJson(&TgTypeParser::parseJsonAndGetChatMemberUpdated, data, "my_chat_member"); + result->chatMember = tryParseJson(&TgTypeParser::parseJsonAndGetChatMemberUpdated, data, "chat_member"); + result->chatJoinRequest = tryParseJson(&TgTypeParser::parseJsonAndGetChatJoinRequest, data, "chat_join_request"); + return result; +} + +std::string TgTypeParser::parseUpdate(const Update::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "update_id", object->updateId); + appendToJson(result, "message", parseMessage(object->message)); + appendToJson(result, "edited_message", parseMessage(object->editedMessage)); + appendToJson(result, "channel_post", parseMessage(object->channelPost)); + appendToJson(result, "edited_channel_post", parseMessage(object->editedChannelPost)); + appendToJson(result, "inline_query", parseInlineQuery(object->inlineQuery)); + appendToJson(result, "chosen_inline_result", parseChosenInlineResult(object->chosenInlineResult)); + appendToJson(result, "callback_query", parseCallbackQuery(object->callbackQuery)); + appendToJson(result, "shipping_query", parseShippingQuery(object->shippingQuery)); + appendToJson(result, "pre_checkout_query", parsePreCheckoutQuery(object->preCheckoutQuery)); + appendToJson(result, "poll", parsePoll(object->poll)); + appendToJson(result, "poll_answer", parsePollAnswer(object->pollAnswer)); + appendToJson(result, "my_chat_member", parseChatMemberUpdated(object->myChatMember)); + appendToJson(result, "chat_member", parseChatMemberUpdated(object->chatMember)); + appendToJson(result, "chat_join_request", parseChatJoinRequest(object->chatJoinRequest)); + removeLastComma(result); + result += '}'; + return result; +} + +WebhookInfo::Ptr TgTypeParser::parseJsonAndGetWebhookInfo(const boost::property_tree::ptree& data) const { + auto result(std::make_shared()); + result->url = data.get("url", ""); + result->hasCustomCertificate = data.get("has_custom_certificate", false); + result->pendingUpdateCount = data.get("pending_update_count", 0); + result->ipAddress = data.get("ip_address", ""); + result->lastErrorDate = data.get("last_error_date", 0); + result->lastErrorMessage = data.get("last_error_message", ""); + result->lastSynchronizationErrorDate = data.get("last_synchronization_error_date", 0); + result->maxConnections = data.get("max_connections", 0); + result->allowedUpdates = parseJsonAndGetArray( + [] (const boost::property_tree::ptree& innerData)->std::string { + return innerData.get(""); + } + , data, "allowed_updates"); + return result; +} + +std::string TgTypeParser::parseWebhookInfo(const WebhookInfo::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "url", object->url); + appendToJson(result, "has_custom_certificate", object->hasCustomCertificate); + appendToJson(result, "pending_update_count", object->pendingUpdateCount); + appendToJson(result, "ip_address", object->ipAddress); + appendToJson(result, "last_error_date", object->lastErrorDate); + appendToJson(result, "last_error_message", object->lastErrorMessage); + appendToJson(result, "last_synchronization_error_date", object->lastSynchronizationErrorDate); + appendToJson(result, "max_connections", object->maxConnections); + appendToJson(result, "allowed_updates", + parseArray([] (const std::string& s)->std::string { + return s; + } + , object->allowedUpdates)); + removeLastComma(result); + result += '}'; + return result; +} + +User::Ptr TgTypeParser::parseJsonAndGetUser(const boost::property_tree::ptree& data) const { + auto result(std::make_shared()); + result->id = data.get("id", 0); + result->isBot = data.get("is_bot", false); + result->firstName = data.get("first_name", ""); + result->lastName = data.get("last_name", ""); + result->username = data.get("username", ""); + result->languageCode = data.get("language_code", ""); + result->isPremium = data.get("is_premium", false); + result->addedToAttachmentMenu = data.get("added_to_attachment_menu", false); + result->canJoinGroups = data.get("can_join_groups", false); + result->canReadAllGroupMessages = data.get("can_read_all_group_messages", false); + result->supportsInlineQueries = data.get("supports_inline_queries", false); + return result; +} + +std::string TgTypeParser::parseUser(const User::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "is_bot", object->isBot); + appendToJson(result, "first_name", object->firstName); + appendToJson(result, "last_name", object->lastName); + appendToJson(result, "username", object->username); + appendToJson(result, "language_code", object->languageCode); + appendToJson(result, "is_premium", object->isPremium); + appendToJson(result, "added_to_attachment_menu", object->addedToAttachmentMenu); + appendToJson(result, "can_join_groups", object->canJoinGroups); + appendToJson(result, "can_read_all_group_messages", object->canReadAllGroupMessages); + appendToJson(result, "supports_inline_queries", object->supportsInlineQueries); + removeLastComma(result); + result += '}'; + return result; +} + Chat::Ptr TgTypeParser::parseJsonAndGetChat(const boost::property_tree::ptree& data) const { auto result(std::make_shared()); result->id = data.get("id", 0); @@ -84,209 +207,72 @@ std::string TgTypeParser::parseChat(const Chat::Ptr& object) const { return result; } -User::Ptr TgTypeParser::parseJsonAndGetUser(const boost::property_tree::ptree& data) const { - auto result(std::make_shared()); - result->id = data.get("id", 0); - result->isBot = data.get("is_bot", false); - result->firstName = data.get("first_name", ""); - result->lastName = data.get("last_name", ""); - result->username = data.get("username", ""); - result->languageCode = data.get("language_code", ""); - result->isPremium = data.get("is_premium", false); - result->addedToAttachmentMenu = data.get("added_to_attachment_menu", false); - result->canJoinGroups = data.get("can_join_groups", false); - result->canReadAllGroupMessages = data.get("can_read_all_group_messages", false); - result->supportsInlineQueries = data.get("supports_inline_queries", false); +Message::Ptr TgTypeParser::parseJsonAndGetMessage(const boost::property_tree::ptree& data) const { + auto result(std::make_shared()); + result->messageId = data.get("message_id", 0); + result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->senderChat = tryParseJson(&TgTypeParser::parseJsonAndGetChat, data, "sender_chat"); + result->date = data.get("date", 0); + result->chat = parseJsonAndGetChat(data.find("chat")->second); + result->forwardFrom = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "forward_from"); + result->forwardFromChat = tryParseJson(&TgTypeParser::parseJsonAndGetChat, data, "forward_from_chat"); + result->forwardFromMessageId = data.get("forward_from_message_id", 0); + result->forwardSignature = data.get("forward_signature", ""); + result->forwardSenderName = data.get("forward_sender_name", ""); + result->forwardDate = data.get("forward_date", 0); + result->isAutomaticForward = data.get("is_automatic_forward", false); + result->replyToMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); + result->viaBot = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "via_bot"); + result->editDate = data.get("edit_date", 0); + result->hasProtectedContent = data.get("has_protected_content", false); + result->mediaGroupId = data.get("media_group_id", ""); + result->authorSignature = data.get("author_signature", ""); + result->text = data.get("text", ""); + result->entities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetMessageEntity, data, "entities"); + result->animation = tryParseJson(&TgTypeParser::parseJsonAndGetAnimation, data, "animation"); + result->audio = tryParseJson