From 05c5cd1046551bead1d075b600c20aed04f65758 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Wed, 22 Feb 2023 15:21:56 +0100 Subject: [PATCH] Remove call to `api.confirm` in `@converse/headless` --- CHANGES.md | 1 + src/headless/plugins/muc/utils.js | 28 +++++++++++++--------------- src/plugins/muc-views/index.js | 3 ++- src/plugins/muc-views/utils.js | 20 ++++++++++++++++++++ 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 63f438892..44e86770c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,7 @@ - Add the ability to set roles and affiliations via the MUC occupant modal - Fix `isOnlyEmojis is not a function` when using only `@converse/headless` - Fix `autojoin` checkbox state in MUC bookmark form +- Remove call to `api.confirm` in `@converse/headless` ## 10.1.2 (2023-02-17) diff --git a/src/headless/plugins/muc/utils.js b/src/headless/plugins/muc/utils.js index c96d6c6a5..2b08c48db 100644 --- a/src/headless/plugins/muc/utils.js +++ b/src/headless/plugins/muc/utils.js @@ -88,7 +88,6 @@ export async function openChatRoom (jid, settings) { * @param { XMLElement } message - The message stanza containing the invitation. */ export async function onDirectMUCInvitation (message) { - const { __ } = _converse; const x_el = sizzle('x[xmlns="jabber:x:conference"]', message).pop(), from = Strophe.getBareJidFromJid(message.getAttribute('from')), room_jid = x_el.getAttribute('jid'), @@ -99,21 +98,20 @@ export async function onDirectMUCInvitation (message) { result = true; } else { // Invite request might come from someone not your roster list - let contact = _converse.roster.get(from); - contact = contact ? contact.getDisplayName() : from; - if (!reason) { - result = await api.confirm(__('%1$s has invited you to join a groupchat: %2$s', contact, room_jid)); - } else { - result = await api.confirm( - __( - '%1$s has invited you to join a groupchat: %2$s, and left the following reason: "%3$s"', - contact, - room_jid, - reason - ) - ); - } + const contact = _converse.roster.get(from)?.getDisplayName() ?? from; + + /** + * *Hook* which is used to gather confirmation whether a direct MUC + * invitation should be accepted or not. + * + * It's meant for consumers of `@converse/headless` to subscribe to + * this hook and then ask the user to confirm. + * + * @event _converse#confirmDirectMUCInvitation + */ + result = await api.hook('confirmDirectMUCInvitation', { contact, reason, jid: room_jid }, false); } + if (result) { const chatroom = await openChatRoom(room_jid, { 'password': x_el.getAttribute('password') }); if (chatroom.session.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED) { diff --git a/src/plugins/muc-views/index.js b/src/plugins/muc-views/index.js index ac24ad3e2..d7543c16a 100644 --- a/src/plugins/muc-views/index.js +++ b/src/plugins/muc-views/index.js @@ -8,7 +8,7 @@ import './affiliation-form.js'; import './role-form.js'; import MUCView from './muc.js'; import { api, converse } from '@converse/headless/core.js'; -import { clearHistory, parseMessageForMUCCommands } from './utils.js'; +import { clearHistory, confirmDirectMUCInvitation, parseMessageForMUCCommands } from './utils.js'; const { Strophe } = converse.env; @@ -92,5 +92,6 @@ converse.plugins.add('converse-muc-views', { }); api.listen.on('parseMessageForCommands', parseMessageForMUCCommands); + api.listen.on('confirmDirectMUCInvitation', confirmDirectMUCInvitation); } }); diff --git a/src/plugins/muc-views/utils.js b/src/plugins/muc-views/utils.js index 745c5063c..2eea173af 100644 --- a/src/plugins/muc-views/utils.js +++ b/src/plugins/muc-views/utils.js @@ -24,6 +24,26 @@ const COMMAND_TO_ROLE = { 'voice': 'participant' }; +/** + * @async + * Presents a confirmation modal to the user asking them to accept or decline a + * MUC invitation. + */ +export function confirmDirectMUCInvitation ({ contact, jid, reason }) { + if (!reason) { + return api.confirm(__('%1$s has invited you to join a groupchat: %2$s', contact, jid)); + } else { + return api.confirm( + __( + '%1$s has invited you to join a groupchat: %2$s, and left the following reason: "%3$s"', + contact, + jid, + reason + ) + ); + } +} + export function clearHistory (jid) { if (_converse.router.history.getFragment() === `converse/room?jid=${jid}`) { _converse.router.navigate('');