diff --git a/src/headless/plugins/blocking/api.js b/src/headless/plugins/blocking/api.js index 7141629949..0cb9ad2562 100644 --- a/src/headless/plugins/blocking/api.js +++ b/src/headless/plugins/blocking/api.js @@ -51,50 +51,52 @@ export default { * @method api.handleBlockingStanza * @param { Object } [stanza] - The incoming stanza to handle */ - handleBlockingStanza (stanza) { - if (stanza.firstElementChild.tagName === 'block') { - const users_to_block = sizzle('item', stanza).map((item) => item.getAttribute('jid')); - users_to_block.forEach(_converse.blocked.get('set').add, _converse.blocked.get('set')); - } else if (stanza.firstElementChild.tagName === 'unblock') { - const users_to_unblock = sizzle('item', stanza).map((item) => item.getAttribute('jid')); - users_to_unblock.forEach(_converse.blocked.get('set').delete, _converse.blocked.get('set')); + handleBlockingStanza ( stanza ) { + const action = stanza.firstElementChild.tagName; + const items = sizzle('item', stanza).map(item => item.getAttribute('jid')); + if (action == 'block') { + items.forEach((item) => { _converse.blocked.get('set').add(item)}); + } else if (action == 'unblock') { + items.forEach((item) => { _converse.blocked.get('set').delete(item)}); } else { - log.error('Received blocklist push update but could not interpret it.'); + log.error("Received a blocklist push update but could not interpret it."); } // TODO: Fix this to not use the length as an update key, and // use a more accurate update method, like a length-extendable hash _converse.blocked.set({ 'len': _converse.blocked.get('set').size }); + + return true; }, /** - * Blocks JIDs by sending an IQ stanza - * @method api.blockUser - * - * @param { Array } [jid_list] - The list of JIDs to block + * Send block/unblock IQ stanzas to the server for the JID specified + * @method api.sendBlockingStanza + * @param { String } action - "block" or "unblock" + * @param { Array } [jid_list] - The list of JIDs to block or unblock */ - async blockUser (jid_list) { - if (!_converse.disco_entities.get(_converse.domain)?.features?.findWhere({ 'var': Strophe.NS.BLOCKING })) { + async sendBlockingStanza ( action, jid_list ) { + if (!_converse.disco_entities.get(_converse.domain)?.features?.findWhere({'var': Strophe.NS.BLOCKING})) { + log.error("block/unblock not supported, aborting"); return false; } if (!_converse.connection) { + log.error("connection down, cannot block/unblock"); return false; } - const block_items = jid_list.map((jid) => Strophe.xmlElement('item', { 'jid': jid })); - const block_element = Strophe.xmlElement('block', { 'xmlns': Strophe.NS.BLOCKING }); - - block_items.forEach(block_element.appendChild, block_element); + const element = Strophe.xmlElement(action, {'xmlns': Strophe.NS.BLOCKING}); + jid_list.forEach((jid) => { + const item = Strophe.xmlElement('item', { 'jid': jid }); + element.append(item); + }); const iq = $iq({ - 'type': 'set', - 'id': u.getUniqueId('block'), - }).cnode(block_element); + 'type': 'set', + 'id': u.getUniqueId(action) + }).cnode(element); - const result = await api.sendIQ(iq).catch((e) => { - log.fatal(e); - return false; - }); - const err_msg = `An error occured while trying to block user(s) ${jid_list}`; + const result = await api.sendIQ(iq).catch(e => { log.fatal(e); return false }); + const err_msg = `An error occured while trying to ${action} user(s) ${jid_list}`; if (result === null) { api.alert('error', __('Error'), err_msg); log(err_msg, Strophe.LogLevel.WARN); @@ -108,43 +110,13 @@ export default { }, /** - * Unblocks JIDs by sending an IQ stanza to the server JID specified - * @method api.unblockUser - * @param { Array } [jid_list] - The list of JIDs to unblock + * Blocks JIDs by sending an IQ stanza + * @method api.blockUser + * + * @param { Array } [jid_list] - The list of JIDs to block */ - async unblockUser (jid_list) { - if (!_converse.disco_entities.get(_converse.domain)?.features?.findWhere({ 'var': Strophe.NS.BLOCKING })) { - return false; - } - if (!_converse.connection) { - return false; - } - - const unblock_items = jid_list.map((jid) => Strophe.xmlElement('item', { 'jid': jid })); - const unblock_element = Strophe.xmlElement('unblock', { 'xmlns': Strophe.NS.BLOCKING }); - - unblock_items.forEach(unblock_element.append, unblock_element); - - const iq = $iq({ - 'type': 'set', - 'id': u.getUniqueId('block'), - }).cnode(unblock_element); - - const result = await api.sendIQ(iq).catch((e) => { - log.fatal(e); - return false; - }); - const err_msg = `An error occured while trying to unblock user(s) ${jid_list}`; - if (result === null) { - api.alert('error', __('Error'), err_msg); - log(err_msg, Strophe.LogLevel.WARN); - return false; - } else if (u.isErrorStanza(result)) { - log.error(err_msg); - log.error(result); - return false; - } - return true; + blockUser ( jid_list ) { + return api.sendBlockingStanza( "block", jid_list ); }, /** @@ -154,4 +126,13 @@ export default { blockedUsers () { return _converse.blocked.get('set'); }, -}; + + /** + * Unblocks JIDs by sending an IQ stanza to the server JID specified + * @method api.unblockUser + * @param { Array } [jid_list] - The list of JIDs to unblock + */ + unblockUser ( jid_list ) { + return api.sendBlockingStanza( "unblock", jid_list ); + } +} diff --git a/src/headless/plugins/blocking/utils.js b/src/headless/plugins/blocking/utils.js index f8faedb98c..45312d0ee8 100644 --- a/src/headless/plugins/blocking/utils.js +++ b/src/headless/plugins/blocking/utils.js @@ -4,5 +4,5 @@ const { Strophe } = converse.env; export function onConnected () { api.refreshBlocklist(); - _converse.connection.addHandler(api.handleBlockingStanza, Strophe.NS.BLOCKING, 'iq', 'set', null, null); + _converse.connection.addHandler(api.handleBlockingStanza, Strophe.NS.BLOCKING, 'iq', 'set'); }