feat: add location streaming failure response code

Before `is_sending_location_to_chat` returned a boolean when location was enabled or disabled for a chat. It now returns an enum value representing Enabled, disabled and failure where failure says that its enabled but (currently) not possible. This should not break cffi because 0 is still disabled, 1 enabled and new code 2 (failure) is handled similar to 1.
This commit is contained in:
Septias
2024-11-10 17:32:11 +01:00
parent 3b2f18f926
commit 092b6e96ea
4 changed files with 44 additions and 10 deletions

View File

@@ -2520,7 +2520,9 @@ pub unsafe extern "C" fn dc_is_sending_locations_to_chat(
Some(ChatId::new(chat_id)) Some(ChatId::new(chat_id))
}; };
block_on(location::is_sending_locations_to_chat(ctx, chat_id)) 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 .unwrap_or_log_default(ctx, "Failed dc_is_sending_locations_to_chat()") as libc::c_int
} }

View File

@@ -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!("Location streaming enabled.");
} }
println!("{cnt} chats"); println!("{cnt} chats");
@@ -841,7 +841,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
} }
println!( println!(
"Location streaming: {}", "Location streaming: {:?}",
location::is_sending_locations_to_chat( location::is_sending_locations_to_chat(
&context, &context,
Some(sel_chat.as_ref().unwrap().get_id()) Some(sel_chat.as_ref().unwrap().get_id())

View File

@@ -87,6 +87,17 @@ pub struct Kml {
pub curr: Location, 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)] #[derive(Default, Debug, Clone, PartialEq, Eq)]
enum KmlTag { enum KmlTag {
#[default] #[default]
@@ -274,7 +285,8 @@ pub async fn send_locations_to_chat(
ensure!(seconds >= 0); ensure!(seconds >= 0);
ensure!(!chat_id.is_special()); ensure!(!chat_id.is_special());
let now = time(); 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 context
.sql .sql
.execute( .execute(
@@ -314,25 +326,43 @@ pub async fn send_locations_to_chat(
pub async fn is_sending_locations_to_chat( pub async fn is_sending_locations_to_chat(
context: &Context, context: &Context,
chat_id: Option<ChatId>, chat_id: Option<ChatId>,
) -> Result<bool> { ) -> Result<LocationSendingStatus> {
let exists = match chat_id { let exists = match chat_id {
Some(chat_id) => { Some(chat_id) => {
context let enabled = context
.sql .sql
.exists( .exists(
"SELECT COUNT(id) FROM chats WHERE id=? AND locations_send_until>?;", "SELECT COUNT(id) FROM chats WHERE id=? AND locations_send_until>?;",
(chat_id, time()), (chat_id, time()),
) )
.await?;
let functional: i32 = context
.sql
.query_get_value("SELECT locations_send_begin FROM chats WHERE id=?;", ())
.await? .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 => { None => {
context if context
.sql .sql
.exists( .exists(
"SELECT COUNT(id) FROM chats WHERE locations_send_until>?;", "SELECT COUNT(id) FROM chats WHERE locations_send_until>?;",
(time(),), (time(),),
) )
.await? .await?
{
LocationSendingStatus::Enabled
} else {
LocationSendingStatus::Disabled
}
} }
}; };
Ok(exists) Ok(exists)

View File

@@ -20,7 +20,7 @@ use crate::e2ee::EncryptHelper;
use crate::ephemeral::Timer as EphemeralTimer; use crate::ephemeral::Timer as EphemeralTimer;
use crate::headerdef::HeaderDef; use crate::headerdef::HeaderDef;
use crate::html::new_html_mimepart; use crate::html::new_html_mimepart;
use crate::location; use crate::location::{self, LocationSendingStatus};
use crate::message::{self, Message, MsgId, Viewtype}; use crate::message::{self, Message, MsgId, Viewtype};
use crate::mimeparser::SystemMessage; use crate::mimeparser::SystemMessage;
use crate::param::Param; use crate::param::Param;
@@ -1372,7 +1372,9 @@ impl MimeFactory {
parts.push(msg_kml_part); 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? { if let Some(part) = self.get_location_kml_part(context).await? {
parts.push(part); parts.push(part);
} }