From 7481643453fa62302894b2e9bebcc446b62052fb Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 28 Mar 2019 15:54:47 +0100 Subject: [PATCH] Validation error when trying to add an already existing contact --- dist/converse.js | 4 ++++ spec/controlbox.js | 11 ++++++++++- src/converse-rosterview.js | 4 ++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/dist/converse.js b/dist/converse.js index 59dfd8ff0..bbc9de27a 100644 --- a/dist/converse.js +++ b/dist/converse.js @@ -59173,6 +59173,10 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_4__["default"].plugins el.textContent = __('You cannot add yourself as a contact'); u.addClass('d-block', el); return false; + } else if (_converse.roster.get(Strophe.getBareJidFromJid(jid))) { + el.textContent = __('This contact has already been added'); + u.addClass('d-block', el); + return false; } u.removeClass('d-block', el); diff --git a/spec/controlbox.js b/spec/controlbox.js index 7a45b15f0..81e929dda 100644 --- a/spec/controlbox.js +++ b/spec/controlbox.js @@ -317,12 +317,16 @@ 'xhr_user_search_url': 'http://example.org/?' }, async function (done, _converse) { + test_utils.createContacts(_converse, 'all').openControlBox(); var modal; const xhr = { 'open': _.noop, 'send': function () { const value = modal.el.querySelector('input[name="name"]').value; - if (value === 'dummy') { + if (value === 'existing') { + const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; + xhr.responseText = JSON.stringify([{"jid": contact_jid, "fullname": mock.cur_names[0]}]); + } else if (value === 'dummy') { xhr.responseText = JSON.stringify([{"jid": "dummy@localhost", "fullname": "Max Mustermann"}]); } else if (value === 'ambiguous') { xhr.responseText = JSON.stringify([ @@ -375,6 +379,11 @@ feedback_el = modal.el.querySelector('.invalid-feedback'); expect(feedback_el.textContent).toBe('You cannot add yourself as a contact'); + input_el.value = 'existing'; + modal.el.querySelector('button[type="submit"]').click(); + feedback_el = modal.el.querySelector('.invalid-feedback'); + expect(feedback_el.textContent).toBe('This contact has already been added'); + input_el.value = 'Marty McFly'; modal.el.querySelector('button[type="submit"]').click(); expect(sent_stanza.toLocaleString()).toEqual( diff --git a/src/converse-rosterview.js b/src/converse-rosterview.js index 0a6cf536f..8506018f5 100644 --- a/src/converse-rosterview.js +++ b/src/converse-rosterview.js @@ -215,6 +215,10 @@ converse.plugins.add('converse-rosterview', { el.textContent = __('You cannot add yourself as a contact') u.addClass('d-block', el); return false; + } else if (_converse.roster.get(Strophe.getBareJidFromJid(jid))) { + el.textContent = __('This contact has already been added') + u.addClass('d-block', el); + return false; } u.removeClass('d-block', el); return true;