refactor: split is_sending_locations_to_chat() into two functions

This commit is contained in:
link2xt
2026-04-24 23:09:07 +02:00
committed by l
parent 287d730556
commit 7f9c184659
4 changed files with 33 additions and 40 deletions

View File

@@ -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]

View File

@@ -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?,
);

View File

@@ -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<ChatId>,
) -> Result<bool> {
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<bool> {
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<bool> {
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.

View File

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