(context: &Context, chat_id: ChatId)
| 524 | /// Returns `location.kml` contents. |
| 525 | #[expect(clippy::arithmetic_side_effects)] |
| 526 | pub async fn get_kml(context: &Context, chat_id: ChatId) -> Result<Option<(String, u32)>> { |
| 527 | let mut last_added_location_id = 0; |
| 528 | |
| 529 | let self_addr = context.get_primary_self_addr().await?; |
| 530 | |
| 531 | let (locations_send_begin, locations_send_until, locations_last_sent) = context.sql.query_row( |
| 532 | "SELECT locations_send_begin, locations_send_until, locations_last_sent FROM chats WHERE id=?;", |
| 533 | (chat_id,), |row| { |
| 534 | let send_begin: i64 = row.get(0)?; |
| 535 | let send_until: i64 = row.get(1)?; |
| 536 | let last_sent: i64 = row.get(2)?; |
| 537 | |
| 538 | Ok((send_begin, send_until, last_sent)) |
| 539 | }) |
| 540 | .await?; |
| 541 | |
| 542 | let now = time(); |
| 543 | let mut location_count = 0; |
| 544 | let mut ret = String::new(); |
| 545 | if locations_send_begin != 0 && now <= locations_send_until { |
| 546 | ret += &format!( |
| 547 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\ |
| 548 | <kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"{self_addr}\">\n", |
| 549 | ); |
| 550 | |
| 551 | context |
| 552 | .sql |
| 553 | .query_map( |
| 554 | "SELECT id, latitude, longitude, accuracy, timestamp \ |
| 555 | FROM locations WHERE from_id=? \ |
| 556 | AND timestamp>=? \ |
| 557 | AND (timestamp>=? OR \ |
| 558 | timestamp=(SELECT MAX(timestamp) FROM locations WHERE from_id=?)) \ |
| 559 | AND independent=0 \ |
| 560 | GROUP BY timestamp \ |
| 561 | ORDER BY timestamp;", |
| 562 | ( |
| 563 | ContactId::SELF, |
| 564 | locations_send_begin, |
| 565 | locations_last_sent, |
| 566 | ContactId::SELF |
| 567 | ), |
| 568 | |row| { |
| 569 | let location_id: i32 = row.get(0)?; |
| 570 | let latitude: f64 = row.get(1)?; |
| 571 | let longitude: f64 = row.get(2)?; |
| 572 | let accuracy: f64 = row.get(3)?; |
| 573 | let timestamp = get_kml_timestamp(row.get(4)?); |
| 574 | |
| 575 | Ok((location_id, latitude, longitude, accuracy, timestamp)) |
| 576 | }, |
| 577 | |rows| { |
| 578 | for row in rows { |
| 579 | let (location_id, latitude, longitude, accuracy, timestamp) = row?; |
| 580 | ret += &format!( |
| 581 | "<Placemark>\ |
| 582 | <Timestamp><when>{timestamp}</when></Timestamp>\ |
| 583 | <Point><coordinates accuracy=\"{accuracy}\">{longitude},{latitude}</coordinates></Point>\ |
no test coverage detected