diff --git a/src/calls/calls_tests.rs b/src/calls/calls_tests.rs index 3f983d843..10b78e0e2 100644 --- a/src/calls/calls_tests.rs +++ b/src/calls/calls_tests.rs @@ -672,3 +672,76 @@ async fn test_no_partial_calls() -> Result<()> { Ok(()) } + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_housekeeping_deletes_old_call_sdps() -> Result<()> { + use crate::sql::housekeeping; + + let alice = TestContext::new_alice().await; + let bob = alice.create_chat_with_contact("", "bob@example.net").await; + + // Place a call + let call_id = alice + .place_outgoing_call(bob.id, PLACE_INFO.to_string()) + .await?; + + // Verify SDP is stored in calls table + let sdp_before: Option = alice + .sql + .query_row_optional( + "SELECT offer_sdp FROM calls WHERE msg_id=?", + (call_id,), + |row| row.get(0), + ) + .await?; + assert_eq!(sdp_before, Some(PLACE_INFO.to_string())); + + // End the call + alice.end_call(call_id).await?; + + // Verify the call message is marked as ended + let call = alice.load_call_by_id(call_id).await?.unwrap(); + assert!(call.is_ended()); + + // SDP should still be there after ending + let sdp_after_end: Option = alice + .sql + .query_row_optional( + "SELECT offer_sdp FROM calls WHERE msg_id=?", + (call_id,), + |row| row.get(0), + ) + .await?; + assert_eq!(sdp_after_end, Some(PLACE_INFO.to_string())); + + // Simulate passage of time by modifying the message timestamp + // to be older than 24 hours + let old_timestamp = crate::tools::time() - 86400 - 1; // 24 hours + 1 second ago + alice + .sql + .execute( + "UPDATE msgs SET timestamp_sent=? WHERE id=?", + (old_timestamp, call_id), + ) + .await?; + + // Run housekeeping + housekeeping(&alice).await?; + + // Verify SDP has been deleted from calls table + let sdp_after_housekeeping: Option = alice + .sql + .query_row_optional( + "SELECT offer_sdp FROM calls WHERE msg_id=?", + (call_id,), + |row| row.get(0), + ) + .await?; + assert_eq!(sdp_after_housekeeping, None); + + // The call message should still exist + let msg = Message::load_from_db(&alice, call_id).await?; + assert_eq!(msg.viewtype, Viewtype::Call); + + Ok(()) +} diff --git a/src/mimefactory.rs b/src/mimefactory.rs index 8f39dea12..ea4e5f72d 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -1682,23 +1682,35 @@ impl MimeFactory { )); // Get SDP answer from the referenced call message in calls table, // or fall back to params if not yet migrated - if let Some(ref quoted_msg_id) = msg.in_reply_to { - let answer_sdp = context + if let Some(ref quoted_rfc724_mid) = msg.in_reply_to { + // Look up msg_id from rfc724_mid first + let quoted_msg_id: Option = context .sql .query_row_optional( - "SELECT answer_sdp FROM calls WHERE msg_id=?", - (quoted_msg_id,), - |row| row.get::<_, Option>(0), + "SELECT id FROM msgs WHERE rfc724_mid=?", + (quoted_rfc724_mid,), + |row| row.get(0), ) - .await? - .flatten() - .or_else(|| msg.param.get(Param::WebrtcAccepted).map(|s| s.to_string())); - - if let Some(answer_sdp) = answer_sdp { - headers.push(( - "Chat-Webrtc-Accepted", - mail_builder::headers::raw::Raw::new(b_encode(&answer_sdp)).into(), - )); + .await?; + + if let Some(quoted_msg_id) = quoted_msg_id { + let answer_sdp = context + .sql + .query_row_optional( + "SELECT answer_sdp FROM calls WHERE msg_id=?", + (quoted_msg_id,), + |row| row.get::<_, Option>(0), + ) + .await? + .flatten() + .or_else(|| msg.param.get(Param::WebrtcAccepted).map(|s| s.to_string())); + + if let Some(answer_sdp) = answer_sdp { + headers.push(( + "Chat-Webrtc-Accepted", + mail_builder::headers::raw::Raw::new(b_encode(&answer_sdp)).into(), + )); + } } } }