From e89d523a7ac895f9bb724f3fa504f49c978aa4df Mon Sep 17 00:00:00 2001 From: Oleg Morozenkov Date: Sun, 6 Dec 2015 23:39:14 +0300 Subject: Some fixes. Closes #9, #10 --- src/TgTypeParser.cpp | 64 +++++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 43 deletions(-) (limited to 'src/TgTypeParser.cpp') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index df80ca7..d84e2a1 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -35,7 +35,16 @@ TgTypeParser& TgTypeParser::getInstance() { Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { Chat::Ptr result(new Chat); result->id = data.get("id"); - result->type = data.get("type"); + 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"); @@ -51,7 +60,15 @@ string TgTypeParser::parseChat(const Chat::Ptr& object) const { string result; result += '{'; appendToJson(result, "id", object->id); - appendToJson(result, "type", object->type); + 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); @@ -85,32 +102,12 @@ string TgTypeParser::parseUser(const User::Ptr& object) const { return result; } -GroupChat::Ptr TgTypeParser::parseJsonAndGetGroupChat(const ptree& data) const { - GroupChat::Ptr result(new GroupChat); - result->id = data.get("id"); - result->title = data.get("title"); - return result; -} - -string TgTypeParser::parseGroupChat(const GroupChat::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "id", object->id); - appendToJson(result, "title", object->title); - result.erase(result.length() - 1); - result += '}'; - return result; -} - Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { Message::Ptr result(new Message); result->messageId = data.get("message_id"); - result->from = parseJsonAndGetUser(data.find("from")->second); + result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); result->date = data.get("date"); - result->chat = tryParseJson(&TgTypeParser::parseJsonAndGetChat, data, "chat"); + result->chat = parseJsonAndGetChat(data.find("chat")->second); result->forwardFrom = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "forward_from"); result->forwardDate = data.get("forward_date", 0); result->replyToMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); @@ -459,25 +456,6 @@ std::string TgTypeParser::parseForceReply(const ForceReply::Ptr& object) const { return result; } -GenericChat::Ptr TgTypeParser::parseJsonAndGetGenericChat(const ptree& data) const { - if (data.find("first_name") == data.not_found()) { - return static_pointer_cast(parseJsonAndGetGroupChat(data)); - } else { - return static_pointer_cast(parseJsonAndGetUser(data)); - } -} - -string TgTypeParser::parseGenericChat(const GenericChat::Ptr& object) const { - if (!object) { - return ""; - } - if (dynamic_pointer_cast(object) == nullptr) { - return parseGroupChat(static_pointer_cast(object)); - } else { - return parseUser(static_pointer_cast(object)); - } -} - GenericReply::Ptr TgTypeParser::parseJsonAndGetGenericReply(const boost::property_tree::ptree& data) const { if (data.find("force_reply") != data.not_found()) { return static_pointer_cast(parseJsonAndGetForceReply(data)); -- cgit v1.2.3