Commit b6dabf73 authored by JC Brand's avatar JC Brand

muc: Ensure that exact nicknames (and not substrings) are matched

parent e2fbfa74
......@@ -991,7 +991,7 @@ describe("A Groupchat Message", function () {
const muc_jid = 'lounge@montague.lit';
await mock.openAndEnterChatRoom(_converse, muc_jid, 'tom');
const view = _converse.api.chatviews.get(muc_jid);
['z3r0', 'mr.robot', 'gibson', 'sw0rdf1sh', 'Link Mauve'].forEach((nick) => {
['z3r0', 'mr.robot', 'gibson', 'sw0rdf1sh', 'Link Mauve', 'robot'].forEach((nick) => {
_converse.connection._dataRecv(mock.createRequest(
$pres({
'to': 'tom@montague.lit/resource',
......@@ -1017,7 +1017,12 @@ describe("A Groupchat Message", function () {
await view.model.handleMessageStanza(stanza);
// Run a few unit tests for the parseTextForReferences method
let [text, references] = view.model.parseTextForReferences('hello z3r0')
let [text, references] = view.model.parseTextForReferences('yo @robot')
expect(text).toBe('yo robot');
expect(references)
.toEqual([{"begin":3,"end":8,"value":"robot","type":"mention","uri":"xmpp:robot@montague.lit"}]);
[text, references] = view.model.parseTextForReferences('hello z3r0')
expect(references.length).toBe(0);
expect(text).toBe('hello z3r0');
......
......@@ -965,7 +965,7 @@ converse.plugins.add('converse-muc', {
const known_nicknames = this.getAllKnownNicknames();
const known_nicknames_with_at_regex = this.getAllKnownNicknamesRegex();
const getMatchesForNickRegex = nick_regex => [...findRegexInMessage(nick_regex)];
const getNicknameFromRegex = p.findFirstMatchInArray(known_nicknames);
const getMatchingNickname = p.findFirstMatchInArray(known_nicknames);
const uriFromNickname = nickname => {
const jid = this.get('jid');
......@@ -978,7 +978,7 @@ converse.plugins.add('converse-muc', {
const at_sign_index = match[0].indexOf('@');
const begin = match.index + at_sign_index;
const end = begin + match[0].length - at_sign_index;
const value = getNicknameFromRegex(RegExp(match[1], 'i'));
const value = getMatchingNickname(match[1]);
const type = 'mention';
const uri = uriFromNickname(value);
return { begin, end, value, type, uri }
......
......@@ -19,9 +19,9 @@ helpers.escapeCharacters = characters => string =>
helpers.escapeRegexString = helpers.escapeCharacters('[\\^$.?*+(){}');
// `for` is ~25% faster than using `Array.find()`
helpers.findFirstMatchInArray = array => regex => {
helpers.findFirstMatchInArray = array => text => {
for (let i = 0; i < array.length; i++) {
if (regex.test(array[i])) {
if (text.localeCompare(array[i], undefined, {sensitivity: 'base'}) === 0) {
return array[i];
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment