From db802822f3b2625ffa19d491d786bdb72666da49 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Sat, 10 Aug 2019 23:06:47 +0200 Subject: [PATCH] fix: empty username causes mention on ever message This fixes #2119 and additionally introduces the possibility to mention users by their public key. --- src/model/message.cpp | 19 ++++++++++++++++++- src/model/message.h | 6 ++++++ src/widget/widget.cpp | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/model/message.cpp b/src/model/message.cpp index f28cd2605..6aa1abc92 100644 --- a/src/model/message.cpp +++ b/src/model/message.cpp @@ -31,6 +31,17 @@ void MessageProcessor::SharedParams::onUserNameSet(const QString& username) QRegularExpression::CaseInsensitiveOption); } +/** + * @brief Set the public key on which a message should be highlighted + * @param pk ToxPk in its hex string form + */ +void MessageProcessor::SharedParams::setPublicKey(const QString& pk) +{ + // no sanitization needed, we expect a ToxPk in its string form + pubKeyMention = QRegularExpression("\\b" + pk + "\\b", + QRegularExpression::CaseInsensitiveOption); +} + MessageProcessor::MessageProcessor(const MessageProcessor::SharedParams& sharedParams) : sharedParams(sharedParams) {} @@ -73,8 +84,9 @@ Message MessageProcessor::processIncomingMessage(bool isAction, QString const& m if (detectingMentions) { auto nameMention = sharedParams.GetNameMention(); auto sanitizedNameMention = sharedParams.GetSanitizedNameMention(); + auto pubKeyMention = sharedParams.GetPublicKeyMention(); - for (auto const& mention : {nameMention, sanitizedNameMention}) { + for (auto const& mention : {nameMention, sanitizedNameMention, pubKeyMention}) { auto matchIt = mention.globalMatch(ret.content); if (!matchIt.hasNext()) { continue; @@ -85,6 +97,11 @@ Message MessageProcessor::processIncomingMessage(bool isAction, QString const& m auto pos = static_cast(match.capturedStart()); auto length = static_cast(match.capturedLength()); + // skip matches on empty usernames + if (length == 0) { + continue; + } + ret.metadata.push_back({MessageMetadataType::selfMention, pos, pos + length}); break; } diff --git a/src/model/message.h b/src/model/message.h index 6a40f726e..0dd9863b7 100644 --- a/src/model/message.h +++ b/src/model/message.h @@ -75,11 +75,17 @@ public: { return sanitizedNameMention; } + QRegularExpression GetPublicKeyMention() const + { + return pubKeyMention; + } void onUserNameSet(const QString& username); + void setPublicKey(const QString& pk); private: QRegularExpression nameMention; QRegularExpression sanitizedNameMention; + QRegularExpression pubKeyMention; }; MessageProcessor(const SharedParams& sharedParams); diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index bae92a665..eaf9a0590 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -691,6 +691,8 @@ void Widget::onCoreChanged(Core& core) connect(this, &Widget::statusSet, &core, &Core::setStatus); connect(this, &Widget::friendRequested, &core, &Core::requestFriendship); connect(this, &Widget::friendRequestAccepted, &core, &Core::acceptFriendRequest); + + sharedMessageProcessorParams.setPublicKey(core.getSelfPublicKey().toString()); } void Widget::onConnected()