mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 01:16:31 +03:00
Avoid ChatId::is_unset when querying location sending
Avoid using the 0 ChatID as a special value, use Options instead. For get_range() also do this for contact_id.
This commit is contained in:
@@ -1943,11 +1943,13 @@ pub unsafe extern "C" fn dc_is_sending_locations_to_chat(
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
let ctx = &*context;
|
let ctx = &*context;
|
||||||
|
let chat_id = if chat_id == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(ChatId::new(chat_id))
|
||||||
|
};
|
||||||
|
|
||||||
block_on(location::is_sending_locations_to_chat(
|
block_on(location::is_sending_locations_to_chat(&ctx, chat_id)) as libc::c_int
|
||||||
&ctx,
|
|
||||||
ChatId::new(chat_id),
|
|
||||||
)) as libc::c_int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@@ -1979,11 +1981,21 @@ pub unsafe extern "C" fn dc_get_locations(
|
|||||||
return ptr::null_mut();
|
return ptr::null_mut();
|
||||||
}
|
}
|
||||||
let ctx = &*context;
|
let ctx = &*context;
|
||||||
|
let chat_id = if chat_id == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(ChatId::new(chat_id))
|
||||||
|
};
|
||||||
|
let contact_id = if contact_id == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(contact_id)
|
||||||
|
};
|
||||||
|
|
||||||
block_on(async move {
|
block_on(async move {
|
||||||
let res = location::get_range(
|
let res = location::get_range(
|
||||||
&ctx,
|
&ctx,
|
||||||
ChatId::new(chat_id),
|
chat_id,
|
||||||
contact_id,
|
contact_id,
|
||||||
timestamp_begin as i64,
|
timestamp_begin as i64,
|
||||||
timestamp_end as i64,
|
timestamp_end as i64,
|
||||||
|
|||||||
@@ -573,7 +573,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if location::is_sending_locations_to_chat(&context, ChatId::new(0)).await {
|
if location::is_sending_locations_to_chat(&context, None).await {
|
||||||
println!("Location streaming enabled.");
|
println!("Location streaming enabled.");
|
||||||
}
|
}
|
||||||
println!("{} chats", cnt);
|
println!("{} chats", cnt);
|
||||||
@@ -735,7 +735,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
|
|||||||
contacts.len(),
|
contacts.len(),
|
||||||
location::is_sending_locations_to_chat(
|
location::is_sending_locations_to_chat(
|
||||||
&context,
|
&context,
|
||||||
sel_chat.as_ref().unwrap().get_id()
|
Some(sel_chat.as_ref().unwrap().get_id())
|
||||||
)
|
)
|
||||||
.await,
|
.await,
|
||||||
);
|
);
|
||||||
@@ -743,10 +743,10 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
|
|||||||
"getlocations" => {
|
"getlocations" => {
|
||||||
ensure!(sel_chat.is_some(), "No chat selected.");
|
ensure!(sel_chat.is_some(), "No chat selected.");
|
||||||
|
|
||||||
let contact_id = arg1.parse().unwrap_or_default();
|
let contact_id: Option<u32> = arg1.parse().ok();
|
||||||
let locations = location::get_range(
|
let locations = location::get_range(
|
||||||
&context,
|
&context,
|
||||||
sel_chat.as_ref().unwrap().get_id(),
|
Some(sel_chat.as_ref().unwrap().get_id()),
|
||||||
contact_id,
|
contact_id,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
|
|||||||
@@ -193,7 +193,8 @@ impl Kml {
|
|||||||
pub async fn send_locations_to_chat(context: &Context, chat_id: ChatId, seconds: i64) {
|
pub async fn send_locations_to_chat(context: &Context, chat_id: ChatId, seconds: i64) {
|
||||||
let now = time();
|
let now = time();
|
||||||
if !(seconds < 0 || chat_id.is_special()) {
|
if !(seconds < 0 || chat_id.is_special()) {
|
||||||
let is_sending_locations_before = is_sending_locations_to_chat(context, chat_id).await;
|
let is_sending_locations_before =
|
||||||
|
is_sending_locations_to_chat(context, Some(chat_id)).await;
|
||||||
if context
|
if context
|
||||||
.sql
|
.sql
|
||||||
.execute(
|
.execute(
|
||||||
@@ -249,15 +250,29 @@ async fn schedule_maybe_send_locations(context: &Context, force_schedule: bool)
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn is_sending_locations_to_chat(context: &Context, chat_id: ChatId) -> bool {
|
/// Returns whether `chat_id` or any chat is sending locations.
|
||||||
context
|
///
|
||||||
.sql
|
/// If `chat_id` is `Some` only that chat is checked, otherwise returns `true` if any chat
|
||||||
.exists(
|
/// is sending locations.
|
||||||
"SELECT id FROM chats WHERE (? OR id=?) AND locations_send_until>?;",
|
pub async fn is_sending_locations_to_chat(context: &Context, chat_id: Option<ChatId>) -> bool {
|
||||||
paramsv![if chat_id.is_unset() { 1 } else { 0 }, chat_id, time()],
|
match chat_id {
|
||||||
)
|
Some(chat_id) => context
|
||||||
.await
|
.sql
|
||||||
.unwrap_or_default()
|
.exists(
|
||||||
|
"SELECT id FROM chats WHERE id=? AND locations_send_until>?;",
|
||||||
|
paramsv![chat_id, time()],
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap_or_default(),
|
||||||
|
None => context
|
||||||
|
.sql
|
||||||
|
.exists(
|
||||||
|
"SELECT id FROM chats WHERE locations_send_until>?;",
|
||||||
|
paramsv![time()],
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap_or_default(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64) -> bool {
|
pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64) -> bool {
|
||||||
@@ -305,14 +320,22 @@ pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64
|
|||||||
|
|
||||||
pub async fn get_range(
|
pub async fn get_range(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
chat_id: ChatId,
|
chat_id: Option<ChatId>,
|
||||||
contact_id: u32,
|
contact_id: Option<u32>,
|
||||||
timestamp_from: i64,
|
timestamp_from: i64,
|
||||||
mut timestamp_to: i64,
|
mut timestamp_to: i64,
|
||||||
) -> Vec<Location> {
|
) -> Vec<Location> {
|
||||||
if timestamp_to == 0 {
|
if timestamp_to == 0 {
|
||||||
timestamp_to = time() + 10;
|
timestamp_to = time() + 10;
|
||||||
}
|
}
|
||||||
|
let (disable_chat_id, chat_id) = match chat_id {
|
||||||
|
Some(chat_id) => (0, chat_id),
|
||||||
|
None => (1, ChatId::new(0)), // this ChatId is unused
|
||||||
|
};
|
||||||
|
let (disable_contact_id, contact_id) = match contact_id {
|
||||||
|
Some(contact_id) => (0, contact_id),
|
||||||
|
None => (1, 0), // this contact_id is unused
|
||||||
|
};
|
||||||
context
|
context
|
||||||
.sql
|
.sql
|
||||||
.query_map(
|
.query_map(
|
||||||
@@ -323,9 +346,9 @@ pub async fn get_range(
|
|||||||
AND (l.independent=1 OR (l.timestamp>=? AND l.timestamp<=?)) \
|
AND (l.independent=1 OR (l.timestamp>=? AND l.timestamp<=?)) \
|
||||||
ORDER BY l.timestamp DESC, l.id DESC, msg_id DESC;",
|
ORDER BY l.timestamp DESC, l.id DESC, msg_id DESC;",
|
||||||
paramsv![
|
paramsv![
|
||||||
if chat_id.is_unset() { 1 } else { 0 },
|
disable_chat_id,
|
||||||
chat_id,
|
chat_id,
|
||||||
if contact_id == 0 { 1 } else { 0 },
|
disable_contact_id,
|
||||||
contact_id as i32,
|
contact_id as i32,
|
||||||
timestamp_from,
|
timestamp_from,
|
||||||
timestamp_to,
|
timestamp_to,
|
||||||
|
|||||||
@@ -978,7 +978,7 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
|
|||||||
parts.push(msg_kml_part);
|
parts.push(msg_kml_part);
|
||||||
}
|
}
|
||||||
|
|
||||||
if location::is_sending_locations_to_chat(context, self.msg.chat_id).await {
|
if location::is_sending_locations_to_chat(context, Some(self.msg.chat_id)).await {
|
||||||
match self.get_location_kml_part().await {
|
match self.get_location_kml_part().await {
|
||||||
Ok(part) => parts.push(part),
|
Ok(part) => parts.push(part),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user