diff --git a/lib/handlers/peer-to-peer-message-handler.js b/lib/handlers/peer-to-peer-message-handler.js --- a/lib/handlers/peer-to-peer-message-handler.js +++ b/lib/handlers/peer-to-peer-message-handler.js @@ -13,14 +13,16 @@ } from '../types/tunnelbroker/peer-to-peer-message-types.js'; import { getConfig } from '../utils/config.js'; import { getContentSigningKey } from '../utils/crypto-utils.js'; +import { getMessageForException } from '../utils/errors.js'; import { hasHigherDeviceID, olmSessionErrors } from '../utils/olm-utils.js'; +import { getClientMessageIDFromTunnelbrokerMessageID } from '../utils/peer-to-peer-communication-utils.js'; async function peerToPeerMessageHandler( message: PeerToPeerMessage, identityClient: IdentityServiceClient, messageID: string, ): Promise { - const { olmAPI } = getConfig(); + const { olmAPI, sqliteAPI } = getConfig(); if (message.type === peerToPeerMessageTypes.OUTBOUND_SESSION_CREATION) { const { senderInfo, encryptedData, sessionVersion } = message; const { userID: senderUserID, deviceID: senderDeviceID } = senderInfo; @@ -162,6 +164,23 @@ `Error verifying device list for user ${message.userID}: ${e}`, ); } + } else if (message.type === peerToPeerMessageTypes.MESSAGE_PROCESSED) { + try { + const { deviceID, messageID: tunnelbrokerMessageID } = message; + const clientMessageID = getClientMessageIDFromTunnelbrokerMessageID( + tunnelbrokerMessageID, + ); + await sqliteAPI.removeOutboundP2PMessagesOlderThan( + clientMessageID, + deviceID, + ); + } catch (e) { + console.log( + `Error removing message after processing: ${ + getMessageForException(e) ?? 'unknown error' + }`, + ); + } } } diff --git a/lib/utils/peer-to-peer-communication-utils.js b/lib/utils/peer-to-peer-communication-utils.js new file mode 100644 --- /dev/null +++ b/lib/utils/peer-to-peer-communication-utils.js @@ -0,0 +1,13 @@ +// @flow + +function getClientMessageIDFromTunnelbrokerMessageID( + tunnelbrokerMessageID: string, +): string { + const ids = tunnelbrokerMessageID.split('#'); + if (ids.length !== 2) { + throw new Error('Invalid tunnelbrokerMessageID'); + } + return ids[1]; +} + +export { getClientMessageIDFromTunnelbrokerMessageID };