refactor: resultify location::set()

This commit is contained in:
link2xt
2023-09-27 17:00:39 +00:00
parent 88bba83383
commit 815c1b9c49
3 changed files with 32 additions and 31 deletions

View File

@@ -2567,7 +2567,12 @@ pub unsafe extern "C" fn dc_set_location(
} }
let ctx = &*context; let ctx = &*context;
block_on(location::set(ctx, latitude, longitude, accuracy)) as _ block_on(async move {
location::set(ctx, latitude, longitude, accuracy)
.await
.log_err(ctx)
.unwrap_or_default()
}) as libc::c_int
} }
#[no_mangle] #[no_mangle]

View File

@@ -894,7 +894,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
let latitude = arg1.parse()?; let latitude = arg1.parse()?;
let longitude = arg2.parse()?; let longitude = arg2.parse()?;
let continue_streaming = location::set(&context, latitude, longitude, 0.).await; let continue_streaming = location::set(&context, latitude, longitude, 0.).await?;
if continue_streaming { if continue_streaming {
println!("Success, streaming should be continued."); println!("Success, streaming should be continued.");
} else { } else {

View File

@@ -328,13 +328,13 @@ pub async fn is_sending_locations_to_chat(
} }
/// Sets current location of the user device. /// Sets current location of the user device.
pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64) -> bool { pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64) -> Result<bool> {
if latitude == 0.0 && longitude == 0.0 { if latitude == 0.0 && longitude == 0.0 {
return true; return Ok(true);
} }
let mut continue_streaming = false; let mut continue_streaming = false;
if let Ok(chats) = context let chats = context
.sql .sql
.query_map( .query_map(
"SELECT id FROM chats WHERE locations_send_until>?;", "SELECT id FROM chats WHERE locations_send_until>?;",
@@ -346,33 +346,29 @@ pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64
.map_err(Into::into) .map_err(Into::into)
}, },
) )
.await .await?;
{
for chat_id in chats {
if let Err(err) = context.sql.execute(
"INSERT INTO locations \
(latitude, longitude, accuracy, timestamp, chat_id, from_id) VALUES (?,?,?,?,?,?);",
(
latitude,
longitude,
accuracy,
time(),
chat_id,
ContactId::SELF,
)
).await {
warn!(context, "failed to store location {:#}", err);
} else {
info!(context, "stored location for chat {}", chat_id);
continue_streaming = true;
}
}
if continue_streaming {
context.emit_event(EventType::LocationChanged(Some(ContactId::SELF)));
};
}
continue_streaming for chat_id in chats {
context.sql.execute(
"INSERT INTO locations \
(latitude, longitude, accuracy, timestamp, chat_id, from_id) VALUES (?,?,?,?,?,?);",
(
latitude,
longitude,
accuracy,
time(),
chat_id,
ContactId::SELF,
)).await.context("Failed to store location")?;
info!(context, "Stored location for chat {chat_id}.");
continue_streaming = true;
}
if continue_streaming {
context.emit_event(EventType::LocationChanged(Some(ContactId::SELF)));
};
Ok(continue_streaming)
} }
/// Searches for locations in the given time range, optionally filtering by chat and contact IDs. /// Searches for locations in the given time range, optionally filtering by chat and contact IDs.