From d47ee877be5d1175bdc36f2d87881ddaf875a8e9 Mon Sep 17 00:00:00 2001 From: Oleg Morozenkov Date: Mon, 23 Jul 2018 01:56:42 +0300 Subject: Refactor http clients, fix webhook server, add more samples, change tabs to 4 spaces --- src/TgTypeParser.cpp | 3183 +++++++++++++++++++++++++------------------------- 1 file changed, 1589 insertions(+), 1594 deletions(-) (limited to 'src/TgTypeParser.cpp') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 4eaeaea..dbfb266 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -27,2027 +27,2022 @@ using namespace boost::property_tree; namespace TgBot { -TgTypeParser& TgTypeParser::getInstance() { - static TgTypeParser result; - return result; -} - Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { - auto result(make_shared()); - result->id = data.get("id"); - string type = data.get("type"); - if (type == "private") { - result->type = Chat::Type::Private; - } else if (type == "group") { - result->type = Chat::Type::Group; - } else if (type == "supergroup") { - result->type = Chat::Type::Supergroup; - } else if (type == "channel") { - result->type = Chat::Type::Channel; - } - result->title = data.get("title", ""); - result->username = data.get("username", ""); - result->firstName = data.get("first_name", ""); - result->lastName = data.get("last_name", ""); - result->allMembersAreAdministrators = data.get("all_members_are_administrators", false); - result->photo = tryParseJson(&TgTypeParser::parseJsonAndGetChatPhoto, data, "photo"); - result->description = data.get("description", ""); - result->inviteLink = data.get("invite_link", ""); - result->pinnedMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); - result->stickerSetName = data.get("sticker_set_name", ""); - result->canSetStickerSet = data.get("can_set_sticker_set", false); - - return result; + auto result(make_shared()); + result->id = data.get("id"); + string type = data.get("type"); + if (type == "private") { + result->type = Chat::Type::Private; + } else if (type == "group") { + result->type = Chat::Type::Group; + } else if (type == "supergroup") { + result->type = Chat::Type::Supergroup; + } else if (type == "channel") { + result->type = Chat::Type::Channel; + } + result->title = data.get("title", ""); + result->username = data.get("username", ""); + result->firstName = data.get("first_name", ""); + result->lastName = data.get("last_name", ""); + result->allMembersAreAdministrators = data.get("all_members_are_administrators", false); + result->photo = tryParseJson(&TgTypeParser::parseJsonAndGetChatPhoto, data, "photo"); + result->description = data.get("description", ""); + result->inviteLink = data.get("invite_link", ""); + result->pinnedMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); + result->stickerSetName = data.get("sticker_set_name", ""); + result->canSetStickerSet = data.get("can_set_sticker_set", false); + + return result; } string TgTypeParser::parseChat(const Chat::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "id", object->id); - if (object->type == Chat::Type::Private) { - appendToJson(result, "type", "private"); - } else if (object->type == Chat::Type::Group) { - appendToJson(result, "type", "group"); - } else if (object->type == Chat::Type::Supergroup) { - appendToJson(result, "type", "supergroup"); - } else if (object->type == Chat::Type::Channel) { - appendToJson(result, "type", "channel"); - } - appendToJson(result, "title", object->title); - appendToJson(result, "username", object->username); - appendToJson(result, "first_name", object->firstName); - appendToJson(result, "last_name", object->lastName); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "id", object->id); + if (object->type == Chat::Type::Private) { + appendToJson(result, "type", "private"); + } else if (object->type == Chat::Type::Group) { + appendToJson(result, "type", "group"); + } else if (object->type == Chat::Type::Supergroup) { + appendToJson(result, "type", "supergroup"); + } else if (object->type == Chat::Type::Channel) { + appendToJson(result, "type", "channel"); + } + appendToJson(result, "title", object->title); + appendToJson(result, "username", object->username); + appendToJson(result, "first_name", object->firstName); + appendToJson(result, "last_name", object->lastName); + removeLastComma(result); + result += '}'; + return result; } User::Ptr TgTypeParser::parseJsonAndGetUser(const ptree& data) const { - auto result(make_shared()); - result->id = data.get("id"); - 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", ""); - return result; + auto result(make_shared()); + result->id = data.get("id"); + 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", ""); + return result; } string TgTypeParser::parseUser(const User::Ptr& object) const { - if (!object) { - return ""; - } - 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); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + 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); + removeLastComma(result); + result += '}'; + return result; } MessageEntity::Ptr TgTypeParser::parseJsonAndGetMessageEntity(const ptree& data) const{ - auto result(make_shared()); - result->type = data.get("type"); - result->offset = data.get("offset"); - result->length = data.get("length"); - result->url = data.get("url", ""); - result->user = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); - return result; + auto result(make_shared()); + result->type = data.get("type"); + result->offset = data.get("offset"); + result->length = data.get("length"); + result->url = data.get("url", ""); + result->user = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); + return result; } string TgTypeParser::parseMessageEntity(const MessageEntity::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "type", object->type); - appendToJson(result, "offset", object->offset); - appendToJson(result, "length", object->length); - appendToJson(result, "url", object->url); - appendToJson(result, "user", parseUser(object->user)); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "type", object->type); + appendToJson(result, "offset", object->offset); + appendToJson(result, "length", object->length); + appendToJson(result, "url", object->url); + appendToJson(result, "user", parseUser(object->user)); + removeLastComma(result); + result += '}'; + return result; } Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { - auto result(make_shared()); - result->messageId = data.get("message_id"); - result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); - result->date = data.get("date"); - 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->forwardDate = data.get("forward_date", 0); - result->replyToMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); - result->editDate = data.get("edit_date", 0); - result->authorSignature = data.get("author_signature", ""); - result->text = data.get("text", ""); - result->entities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetMessageEntity, data, "entities"); - result->captionEntities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); - result->audio = tryParseJson