Improve calls table with FOREIGN KEY, STRICT mode, and better housekeeping query

Co-authored-by: link2xt <18373967+link2xt@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-06 20:14:49 +00:00
parent 19be18dcbf
commit c6a871c64a
3 changed files with 12 additions and 8 deletions

View File

@@ -509,7 +509,10 @@ impl Context {
// Loads information about the call given the `Message`.
//
// If the `Message` is not a call message, returns `None`
// If the `Message` is not a call message, returns `None`.
//
// This function is async because it queries the calls table
// to retrieve SDP offers and answers.
async fn load_call_by_message(&self, call: Message) -> Result<Option<CallInfo>> {
if call.viewtype != Viewtype::Call {
// This can happen e.g. if a "call accepted"

View File

@@ -922,17 +922,18 @@ pub async fn housekeeping(context: &Context) -> Result<()> {
.ok();
// Delete call SDPs for ended calls (older than 24 hours) or orphaned calls.
// Ended calls have Param::Arg4 (H=timestamp) set in their params.
// We clean up calls that ended more than 24 hours ago to protect privacy
// as SDPs contain IP addresses.
// as SDPs contain IP addresses. Ended calls are identified by having
// the CALL_ENDED_TIMESTAMP parameter (Param::Arg4) set.
// The ON DELETE CASCADE foreign key will handle orphaned entries automatically,
// but we also check for trash and old ended calls.
context
.sql
.execute(
"DELETE FROM calls WHERE msg_id IN (
SELECT calls.msg_id FROM calls
LEFT JOIN msgs ON calls.msg_id = msgs.id
WHERE msgs.id IS NULL
OR msgs.chat_id = ?
INNER JOIN msgs ON calls.msg_id = msgs.id
WHERE msgs.chat_id = ?
OR (msgs.param LIKE '%H=%'
AND msgs.timestamp_sent < ?)
)",

View File

@@ -1344,10 +1344,10 @@ CREATE INDEX gossip_timestamp_index ON gossip_timestamp (chat_id, fingerprint);
sql.execute_migration(
"CREATE TABLE calls(
id INTEGER PRIMARY KEY AUTOINCREMENT,
msg_id INTEGER NOT NULL UNIQUE,
msg_id INTEGER NOT NULL UNIQUE REFERENCES msgs(id) ON DELETE CASCADE,
offer_sdp TEXT,
answer_sdp TEXT
);
) STRICT;
CREATE INDEX calls_msg_id_index ON calls (msg_id);",
migration_version,
)