From 4a15c23bc68ca548f32bc8f1753ecef51535bf71 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 21 Mar 2025 02:24:48 +1000 Subject: [PATCH] Group Send Endorsements: Don't fetch blocked groups --- ts/groups.ts | 50 +++++++++++++++++++++++--------- ts/util/groupSendEndorsements.ts | 8 +++++ 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/ts/groups.ts b/ts/groups.ts index 7ee876c0e..d7fa41c2b 100644 --- a/ts/groups.ts +++ b/ts/groups.ts @@ -3225,32 +3225,54 @@ async function updateGroup( item => item.serviceId === ourAci || item.serviceId === ourPni )?.addedByUserId || newAttributes.addedBy; - if (justAdded && addedBy) { + if (justAdded) { const adder = window.ConversationController.get(addedBy); + // Wait for empty queue to make it more likely the group update succeeds + const waitThenLeave = async (reason: string) => { + log.warn( + `waitThenLeave/${logId}/${reason}: Waiting for empty event queue.` + ); + await window.waitForEmptyEventQueue(); + log.warn( + `waitThenLeave/${logId}/${reason}: Empty event queue, starting group leave.` + ); + + // We're guaranteed to fail if we're not up to date in the group, which we won't be + // if we're dropping updates. So we prepare for failure. + try { + await conversation.leaveGroupV2(); + log.warn(`waitThenLeave/${logId}/${reason}: Leave complete.`); + } catch (error) { + log.error( + `waitThenLeave/${logId}/${reason}: Failed to leave group.`, + Errors.toLogFormat(error) + ); + } + }; + if (adder && adder.isBlocked()) { log.warn( `updateGroup/${logId}: Added to group by blocked user ${adder.idForLogging()}. Scheduling group leave.` ); - // Wait for empty queue to make it more likely the group update succeeds - const waitThenLeave = async () => { - log.warn(`waitThenLeave/${logId}: Waiting for empty event queue.`); - await window.waitForEmptyEventQueue(); - log.warn( - `waitThenLeave/${logId}: Empty event queue, starting group leave.` - ); - - await conversation.leaveGroupV2(); - log.warn(`waitThenLeave/${logId}: Leave complete.`); - }; - // Cannot await here, would infinitely block queue - drop(waitThenLeave()); + drop(waitThenLeave('added by blocked user')); // Return early to discard group changes resulting from the blocked user's action. return; } + if (conversation.isBlocked()) { + log.warn( + `updateGroup/${logId}: We were added to a group we blocked. Scheduling group leave.` + ); + + // Cannot await here, would infinitely block queue + drop(waitThenLeave('group is blocked')); + + // Return early to discard group changes resulting from unwanted group add + return; + } } // We update group membership last to ensure that all notifications are in place before diff --git a/ts/util/groupSendEndorsements.ts b/ts/util/groupSendEndorsements.ts index ae161b31a..c5dcafb4f 100644 --- a/ts/util/groupSendEndorsements.ts +++ b/ts/util/groupSendEndorsements.ts @@ -417,6 +417,14 @@ export async function maybeCreateGroupSendEndorsementState( ); return { state: null, didRefreshGroupState: false }; } + if (conversation.isBlocked()) { + onFailedToSendWithEndorsements( + new Error( + `${logId}: Group is blocked and endorsements are invalid: ${result.reason}` + ) + ); + return { state: null, didRefreshGroupState: false }; + } log.info( `${logId}: Endorsements invalid, refreshing group: ${result.reason}`