diff --git a/src/calls.rs b/src/calls.rs index 3b3e503ee..b8f6ec18d 100644 --- a/src/calls.rs +++ b/src/calls.rs @@ -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> { if call.viewtype != Viewtype::Call { // This can happen e.g. if a "call accepted" diff --git a/src/sql.rs b/src/sql.rs index 8bdcf93dd..e3956941a 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -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 < ?) )", diff --git a/src/sql/migrations.rs b/src/sql/migrations.rs index d5ad55ea2..6faa838c8 100644 --- a/src/sql/migrations.rs +++ b/src/sql/migrations.rs @@ -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, )