Bladeren bron

Bugfix. Only publish one bookmark at a time.

JC Brand 2 maanden geleden
bovenliggende
commit
e4e2c841c3

+ 25 - 23
src/headless/plugins/bookmarks/collection.js

@@ -9,7 +9,7 @@ import _converse from '../../shared/_converse.js';
 import api from '../../shared/api/index.js';
 import converse from '../../shared/api/public.js';
 import { parseErrorStanza } from '../../shared/parsers.js';
-import log from "@converse/log";
+import log from '@converse/log';
 import { initStorage } from '../../utils/storage.js';
 import { parseStanzaForBookmarks } from './parsers.js';
 import '../../plugins/muc/index.js';
@@ -29,7 +29,12 @@ class Bookmarks extends Collection {
         );
         this.on('remove', this.leaveRoom, this);
         this.on('change:autojoin', this.onAutoJoinChanged, this);
-        this.on('remove', this.sendBookmarkStanza, this);
+        this.on(
+            'remove',
+            /** @param {Bookmark} bookmark */
+            (_, bookmark) => this.sendBookmarkStanza(bookmark),
+            this
+        );
 
         const { session } = _converse;
         const cache_key = `converse.room-bookmarks${session.get('bare_jid')}`;
@@ -95,45 +100,44 @@ class Bookmarks extends Collection {
     /**
      * @param {import('./types').BookmarkAttrs} attrs
      */
-    setBookmark(attrs, create=true) {
+    setBookmark(attrs, create = true) {
         if (!attrs.jid) return log.warn('No JID provided for setBookmark');
 
         let send_stanza = false;
 
-        const existing = this.get(attrs.jid);
-        if (existing) {
+        let bookmark = this.get(attrs.jid);
+        if (bookmark) {
             // Check if any attrs changed
             const has_changed = Object.keys(attrs).reduce((result, k) => {
-                return result || (attrs[k] ?? '') !== (existing.attributes[k] ?? '');
+                return result || (attrs[k] ?? '') !== (bookmark.attributes[k] ?? '');
             }, false);
             if (has_changed) {
-                existing.save(attrs);
+                bookmark.save(attrs);
                 send_stanza = true;
             }
         } else if (create) {
-            this.create(attrs);
+            bookmark = this.create(attrs);
             send_stanza = true;
         }
         if (send_stanza) {
-            this.sendBookmarkStanza().catch((iq) => this.onBookmarkError(iq, attrs));
+            this.sendBookmarkStanza(bookmark).catch((iq) => this.onBookmarkError(iq, attrs));
         }
     }
 
     /**
      * @param {'urn:xmpp:bookmarks:1'|'storage:bookmarks'} node
+     * @param {Bookmark} bookmark
      * @returns {Stanza|Stanza[]}
      */
-    getPublishedItems(node) {
+    getPublishedItems(node, bookmark) {
         if (node === Strophe.NS.BOOKMARKS2) {
-            return this.map(
-                /** @param {MUC} model */ (model) => {
-                    const extensions = model.get('extensions') ?? [];
-                    return stx`<item id="${model.get('jid')}">
+            const extensions = bookmark.get('extensions') ?? [];
+            return stx`<item id="${bookmark.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>` : ''}
+                                name="${bookmark.get('name')}"
+                                autojoin="${bookmark.get('autojoin')}">
+                            ${bookmark.get('nick') ? stx`<nick>${bookmark.get('nick')}</nick>` : ''}
+                            ${bookmark.get('password') ? stx`<password>${bookmark.get('password')}</password>` : ''}
                         ${
                             extensions.length
                                 ? stx`<extensions>${extensions.map((e) => Stanza.fromString(e))}</extensions>`
@@ -141,8 +145,6 @@ class Bookmarks extends Collection {
                         }
                         </conference>
                     </item>`;
-                }
-            );
         } else {
             return stx`<item id="current">
                 <storage xmlns="${Strophe.NS.BOOKMARKS}">
@@ -160,14 +162,15 @@ class Bookmarks extends Collection {
     }
 
     /**
+     * @param {Bookmark} bookmark
      * @returns {Promise<void|Element>}
      */
-    async sendBookmarkStanza() {
+    async sendBookmarkStanza(bookmark) {
         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), {
+        return api.pubsub.publish(null, node, this.getPublishedItems(node, bookmark), {
             persist_items: true,
             max_items: 'max',
             send_last_published_item: 'never',
@@ -274,7 +277,6 @@ class Bookmarks extends Collection {
                 ),
             ]);
             deferred?.reject(new Error('Could not fetch bookmarks'));
-
         } else {
             const { errors } = converse.env;
             const e = await parseErrorStanza(iq);

+ 0 - 11
src/headless/plugins/bookmarks/tests/bookmarks.js

@@ -358,9 +358,6 @@ describe("A bookmark", function () {
                                 <nick>romeo</nick>
                             </conference>
                         </item>
-                        <item id="${muc1_jid}">
-                            <conference xmlns="urn:xmpp:bookmarks:1" autojoin="true" name="Hamlet"/>
-                        </item>
                     </publish>
                     <publish-options>
                         <x type="submit" xmlns="jabber:x:data">
@@ -404,11 +401,6 @@ describe("A bookmark", function () {
             <iq xmlns="jabber:client" type="set" from="${bare_jid}" to="${bare_jid}" id="${sent_stanza.getAttribute('id')}">
                 <pubsub xmlns="http://jabber.org/protocol/pubsub">
                     <publish node="urn:xmpp:bookmarks:1">
-                        <item id="${muc2_jid}">
-                            <conference xmlns="urn:xmpp:bookmarks:1" autojoin="true" name="Balcony">
-                                <nick>romeo</nick>
-                            </conference>
-                        </item>
                         <item id="${muc3_jid}">
                             <conference xmlns="urn:xmpp:bookmarks:1" autojoin="false" name="Garden">
                                 <nick>r0meo</nick>
@@ -419,9 +411,6 @@ describe("A bookmark", function () {
                                 </extensions>
                             </conference>
                         </item>
-                        <item id="${muc1_jid}">
-                            <conference xmlns="urn:xmpp:bookmarks:1" autojoin="true" name="Hamlet"/>
-                        </item>
                     </publish>
                     <publish-options>
                         <x type="submit" xmlns="jabber:x:data">

+ 0 - 1
src/plugins/modal/templates/prompt.js

@@ -30,7 +30,6 @@ function tplField(f) {
  * @param {import('../confirm').default} el
  */
 export default (el) => {
-
     const first_msg = el.model.get('messages')?.[0];
     const subsequent_msgs = el.model.get('messages')?.slice(1) || [];
     return html`<form