Skip to content

Commit

Permalink
Refactor the pubsub plugin
Browse files Browse the repository at this point in the history
- Turn it into a folder
- Use the stx tagged template literal
  • Loading branch information
jcbrand committed Dec 21, 2024
1 parent 44ea0b7 commit 4e4db7d
Show file tree
Hide file tree
Showing 17 changed files with 636 additions and 482 deletions.
3 changes: 1 addition & 2 deletions src/headless/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ export { MAMPlaceholderMessage } from './plugins/mam/index.js';
// XEP-0045 Multi-user chat
export { MUCMessage, MUCMessages, MUC, MUCOccupant, MUCOccupants } from './plugins/muc/index.js';


import './plugins/ping/index.js'; // XEP-0199 XMPP Ping
import './plugins/pubsub.js'; // XEP-0060 Pubsub
import './plugins/pubsub/index.js'; // XEP-0060 Pubsub

// RFC-6121 Contacts Roster
export { RosterContact, RosterContacts, RosterFilter, Presence, Presences } from './plugins/roster/index.js';
Expand Down
108 changes: 49 additions & 59 deletions src/headless/plugins/bookmarks/collection.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* @typedef {import('../muc/muc.js').default} MUC
*/
import { Stanza } from 'strophe.js';
import { Collection } from '@converse/skeletor';
import { getOpenPromise } from '@converse/openpromise';
import '../../plugins/muc/index.js';
import Bookmark from './model.js';
import _converse from '../../shared/_converse.js';
import api from '../../shared/api/index.js';
import converse from '../../shared/api/public.js';
import log from '../../log.js';
import { initStorage } from '../../utils/storage.js';
import { parseStanzaForBookmarks } from './parsers.js';
import { Stanza } from 'strophe.js';
import '../../plugins/muc/index.js';

const { Strophe, sizzle, stx } = converse.env;

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable sizzle.

Expand Down Expand Up @@ -96,69 +96,59 @@ class Bookmarks extends Collection {
}

