diff --git a/python/tests/test_account.py b/python/tests/test_account.py index e134ab25f..839dc7510 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -823,9 +823,12 @@ class TestOnlineAccount: ac1._evlogger.consume_events() ac2._evlogger.consume_events() + lp.sec("ac1: enable location sending in chat") chat1.enable_sending_locations(seconds=100) assert chat1.is_sending_locations() + + lp.sec("ac1: setting location") ac1.set_location(latitude=1.0, longitude=2.0) ac1._evlogger.get_matching("DC_EVENT_LOCATION_CHANGED") chat1.send_text("hello") diff --git a/src/location.rs b/src/location.rs index f0d0a943d..950aebaa3 100644 --- a/src/location.rs +++ b/src/location.rs @@ -369,9 +369,6 @@ pub fn delete_all(context: &Context) -> Result<(), Error> { } pub fn get_kml(context: &Context, chat_id: u32) -> Result<(String, u32), Error> { - let now = time(); - let mut location_count = 0; - let mut ret = String::new(); let mut last_added_location_id = 0; let self_addr = context @@ -388,14 +385,17 @@ pub fn get_kml(context: &Context, chat_id: u32) -> Result<(String, u32), Error> Ok((send_begin, send_until, last_sent)) })?; - if !(locations_send_begin == 0 || now > locations_send_until) { + let now = time(); + let mut location_count = 0; + let mut ret = String::new(); + if locations_send_begin != 0 && now <= locations_send_until { ret += &format!( "\n\n\n", self_addr, ); context.sql.query_map( - "SELECT id, latitude, longitude, accuracy, timestamp\ + "SELECT id, latitude, longitude, accuracy, timestamp \ FROM locations WHERE from_id=? \ AND timestamp>=? \ AND (timestamp>=? OR timestamp=(SELECT MAX(timestamp) FROM locations WHERE from_id=?)) \ @@ -416,7 +416,7 @@ pub fn get_kml(context: &Context, chat_id: u32) -> Result<(String, u32), Error> for row in rows { let (location_id, latitude, longitude, accuracy, timestamp) = row?; ret += &format!( - "{}{},{}\n\x00", + "{}{},{}\n", timestamp, accuracy, longitude, @@ -428,10 +428,10 @@ pub fn get_kml(context: &Context, chat_id: u32) -> Result<(String, u32), Error> Ok(()) } )?; + ret += "\n"; } ensure!(location_count > 0, "No locations processed"); - ret += "\n"; Ok((ret, last_added_location_id)) } diff --git a/src/mimefactory.rs b/src/mimefactory.rs index c18ae0a26..c5c681b21 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -484,6 +484,7 @@ impl<'a> MimeFactory<'a> { if !meta_part.is_null() { mailmime_smart_add_part(message, meta_part); } + if self.msg.param.exists(Param::SetLatitude) { let param = &self.msg.param; let kml_file = location::get_message_kml( @@ -500,18 +501,25 @@ impl<'a> MimeFactory<'a> { } if location::is_sending_locations_to_chat(context, self.msg.chat_id) { - if let Ok((kml_file, last_added_location_id)) = - location::get_kml(context, self.msg.chat_id) - { - wrapmime::add_filename_part( - message, - "location.kml", - "application/vnd.google-earth.kml+xml", - &kml_file, - )?; - if !self.msg.param.exists(Param::SetLatitude) { - // otherwise, the independent location is already filed - self.out_last_added_location_id = last_added_location_id; + match location::get_kml(context, self.msg.chat_id) { + Ok((kml_content, last_added_location_id)) => { + info!( + context, + "adding location.kml to mime message: {}", kml_content + ); + wrapmime::add_filename_part( + message, + "location.kml", + "application/vnd.google-earth.kml+xml", + &kml_content, + )?; + if !self.msg.param.exists(Param::SetLatitude) { + // otherwise, the independent location is already filed + self.out_last_added_location_id = last_added_location_id; + } + } + Err(err) => { + warn!(context, "mimefactory: could not get location: {}", err); } } }