From 8c67a4b991ab12c8f5deeee1ae073e0e66e5bf42 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 5 Jun 2019 21:40:21 +0300 Subject: [PATCH] Strip auto-hashtag in support mode message editing. --- .../SourceFiles/boxes/edit_caption_box.cpp | 6 +-- .../chat_helpers/message_field.cpp | 40 +++++++++++++++++++ .../SourceFiles/chat_helpers/message_field.h | 1 + .../SourceFiles/history/history_widget.cpp | 14 ++----- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/Telegram/SourceFiles/boxes/edit_caption_box.cpp b/Telegram/SourceFiles/boxes/edit_caption_box.cpp index cf3b181484..110680a618 100644 --- a/Telegram/SourceFiles/boxes/edit_caption_box.cpp +++ b/Telegram/SourceFiles/boxes/edit_caption_box.cpp @@ -75,11 +75,7 @@ EditCaptionBox::EditCaptionBox( } doc = document; } - const auto original = item->originalText(); - const auto editData = TextWithTags { - original.text, - ConvertEntitiesToTextTags(original.entities) - }; + const auto editData = PrepareEditText(item); if (!_animated && (dimensions.isEmpty() || doc || !image)) { if (!image) { diff --git a/Telegram/SourceFiles/chat_helpers/message_field.cpp b/Telegram/SourceFiles/chat_helpers/message_field.cpp index 73be90b1a3..96aa311258 100644 --- a/Telegram/SourceFiles/chat_helpers/message_field.cpp +++ b/Telegram/SourceFiles/chat_helpers/message_field.cpp @@ -8,6 +8,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "chat_helpers/message_field.h" #include "history/history_widget.h" +#include "history/history.h" // History::session +#include "history/history_item.h" // HistoryItem::originalText #include "base/qthelp_regex.h" #include "base/qthelp_url.h" #include "boxes/abstract_box.h" @@ -172,6 +174,34 @@ void EditLinkBox::prepare() { }; } +TextWithEntities StripSupportHashtag(TextWithEntities &&text) { + static const auto expression = QRegularExpression( + qsl("\\n?#tsf[a-z0-9_-]*[\\s#a-z0-9_-]*$"), + QRegularExpression::CaseInsensitiveOption); + const auto match = expression.match(text.text); + if (!match.hasMatch()) { + return text; + } + text.text.chop(match.capturedLength()); + const auto length = text.text.size(); + if (!length) { + return TextWithEntities(); + } + for (auto i = text.entities.begin(); i != text.entities.end();) { + auto &entity = *i; + if (entity.offset() >= length) { + i = text.entities.erase(i); + } else if (entity.offset() + entity.length() > length) { + entity.shrinkFromRight(length - entity.offset()); + ++i; + } + } + if (!text.text.isEmpty() && !text.text.endsWith('\n')) { + text.text.append('\n'); + } + return text; +} + } // namespace QString ConvertTagToMimeTag(const QString &tagId) { @@ -286,6 +316,16 @@ void SetClipboardText( } } +TextWithTags PrepareEditText(not_null item) { + const auto original = item->history()->session().supportMode() + ? StripSupportHashtag(item->originalText()) + : item->originalText(); + return TextWithTags{ + original.text, + ConvertEntitiesToTextTags(original.entities) + }; +} + Fn MimeDataFromText(const TextForMimeData &text); void SetClipboardText( const TextForMimeData &text, QClipboard::Mode mode = QClipboard::Clipboard); +TextWithTags PrepareEditText(not_null item); Fn item) { } } - const auto original = item->originalText(); - const auto editData = TextWithTags { - original.text, - ConvertEntitiesToTextTags(original.entities) - }; + const auto editData = PrepareEditText(item); const auto cursor = MessageCursor { editData.text.size(), editData.text.size(), @@ -6353,12 +6349,8 @@ void HistoryWidget::escape() { } else if (_isInlineBot) { onInlineBotCancel(); } else if (_editMsgId) { - auto original = _replyEditMsg ? _replyEditMsg->originalText() : TextWithEntities(); - auto editData = TextWithTags{ - original.text, - ConvertEntitiesToTextTags(original.entities) - }; - if (_replyEditMsg && editData != _field->getTextWithTags()) { + if (_replyEditMsg + && PrepareEditText(_replyEditMsg) != _field->getTextWithTags()) { Ui::show(Box( lang(lng_cancel_edit_post_sure), lang(lng_cancel_edit_post_yes),