فهرست منبع

Move Add-Hoc commands UI code into its own plugin

JC Brand 2 سال پیش
والد
کامیت
4531dd4363

+ 1 - 0
src/converse.js

@@ -18,6 +18,7 @@ import 'shared/styles/index.scss';
  * Any of the following plugin imports may be removed if the plugin is not needed
  */
 import "./plugins/modal/index.js";
+import "./plugins/adhoc-views/index.js";    // Views for XEP-0050 Ad-Hoc commands
 import "./plugins/bookmark-views/index.js"; // Views for XEP-0048 Bookmarks
 import "./plugins/chatview/index.js";       // Renders standalone chat boxes for single user chat
 import "./plugins/controlbox/index.js";     // The control box

+ 0 - 0
src/plugins/muc-views/adhoc-commands.js → src/plugins/adhoc-views/adhoc-commands.js


+ 23 - 0
src/plugins/adhoc-views/index.js

@@ -0,0 +1,23 @@
+/**
+ * @description
+ *  Converse.js plugin which provides the UI for XEP-0050 Ad-Hoc commands
+ * @copyright 2022, the Converse.js contributors
+ * @license Mozilla Public License (MPLv2)
+ */
+import './adhoc-commands.js';
+import { api, converse } from "@converse/headless/core.js";
+
+
+converse.plugins.add('converse-adhoc-views', {
+
+    dependencies: [
+        "converse-controlbox",
+        "converse-muc",
+    ],
+
+    initialize () {
+        api.settings.extend({
+            'allow_adhoc_commands': true,
+        });
+    }
+});

+ 0 - 0
src/plugins/muc-views/templates/ad-hoc-command-form.js → src/plugins/adhoc-views/templates/ad-hoc-command-form.js


+ 0 - 0
src/plugins/muc-views/templates/ad-hoc-command.js → src/plugins/adhoc-views/templates/ad-hoc-command.js


+ 0 - 0
src/plugins/muc-views/templates/ad-hoc.js → src/plugins/adhoc-views/templates/ad-hoc.js


+ 34 - 0
src/plugins/adhoc-views/utils.js

@@ -0,0 +1,34 @@
+import log from "@converse/headless/log";
+import { api, converse } from "@converse/headless/core";
+
+const { Strophe, $iq, sizzle, u } = converse.env;
+
+export async function fetchCommandForm (command) {
+    const node = command.node;
+    const jid = command.jid;
+    const stanza = $iq({
+        'type': 'set',
+        'to': jid
+    }).c('command', {
+        'xmlns': Strophe.NS.ADHOC,
+        'node': node,
+        'action': 'execute'
+    });
+    try {
+        const iq = await api.sendIQ(stanza);
+        const cmd_el = sizzle(`command[xmlns="${Strophe.NS.ADHOC}"]`, iq).pop();
+        command.sessionid = cmd_el.getAttribute('sessionid');
+        command.instructions = sizzle('x[type="form"][xmlns="jabber:x:data"] instructions', cmd_el).pop()?.textContent;
+        command.fields = sizzle('x[type="form"][xmlns="jabber:x:data"] field', cmd_el)
+            .map(f => u.xForm2TemplateResult(f, cmd_el));
+
+    } catch (e) {
+        if (e === null) {
+            log.error(`Error: timeout while trying to execute command for ${jid}`);
+        } else {
+            log.error(`Error while trying to execute command for ${jid}`);
+            log.error(e);
+        }
+        command.fields = [];
+    }
+}

+ 1 - 2
src/plugins/muc-views/index.js

@@ -4,9 +4,8 @@
  * @license Mozilla Public License (MPLv2)
  */
 import '../chatboxviews/index.js';
-import './adhoc-commands.js';
 import MUCView from './muc.js';
-import { api, converse } from '@converse/headless/core';
+import { api, converse } from '@converse/headless/core.js';
 import { clearHistory, parseMessageForMUCCommands } from './utils.js';
 
 const { Strophe } = converse.env;

+ 1 - 32
src/plugins/muc-views/utils.js

@@ -7,7 +7,7 @@ import { _converse, api, converse } from "@converse/headless/core";
 import { html } from "lit";
 import { setAffiliation } from '@converse/headless/plugins/muc/affiliations/utils.js';
 
-const { Strophe, $iq, sizzle, u } = converse.env;
+const { Strophe, u } = converse.env;
 
 const COMMAND_TO_AFFILIATION = {
     'admin': 'admin',
@@ -133,37 +133,6 @@ export async function getAutoCompleteList () {
     return jids;
 }
 
-export async function fetchCommandForm (command) {
-    const node = command.node;
-    const jid = command.jid;
-    const stanza = $iq({
-        'type': 'set',
-        'to': jid
-    }).c('command', {
-        'xmlns': Strophe.NS.ADHOC,
-        'node': node,
-        'action': 'execute'
-    });
-    try {
-        const iq = await api.sendIQ(stanza);
-        const cmd_el = sizzle(`command[xmlns="${Strophe.NS.ADHOC}"]`, iq).pop();
-        command.sessionid = cmd_el.getAttribute('sessionid');
-        command.instructions = sizzle('x[type="form"][xmlns="jabber:x:data"] instructions', cmd_el).pop()?.textContent;
-        command.fields = sizzle('x[type="form"][xmlns="jabber:x:data"] field', cmd_el)
-            .map(f => u.xForm2TemplateResult(f, cmd_el));
-
-    } catch (e) {
-        if (e === null) {
-            log.error(`Error: timeout while trying to execute command for ${jid}`);
-        } else {
-            log.error(`Error while trying to execute command for ${jid}`);
-            log.error(e);
-        }
-        command.fields = [];
-    }
-}
-
-
 function setRole (muc, command, args, required_affiliations = [], required_roles = []) {
     const role = COMMAND_TO_ROLE[command];
     if (!role) {

+ 9 - 6
src/plugins/profile/index.js

@@ -11,15 +11,18 @@ import '@converse/headless/plugins/status';
 import '@converse/headless/plugins/vcard';
 import { api, converse } from '@converse/headless/core';
 
-
 converse.plugins.add('converse-profile', {
-
-    dependencies: ["converse-status", "converse-modal", "converse-vcard", "converse-chatboxviews"],
+    dependencies: [
+        'converse-status',
+        'converse-modal',
+        'converse-vcard',
+        'converse-chatboxviews',
+        'converse-adhoc-views',
+    ],
 
     initialize () {
         api.settings.extend({
-            'allow_adhoc_commands': true,
-            'show_client_info': true
+            'show_client_info': true,
         });
-    }
+    },
 });

+ 1 - 0
src/shared/constants.js

@@ -4,6 +4,7 @@
 // as a whitelist (see src/converse.js) in addition to the
 // CORE_PLUGINS list in src/headless/consts.js.
 export const VIEW_PLUGINS = [
+    'converse-adhoc-views',
     'converse-bookmark-views',
     'converse-chatboxviews',
     'converse-chatview',