From fb838860867fa57bc1d22bc7e6a4e4382ae397eb Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Mon, 31 Aug 2020 12:08:38 +0200 Subject: [PATCH] fix(Chat): ensure only mentions with leading whitespace are replaced --- src/app/chat/view.nim | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index 8ddcc0d948..25065e8716 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -102,12 +102,21 @@ QtObject: self.chats.getChannelColor(channel) proc replaceMentionsWithPubKeys(self: ChatsView, mentions: seq[string], contacts: seq[Profile], message: string, predicate: proc (contact: Profile): string): string = - result = message + var updatedMessage = message for mention in mentions: let matches = contacts.filter(c => "@" & predicate(c).toLowerAscii == mention.toLowerAscii).map(c => c.address) if matches.len > 0: let pubKey = matches[0] - result = message.replace(mention, "@" & pubKey) + var startIndex = 0 + var index = updatedMessage.find(mention) + + while index > -1: + if index == 0 or updatedMessage[index-1] == ' ': + updatedMessage = updatedMessage.replaceWord(mention, '@' & pubKey) + startIndex = index + mention.len + index = updatedMessage.find(mention, startIndex) + + result = updatedMessage proc plainText(self: ChatsView, input: string): string {.slot.} = result = plain_text(input)