api: add JSON-RPC APIs for location streaming

New API stop_sending_locations() only available
in JSON-RPC stops location streaming
in all accounts and chats.
This commit is contained in:
link2xt
2026-04-24 20:58:58 +02:00
committed by l
parent 7f9c184659
commit 8c927c7f86
7 changed files with 151 additions and 0 deletions

View File

@@ -22,6 +22,8 @@ use tokio::time::{Duration, sleep};
use crate::context::{Context, ContextBuilder};
use crate::events::{Event, EventEmitter, EventType, Events};
use crate::location;
use crate::location::stop_sending_locations;
use crate::log::warn;
use crate::push::PushSubscriber;
use crate::stock_str::StockStrings;
@@ -536,6 +538,27 @@ impl Accounts {
self.push_subscriber.set_device_token(token).await;
Ok(())
}
/// Sets location for all accounts.
///
/// Returns true if location should still be streamed.
pub async fn set_location(&self, latitude: f64, longitude: f64, accuracy: f64) -> Result<bool> {
let mut continue_streaming = false;
for account in self.accounts.values() {
if location::set(account, latitude, longitude, accuracy).await? {
continue_streaming = true;
}
}
Ok(continue_streaming)
}
/// Stops sending locations to all chats.
pub async fn stop_sending_locations(&self) -> Result<()> {
for account in self.accounts.values() {
stop_sending_locations(account).await?;
}
Ok(())
}
}
/// Configuration file name.

View File

@@ -327,6 +327,29 @@ pub async fn is_sending_locations_to_chat(context: &Context, chat_id: ChatId) ->
.await
}
/// Returns a list of chats in which location streaming is enabled.
async fn get_chats_with_location_streaming(context: &Context) -> Result<Vec<ChatId>> {
context
.sql
.query_map_vec(
"SELECT id FROM chats WHERE locations_send_until>?",
(time(),),
|row| {
let chat_id: ChatId = row.get(0)?;
Ok(chat_id)
},
)
.await
}
/// Stop sending locations in all chats.
pub async fn stop_sending_locations(context: &Context) -> Result<()> {
for chat_id in get_chats_with_location_streaming(context).await? {
send_locations_to_chat(context, chat_id, 0).await?;
}
Ok(())
}
/// Sets current location of the user device.
pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64) -> Result<bool> {
if latitude == 0.0 && longitude == 0.0 {