From ce16066313363f6b53017c3a8902eb16ef2cc108 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Wed, 27 Oct 2021 11:44:37 +1100 Subject: [PATCH] do not try to do an unsend request on a group swarm --- .../conversations/unsendingInteractions.ts | 46 ++++++++++++++++--- ts/receiver/cache.ts | 1 - ts/receiver/contentMessage.ts | 15 +++++- ts/session/sending/MessageSender.ts | 2 +- ts/session/snode_api/SNodeAPI.ts | 10 ++-- 5 files changed, 60 insertions(+), 14 deletions(-) diff --git a/ts/interactions/conversations/unsendingInteractions.ts b/ts/interactions/conversations/unsendingInteractions.ts index 97779af3d..d360941fd 100644 --- a/ts/interactions/conversations/unsendingInteractions.ts +++ b/ts/interactions/conversations/unsendingInteractions.ts @@ -7,6 +7,7 @@ import { ApiV2 } from '../../opengroup/opengroupV2'; import { getMessageQueue } from '../../session'; import { getConversationController } from '../../session/conversations'; import { UnsendMessage } from '../../session/messages/outgoing/controlMessage/UnsendMessage'; +import { ed25519Str } from '../../session/onions/onionPath'; import { networkDeleteMessages } from '../../session/snode_api/SNodeAPI'; import { PubKey } from '../../session/types'; import { ToastUtils, UserUtils } from '../../session/utils'; @@ -51,11 +52,12 @@ async function unsendMessagesForEveryone( } else if (conversation.isClosedGroup()) { // sending to recipient all the messages separately for now await Promise.all( - unsendMsgObjects.map(unsendObject => + unsendMsgObjects.map(unsendObject => { + console.warn('sending unsend message', unsendObject); getMessageQueue() .sendToGroup(unsendObject, undefined, new PubKey(destinationId)) - .catch(window?.log?.error) - ) + .catch(window?.log?.error); + }) ); } await deleteMessagesFromSwarmAndCompletelyLocally(conversation, msgsToDelete); @@ -92,18 +94,24 @@ function getUnsendMessagesObjects(messages: Array) { * Do a single request to the swarm with all the message hashes to delete from the swarm. * * It does not delete anything locally. + * + * Returns true if no errors happened, false in an error happened */ export async function deleteMessagesFromSwarmOnly(messages: Array) { try { const deletionMessageHashes = _.compact(messages.map(m => m.get('messageHash'))); if (deletionMessageHashes.length > 0) { - await networkDeleteMessages(deletionMessageHashes); + const errorOnSnode = await networkDeleteMessages(deletionMessageHashes); + return errorOnSnode === null || errorOnSnode.length === 0; } + window.log?.warn( + 'deleteMessagesFromSwarmOnly: We do not have hashes for some of those messages' + ); + return false; } catch (e) { - window.log?.error('Error deleting message from swarm', e); + window.log?.error('deleteMessagesFromSwarmOnly: Error deleting message from swarm', e); return false; } - return true; } /** @@ -114,10 +122,25 @@ export async function deleteMessagesFromSwarmAndCompletelyLocally( conversation: ConversationModel, messages: Array ) { + if (conversation.isMediumGroup()) { + window.log.info('Cannot delete message from a closed group swarm, so we just complete delete.'); + await Promise.all( + messages.map(async message => { + return deleteMessageLocallyOnly({ conversation, message, deletionType: 'complete' }); + }) + ); + return; + } + window.log.warn( + 'Deleting from swarm of ', + ed25519Str(conversation.id), + ' hashes: ', + messages.map(m => m.get('messageHash')) + ); const deletedFromSwarm = await deleteMessagesFromSwarmOnly(messages); if (!deletedFromSwarm) { window.log.warn( - 'deleteMessagesFromSwarmAndCompletelyLocally: some messages failed to be deleted ' + 'deleteMessagesFromSwarmAndCompletelyLocally: some messages failed to be deleted. Maybe they were already deleted?' ); } await Promise.all( @@ -135,6 +158,15 @@ export async function deleteMessagesFromSwarmAndMarkAsDeletedLocally( conversation: ConversationModel, messages: Array ) { + if (conversation.isMediumGroup()) { + window.log.info('Cannot delete messages from a closed group swarm, so we just markDeleted.'); + await Promise.all( + messages.map(async message => { + return deleteMessageLocallyOnly({ conversation, message, deletionType: 'markDeleted' }); + }) + ); + return; + } const deletedFromSwarm = await deleteMessagesFromSwarmOnly(messages); if (!deletedFromSwarm) { window.log.warn( diff --git a/ts/receiver/cache.ts b/ts/receiver/cache.ts index 793e5a11a..a98a79c5b 100644 --- a/ts/receiver/cache.ts +++ b/ts/receiver/cache.ts @@ -16,7 +16,6 @@ import { export async function removeFromCache(envelope: EnvelopePlus) { const { id } = envelope; window?.log?.info(`removing from cache envelope: ${id}`); - return removeUnprocessed(id); } diff --git a/ts/receiver/contentMessage.ts b/ts/receiver/contentMessage.ts index bf3f85df6..7e160ca5e 100644 --- a/ts/receiver/contentMessage.ts +++ b/ts/receiver/contentMessage.ts @@ -512,16 +512,22 @@ async function handleUnsendMessage(envelope: EnvelopePlus, unsendMessage: Signal if (!unsendMessage) { //#region early exit conditions window?.log?.error('handleUnsendMessage: Invalid parameters -- dropping message.'); + await removeFromCache(envelope); + + return; } if (!timestamp) { window?.log?.error('handleUnsendMessage: Invalid timestamp -- dropping message'); + await removeFromCache(envelope); + + return; } const messageToDelete = await getMessageBySenderAndTimestamp({ source: messageAuthor, timestamp: Lodash.toNumber(timestamp), }); - const messageHash = messageToDelete?.getPropsForMessage().messageHash; + const messageHash = messageToDelete?.get('messageHash'); //#endregion //#region executing deletion @@ -539,6 +545,13 @@ async function handleUnsendMessage(envelope: EnvelopePlus, unsendMessage: Signal } else { void deleteMessagesFromSwarmAndMarkAsDeletedLocally(conversation, [messageToDelete]); } + } else { + window.log.info( + 'handleUnsendMessage: got a request to delete an unknown messageHash:', + messageHash, + ' and found messageToDelete:', + messageToDelete?.id + ); } await removeFromCache(envelope); diff --git a/ts/session/sending/MessageSender.ts b/ts/session/sending/MessageSender.ts index 8946425e0..dde8630de 100644 --- a/ts/session/sending/MessageSender.ts +++ b/ts/session/sending/MessageSender.ts @@ -21,7 +21,7 @@ import { getMessageById } from '../../../ts/data/data'; import { SNodeAPI } from '../snode_api'; import { getConversationController } from '../conversations'; -const DEFAULT_CONNECTIONS = 3; +const DEFAULT_CONNECTIONS = 1; // ================ SNODE STORE ================ diff --git a/ts/session/snode_api/SNodeAPI.ts b/ts/session/snode_api/SNodeAPI.ts index ada1a0651..849da9af7 100644 --- a/ts/session/snode_api/SNodeAPI.ts +++ b/ts/session/snode_api/SNodeAPI.ts @@ -694,7 +694,9 @@ export const TEST_getMinTimeout = () => 500; * Locally deletes message and deletes message on the network (all nodes that contain the message) */ // tslint:disable-next-line: max-func-body-length -export const networkDeleteMessages = async (hashes: Array): Promise => { +export const networkDeleteMessages = async ( + hashes: Array, +): Promise | null> => { const sodium = await getSodium(); const userX25519PublicKey = UserUtils.getOurPubKeyStrFromCache(); @@ -778,7 +780,7 @@ export const networkDeleteMessages = async (hashes: Array): Promise const statusCode = snodeJson.code; if (reason && statusCode) { window?.log?.warn( - `Could not delete data from ${ed25519Str( + `Could not delete msgs from ${ed25519Str( snodeToMakeRequestTo.pubkey_ed25519 )} due to error: ${reason}: ${statusCode}` ); @@ -789,8 +791,8 @@ export const networkDeleteMessages = async (hashes: Array): Promise ); } } else { - window?.log?.warn( - `Could not delete data from ${ed25519Str( + window?.log?.info( + `Could not delete msgs from ${ed25519Str( snodeToMakeRequestTo.pubkey_ed25519 )}` );