From 4b83e84bb9e9e2fcea19e51b2251d7eceec9be0d Mon Sep 17 00:00:00 2001 From: link2xt Date: Fri, 24 Apr 2026 23:09:07 +0200 Subject: [PATCH] refactor: split is_sending_locations_to_chat() into two functions --- deltachat-ffi/src/lib.rs | 16 ++++++----- deltachat-repl/src/cmdline.rs | 4 +-- src/location.rs | 51 +++++++++++++++-------------------- src/mimefactory.rs | 2 +- 4 files changed, 33 insertions(+), 40 deletions(-) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 5a98072c6..5cef2111c 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -2561,14 +2561,16 @@ pub unsafe extern "C" fn dc_is_sending_locations_to_chat( return 0; } let ctx = &*context; - let chat_id = if chat_id == 0 { - None + if chat_id == 0 { + block_on(location::is_sending_locations(ctx)) + .unwrap_or_log_default(ctx, "Failed is_sending_locations()") as libc::c_int } else { - Some(ChatId::new(chat_id)) - }; - - block_on(location::is_sending_locations_to_chat(ctx, chat_id)) - .unwrap_or_log_default(ctx, "Failed dc_is_sending_locations_to_chat()") as libc::c_int + block_on(location::is_sending_locations_to_chat( + ctx, + ChatId::new(chat_id), + )) + .unwrap_or_log_default(ctx, "Failed is_sending_locations_to_chat()") as libc::c_int + } } #[no_mangle] diff --git a/deltachat-repl/src/cmdline.rs b/deltachat-repl/src/cmdline.rs index d3b0ceb76..5b71ac568 100644 --- a/deltachat-repl/src/cmdline.rs +++ b/deltachat-repl/src/cmdline.rs @@ -573,7 +573,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu ); } } - if location::is_sending_locations_to_chat(&context, None).await? { + if location::is_sending_locations(&context).await? { println!("Location streaming enabled."); } println!("{cnt} chats"); @@ -782,7 +782,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu "Location streaming: {}", location::is_sending_locations_to_chat( &context, - Some(sel_chat.as_ref().unwrap().get_id()) + sel_chat.as_ref().unwrap().get_id() ) .await?, ); diff --git a/src/location.rs b/src/location.rs index 44371c42e..b162ced1a 100644 --- a/src/location.rs +++ b/src/location.rs @@ -272,7 +272,7 @@ pub async fn send_locations_to_chat( ensure!(seconds >= 0); ensure!(!chat_id.is_special()); let now = time(); - let is_sending_locations_before = is_sending_locations_to_chat(context, Some(chat_id)).await?; + let is_sending_locations_before = is_sending_locations_to_chat(context, chat_id).await?; context .sql .execute( @@ -305,35 +305,26 @@ pub async fn send_locations_to_chat( Ok(()) } -/// Returns whether `chat_id` or any chat is sending locations. -/// -/// If `chat_id` is `Some` only that chat is checked, otherwise returns `true` if any chat -/// is sending locations. -pub async fn is_sending_locations_to_chat( - context: &Context, - chat_id: Option, -) -> Result { - let exists = match chat_id { - Some(chat_id) => { - context - .sql - .exists( - "SELECT COUNT(id) FROM chats WHERE id=? AND locations_send_until>?;", - (chat_id, time()), - ) - .await? - } - None => { - context - .sql - .exists( - "SELECT COUNT(id) FROM chats WHERE locations_send_until>?;", - (time(),), - ) - .await? - } - }; - Ok(exists) +/// Returns whether any chat is sending locations. +pub async fn is_sending_locations(context: &Context) -> Result { + context + .sql + .exists( + "SELECT COUNT(id) FROM chats WHERE locations_send_until>?", + (time(),), + ) + .await +} + +/// Returns whether `chat_id` is sending locations. +pub async fn is_sending_locations_to_chat(context: &Context, chat_id: ChatId) -> Result { + context + .sql + .exists( + "SELECT COUNT(id) FROM chats WHERE id=? AND locations_send_until>?", + (chat_id, time()), + ) + .await } /// Sets current location of the user device. diff --git a/src/mimefactory.rs b/src/mimefactory.rs index 3a07a750e..7d167f9b8 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -1829,7 +1829,7 @@ impl MimeFactory { parts.push(msg_kml_part); } - if location::is_sending_locations_to_chat(context, Some(msg.chat_id)).await? + if location::is_sending_locations_to_chat(context, msg.chat_id).await? && let Some(part) = self.get_location_kml_part(context).await? { parts.push(part);