Skip to content

Commit fd3863d

Browse files
committed
Add and use RichText::remove_unallowed_quote_entities.
1 parent 8300699 commit fd3863d

5 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎td/telegram/MessageQuote.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ static vector<RichText> get_quote_rich_texts(
3232
vector<RichText> result;
3333
for (auto &rich_text_ptr : rich_text_ptrs) {
3434
RichText rich_text(std::move(rich_text_ptr), FlatHashMap<int64, FileId>());
35+
if (rich_text.remove_unallowed_quote_entities()) {
36+
LOG(ERROR) << "Receive a rich quote with unexpected entities";
37+
}
3538
if (!rich_text.empty()) {
3639
result.push_back(std::move(rich_text));
3740
}
@@ -111,6 +114,19 @@ MessageQuote::MessageQuote(Td *td, td_api::object_ptr<td_api::inputTextQuote> qu
111114
}
112115
}
113116

117+
bool MessageQuote::remove_unallowed_quote_entities_from_rich_texts(vector<RichText> &rich_texts) {
118+
bool is_changed = false;
119+
for (auto &rich_text : rich_texts) {
120+
if (rich_text.remove_unallowed_quote_entities()) {
121+
is_changed = true;
122+
}
123+
}
124+
if (td::remove_if(rich_texts, [](const RichText &rich_text) { return rich_text.empty(); })) {
125+
is_changed = true;
126+
}
127+
return is_changed;
128+
}
129+
114130
MessageQuote MessageQuote::clone(Td *td, DialogId dialog_id, MessageContentDupType dup_type,
115131
bool ignore_is_manual) const {
116132
CloneWebPageBlockContext context;

‎td/telegram/MessageQuote.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class MessageQuote {
3030

3131
vector<td_api::object_ptr<td_api::RichText>> get_rich_texts_object(Td *td) const;
3232

33+
static bool remove_unallowed_quote_entities_from_rich_texts(vector<RichText> &rich_texts);
34+
3335
friend bool operator==(const MessageQuote &lhs, const MessageQuote &rhs);
3436

3537
friend StringBuilder &operator<<(StringBuilder &string_builder, const MessageQuote &quote);
@@ -45,6 +47,7 @@ class MessageQuote {
4547
MessageQuote(FormattedText &&text, vector<RichText> &&rich_texts, int32 position, bool is_manual = true)
4648
: text_(std::move(text)), rich_texts_(std::move(rich_texts)), position_(max(0, position)), is_manual_(is_manual) {
4749
remove_unallowed_quote_entities(text_);
50+
remove_unallowed_quote_entities_from_rich_texts(rich_texts_);
4851
}
4952

5053
MessageQuote(Td *td, telegram_api::object_ptr<telegram_api::inputReplyToMessage> &input_reply_to_message);

‎td/telegram/MessageQuote.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void MessageQuote::parse(ParserT &parser) {
5656
}
5757
if (has_rich_texts) {
5858
td::parse(rich_texts_, parser);
59+
remove_unallowed_quote_entities_from_rich_texts(rich_texts_);
5960
}
6061
}
6162

‎td/telegram/RichText.cpp‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,74 @@ RichText RichText::get_quote_fragment_impl(size_t &pos, size_t begin, size_t end
713713
}
714714
}
715715

716+
bool RichText::remove_unallowed_quote_entities() {
717+
switch (type_) {
718+
case Type::Plain:
719+
case Type::Math:
720+
case Type::CustomEmoji:
721+
return false;
722+
case Type::Icon:
723+
// icons can't be quoted
724+
*this = RichText();
725+
return true;
726+
case Type::Concatenation: {
727+
bool is_changed = false;
728+
for (auto &text : texts_) {
729+
if (text.remove_unallowed_quote_entities()) {
730+
is_changed = true;
731+
}
732+
}
733+
if (td::remove_if(texts_, [](const RichText &text) { return text.empty(); })) {
734+
is_changed = true;
735+
}
736+
if (texts_.empty()) {
737+
*this = RichText();
738+
return true;
739+
}
740+
return is_changed;
741+
}
742+
case Type::Fixed:
743+
case Type::Url:
744+
case Type::EmailAddress:
745+
case Type::Marked:
746+
case Type::PhoneNumber:
747+
case Type::Anchor:
748+
case Type::Mention:
749+
case Type::Hashtag:
750+
case Type::Cashtag:
751+
case Type::BotCommand:
752+
case Type::AutoUrl:
753+
case Type::AutoEmailAddress:
754+
case Type::AutoPhoneNumber:
755+
case Type::BankCardNumber:
756+
case Type::MentionName:
757+
case Type::Button:
758+
case Type::TonAddress:
759+
case Type::Diff: {
760+
// the formatting isn't supported in quotes, so only the text is kept
761+
CHECK(!texts_.empty());
762+
auto text = std::move(texts_[0]);
763+
text.remove_unallowed_quote_entities();
764+
*this = std::move(text);
765+
return true;
766+
}
767+
default:
768+
if (texts_.size() != 1u) {
769+
LOG(ERROR) << "Receive rich text of type " << static_cast<int32>(type_) << " with " << texts_.size()
770+
<< " texts";
771+
*this = RichText();
772+
return true;
773+
}
774+
if (!texts_[0].remove_unallowed_quote_entities()) {
775+
return false;
776+
}
777+
if (texts_[0].empty()) {
778+
*this = RichText();
779+
}
780+
return true;
781+
}
782+
}
783+
716784
int32 RichText::get_index_mask() const {
717785
switch (type_) {
718786
case Type::Url:

‎td/telegram/RichText.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ class RichText {
110110
// returns part of the rich text between begin and end UTF-8 characters of its quote text, which can be used in a quote
111111
RichText get_quote_fragment(size_t begin, size_t end) const;
112112

113+
// removes formatting, which isn't supported in quotes; returns true, if the text was changed
114+
bool remove_unallowed_quote_entities();
115+
113116
int32 get_index_mask() const;
114117

115118
void append_user_ids(vector<UserId> &user_ids) const;

0 commit comments

Comments
 (0)