@@ -205,6 +205,42 @@ export default {
205205 }
206206 } ,
207207
208+ /**
209+ * Retracts (deletes) an item from a PubSub node (XEP-0060 § 7.2).
210+ * @method _converse.api.pubsub.retract
211+ * @param {string } jid - The JID of the pubsub service where the node
212+ * resides. Pass a falsy value to retract from your own PEP service.
213+ * @param {string } node - The node to retract the item from
214+ * @param {string } id - The id of the item to retract
215+ * @param {object } [options]
216+ * @param {boolean } [options.notify=true] - Whether to ask the server to
217+ * notify subscribers of the retraction.
218+ * @returns {Promise<void> }
219+ */
220+ async retract ( jid , node , id , options = { } ) {
221+ if ( ! node ) throw new Error ( 'api.pubsub.retract: node value required' ) ;
222+ if ( ! id ) throw new Error ( 'api.pubsub.retract: id value required' ) ;
223+
224+ const { notify = true } = options ;
225+ const bare_jid = _converse . session . get ( 'bare_jid' ) ;
226+ const entity_jid = jid || bare_jid ;
227+
228+ const stanza = stx `
229+ <iq xmlns="jabber:client" from="${ bare_jid } " type="set" to="${ entity_jid } ">
230+ <pubsub xmlns="${ Strophe . NS . PUBSUB } ">
231+ <retract node="${ node } " notify="${ notify ? 'true' : 'false' } ">
232+ <item id="${ id } "/>
233+ </retract>
234+ </pubsub>
235+ </iq>` ;
236+
237+ try {
238+ await api . sendIQ ( stanza ) ;
239+ } catch ( error ) {
240+ throw await parseErrorStanza ( error ) ;
241+ }
242+ } ,
243+
208244 /**
209245 * Creates a PubSub node at a given service
210246 * @param {string } jid - The PubSub service JID
@@ -213,6 +249,8 @@ export default {
213249 * @returns {Promise<void> }
214250 */
215251 async create ( jid , node , config ) {
252+ if ( ! node ) throw new Error ( 'api.pubsub.create: node value required' ) ;
253+
216254 const own_jid = _converse . state . session . get ( 'jid' ) ;
217255 const iq = stx `
218256 <iq xmlns="jabber:client"
@@ -233,7 +271,12 @@ export default {
233271 </configure>
234272 </pubsub>
235273 </iq>` ;
236- return await api . sendIQ ( iq ) ;
274+
275+ try {
276+ await api . sendIQ ( iq ) ;
277+ } catch ( error ) {
278+ throw await parseErrorStanza ( error ) ;
279+ }
237280 } ,
238281
239282 /**
@@ -244,6 +287,8 @@ export default {
244287 * @returns {Promise<void> }
245288 */
246289 async subscribe ( jid , node ) {
290+ if ( ! node ) throw new Error ( 'api.pubsub.subscribe: node value required' ) ;
291+
247292 const service = jid || ( await api . disco . entities . find ( 'http://jabber.org/protocol/pubsub' ) ) ;
248293 const own_jid = _converse . session . get ( 'jid' ) ;
249294 const iq = stx `
@@ -252,7 +297,12 @@ export default {
252297 <subscribe node="${ node } " jid="${ own_jid } "/>
253298 </pubsub>
254299 </iq>` ;
255- return await api . sendIQ ( iq ) ;
300+
301+ try {
302+ await api . sendIQ ( iq ) ;
303+ } catch ( error ) {
304+ throw await parseErrorStanza ( error ) ;
305+ }
256306 } ,
257307
258308 /**
@@ -263,14 +313,21 @@ export default {
263313 * @returns {Promise<void> }
264314 */
265315 async unsubscribe ( jid , node ) {
316+ if ( ! node ) throw new Error ( 'api.pubsub.unsubscribe: node value required' ) ;
317+
266318 const own_jid = _converse . session . get ( 'jid' ) ;
267319 const iq = stx `
268320 <iq type="set" from="${ own_jid } " to="${ jid } " xmlns="jabber:client">
269321 <pubsub xmlns="${ Strophe . NS . PUBSUB } ">
270322 <unsubscribe node="${ node } " jid="${ own_jid } "/>
271323 </pubsub>
272324 </iq>` ;
273- await api . sendIQ ( iq ) ;
325+
326+ try {
327+ await api . sendIQ ( iq ) ;
328+ } catch ( error ) {
329+ throw await parseErrorStanza ( error ) ;
330+ }
274331 } ,
275332
276333 /**
0 commit comments