From 8c67a4b991ab12c8f5deeee1ae073e0e66e5bf42 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
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 cf3b18148..110680a61 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 73be90b1a..96aa31125 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<HistoryItem*> item) {
+	const auto original = item->history()->session().supportMode()
+		? StripSupportHashtag(item->originalText())
+		: item->originalText();
+	return TextWithTags{
+		original.text,
+		ConvertEntitiesToTextTags(original.entities)
+	};
+}
+
 Fn<bool(
 	Ui::InputField::EditLinkSelection selection,
 	QString text,
diff --git a/Telegram/SourceFiles/chat_helpers/message_field.h b/Telegram/SourceFiles/chat_helpers/message_field.h
index b624f4b2f..f047c01ed 100644
--- a/Telegram/SourceFiles/chat_helpers/message_field.h
+++ b/Telegram/SourceFiles/chat_helpers/message_field.h
@@ -25,6 +25,7 @@ std::unique_ptr<QMimeData> MimeDataFromText(const TextForMimeData &text);
 void SetClipboardText(
 	const TextForMimeData &text,
 	QClipboard::Mode mode = QClipboard::Clipboard);
+TextWithTags PrepareEditText(not_null<HistoryItem*> item);
 
 Fn<bool(
 	Ui::InputField::EditLinkSelection selection,
diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp
index b6a6bcf07..d7328a2ad 100644
--- a/Telegram/SourceFiles/history/history_widget.cpp
+++ b/Telegram/SourceFiles/history/history_widget.cpp
@@ -5879,11 +5879,7 @@ void HistoryWidget::editMessage(not_null<HistoryItem*> 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<ConfirmBox>(
 				lang(lng_cancel_edit_post_sure),
 				lang(lng_cancel_edit_post_yes),