Compare commits

...

3 Commits

Author SHA1 Message Date
Sebastian Klähn
7694bc0ba8 fix enum repr 2024-11-12 10:25:12 +01:00
Septias
9f1eb45acd fmt 2024-11-10 17:37:02 +01:00
Septias
092b6e96ea 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.
2024-11-10 17:32:11 +01:00
4 changed files with 48 additions and 10 deletions

View File

@@ -2520,8 +2520,12 @@ 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]

View File

@@ -629,7 +629,9 @@ 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 +843,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())

View File

@@ -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 disabled.
Disabled = 0,
/// Location streaming is enabled.
Enabled = 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<ChatId>,
) -> Result<bool> {
) -> Result<LocationSendingStatus> {
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)

View File

@@ -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);
}