diff --git a/src/location.rs b/src/location.rs index 26b95903e..ddc0c3854 100644 --- a/src/location.rs +++ b/src/location.rs @@ -8,6 +8,7 @@ use quick_xml::events::{BytesEnd, BytesStart, BytesText}; use tokio::time::timeout; use crate::chat::{self, ChatId}; +use crate::constants::DC_CHAT_ID_TRASH; use crate::contact::ContactId; use crate::context::Context; use crate::events::EventType; @@ -501,6 +502,18 @@ pub(crate) async fn delete_poi_location(context: &Context, location_id: u32) -> Ok(()) } +/// Deletes POI locations that don't have corresponding message anymore. +pub(crate) async fn delete_orphaned_poi_locations(context: &Context) -> Result<()> { + context.sql.execute(" + DELETE FROM locations + WHERE independent=1 AND id NOT IN + (SELECT location_id from MSGS LEFT JOIN locations + ON locations.id=location_id + WHERE location_id>0 -- This check makes the query faster by not looking for locations with ID 0 that don't exist. + AND msgs.chat_id != ?)", (DC_CHAT_ID_TRASH,)).await?; + Ok(()) +} + /// Returns `location.kml` contents. pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result> { let mut last_added_location_id = 0; diff --git a/src/sql.rs b/src/sql.rs index f971bbcff..8300a2c33 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -15,6 +15,7 @@ use crate::context::Context; use crate::debug_logging::set_debug_logging_xdc; use crate::ephemeral::start_ephemeral_timers; use crate::imex::BLOBS_BACKUP_NAME; +use crate::location::delete_orphaned_poi_locations; use crate::log::LogExt; use crate::message::{Message, MsgId, Viewtype}; use crate::param::{Param, Params}; @@ -779,6 +780,14 @@ pub async fn housekeeping(context: &Context) -> Result<()> { .log_err(context) .ok(); + // Delete POI locations + // which don't have corresponding message. + delete_orphaned_poi_locations(context) + .await + .context("Failed to delete orphaned POI locations") + .log_err(context) + .ok(); + info!(context, "Housekeeping done."); Ok(()) }