Skip to content

Commit

Permalink
fix: interrupt location loop when new location is stored
Browse files Browse the repository at this point in the history
Otherwise location-only messages
that should be sent every 60 seconds
are never sent because location loop
waits until the end of location streaming
and is only interrupted by location streaming
ending in other chats or being enabled in other chats.
  • Loading branch information
link2xt committed Apr 27, 2024
1 parent 496a8e3 commit f49588e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions python/tests/test_1_online.py
Original file line number Diff line number Diff line change
Expand Up @@ -2036,14 +2036,15 @@ def test_send_receive_locations(acfactory, lp):
assert chat1.is_sending_locations()
ac1._evtracker.get_matching("DC_EVENT_SMTP_MESSAGE_SENT")

# Wait for "enabled location streaming" message.
ac2._evtracker.wait_next_incoming_message()

# First location is sent immediately as a location-only message.
ac1.set_location(latitude=2.0, longitude=3.0, accuracy=0.5)
ac1._evtracker.get_matching("DC_EVENT_LOCATION_CHANGED")
chat1.send_text("馃崬")
ac1._evtracker.get_matching("DC_EVENT_SMTP_MESSAGE_SENT")

lp.sec("ac2: wait for incoming location message")

# currently core emits location changed before event_incoming message
ac2._evtracker.get_matching("DC_EVENT_LOCATION_CHANGED")

locations = chat2.get_locations()
Expand All @@ -2052,7 +2053,7 @@ def test_send_receive_locations(acfactory, lp):
assert locations[0].longitude == 3.0
assert locations[0].accuracy == 0.5
assert locations[0].timestamp > now
assert locations[0].marker == "馃崬"
assert locations[0].marker is None

contact = ac2.create_contact(ac1)
locations2 = chat2.get_locations(contact=contact)
Expand Down
6 changes: 6 additions & 0 deletions src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64
)
.await?;

let mut stored_location = false;
for chat_id in chats {
context.sql.execute(
"INSERT INTO locations \
Expand All @@ -362,13 +363,18 @@ pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64
chat_id,
ContactId::SELF,
)).await.context("Failed to store location")?;
stored_location = true;

info!(context, "Stored location for chat {chat_id}.");
continue_streaming = true;
}
if continue_streaming {
context.emit_location_changed(Some(ContactId::SELF)).await?;
};
if stored_location {
// Interrupt location loop so it may send a location-only message.
context.scheduler.interrupt_location().await;
}

Ok(continue_streaming)
}
Expand Down

0 comments on commit f49588e

Please sign in to comment.