浏览代码

Remove overrides in the vcard plugin

JC Brand 4 月之前
父节点
当前提交
c2a9cd9b70

+ 14 - 20
src/headless/plugins/roster/contact.js

@@ -14,25 +14,29 @@ class RosterContact extends ColorAwareModel(Model) {
         return 'jid';
     }
 
+    constructor (attrs, options) {
+        super(attrs, options);
+        /** @type {import('../vcard/vcard').default} */
+        this.vcard = null;
+    }
+
     defaults () {
         return {
-            'chat_state': undefined,
-            'groups': [],
-            'num_unread': 0,
-            'status': undefined,
+            groups: [],
+            num_unread: 0,
         }
     }
 
-    async initialize (attributes) {
+    async initialize (attrs) {
         super.initialize();
         this.initialized = getOpenPromise();
         this.setPresence();
-        const { jid } = attributes;
+        const { jid } = attrs;
         this.set({
-            ...attributes,
+            ...attrs,
             ...{
-                'jid': Strophe.getBareJidFromJid(jid).toLowerCase(),
-                'user_id': Strophe.getNodeFromJid(jid)
+                jid: Strophe.getBareJidFromJid(jid).toLowerCase(),
+                user_id: Strophe.getNodeFromJid(jid)
             }
         });
         /**
@@ -68,17 +72,7 @@ class RosterContact extends ColorAwareModel(Model) {
     }
 
     getDisplayName () {
-        // Gets overridden in converse-vcard where the fullname is may be returned
-        if (this.get('nickname')) {
-            return this.get('nickname');
-        } else {
-            return this.get('jid');
-        }
-    }
-
-    getFullname () {
-        // Gets overridden in converse-vcard where the fullname may be returned
-        return this.get('jid');
+        return this.get('nickname') || this.vcard?.getDisplayName() || this.get('jid');
     }
 
     /**

+ 5 - 8
src/headless/plugins/roster/contacts.js

@@ -121,16 +121,13 @@ class RosterContacts extends Collection {
      * @param {Element} msg
      */
     subscribeToSuggestedItems (msg) {
-        const { xmppstatus } = _converse.state;
         Array.from(msg.querySelectorAll('item')).forEach((item) => {
             if (item.getAttribute('action') === 'add') {
-                this.addContact(
-                    {
-                        jid: item.getAttribute('jid'),
-                        name: xmppstatus.getNickname() || xmppstatus.getFullname(),
-                        subscription: 'to',
-                    },
-                );
+                this.addContact({
+                    jid: item.getAttribute('jid'),
+                    name: item.getAttribute('name'),
+                    subscription: 'to',
+                });
             }
         });
         return true;

+ 3 - 6
src/headless/plugins/status/status.js

@@ -57,15 +57,12 @@ export default class XMPPStatus extends ColorAwareModel(Model) {
     }
 
     getDisplayName() {
-        return this.getFullname() || this.getNickname() || this.get('jid');
+        return this.vcard?.get('fullname') || this.getNickname() || this.get('jid');
     }
 
     getNickname() {
-        return api.settings.get('nickname');
-    }
-
-    getFullname() {
-        return ''; // Gets overridden in converse-vcard
+        debugger;
+        return this.vcard?.get('nickname') || api.settings.get('nickname');
     }
 
     /** Constructs a presence stanza

+ 0 - 47
src/headless/plugins/vcard/plugin.js

@@ -25,53 +25,6 @@ converse.plugins.add('converse-vcard', {
 
     dependencies: ["converse-status", "converse-roster"],
 
-    // Overrides mentioned here will be picked up by converse.js's
-    // plugin architecture they will replace existing methods on the
-    // relevant objects or classes.
-    // New functions which don't exist yet can also be added.
-    overrides: {
-        XMPPStatus: {
-            getNickname () {
-                const { _converse } = this.__super__;
-                const { xmppstatus } = _converse.state;
-                const nick = this.__super__.getNickname.apply(this);
-                if (!nick && xmppstatus?.vcard) {
-                    return xmppstatus.vcard.get('nickname');
-                } else {
-                    return nick;
-                }
-            },
-
-            getFullname () {
-                const { _converse } = this.__super__;
-                const { xmppstatus } = _converse.state;
-                const fullname = this.__super__.getFullname.apply(this);
-                if (!fullname && xmppstatus?.vcard) {
-                    return xmppstatus.vcard.get('fullname');
-                } else {
-                    return fullname;
-                }
-            }
-        },
-
-        RosterContact: {
-            getDisplayName () {
-                if (!this.get('nickname') && this.vcard) {
-                    return this.vcard.getDisplayName();
-                } else {
-                    return this.__super__.getDisplayName.apply(this);
-                }
-            },
-            getFullname () {
-                if (this.vcard) {
-                    return this.vcard.get('fullname');
-                } else {
-                    return this.__super__.getFullname.apply(this);
-                }
-            }
-        }
-    },
-
     initialize () {
         api.promises.add('VCardsInitialized');
 

+ 4 - 4
src/headless/types/plugins/roster/contact.d.ts

@@ -70,20 +70,20 @@ declare const RosterContact_base: {
     };
 } & typeof Model;
 declare class RosterContact extends RosterContact_base {
+    constructor(attrs: any, options: any);
+    /** @type {import('../vcard/vcard').default} */
+    vcard: import("../vcard/vcard").default;
     defaults(): {
-        chat_state: any;
         groups: any[];
         num_unread: number;
-        status: any;
     };
-    initialize(attributes: any): Promise<void>;
+    initialize(attrs: any): Promise<void>;
     initialized: any;
     setPresence(): void;
     presence: any;
     getStatus(): any;
     openChat(): void;
     getDisplayName(): any;
-    getFullname(): any;
     /**
      * Send a presence subscription request to this roster contact
      * @param {string} message - An optional message to explain the

+ 0 - 1
src/headless/types/plugins/status/status.d.ts

@@ -83,7 +83,6 @@ export default class XMPPStatus extends XMPPStatus_base {
     set(key: string | any, val?: string | any, options?: any): false | this;
     getDisplayName(): any;
     getNickname(): any;
-    getFullname(): string;
     /** Constructs a presence stanza
      * @param {string} [type]
      * @param {string} [to] - The JID to which this presence should be sent

+ 2 - 16
src/plugins/muc-views/tests/nickname.js

@@ -310,6 +310,8 @@ describe("A MUC", function () {
 
             const { api } = _converse;
             const muc_jid = 'roomy@muc.montague.lit';
+            // Delete the VCard
+            delete _converse.state.xmppstatus.vcard;
             api.rooms.open(muc_jid);
             await mock.waitForMUCDiscoInfo(_converse, muc_jid);
             await mock.waitForReservedNick(_converse, muc_jid, '');
@@ -531,21 +533,5 @@ describe("A MUC", function () {
             const nick_input = modal.querySelector('input[name="nickname"]');
             expect(nick_input.value).toBe('romeo');
         }));
-
-        it("uses the nickname passed in to converse.initialize",
-                mock.initConverse(['chatBoxesFetched'], {'nickname': 'st.nick'}, async function (_converse) {
-
-            await mock.openControlBox(_converse);
-            await mock.waitForRoster(_converse, 'current', 0);
-            const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
-            roomspanel.querySelector('.show-add-muc-modal').click();
-
-            const modal = _converse.api.modal.get('converse-add-muc-modal');
-            await u.waitUntil(() => u.isVisible(modal), 1000)
-            const label_nick = modal.querySelector('label[for="nickname"]');
-            expect(label_nick.textContent.trim()).toBe('Nickname:');
-            const nick_input = modal.querySelector('input[name="nickname"]');
-            expect(nick_input.value).toBe('st.nick');
-        }));
     });
 });

+ 4 - 1
src/plugins/rosterview/modals/accept-contact-request.js

@@ -55,7 +55,10 @@ export default class AcceptContactRequest extends BaseModal {
             this.model.set("error", __("Sorry, something went wrong"));
             return;
         }
-        this.contact.save({ groups: [group] });
+        this.contact.save({
+            nickname: name,
+            groups: [group]
+        });
         this.modal.hide();
     }
 }