From 7940ef24ab468e0fb92055c96dd9033e2fbc01ba Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 6 Mar 2018 18:29:45 +0300 Subject: [PATCH] Disallow hashtags of digits only. --- .../SourceFiles/dialogs/dialogs_inner_widget.cpp | 6 +++++- Telegram/SourceFiles/mainwidget.cpp | 6 +++++- Telegram/SourceFiles/ui/text/text_entity.cpp | 16 ++++++++++++++++ Telegram/SourceFiles/ui/text/text_entity.h | 1 + 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp index 1608e0cab..5578d2bc5 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp @@ -2487,12 +2487,16 @@ void DialogsInner::saveRecentHashtags(const QString &text) { --next; } } + const auto tag = text.mid(i + 1, next - i - 1); + if (TextUtilities::RegExpHashtagExclude().match(tag).hasMatch()) { + continue; + } if (!found && cRecentWriteHashtags().isEmpty() && cRecentSearchHashtags().isEmpty()) { Local::readRecentHashtagsAndBots(); recent = cRecentSearchHashtags(); } found = true; - Stickers::IncrementRecentHashtag(recent, text.mid(i + 1, next - i - 1)); + Stickers::IncrementRecentHashtag(recent, tag); } if (found) { cSetRecentSearchHashtags(recent); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 598be8182..086ec7b37 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -1365,12 +1365,16 @@ void MainWidget::saveRecentHashtags(const QString &text) { --next; } } + const auto tag = text.mid(i + 1, next - i - 1); + if (TextUtilities::RegExpHashtagExclude().match(tag).hasMatch()) { + continue; + } if (!found && cRecentWriteHashtags().isEmpty() && cRecentSearchHashtags().isEmpty()) { Local::readRecentHashtagsAndBots(); recent = cRecentWriteHashtags(); } found = true; - Stickers::IncrementRecentHashtag(recent, text.mid(i + 1, next - i - 1)); + Stickers::IncrementRecentHashtag(recent, tag); } if (found) { cSetRecentWriteHashtags(recent); diff --git a/Telegram/SourceFiles/ui/text/text_entity.cpp b/Telegram/SourceFiles/ui/text/text_entity.cpp index 03a036dd1..c8f84eb8f 100644 --- a/Telegram/SourceFiles/ui/text/text_entity.cpp +++ b/Telegram/SourceFiles/ui/text/text_entity.cpp @@ -40,6 +40,10 @@ QString ExpressionHashtag() { return qsl("(^|[") + ExpressionSeparators(qsl("`\\*/")) + qsl("])#[\\w]{2,64}([\\W]|$)"); } +QString ExpressionHashtagExclude() { + return qsl("^#?\\d+$"); +} + QString ExpressionMention() { return qsl("(^|[") + ExpressionSeparators(qsl("`\\*/")) + qsl("])@[A-Za-z_0-9]{1,32}([\\W]|$)"); } @@ -1141,6 +1145,11 @@ const QRegularExpression &RegExpHashtag() { return result; } +const QRegularExpression &RegExpHashtagExclude() { + static const auto result = CreateRegExp(ExpressionHashtagExclude()); + return result; +} + const QRegularExpression &RegExpMention() { static const auto result = CreateRegExp(ExpressionMention()); return result; @@ -1844,6 +1853,13 @@ void ParseEntities(TextWithEntities &result, int32 flags, bool rich) { if (!mHashtag.capturedRef(2).isEmpty()) { --hashtagEnd; } + if (RegExpHashtagExclude().match( + result.text.mid( + hashtagStart + 1, + hashtagEnd - hashtagStart - 1)).hasMatch()) { + hashtagStart = INT_MAX; + hashtagEnd = INT_MAX; + } } while (mMention.hasMatch()) { if (!mMention.capturedRef(1).isEmpty()) { diff --git a/Telegram/SourceFiles/ui/text/text_entity.h b/Telegram/SourceFiles/ui/text/text_entity.h index 33fd8bf6c..1dcc9a796 100644 --- a/Telegram/SourceFiles/ui/text/text_entity.h +++ b/Telegram/SourceFiles/ui/text/text_entity.h @@ -160,6 +160,7 @@ const QRegularExpression &RegExpDomain(); const QRegularExpression &RegExpDomainExplicit(); const QRegularExpression &RegExpMailNameAtEnd(); const QRegularExpression &RegExpHashtag(); +const QRegularExpression &RegExpHashtagExclude(); const QRegularExpression &RegExpMention(); const QRegularExpression &RegExpBotCommand(); const QRegularExpression &RegExpMarkdownBold();