Skip to content

Commit

Permalink
Wrap optional fields with boost::optional (LinkPreviewOptions, ReplyP…
Browse files Browse the repository at this point in the history
…arameters only)
  • Loading branch information
uralm1 committed Nov 8, 2024
1 parent da74ac4 commit 66b89bf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
11 changes: 6 additions & 5 deletions include/tgbot/types/LinkPreviewOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <memory>
#include <string>
#include <boost/optional.hpp>

namespace TgBot {

Expand All @@ -19,29 +20,29 @@ class LinkPreviewOptions {
/**
* @brief Optional. True, if the link preview is disabled
*/
bool isDisabled;
boost::optional<bool> isDisabled;

/**
* @brief Optional. URL to use for the link preview.
*
* If empty, then the first URL found in the message text will be used
*/
std::string url;
boost::optional<std::string> url;

/**
* @brief Optional. True, if the media in the link preview is supposed to be shrunk; ignored if the URL isn't explicitly specified or media size change isn't supported for the preview
*/
bool preferSmallMedia;
boost::optional<bool> preferSmallMedia;

/**
* @brief Optional. True, if the media in the link preview is supposed to be enlarged; ignored if the URL isn't explicitly specified or media size change isn't supported for the preview
*/
bool preferLargeMedia;
boost::optional<bool> preferLargeMedia;

/**
* @brief Optional. True, if the link preview must be shown above the message text; otherwise, the link preview will be shown below the message text
*/
bool showAboveText;
boost::optional<bool> showAboveText;
};
}

Expand Down
13 changes: 7 additions & 6 deletions include/tgbot/types/ReplyParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>
#include <string>
#include <vector>
#include <boost/optional.hpp>

namespace TgBot {

Expand All @@ -30,42 +31,42 @@ class ReplyParameters {
*
* Not supported for messages sent on behalf of a business account.
*/
std::int64_t chatId;
boost::optional<std::int64_t> chatId;

/**
* @brief Optional. Pass True if the message should be sent even if the specified message to be replied to is not found.
*
* Always False for replies in another chat or forum topic.
* Always True for messages sent on behalf of a business account.
*/
bool allowSendingWithoutReply;
boost::optional<bool> allowSendingWithoutReply;

/**
* @brief Optional. Quoted part of the message to be replied to; 0-1024 characters after entities parsing.
*
* The quote must be an exact substring of the message to be replied to, including bold, italic, underline, strikethrough, spoiler, and customEmoji entities.
* The message will fail to send if the quote isn't found in the original message.
*/
std::string quote;
boost::optional<std::string> quote;

/**
* @brief Optional. Mode for parsing entities in the quote.
*
* See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
*/
std::string quoteParseMode;
boost::optional<std::string> quoteParseMode;

/**
* @brief Optional. A JSON-serialized list of special entities that appear in the quote.
*
* It can be specified instead of quoteParseMode.
*/
std::vector<MessageEntity::Ptr> quoteEntities;
boost::optional<std::vector<MessageEntity::Ptr>> quoteEntities;

/**
* @brief Optional. Position of the quote in the original message in UTF-16 code units
*/
std::int32_t quotePosition;
boost::optional<std::int32_t> quotePosition;
};
}

Expand Down
46 changes: 24 additions & 22 deletions src/TgTypeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,12 +675,14 @@ std::string TgTypeParser::parseExternalReplyInfo(const ExternalReplyInfo::Ptr& o
ReplyParameters::Ptr TgTypeParser::parseJsonAndGetReplyParameters(const boost::property_tree::ptree& data) const {
auto result(std::make_shared<ReplyParameters>());
result->messageId = data.get<std::int32_t>("message_id", 0);
result->chatId = data.get<std::int64_t>("chat_id", 0);
result->allowSendingWithoutReply = data.get<bool>("allow_sending_without_reply", false);
result->quote = data.get<std::string>("quote", "");
result->quoteParseMode = data.get<std::string>("quote_parse_mode", "");
result->quoteEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "quote_entities");
result->quotePosition = data.get<std::int32_t>("quote_position", 0);
result->chatId = data.get_optional<std::int64_t>("chat_id");
result->allowSendingWithoutReply = data.get_optional<bool>("allow_sending_without_reply");
result->quote = data.get_optional<std::string>("quote");
result->quoteParseMode = data.get_optional<std::string>("quote_parse_mode");
if (data.find("quote_entities") != data.not_found()) {
result->quoteEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "quote_entities");
}
result->quotePosition = data.get_optional<std::int32_t>("quote_position");
return result;
}

