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 () { ...@@ -991,7 +991,7 @@ describe("A Groupchat Message", function () {
const muc_jid = 'lounge@montague.lit'; const muc_jid = 'lounge@montague.lit';
await mock.openAndEnterChatRoom(_converse, muc_jid, 'tom'); await mock.openAndEnterChatRoom(_converse, muc_jid, 'tom');
const view = _converse.api.chatviews.get(muc_jid); 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( _converse.connection._dataRecv(mock.createRequest(
$pres({ $pres({
'to': 'tom@montague.lit/resource', 'to': 'tom@montague.lit/resource',
...@@ -1017,7 +1017,12 @@ describe("A Groupchat Message", function () { ...@@ -1017,7 +1017,12 @@ describe("A Groupchat Message", function () {
await view.model.handleMessageStanza(stanza); await view.model.handleMessageStanza(stanza);
// Run a few unit tests for the parseTextForReferences method // 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(references.length).toBe(0);
expect(text).toBe('hello z3r0'); expect(text).toBe('hello z3r0');
......
...@@ -965,7 +965,7 @@ converse.plugins.add('converse-muc', { ...@@ -965,7 +965,7 @@ converse.plugins.add('converse-muc', {
const known_nicknames = this.getAllKnownNicknames(); const known_nicknames = this.getAllKnownNicknames();
const known_nicknames_with_at_regex = this.getAllKnownNicknamesRegex(); const known_nicknames_with_at_regex = this.getAllKnownNicknamesRegex();
const getMatchesForNickRegex = nick_regex => [...findRegexInMessage(nick_regex)]; const getMatchesForNickRegex = nick_regex => [...findRegexInMessage(nick_regex)];
const getNicknameFromRegex = p.findFirstMatchInArray(known_nicknames); const getMatchingNickname = p.findFirstMatchInArray(known_nicknames);
const uriFromNickname = nickname => { const uriFromNickname = nickname => {
const jid = this.get('jid'); const jid = this.get('jid');
...@@ -978,7 +978,7 @@ converse.plugins.add('converse-muc', { ...@@ -978,7 +978,7 @@ converse.plugins.add('converse-muc', {
const at_sign_index = match[0].indexOf('@'); const at_sign_index = match[0].indexOf('@');
const begin = match.index + at_sign_index; const begin = match.index + at_sign_index;
const end = begin + match[0].length - 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 type = 'mention';
const uri = uriFromNickname(value); const uri = uriFromNickname(value);
return { begin, end, value, type, uri } return { begin, end, value, type, uri }
......
...@@ -19,9 +19,9 @@ helpers.escapeCharacters = characters => string => ...@@ -19,9 +19,9 @@ helpers.escapeCharacters = characters => string =>
helpers.escapeRegexString = helpers.escapeCharacters('[\\^$.?*+(){}'); helpers.escapeRegexString = helpers.escapeCharacters('[\\^$.?*+(){}');
// `for` is ~25% faster than using `Array.find()` // `for` is ~25% faster than using `Array.find()`
helpers.findFirstMatchInArray = array => regex => { helpers.findFirstMatchInArray = array => text => {
for (let i = 0; i < array.length; i++) { 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]; 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