From fc20bafde38408a57239345b1ebf2a7e676ad5dd Mon Sep 17 00:00:00 2001
From: Audric Ackermann <audric@getsession.org>
Date: Wed, 4 Dec 2024 09:56:57 +1100
Subject: [PATCH] fix: send approved message only when previously unapproved

---
 ts/interactions/conversationInteractions.ts | 2 +-
 ts/session/utils/calling/CallManager.ts     | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/ts/interactions/conversationInteractions.ts b/ts/interactions/conversationInteractions.ts
index 5f0265292..ab483c60d 100644
--- a/ts/interactions/conversationInteractions.ts
+++ b/ts/interactions/conversationInteractions.ts
@@ -91,7 +91,7 @@ export async function unblockConvoById(conversationId: string) {
 export const approveConvoAndSendResponse = async (conversationId: string) => {
   const convoToApprove = getConversationController().get(conversationId);
 
-  if (!convoToApprove) {
+  if (!convoToApprove || convoToApprove.isApproved()) {
     window?.log?.info('Conversation is already approved.');
     return;
   }
diff --git a/ts/session/utils/calling/CallManager.ts b/ts/session/utils/calling/CallManager.ts
index 862b40919..62fa76d2b 100644
--- a/ts/session/utils/calling/CallManager.ts
+++ b/ts/session/utils/calling/CallManager.ts
@@ -35,6 +35,7 @@ import { MessageSender } from '../../sending';
 import { getIsRinging } from '../RingingManager';
 import { getBlackSilenceMediaStream } from './Silence';
 import { ed25519Str } from '../String';
+import { sleepFor } from '../Promise';
 
 export type InputItem = { deviceId: string; label: string };
 
@@ -534,6 +535,10 @@ export async function USER_callRecipient(recipient: string) {
   calledConvo.set('active_at', Date.now()); // addSingleOutgoingMessage does the commit for us on the convo
   await calledConvo.unhideIfNeeded(false);
   weAreCallerOnCurrentCall = true;
+  // Not ideal, but also temporary (see you in 2 years).
+  // We need to make sure the preoffer AND the messageRequestResponse sent in
+  // approveConvoAndSendResponse have different timestamps, as iOS will throw an error otherwise
+  await sleepFor(2);
 
   // initiating a call is analogous to sending a message request
   await approveConvoAndSendResponse(recipient);