diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 47951a2e1..3cd2cc5d5 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -2520,8 +2520,10 @@ pub unsafe extern "C" fn dc_is_sending_locations_to_chat( 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(async { + location::is_sending_locations_to_chat(ctx, chat_id).await.map(|res| res as u8) + }) + .unwrap_or_log_default(ctx, "Failed dc_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 2f636ee86..51bd3d2a6 100644 --- a/deltachat-repl/src/cmdline.rs +++ b/deltachat-repl/src/cmdline.rs @@ -629,7 +629,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_to_chat(&context, None).await? != location::LocationSendingStatus::Disabled { println!("Location streaming enabled."); } println!("{cnt} chats"); @@ -841,7 +841,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu } println!( - "Location streaming: {}", + "Location streaming: {:?}", location::is_sending_locations_to_chat( &context, Some(sel_chat.as_ref().unwrap().get_id()) diff --git a/src/location.rs b/src/location.rs index f2fa919b8..6b5bf8886 100644 --- a/src/location.rs +++ b/src/location.rs @@ -87,6 +87,17 @@ pub struct Kml { pub curr: Location, } +/// Location streaming status for one chat. +#[derive(Debug, PartialEq, Eq)] +pub enum LocationSendingStatus { + /// Location streaming is enabled. + Enabled = 0, + /// Location streaming is disabled. + Disabled = 1, + /// Location streaming is enabled but (currently) not possible. + Failure = 2, +} + #[derive(Default, Debug, Clone, PartialEq, Eq)] enum KmlTag { #[default] @@ -274,7 +285,8 @@ 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, Some(chat_id)).await? + == LocationSendingStatus::Enabled; context .sql .execute( @@ -314,25 +326,43 @@ pub async fn send_locations_to_chat( pub async fn is_sending_locations_to_chat( context: &Context, chat_id: Option, -) -> Result { +) -> Result { let exists = match chat_id { Some(chat_id) => { - context + let enabled = context .sql .exists( "SELECT COUNT(id) FROM chats WHERE id=? AND locations_send_until>?;", (chat_id, time()), ) + .await?; + let functional: i32 = context + .sql + .query_get_value("SELECT locations_send_begin FROM chats WHERE id=?;", ()) .await? + .ok_or(anyhow::anyhow!("not able to select"))?; + + if enabled && functional > 0 { + LocationSendingStatus::Enabled + } else if enabled && functional == 0 { + LocationSendingStatus::Failure + } else { + LocationSendingStatus::Disabled + } } None => { - context + if context .sql .exists( "SELECT COUNT(id) FROM chats WHERE locations_send_until>?;", (time(),), ) .await? + { + LocationSendingStatus::Enabled + } else { + LocationSendingStatus::Disabled + } } }; Ok(exists) diff --git a/src/mimefactory.rs b/src/mimefactory.rs index 83556c0d0..758916801 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -20,7 +20,7 @@ use crate::e2ee::EncryptHelper; use crate::ephemeral::Timer as EphemeralTimer; use crate::headerdef::HeaderDef; use crate::html::new_html_mimepart; -use crate::location; +use crate::location::{self, LocationSendingStatus}; use crate::message::{self, Message, MsgId, Viewtype}; use crate::mimeparser::SystemMessage; use crate::param::Param; @@ -1372,7 +1372,9 @@ 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, Some(msg.chat_id)).await? + != LocationSendingStatus::Disabled + { if let Some(part) = self.get_location_kml_part(context).await? { parts.push(part); }