Expand All @@ -691,12 +693,12 @@ std::string TgTypeParser::parseReplyParameters(const ReplyParameters::Ptr& objec
std::string result;
result += '{';
appendToJson(result, "message_id", object->messageId);
appendToJson(result, "chat_id", object->chatId);
appendToJson(result, "allow_sending_without_reply", object->allowSendingWithoutReply);
appendToJson(result, "quote", object->quote);
appendToJson(result, "quote_parse_mode", object->quoteParseMode);
appendToJson(result, "quote_entities", parseArray(&TgTypeParser::parseMessageEntity, object->quoteEntities));
appendToJson(result, "quote_position", object->quotePosition);
if (object->chatId) appendToJson(result, "chat_id", *(object->chatId));
if (object->allowSendingWithoutReply) appendToJson(result, "allow_sending_without_reply", *(object->allowSendingWithoutReply));
if (object->quote) appendToJson(result, "quote", *(object->quote));
if (object->quoteParseMode) appendToJson(result, "quote_parse_mode", *(object->quoteParseMode));
if (object->quoteEntities) appendToJson(result, "quote_entities", parseArray(&TgTypeParser::parseMessageEntity, *(object->quoteEntities)));
if (object->quotePosition) appendToJson(result, "quote_position", *(object->quotePosition));
removeLastComma(result);
result += '}';
return result;
Expand Down Expand Up @@ -1720,11 +1722,11 @@ std::string TgTypeParser::parseGiveawayCompleted(const GiveawayCompleted::Ptr& o

LinkPreviewOptions::Ptr TgTypeParser::parseJsonAndGetLinkPreviewOptions(const boost::property_tree::ptree& data) const {
auto result(std::make_shared<LinkPreviewOptions>());
result->isDisabled = data.get<bool>("is_disabled", false);
result->url = data.get<std::string>("url", "");
result->preferSmallMedia = data.get<bool>("prefer_small_media", false);
result->preferLargeMedia = data.get<bool>("prefer_large_media", false);
result->showAboveText = data.get<bool>("show_above_text", false);
result->isDisabled = data.get_optional<bool>("is_disabled");
result->url = data.get_optional<std::string>("url");
result->preferSmallMedia = data.get_optional<bool>("prefer_small_media");
result->preferLargeMedia = data.get_optional<bool>("prefer_large_media");
result->showAboveText = data.get_optional<bool>("show_above_text");
return result;
}

Expand All @@ -1734,11 +1736,11 @@ std::string TgTypeParser::parseLinkPreviewOptions(const LinkPreviewOptions::Ptr&
}
std::string result;
result += '{';
appendToJson(result, "is_disabled", object->isDisabled);
appendToJson(result, "url", object->url);
appendToJson(result, "prefer_small_media", object->preferSmallMedia);
appendToJson(result, "prefer_large_media", object->preferLargeMedia);
appendToJson(result, "show_above_text", object->showAboveText);
if (object->isDisabled) appendToJson(result, "is_disabled", *(object->isDisabled));
if (object->url) appendToJson(result, "url", *(object->url));
if (object->preferSmallMedia) appendToJson(result, "prefer_small_media", *(object->preferSmallMedia));
if (object->preferLargeMedia) appendToJson(result, "prefer_large_media", *(object->preferLargeMedia));
if (object->showAboveText) appendToJson(result, "show_above_text", *(object->showAboveText));
removeLastComma(result);
result += '}';
return result;
Expand Down

0 comments on commit 66b89bf

Please sign in to comment.