fix(Chat): ensure only mentions with leading whitespace are replaced

This commit is contained in:
Pascal Precht 2020-08-31 12:08:38 +02:00 committed by Pascal Precht
parent e95e052575
commit fb83886086
1 changed files with 11 additions and 2 deletions

View File

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