2
0
Эх сурвалжийг харах

Automatically reject and remove requesting contacts who are blocked

JC Brand 6 сар өмнө
parent
commit
79cfa0b48e

+ 15 - 0
src/headless/plugins/blocklist/collection.js

@@ -25,6 +25,8 @@ class Blocklist extends Collection {
         this.fetched_flag = `${cache_key}-fetched`;
         initStorage(this, cache_key);
 
+        this.on('add', this.rejectContactRequest);
+
         await this.fetchBlocklist();
 
         /**
@@ -37,6 +39,19 @@ class Blocklist extends Collection {
         api.trigger('blocklistInitialized', this);
     }
 
+    /**
+     * @param {BlockedEntity} item
+     */
+    async rejectContactRequest(item) {
+        const roster = await api.waitUntil('rosterContactsFetched');
+        const contact = roster.get(item.get('jid'));
+        if (contact?.get('requesting')) {
+            const chat = await api.chats.get(contact.get('jid'));
+            chat?.close();
+            contact.unauthorize().destroy();
+        }
+    }
+
     fetchBlocklist() {
         const deferred = getOpenPromise();
         if (window.sessionStorage.getItem(this.fetched_flag)) {

+ 2 - 2
src/headless/plugins/roster/utils.js

@@ -45,7 +45,7 @@ function initRoster () {
      * @example _converse.api.listen.on('rosterInitialized', () => { ... });
      * @example _converse.api.waitUntil('rosterInitialized').then(() => { ... });
      */
-    api.trigger('rosterInitialized');
+    api.trigger('rosterInitialized', roster);
 }
 
 
@@ -64,7 +64,7 @@ async function populateRoster (ignore_cache=false) {
     const roster = /** @type {RosterContacts} */(_converse.state.roster);
     try {
         await roster.fetchRosterContacts();
-        api.trigger('rosterContactsFetched');
+        api.trigger('rosterContactsFetched', roster);
     } catch (reason) {
         log.error(reason);
     } finally {