Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix: empty username causes mention on ever message
Browse files Browse the repository at this point in the history
This fixes #2119 and additionally introduces the possibility to mention
users by their public key.
  • Loading branch information
sudden6 authored and anthonybilinski committed Aug 27, 2019
1 parent 7437743 commit db80282
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/model/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{}
Expand Down Expand Up @@ -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;
Expand All @@ -85,6 +97,11 @@ Message MessageProcessor::processIncomingMessage(bool isAction, QString const& m
auto pos = static_cast<size_t>(match.capturedStart());
auto length = static_cast<size_t>(match.capturedLength());

// skip matches on empty usernames
if (length == 0) {
continue;
}

ret.metadata.push_back({MessageMetadataType::selfMention, pos, pos + length});
break;
}
Expand Down
6 changes: 6 additions & 0 deletions src/model/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ class MessageProcessor
{
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);
Expand Down
2 changes: 2 additions & 0 deletions src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit db80282

Please sign in to comment.