Skip to content

Commit

Permalink
refactor block/unblock code
Browse files Browse the repository at this point in the history
* remove duplicate code: use sendBlockingStanza() for block and unblock
* return true in handleBlockingStanza() for handler to be invoked again
* change Array.forEach() to avoid garbage strings in <block>/<unblock>
  • Loading branch information
udanieli committed May 21, 2023
1 parent 31df476 commit 5b67845
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 64 deletions.
107 changes: 44 additions & 63 deletions src/headless/plugins/blocking/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 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);
Expand All @@ -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 );
},

/**
Expand All @@ -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 );
}
}
2 changes: 1 addition & 1 deletion src/headless/plugins/blocking/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

0 comments on commit 5b67845

Please sign in to comment.