Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2839,7 +2839,6 @@ pub(crate) async fn create_send_msg_jobs(context: &Context, msg: &mut Message) -
return Err(err);
}
};
let attach_selfavatar = mimefactory.attach_selfavatar;
let mut recipients = mimefactory.recipients();

let from = context.get_primary_self_addr().await?;
Expand Down Expand Up @@ -2926,15 +2925,20 @@ pub(crate) async fn create_send_msg_jobs(context: &Context, msg: &mut Message) -

let now = time();

if rendered_msg.last_added_location_id.is_some()
&& let Err(err) = location::set_kml_sent_timestamp(context, msg.chat_id, now).await
{
error!(context, "Failed to set kml sent_timestamp: {err:#}.");
if let Some(last_added_location_timestamp) = rendered_msg.last_added_location_timestamp {
location::set_kml_sent_timestamp(context, msg.chat_id, last_added_location_timestamp)
.await?;
}

if attach_selfavatar && let Err(err) = msg.chat_id.set_selfavatar_timestamp(context, now).await
if rendered_msg.avatar_is_attached
|| rendered_pre_msg
.as_ref()
.is_some_and(|msg| msg.avatar_is_attached)
{
error!(context, "Failed to set selfavatar timestamp: {err:#}.");
msg.chat_id
.set_selfavatar_timestamp(context, now)
.await
.context("Failed to set selfavatar timestamp")?;
}

if rendered_msg.is_encrypted {
Expand Down
35 changes: 16 additions & 19 deletions src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,9 @@ pub(crate) async fn delete_orphaned_poi(context: &Context) -> Result<()> {
Ok(())
}

/// Returns `location.kml` contents.
#[expect(clippy::arithmetic_side_effects)]
pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(String, u32)>> {
let mut last_added_location_id = 0;
/// Returns `location.kml` contents and the largest location timestamp, if any.
pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(String, i64)>> {
let mut last_added_location_timestamp: Option<i64> = None;

let self_addr = context.get_primary_self_addr().await?;

Expand All @@ -540,7 +539,6 @@ pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(Strin
.await?;

let now = time();
let mut location_count = 0;
let mut ret = String::new();
if locations_send_begin != 0 && now <= locations_send_until {
ret += &format!(
Expand All @@ -551,10 +549,10 @@ pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(Strin
context
.sql
.query_map(
"SELECT id, latitude, longitude, accuracy, timestamp \
"SELECT latitude, longitude, accuracy, timestamp \
FROM locations WHERE from_id=? \
AND timestamp>=? \
AND (timestamp>=? OR \
AND (timestamp>? OR \
timestamp=(SELECT MAX(timestamp) FROM locations WHERE from_id=?)) \
AND independent=0 \
GROUP BY timestamp \
Expand All @@ -566,25 +564,24 @@ pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(Strin
ContactId::SELF
),
|row| {
let location_id: i32 = row.get(0)?;
let latitude: f64 = row.get(1)?;
let longitude: f64 = row.get(2)?;
let accuracy: f64 = row.get(3)?;
let timestamp = get_kml_timestamp(row.get(4)?);
let latitude: f64 = row.get(0)?;
let longitude: f64 = row.get(1)?;
let accuracy: f64 = row.get(2)?;
let timestamp: i64 = row.get(3)?;

Ok((location_id, latitude, longitude, accuracy, timestamp))
Ok((latitude, longitude, accuracy, timestamp))
},
|rows| {
for row in rows {
let (location_id, latitude, longitude, accuracy, timestamp) = row?;
let (latitude, longitude, accuracy, timestamp) = row?;
let kml_timestamp = get_kml_timestamp(timestamp);
ret += &format!(
"<Placemark>\
<Timestamp><when>{timestamp}</when></Timestamp>\
<Timestamp><when>{kml_timestamp}</when></Timestamp>\
<Point><coordinates accuracy=\"{accuracy}\">{longitude},{latitude}</coordinates></Point>\
</Placemark>\n"
);
location_count += 1;
last_added_location_id = location_id as u32;
last_added_location_timestamp = std::cmp::max(last_added_location_timestamp, Some(timestamp));
}
Ok(())
},
Expand All @@ -593,8 +590,8 @@ pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(Strin
ret += "</Document>\n</kml>";
}

if location_count > 0 {
Ok(Some((ret, last_added_location_id)))
if let Some(last_added_location_timestamp) = last_added_location_timestamp {
Ok(Some((ret, last_added_location_timestamp)))
} else {
Ok(None)
}
Expand Down
Loading