Skip to content

Commit e4bf9d8

Browse files
committed
Add subscribe/unsubscribe pubsub api methods
1 parent 3e5414a commit e4bf9d8

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

src/headless/plugins/pubsub/api.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import converse from '../../shared/api/public.js';
66
import _converse from '../../shared/_converse.js';
77
import api from '../../shared/api/index.js';
8-
import log from "@converse/log";
8+
import log from '@converse/log';
99
import { parseErrorStanza } from '../../shared/parsers.js';
1010
import { parseStanzaForPubSubConfig } from './parsers.js';
1111

@@ -176,7 +176,8 @@ export default {
176176
const e = await parseErrorStanza(iq);
177177
if (
178178
e.name === 'conflict' &&
179-
/** @type {import('shared/errors').StanzaError} */(e).extra[Strophe.NS.PUBSUB_ERROR] === 'precondition-not-met'
179+
/** @type {import('shared/errors').StanzaError} */ (e).extra[Strophe.NS.PUBSUB_ERROR] ===
180+
'precondition-not-met'
180181
) {
181182
// Manually configure the node if we can't set it via publish-options
182183
await api.pubsub.config.set(entity_jid, node, options);
@@ -198,5 +199,43 @@ export default {
198199
}
199200
}
200201
},
202+
/**
203+
* Subscribes the local user to a PubSub node.
204+
*
205+
* @method _converse.api.pubsub.subscribe
206+
* @param {string} jid - PubSub service JID.
207+
* @param {string} node - The node to subscribe to
208+
* @returns {Promise<void>}
209+
*/
210+
async subscribe(jid, node) {
211+
const service = jid || (await api.disco.entities.find('http://jabber.org/protocol/pubsub'));
212+
const bare_jid = _converse.session.get('bare_jid');
213+
const iq = stx`
214+
<iq type="set" from="${bare_jid}" to="${service}" xmlns="jabber:client">
215+
<pubsub xmlns="${Strophe.NS.PUBSUB}">
216+
<subscribe node="${node}" jid="${bare_jid}"/>
217+
</pubsub>
218+
</iq>`;
219+
await api.sendIQ(iq);
220+
},
221+
222+
/**
223+
* Unsubscribes the local user from a PubSub node.
224+
*
225+
* @method _converse.api.pubsub.unsubscribe
226+
* @param {string} jid - The PubSub service JID
227+
* @param {string} node - The node to unsubscribe from
228+
* @returns {Promise<void>}
229+
*/
230+
async unsubscribe(jid, node) {
231+
const bare_jid = _converse.session.get('bare_jid');
232+
const iq = stx`
233+
<iq type="set" from="${bare_jid}" to="${jid}" xmlns="jabber:client">
234+
<pubsub xmlns="${Strophe.NS.PUBSUB}">
235+
<unsubscribe node="${node}" jid="${bare_jid}"/>
236+
</pubsub>
237+
</iq>`;
238+
await api.sendIQ(iq);
239+
},
201240
},
202241
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* global converse */
2+
import mock from '../../../tests/mock.js';
3+
4+
const { stx, Strophe } = converse.env;
5+
6+
describe('pubsub subscribe/unsubscribe API', function () {
7+
beforeAll(() => jasmine.addMatchers({ toEqualStanza: jasmine.toEqualStanza }));
8+
9+
it(
10+
'sends correct IQ for subscribe',
11+
mock.initConverse([], {}, async function (_converse) {
12+
await mock.waitForRoster(_converse, 'current', 0);
13+
const { api } = _converse;
14+
const sent = api.connection.get().sent_stanzas;
15+
const service = 'pubsub.example.org';
16+
const node = 'testnode';
17+
const subscribePromise = api.pubsub.subscribe(service, node);
18+
19+
const stanza = sent.filter((iq) => iq.querySelector('pubsub subscribe')).pop();
20+
expect(stanza).toEqualStanza(stx`
21+
<iq type="set"
22+
from="${_converse.bare_jid}"
23+
to="${service}"
24+
xmlns="jabber:client"
25+
id="${stanza.getAttribute('id')}">
26+
<pubsub xmlns="${Strophe.NS.PUBSUB}">
27+
<subscribe node="${node}" jid="${_converse.bare_jid}"/>
28+
</pubsub>
29+
</iq>`);
30+
31+
_converse.api.connection.get()._dataRecv(
32+
mock.createRequest(stx`
33+
<iq type="result"
34+
xmlns="jabber:client"
35+
from="${service}"
36+
to="${_converse.bare_jid}"
37+
id="${stanza.getAttribute('id')}"/>
38+
`)
39+
);
40+
await subscribePromise;
41+
})
42+
);
43+
44+
it(
45+
'sends correct IQ for unsubscribe',
46+
mock.initConverse([], {}, async function (_converse) {
47+
await mock.waitForRoster(_converse, 'current', 0);
48+
const { api } = _converse;
49+
const sent = api.connection.get().sent_stanzas;
50+
const service = 'pubsub.example.org';
51+
const node = 'testnode';
52+
const unsubscribePromise = api.pubsub.unsubscribe(service, node);
53+
const stanza = sent.filter((iq) => iq.querySelector('pubsub unsubscribe')).pop();
54+
_converse.api.connection.get()._dataRecv(
55+
mock.createRequest(stx`
56+
<iq type="result"
57+
xmlns="jabber:client"
58+
from="${service}"
59+
to="${_converse.bare_jid}"
60+
id="${stanza.getAttribute('id')}"/>
61+
`)
62+
);
63+
await unsubscribePromise;
64+
expect(stanza).toEqualStanza(stx`
65+
<iq type="set"
66+
from="${_converse.bare_jid}"
67+
to="${service}"
68+
xmlns="jabber:client"
69+
id="${stanza.getAttribute('id')}">
70+
<pubsub xmlns="${Strophe.NS.PUBSUB}">
71+
<unsubscribe node="${node}" jid="${_converse.bare_jid}"/>
72+
</pubsub>
73+
</iq>`);
74+
})
75+
);
76+
});

src/headless/types/plugins/pubsub/api.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ declare namespace _default {
3434
* @returns {Promise<void|Element>}
3535
*/
3636
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>;
37+
/**
38+
* Subscribes the local user to a PubSub node.
39+
*
40+
* @method _converse.api.pubsub.subscribe
41+
* @param {string} jid - PubSub service JID.
42+
* @param {string} node - The node to subscribe to
43+
* @returns {Promise<void>}
44+
*/
45+
function subscribe(jid: string, node: string): Promise<void>;
46+
/**
47+
* Unsubscribes the local user from a PubSub node.
48+
*
49+
* @method _converse.api.pubsub.unsubscribe
50+
* @param {string} jid - The PubSub service JID
51+
* @param {string} node - The node to unsubscribe from
52+
* @returns {Promise<void>}
53+
*/
54+
function unsubscribe(jid: string, node: string): Promise<void>;
3755
}
3856
}
3957
export default _default;

0 commit comments

Comments
 (0)