/**
* @returns {Promise<Stanza>}
* @param {'urn:xmpp:bookmarks:1'|'storage:bookmarks'} node
* @returns {Stanza|Stanza[]}
*/
async createPublishNode() {
const bare_jid = _converse.session.get('bare_jid');
if (await api.disco.supports(`${Strophe.NS.BOOKMARKS2}#compat`, bare_jid)) {
return stx`
<publish node="${Strophe.NS.BOOKMARKS2}">
${this.map(
/** @param {MUC} model */ (model) => {
const extensions = model.get('extensions') ?? [];
return stx`<item id="${model.get('jid')}">
<conference xmlns="${Strophe.NS.BOOKMARKS2}"
name="${model.get('name')}"
autojoin="${model.get('autojoin')}">
${model.get('nick') ? stx`<nick>${model.get('nick')}</nick>` : ''}
${model.get('password') ? stx`<password>${model.get('password')}</password>` : ''}
${
extensions.length
? stx`<extensions>${extensions.map((e) => Stanza.unsafeXML(e))}</extensions>`
: ''
};
</conference>
</item>`;
}
)}
</publish>`;
getPublishedItems(node) {
if (node === Strophe.NS.BOOKMARKS2) {
return this.map(
/** @param {MUC} model */ (model) => {
const extensions = model.get('extensions') ?? [];
return stx`<item id="${model.get('jid')}">
<conference xmlns="${Strophe.NS.BOOKMARKS2}"
name="${model.get('name')}"
autojoin="${model.get('autojoin')}">
${model.get('nick') ? stx`<nick>${model.get('nick')}</nick>` : ''}
${model.get('password') ? stx`<password>${model.get('password')}</password>` : ''}
${
extensions.length
? stx`<extensions>${extensions.map((e) => Stanza.unsafeXML(e))}</extensions>`
: ''
};
</conference>
</item>`;
}
);
} else {
return stx`
<publish node="${Strophe.NS.BOOKMARKS}">
<item id="current">
<storage xmlns="${Strophe.NS.BOOKMARKS}">
${this.map(
/** @param {MUC} model */ (model) =>
stx`<conference name="${model.get('name')}" autojoin="${model.get('autojoin')}"
jid="${model.get('jid')}">
${model.get('nick') ? stx`<nick>${model.get('nick')}</nick>` : ''}
${model.get('password') ? stx`<password>${model.get('password')}</password>` : ''}
</conference>`
)}
</storage>
</item>
</publish>`;
return stx`<item id="current">
<storage xmlns="${Strophe.NS.BOOKMARKS}">
${this.map(
/** @param {MUC} model */ (model) =>
stx`<conference name="${model.get('name')}" autojoin="${model.get('autojoin')}"
jid="${model.get('jid')}">
${model.get('nick') ? stx`<nick>${model.get('nick')}</nick>` : ''}
${model.get('password') ? stx`<password>${model.get('password')}</password>` : ''}
</conference>`
)}
</storage>
</item>`;
}
}

/**
* @returns {Promise<void|Element>}
*/
async sendBookmarkStanza() {
return api.sendIQ(stx`
<iq type="set" from="${api.connection.get().jid}" xmlns="jabber:client">
<pubsub xmlns="${Strophe.NS.PUBSUB}">
${await this.createPublishNode()}
<publish-options>
<x xmlns="${Strophe.NS.XFORM}" type="submit">
<field var='FORM_TYPE' type='hidden'>
<value>${Strophe.NS.PUBSUB}#publish-options</value>
</field>
<field var='pubsub#persist_items'><value>true</value></field>
<field var='pubsub#max_items'><value>max</value></field>
<field var='pubsub#send_last_published_item'><value>never</value></field>
<field var='pubsub#access_model'><value>whitelist</value></field>
</x>
</publish-options>
</pubsub>
</iq>`);
const bare_jid = _converse.session.get('bare_jid');
const node = (await api.disco.supports(`${Strophe.NS.BOOKMARKS2}#compat`, bare_jid))
? Strophe.NS.BOOKMARKS2
: Strophe.NS.BOOKMARKS;
return api.pubsub.publish(null, node, this.getPublishedItems(node), {
persist_items: true,
max_items: 'max',
send_last_published_item: 'never',
access_model: 'whitelist',
});
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/headless/plugins/bookmarks/tests/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ describe("A chat room", function () {
const jid = '[email protected]';
const { bookmarks } = _converse.state;
const model = bookmarks.create({
'jid': jid,
jid,
'autojoin': false,
'name': 'The Play',
'nick': ''
});
expect(_converse.api.rooms.create).not.toHaveBeenCalled();
bookmarks.remove(model);
bookmarks.create({
'jid': jid,
jid,
'autojoin': true,
'name': 'Hamlet',
'nick': ''
Expand All @@ -43,7 +43,7 @@ describe("A bookmark", function () {
await mock.waitForRoster(_converse, 'current', 0);
await mock.waitUntilBookmarksReturned(_converse);

const jid = _converse.session.get('jid');
const bare_jid = _converse.session.get('bare_jid');
const muc1_jid = '[email protected]';
const { bookmarks } = _converse.state;

Expand All @@ -59,7 +59,7 @@ describe("A bookmark", function () {
() => IQ_stanzas.filter(s => sizzle('publish[node="urn:xmpp:bookmarks:1"]', s).length).pop());

expect(sent_stanza).toEqualStanza(stx`
<iq from="${jid}" id="${sent_stanza.getAttribute('id')}" type="set" xmlns="jabber:client">
<iq from="${bare_jid}" id="${sent_stanza.getAttribute('id')}" type="set" xmlns="jabber:client">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<publish node="urn:xmpp:bookmarks:1">
<item id="${muc1_jid}">
Expand Down Expand Up @@ -101,7 +101,7 @@ describe("A bookmark", function () {
() => IQ_stanzas.filter(s => sizzle('publish[node="urn:xmpp:bookmarks:1"] conference[name="Balcony"]', s).length).pop());

expect(sent_stanza).toEqualStanza(stx`
<iq from="${jid}" id="${sent_stanza.getAttribute('id')}" type="set" xmlns="jabber:client">
<iq from="${bare_jid}" id="${sent_stanza.getAttribute('id')}" type="set" xmlns="jabber:client">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<publish node="urn:xmpp:bookmarks:1">
<item id="${muc2_jid}">
Expand Down Expand Up @@ -152,7 +152,7 @@ describe("A bookmark", function () {
() => IQ_stanzas.filter(s => sizzle('publish[node="urn:xmpp:bookmarks:1"] conference[name="Garden"]', s).length).pop());

expect(sent_stanza).toEqualStanza(stx`
<iq xmlns="jabber:client" type="set" from="${jid}" id="${sent_stanza.getAttribute('id')}">
<iq xmlns="jabber:client" type="set" from="${bare_jid}" id="${sent_stanza.getAttribute('id')}">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<publish node="urn:xmpp:bookmarks:1">
<item id="${muc2_jid}">
Expand Down
6 changes: 3 additions & 3 deletions src/headless/plugins/bookmarks/tests/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("A bookmark", function () {
'storage:bookmarks'
);

const jid = _converse.session.get('jid');
const bare_jid = _converse.session.get('bare_jid');
const muc1_jid = '[email protected]';
const { bookmarks } = _converse.state;

Expand All @@ -31,7 +31,7 @@ describe("A bookmark", function () {
() => IQ_stanzas.filter(s => sizzle('item[id="current"]', s).length).pop());

expect(sent_stanza).toEqualStanza(stx`
<iq from="${jid}" id="${sent_stanza.getAttribute('id')}" type="set" xmlns="jabber:client">
<iq from="${bare_jid}" id="${sent_stanza.getAttribute('id')}" type="set" xmlns="jabber:client">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<publish node="storage:bookmarks">
<item id="current">
Expand Down Expand Up @@ -75,7 +75,7 @@ describe("A bookmark", function () {
() => IQ_stanzas.filter(s => sizzle('item[id="current"] conference[name="Balcony"]', s).length).pop());

expect(sent_stanza).toEqualStanza(stx`
<iq from="${jid}" id="${sent_stanza.getAttribute('id')}" type="set" xmlns="jabber:client">
<iq from="${bare_jid}" id="${sent_stanza.getAttribute('id')}" type="set" xmlns="jabber:client">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<publish node="storage:bookmarks">
<item id="current">
Expand Down
93 changes: 0 additions & 93 deletions src/headless/plugins/pubsub.js

This file was deleted.

Loading

0 comments on commit 4e4db7d

Please sign in to comment.