浏览代码

Move adhoc plugin into own directory with separate files

JC Brand 2 年之前
父节点
当前提交
5db3e8ca51

+ 1 - 1
src/headless/headless.js

@@ -2,13 +2,13 @@
  * --------------------
  * Any of the following components may be removed if they're not needed.
  */
-import "./plugins/adhoc.js";            // XEP-0050 Ad Hoc Commands
 import "./plugins/bookmarks/index.js";  // XEP-0199 XMPP Ping
 import "./plugins/bosh.js";             // XEP-0206 BOSH
 import "./plugins/caps/index.js";       // XEP-0115 Entity Capabilities
 import "./plugins/chat/index.js";       // RFC-6121 Instant messaging
 import "./plugins/chatboxes/index.js";
 import "./plugins/disco/index.js";      // XEP-0030 Service discovery
+import "./plugins/adhoc/index.js";      // XEP-0050 Ad Hoc Commands
 import "./plugins/headlines/index.js";  // Support for headline messages
 import "./plugins/mam/index.js";        // XEP-0313 Message Archive Management
 import "./plugins/muc/index.js";        // XEP-0045 Multi-user chat

+ 1 - 1
src/headless/log.js

@@ -1,4 +1,4 @@
-import isElement from 'lodash-es/isElement';
+import { isElement } from './utils/core.js';
 
 const LEVELS = {
     'debug': 0,

+ 11 - 45
src/headless/plugins/adhoc.js → src/headless/plugins/adhoc/api.js

@@ -1,34 +1,11 @@
-import log from "@converse/headless/log";
-import sizzle from 'sizzle';
-import { __ } from 'i18n';
-import { converse } from "../core.js";
-import { getAttributes } from '@converse/headless/shared/parsers';
+import log from '@converse/headless/log';
+import { _converse, api, converse } from "@converse/headless/core";
+import { getCommandFields, parseForCommands } from './utils.js';
 
-const { Strophe, u, stx, $iq } = converse.env;
-let api;
+const { Strophe, $iq, u, stx } = converse.env;
 
-Strophe.addNamespace('ADHOC', 'http://jabber.org/protocol/commands');
 
-
-function parseForCommands (stanza) {
-    const items = sizzle(`query[xmlns="${Strophe.NS.DISCO_ITEMS}"][node="${Strophe.NS.ADHOC}"] item`, stanza);
-    return items.map(getAttributes)
-}
-
-function getCommandFields (iq, jid) {
-    const cmd_el = sizzle(`command[xmlns="${Strophe.NS.ADHOC}"]`, iq).pop();
-    const data = {
-        sessionid: cmd_el.getAttribute('sessionid'),
-        instructions: sizzle('x[type="form"][xmlns="jabber:x:data"] instructions', cmd_el).pop()?.textContent,
-        fields: sizzle('x[type="form"][xmlns="jabber:x:data"] field', cmd_el)
-            .map(f => u.xForm2TemplateResult(f, cmd_el, { domain: jid })),
-        actions: Array.from(cmd_el.querySelector('actions')?.children).map((a) => a.nodeName.toLowerCase()) ?? []
-    }
-    return data;
-}
-
-
-const adhoc_api = {
+export default {
     /**
      * The XEP-0050 Ad-Hoc Commands API
      *
@@ -80,6 +57,7 @@ const adhoc_api = {
                     log.error(`Error while trying to execute command for ${jid}`);
                     log.error(e);
                 }
+                const { __ } = _converse;
                 return {
                     instructions: __('An error occurred while trying to fetch the command form'),
                     fields: []
@@ -99,15 +77,17 @@ const adhoc_api = {
             const iq =
                 stx`<iq type="set" to="${jid}" xmlns="jabber:client">
                     <command sessionid="${sessionid}" node="${node}" action="${action}" xmlns="${Strophe.NS.ADHOC}">
-                        <x xmlns="${Strophe.NS.XFORM}" type="submit">
-                            ${ inputs.reduce((out, { name, value }) => out + `<field var="${name}"><value>${value}</value></field>`, '') }
-                        </x>
+                        ${ !['cancel', 'prev'].includes(action) ? stx`
+                            <x xmlns="${Strophe.NS.XFORM}" type="submit">
+                                ${ inputs.reduce((out, { name, value }) => out + `<field var="${name}"><value>${value}</value></field>`, '') }
+                            </x>` : '' }
                     </command>
                 </iq>`;
 
             const result = await api.sendIQ(iq, null, false);
             if (result === null) {
                 log.warn(`A timeout occurred while trying to run an ad-hoc command`);
+                const { __ } = _converse;
                 return {
                     status: 'error',
                     note: __('A timeout occurred'),
@@ -127,17 +107,3 @@ const adhoc_api = {
         }
     }
 }
-
-
-converse.plugins.add('converse-adhoc', {
-
-    dependencies: ["converse-disco"],
-
-    initialize () {
-        const _converse = this._converse;
-        api  = _converse.api;
-        Object.assign(api, adhoc_api);
-    }
-});
-
-export default adhoc_api;

+ 16 - 0
src/headless/plugins/adhoc/index.js

@@ -0,0 +1,16 @@
+import adhoc_api from './api.js';
+import { converse } from "@converse/headless/core";
+
+const { Strophe } = converse.env;
+
+Strophe.addNamespace('ADHOC', 'http://jabber.org/protocol/commands');
+
+
+converse.plugins.add('converse-adhoc', {
+
+    dependencies: ["converse-disco"],
+
+    initialize () {
+        Object.assign(this._converse.api, adhoc_api);
+    }
+});

+ 22 - 0
src/headless/plugins/adhoc/utils.js

@@ -0,0 +1,22 @@
+import sizzle from 'sizzle';
+import { converse } from "@converse/headless/core";
+import { getAttributes } from '@converse/headless/shared/parsers';
+
+const { Strophe, u } = converse.env;
+
+export function parseForCommands (stanza) {
+    const items = sizzle(`query[xmlns="${Strophe.NS.DISCO_ITEMS}"][node="${Strophe.NS.ADHOC}"] item`, stanza);
+    return items.map(getAttributes)
+}
+
+export function getCommandFields (iq, jid) {
+    const cmd_el = sizzle(`command[xmlns="${Strophe.NS.ADHOC}"]`, iq).pop();
+    const data = {
+        sessionid: cmd_el.getAttribute('sessionid'),
+        instructions: sizzle('x[type="form"][xmlns="jabber:x:data"] instructions', cmd_el).pop()?.textContent,
+        fields: sizzle('x[type="form"][xmlns="jabber:x:data"] field', cmd_el)
+            .map(f => u.xForm2TemplateResult(f, cmd_el, { domain: jid })),
+        actions: Array.from(cmd_el.querySelector('actions')?.children).map((a) => a.nodeName.toLowerCase()) ?? []
+    }
+    return data;
+}