1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

fix: empty username causes mention on ever message

This fixes #2119 and additionally introduces the possibility to mention
users by their public key.
This commit is contained in:
sudden6 2019-08-10 23:06:47 +02:00 committed by Anthony Bilinski
parent 74377430ce
commit db802822f3
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
3 changed files with 26 additions and 1 deletions

View File

@ -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<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;
}

View File

@ -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);

View File

@ -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()