2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-09-03 08:05:12 +00:00

Strip auto-hashtag in support mode message editing.

This commit is contained in:
John Preston
2019-06-05 21:40:21 +03:00
parent 126ffc8769
commit 8c67a4b991
4 changed files with 45 additions and 16 deletions

View File

@@ -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,