浏览代码

Remove call to `api.confirm` in `@converse/headless`

JC Brand 2 年之前
父节点
当前提交
05c5cd1046
共有 4 个文件被更改,包括 36 次插入16 次删除
  1. 1 0
      CHANGES.md
  2. 13 15
      src/headless/plugins/muc/utils.js
  3. 2 1
      src/plugins/muc-views/index.js
  4. 20 0
      src/plugins/muc-views/utils.js

+ 1 - 0
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)
 

+ 13 - 15
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) {

+ 2 - 1
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);
     }
 });

+ 20 - 0
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('');