From c2e5c358f1f5459db7eca219db2b615c82f9c23c Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 28 Oct 2024 19:33:20 +1100 Subject: [PATCH] fix: fixed a bug with delete attachments before not working locally --- .../overlay/OverlayRightPanelSettings.tsx | 60 +++++++++++++++---- .../SwarmPollingGroupConfig.ts | 16 +++-- ts/state/ducks/metaGroups.ts | 52 ++++++++++++++++ 3 files changed, 106 insertions(+), 22 deletions(-) diff --git a/ts/components/conversation/right-panel/overlay/OverlayRightPanelSettings.tsx b/ts/components/conversation/right-panel/overlay/OverlayRightPanelSettings.tsx index a2b91f4de..04cefd52f 100644 --- a/ts/components/conversation/right-panel/overlay/OverlayRightPanelSettings.tsx +++ b/ts/components/conversation/right-panel/overlay/OverlayRightPanelSettings.tsx @@ -342,19 +342,53 @@ export const OverlayRightPanelSettings = () => { )} {hasClosedGroupV2QAButtons() && isGroupV2 ? ( - { - if (!PubKey.is03Pubkey(selectedConvoKey)) { - throw new Error('triggerFakeAvatarUpdate needs a 03 pubkey'); - } - window.inboxStore?.dispatch( - groupInfoActions.triggerFakeAvatarUpdate({ groupPk: selectedConvoKey }) as any - ); - }} - dataTestId="edit-group-name" - /> + <> + { + if (!PubKey.is03Pubkey(selectedConvoKey)) { + throw new Error('triggerFakeAvatarUpdate needs a 03 pubkey'); + } + window.inboxStore?.dispatch( + groupInfoActions.triggerFakeAvatarUpdate({ groupPk: selectedConvoKey }) as any + ); + }} + dataTestId="edit-group-name" + /> + { + if (!PubKey.is03Pubkey(selectedConvoKey)) { + throw new Error('We need a 03 pubkey'); + } + window.inboxStore?.dispatch( + groupInfoActions.triggerFakeDeleteMsgBeforeNow({ + groupPk: selectedConvoKey, + messagesWithAttachmentsOnly: false, + }) as any + ); + }} + dataTestId="edit-group-name" + /> + { + if (!PubKey.is03Pubkey(selectedConvoKey)) { + throw new Error('We need a 03 pubkey'); + } + window.inboxStore?.dispatch( + groupInfoActions.triggerFakeDeleteMsgBeforeNow({ + groupPk: selectedConvoKey, + messagesWithAttachmentsOnly: true, + }) as any + ); + }} + dataTestId="edit-group-name" + /> + ) : null} {showAddRemoveModeratorsButton && ( diff --git a/ts/session/apis/snode_api/swarm_polling_config/SwarmPollingGroupConfig.ts b/ts/session/apis/snode_api/swarm_polling_config/SwarmPollingGroupConfig.ts index 56563c3d9..94995ee54 100644 --- a/ts/session/apis/snode_api/swarm_polling_config/SwarmPollingGroupConfig.ts +++ b/ts/session/apis/snode_api/swarm_polling_config/SwarmPollingGroupConfig.ts @@ -17,6 +17,7 @@ import { ConvoHub } from '../../../conversations'; import { ProfileManager } from '../../../profile_manager/ProfileManager'; import { UserUtils } from '../../../utils'; import { GroupSync } from '../../../utils/job_runners/jobs/GroupSyncJob'; +import { destroyMessagesAndUpdateRedux } from '../../../disappearing_messages'; /** * This is a basic optimization to avoid running the logic when the `deleteBeforeSeconds` @@ -52,8 +53,7 @@ async function handleMetaMergeResults(groupPk: GroupPubkeyType) { isNumber(infos.deleteBeforeSeconds) && isFinite(infos.deleteBeforeSeconds) && infos.deleteBeforeSeconds > 0 && - (lastAppliedRemoveMsgSentBeforeSeconds.get(groupPk) || Number.MAX_SAFE_INTEGER) > - infos.deleteBeforeSeconds + (lastAppliedRemoveMsgSentBeforeSeconds.get(groupPk) || 0) < infos.deleteBeforeSeconds ) { // delete any messages in this conversation sent before that timestamp (in seconds) const deletedMsgIds = await Data.removeAllMessagesInConversationSentBefore({ @@ -74,7 +74,7 @@ async function handleMetaMergeResults(groupPk: GroupPubkeyType) { isNumber(infos.deleteAttachBeforeSeconds) && isFinite(infos.deleteAttachBeforeSeconds) && infos.deleteAttachBeforeSeconds > 0 && - (lastAppliedRemoveAttachmentSentBeforeSeconds.get(groupPk) || Number.MAX_SAFE_INTEGER) > + (lastAppliedRemoveAttachmentSentBeforeSeconds.get(groupPk) || 0) < infos.deleteAttachBeforeSeconds ) { // delete any attachments in this conversation sent before that timestamp (in seconds) @@ -87,12 +87,10 @@ async function handleMetaMergeResults(groupPk: GroupPubkeyType) { impactedMsgModels.map(m => m.id) ); - for (let index = 0; index < impactedMsgModels.length; index++) { - const msg = impactedMsgModels[index]; + await destroyMessagesAndUpdateRedux( + impactedMsgModels.map(m => ({ conversationKey: groupPk, messageId: m.id })) + ); - // eslint-disable-next-line no-await-in-loop - await msg?.cleanup(); - } lastAppliedRemoveAttachmentSentBeforeSeconds.set(groupPk, infos.deleteAttachBeforeSeconds); } } @@ -239,7 +237,7 @@ async function handleGroupSharedConfigMessages( window.inboxStore?.dispatch( groupInfoActions.refreshGroupDetailsFromWrapper({ groupPk, - })as any + }) as any ); } catch (e) { window.log.warn( diff --git a/ts/state/ducks/metaGroups.ts b/ts/state/ducks/metaGroups.ts index 65ee71451..370fa0705 100644 --- a/ts/state/ducks/metaGroups.ts +++ b/ts/state/ducks/metaGroups.ts @@ -988,6 +988,57 @@ const triggerFakeAvatarUpdate = createAsyncThunk( } ); +const triggerFakeDeleteMsgBeforeNow = createAsyncThunk( + 'group/triggerFakeDeleteMsgBeforeNow', + async ( + { + groupPk, + messagesWithAttachmentsOnly, + }: { + groupPk: GroupPubkeyType; + messagesWithAttachmentsOnly: boolean; + }, + payloadCreator + ): Promise => { + const state = payloadCreator.getState() as StateType; + if (!state.groups.infos[groupPk]) { + throw new PreConditionFailed( + 'triggerFakeDeleteMsgBeforeNow group not present in redux slice' + ); + } + const convo = ConvoHub.use().get(groupPk); + const group = await UserGroupsWrapperActions.getGroup(groupPk); + if (!convo || !group || !group.secretKey || isEmpty(group.secretKey)) { + throw new Error( + 'triggerFakeDeleteMsgBeforeNow: tried to make change to group but we do not have the admin secret key' + ); + } + + const nowSeconds = Math.floor(NetworkTime.now() / 1000); + const infoGet = await MetaGroupWrapperActions.infoGet(groupPk); + if (messagesWithAttachmentsOnly) { + infoGet.deleteAttachBeforeSeconds = nowSeconds; + } else { + infoGet.deleteBeforeSeconds = nowSeconds; + } + + await MetaGroupWrapperActions.infoSet(groupPk, infoGet); + + const extraStoreRequests = await StoreGroupRequestFactory.makeGroupMessageSubRequest([], group); + + const batchResult = await GroupSync.pushChangesToGroupSwarmIfNeeded({ + groupPk, + extraStoreRequests, + }); + if (!batchResult) { + window.log.warn( + `failed to send deleteBeforeSeconds/deleteAttachBeforeSeconds message for group ${ed25519Str(groupPk)}` + ); + throw new Error('failed to send deleteBeforeSeconds/deleteAttachBeforeSeconds message'); + } + } +); + /** * This action is used to trigger a change when the local user does a change to a group v2 members list. * GroupV2 added members can be added two ways: with and without the history of messages. @@ -1363,6 +1414,7 @@ export const groupInfoActions = { handleMemberLeftMessage, currentDeviceGroupNameChange, triggerFakeAvatarUpdate, + triggerFakeDeleteMsgBeforeNow, ...metaGroupSlice.actions, };