JC Brand 1 månad sedan
förälder
incheckning
2fa8e187c9

+ 8 - 3
src/headless/plugins/pubsub/api.js

@@ -204,7 +204,7 @@ export default {
          * Creates a PubSub node at a given service
          * @param {string} jid - The PubSub service JID
          * @param {string} node - The node to create
-         * @param {PubSubConfigOptions} config The configuration options
+         * @param {PubSubConfigOptions} [config] The configuration options
          * @returns {Promise<void>}
          */
         async create(jid, node, config) {
@@ -216,14 +216,19 @@ export default {
                     to="${jid}">
                     <pubsub xmlns="http://jabber.org/protocol/pubsub">
                         <create node="${node}"/>
-                        <configure>
+                        ${
+                            config
+                                ? stx`
+                            <configure>
                             <x xmlns="${Strophe.NS.XFORM}" type="submit">
                                 <field var="FORM_TYPE" type="hidden">
                                     <value>${Strophe.NS.PUBSUB}#node_config</value>
                                 </field>
                                 ${Object.entries(config).map(([k, v]) => stx`<field var="pubsub#${k}"><value>${v}</value></field>`)}
                             </x>
-                        </configure>
+                            </configure>`
+                                : ''
+                        }
                     </pubsub>
                 </iq>`;
             return await api.sendIQ(iq);

+ 22 - 4
src/plugins/todo/modals/add-todo-modal.js

@@ -1,8 +1,10 @@
-import { _converse, api } from '@converse/headless';
+import { _converse, api, converse, parsers } from '@converse/headless';
 import BaseModal from 'plugins/modal/modal.js';
 import tplAddTodo from './templates/add-todo-modal.js';
 import { __ } from 'i18n';
 
+const { Strophe } = converse.env;
+
 export default class AddTodoModal extends BaseModal {
     initialize() {
         super.initialize();
@@ -29,13 +31,29 @@ export default class AddTodoModal extends BaseModal {
      */
     async createTodo(ev) {
         ev.preventDefault();
-
         const form = /** @type {HTMLFormElement} */ (ev.target);
         const data = new FormData(form);
         const name = data.get('name');
-        const jid = data.get('jid');
+        const jid = data.get('jid') ?? _converse.state.session.get('domain');
+
+        const service_jids = await api.disco.entities.find(Strophe.NS.PUBSUB, jid);
+        if (service_jids.length === 0) {
+            this.alert(__('Could not find a PubSub service to host your todo list'), 'danger');
+            this.state.set({ manual_jid: true });
+            return;
+        } else if (service_jids.length > 1) {
+            this.alert(__('Found multiple possible PubSub services to host your todo list, please choose one.'));
+            this.state.set({ services: service_jids, manual_jid: true });
+            return;
+        }
 
-        api.pubsub.nodes.create(jid, name);
+        try {
+            await api.pubsub.create(service_jids[0].get('jid'), name);
+        } catch (e) {
+            const err = await parsers.parseErrorStanza(e);
+            this.alert(__('Sorry, an error occurred: %s', err.message), 'danger');
+            return;
+        }
         form.reset();
         this.modal.hide();
     }

+ 27 - 9
src/plugins/todo/modals/templates/add-todo-modal.js

@@ -8,18 +8,36 @@ import { __ } from 'i18n';
 export default (el) => {
     const i18n_create = __('Create');
     const label_name = __('Todo name');
+    const label_service = __('PubSub service (XMPP Address) for your todo list');
 
-    return html` <form
-        class="converse-form add-chatroom needs-validation"
-        @submit=${(ev) => el.createTodo(ev)}
-        novalidate
-    >
+    const pubsub_services = el.state.get('services') ?? [];
+
+    return html`<form class="converse-form" @submit=${(ev) => el.createTodo(ev)}>
         <div class="mb-3">
-            <label for="chatroom" class="form-label">${label_name}:</label>
-            <div class="input-group">
-                <input type="text" class="form-control" id="chatroom" placeholder="${label_name}" required />
-            </div>
+            <label for="todo-name" class="form-label">${label_name}:</label>
+            <input type="text" id="todo-name" name="name" class="form-control" placeholder="${label_name}" required />
         </div>
+
+        ${el.state.get('manual_jid')
+            ? html`<div class="mb-3">
+                  ${pubsub_services.length > 1
+                      ? html`${__('Available PubSub services')}
+                            <ul class="list-group">
+                                ${(pubsub_services ?? []).map((jid) => html`<li class="list-group-item">${jid}</li>`)}
+                            </ul>`
+                      : ''}
+
+                  <label for="todo-jid" class="form-label">${label_service}:</label>
+                  <input
+                      type="text"
+                      id="todo-jid"
+                      name="jid"
+                      class="form-control"
+                      required
+                  />
+              </div>`
+            : ''}
+
         <input type="submit" class="btn btn-primary mt-3" value="${i18n_create || ''}" />
     </form>`;
 };