Skip to content

Commit

Permalink
Automatically reject and remove requesting contacts who are blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Jan 5, 2025
1 parent 70a707a commit 3959b68
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/headless/plugins/blocklist/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand All @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/headless/plugins/roster/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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 {
Expand Down

0 comments on commit 3959b68

Please sign in to comment.