Selaa lähdekoodia

Add a `api.pubsub.create` method to create new nodes

JC Brand 4 viikkoa sitten
vanhempi
commit
f39d5affbd

+ 32 - 2
src/headless/plugins/pubsub/api.js

@@ -81,7 +81,7 @@ export default {
                         <configure node="${node}">
                             <x xmlns="${Strophe.NS.XFORM}" type="submit">
                                 <field var="FORM_TYPE" type="hidden">
-                                    <value>${Strophe.NS.PUBSUB}#nodeconfig</value>
+                                    <value>${Strophe.NS.PUBSUB}#node_config</value>
                                 </field>
                                 ${Object.entries(new_config).map(([k, v]) => stx`<field var="pubsub#${k}"><value>${v}</value></field>`)}
                             </x>
@@ -199,6 +199,36 @@ 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
+         * @returns {Promise<void>}
+         */
+        async create(jid, node, config) {
+            const own_jid = _converse.state.session.get('jid');
+            const iq = stx`
+                <iq xmlns="jabber:client"
+                    type="set"
+                    from="${own_jid}"
+                    to="${jid}">
+                    <pubsub xmlns="http://jabber.org/protocol/pubsub">
+                        <create node="${node}"/>
+                        <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>
+                    </pubsub>
+                </iq>`;
+            return await api.sendIQ(iq);
+        },
+
         /**
          * Subscribes the local user to a PubSub node.
          *
@@ -216,7 +246,7 @@ export default {
                     <subscribe node="${node}" jid="${own_jid}"/>
                   </pubsub>
                 </iq>`;
-            await api.sendIQ(iq);
+            return await api.sendIQ(iq);
         },
 
         /**

+ 46 - 1
src/headless/plugins/pubsub/tests/api.js

@@ -1,7 +1,7 @@
 /* global converse */
 import mock from '../../../tests/mock.js';
 
-const { stx, Strophe } = converse.env;
+const { stx, Strophe, u } = converse.env;
 
 describe('pubsub subscribe/unsubscribe API', function () {
     beforeAll(() => jasmine.addMatchers({ toEqualStanza: jasmine.toEqualStanza }));
@@ -75,4 +75,49 @@ describe('pubsub subscribe/unsubscribe API', function () {
                 </iq>`);
         })
     );
+
+    it(
+        'sends correct IQ for create',
+        mock.initConverse([], {}, async function (_converse) {
+            await mock.waitForRoster(_converse, 'current', 0);
+            const { api, state } = _converse;
+            const own_jid = state.session.get('jid');
+            const sent = api.connection.get().sent_stanzas;
+            const service = 'pubsub.example.org';
+            const node = 'newnode';
+            const config = { access_model: 'open', max_items: '10' };
+            const createPromise = api.pubsub.create(service, node, config);
+            const stanza = await u.waitUntil(() => sent.filter((iq) => iq.querySelector('pubsub create')).pop());
+            expect(stanza).toEqualStanza(stx`
+                <iq type="set"
+                    from="${own_jid}"
+                    to="${service}"
+                    xmlns="jabber:client"
+                    id="${stanza.getAttribute('id')}">
+                  <pubsub xmlns="${Strophe.NS.PUBSUB}">
+                    <create node="${node}"/>
+                    <configure>
+                      <x xmlns="${Strophe.NS.XFORM}" type="submit">
+                        <field var="FORM_TYPE" type="hidden">
+                          <value>${Strophe.NS.PUBSUB}#nodeconfig</value>
+                        </field>
+                        <field var="pubsub#access_model"><value>${config.access_model}</value></field>
+                        <field var="pubsub#max_items"><value>${config.max_items}</value></field>
+                      </x>
+                    </configure>
+                  </pubsub>
+                </iq>`);
+
+            _converse.api.connection.get()._dataRecv(
+                mock.createRequest(stx`
+                <iq type="result"
+                    xmlns="jabber:client"
+                    from="${service}"
+                    to="${_converse.bare_jid}"
+                    id="${stanza.getAttribute('id')}"/>
+            `)
+            );
+            await createPromise;
+        })
+    );
 });

+ 8 - 0
src/headless/types/plugins/pubsub/api.d.ts

@@ -34,6 +34,14 @@ declare namespace _default {
          * @returns {Promise<void|Element>}
          */
         function publish(jid: string, node: string, item: import("strophe.js").Builder | import("strophe.js").Stanza | (import("strophe.js").Builder | import("strophe.js").Stanza)[], options: import("./types").PubSubConfigOptions, strict_options?: boolean): Promise<void | Element>;
+        /**
+         * 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
+         * @returns {Promise<void>}
+         */
+        function create(jid: string, node: string, config: import("./types").PubSubConfigOptions): Promise<void>;
         /**
          * Subscribes the local user to a PubSub node.
          *