Skip to content

Commit ed252e0

Browse files
committed
Add a pubsub API method to create new nodes
1 parent f5adf45 commit ed252e0

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/headless/plugins/pubsub/api.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,36 @@ export default {
199199
}
200200
}
201201
},
202+
203+
/**
204+
* Creates a PubSub node at a given service
205+
* @param {string} jid - The PubSub service JID
206+
* @param {string} node - The node to create
207+
* @param {PubSubConfigOptions} config The configuration options
208+
* @returns {Promise<void>}
209+
*/
210+
async create(jid, node, config) {
211+
const own_jid = _converse.state.session.get('jid');
212+
const iq = stx`
213+
<iq xmlns="jabber:client"
214+
type="set"
215+
from="${own_jid}"
216+
to="${jid}">
217+
<pubsub xmlns="http://jabber.org/protocol/pubsub">
218+
<create node="${node}"/>
219+
<configure>
220+
<x xmlns="${Strophe.NS.XFORM}" type="submit">
221+
<field var="FORM_TYPE" type="hidden">
222+
<value>${Strophe.NS.PUBSUB}#nodeconfig</value>
223+
</field>
224+
${Object.entries(config).map(([k, v]) => stx`<field var="pubsub#${k}"><value>${v}</value></field>`)}
225+
</x>
226+
</configure>
227+
</pubsub>
228+
</iq>`;
229+
return await api.sendIQ(iq);
230+
},
231+
202232
/**
203233
* Subscribes the local user to a PubSub node.
204234
*
@@ -216,7 +246,7 @@ export default {
216246
<subscribe node="${node}" jid="${own_jid}"/>
217247
</pubsub>
218248
</iq>`;
219-
await api.sendIQ(iq);
249+
return await api.sendIQ(iq);
220250
},
221251

222252
/**

src/headless/plugins/pubsub/tests/api.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global converse */
22
import mock from '../../../tests/mock.js';
33

4-
const { stx, Strophe } = converse.env;
4+
const { stx, Strophe, u } = converse.env;
55

66
describe('pubsub subscribe/unsubscribe API', function () {
77
beforeAll(() => jasmine.addMatchers({ toEqualStanza: jasmine.toEqualStanza }));
@@ -75,4 +75,49 @@ describe('pubsub subscribe/unsubscribe API', function () {
7575
</iq>`);
7676
})
7777
);
78+
79+
it(
80+
'sends correct IQ for create',
81+
mock.initConverse([], {}, async function (_converse) {
82+
await mock.waitForRoster(_converse, 'current', 0);
83+
const { api, state } = _converse;
84+
const own_jid = state.session.get('jid');
85+
const sent = api.connection.get().sent_stanzas;
86+
const service = 'pubsub.example.org';
87+
const node = 'newnode';
88+
const config = { access_model: 'open', max_items: '10' };
89+
const createPromise = api.pubsub.create(service, node, config);
90+
const stanza = await u.waitUntil(() => sent.filter((iq) => iq.querySelector('pubsub create')).pop());
91+
expect(stanza).toEqualStanza(stx`
92+
<iq type="set"
93+
from="${own_jid}"
94+
to="${service}"
95+
xmlns="jabber:client"
96+
id="${stanza.getAttribute('id')}">
97+
<pubsub xmlns="${Strophe.NS.PUBSUB}">
98+
<create node="${node}"/>
99+
<configure>
100+
<x xmlns="${Strophe.NS.XFORM}" type="submit">
101+
<field var="FORM_TYPE" type="hidden">
102+
<value>${Strophe.NS.PUBSUB}#nodeconfig</value>
103+
</field>
104+
<field var="pubsub#access_model"><value>${config.access_model}</value></field>
105+
<field var="pubsub#max_items"><value>${config.max_items}</value></field>
106+
</x>
107+
</configure>
108+
</pubsub>
109+
</iq>`);
110+
111+
_converse.api.connection.get()._dataRecv(
112+
mock.createRequest(stx`
113+
<iq type="result"
114+
xmlns="jabber:client"
115+
from="${service}"
116+
to="${_converse.bare_jid}"
117+
id="${stanza.getAttribute('id')}"/>
118+
`)
119+
);
120+
await createPromise;
121+
})
122+
);
78123
});

0 commit comments

Comments
 (0)