fix(location): do not sql recurse in location sending

This commit is contained in:
dignifiedquire
2019-09-11 17:46:05 +02:00
parent 68b5e34fed
commit 64b00fce7d

View File

@@ -548,73 +548,70 @@ pub fn job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: &Job) {
" ----------------- MAYBE_SEND_LOCATIONS -------------- ", " ----------------- MAYBE_SEND_LOCATIONS -------------- ",
); );
context if let Ok(rows) = context.sql.query_map(
.sql "SELECT id, locations_send_begin, locations_last_sent \
.query_map( FROM chats \
"SELECT id, locations_send_begin, locations_last_sent \ WHERE locations_send_until>?;",
FROM chats \ params![now],
WHERE locations_send_until>?;", |row| {
params![now], let chat_id: i32 = row.get(0)?;
|row| { let locations_send_begin: i64 = row.get(1)?;
let chat_id: i32 = row.get(0)?; let locations_last_sent: i64 = row.get(2)?;
let locations_send_begin: i64 = row.get(1)?; continue_streaming = 1;
let locations_last_sent: i64 = row.get(2)?;
continue_streaming = 1;
// be a bit tolerant as the timer may not align exactly with time(NULL) // be a bit tolerant as the timer may not align exactly with time(NULL)
if now - locations_last_sent < (60 - 3) { if now - locations_last_sent < (60 - 3) {
Ok(None) Ok(None)
} else { } else {
Ok(Some((chat_id, locations_send_begin, locations_last_sent))) Ok(Some((chat_id, locations_send_begin, locations_last_sent)))
} }
}, },
|rows| { |rows| {
context.sql.prepare( rows.filter_map(|v| v.transpose())
"SELECT id \ .collect::<Result<Vec<_>, _>>()
FROM locations \ .map_err(Into::into)
WHERE from_id=? \ },
AND timestamp>=? \ ) {
AND timestamp>? \ context
AND independent=0 \ .sql
ORDER BY timestamp;", .prepare(
|mut stmt_locations, _| { "SELECT id \
for (chat_id, locations_send_begin, locations_last_sent) in FROM locations \
rows.filter_map(|r| match r { WHERE from_id=? \
Ok(Some(v)) => Some(v), AND timestamp>=? \
_ => None, AND timestamp>? \
}) AND independent=0 \
ORDER BY timestamp;",
|mut stmt_locations, _| {
for (chat_id, locations_send_begin, locations_last_sent) in rows {
if !stmt_locations
.exists(params![1, locations_send_begin, locations_last_sent,])
.unwrap_or_default()
{ {
// TODO: do I need to reset? // if there is no new location, there's nothing to send.
if !stmt_locations // however, maybe we want to bypass this test eg. 15 minutes
.exists(params![1, locations_send_begin, locations_last_sent,]) continue;
.unwrap_or_default()
{
// if there is no new location, there's nothing to send.
// however, maybe we want to bypass this test eg. 15 minutes
continue;
}
// pending locations are attached automatically to every message,
// so also to this empty text message.
// DC_CMD_LOCATION is only needed to create a nicer subject.
//
// for optimisation and to avoid flooding the sending queue,
// we could sending these messages only if we're really online.
// the easiest way to determine this, is to check for an empty message queue.
// (might not be 100%, however, as positions are sent combined later
// and dc_set_location() is typically called periodically, this is ok)
let mut msg = dc_msg_new(context, Viewtype::Text);
msg.hidden = true;
msg.param.set_int(Param::Cmd, 9);
// TODO: handle cleanup on error
chat::send_msg(context, chat_id as u32, &mut msg).unwrap();
} }
Ok(()) // pending locations are attached automatically to every message,
}, // so also to this empty text message.
) // DC_CMD_LOCATION is only needed to create a nicer subject.
}, //
) // for optimisation and to avoid flooding the sending queue,
.unwrap(); // TODO: Better error handling // we could sending these messages only if we're really online.
// the easiest way to determine this, is to check for an empty message queue.
// (might not be 100%, however, as positions are sent combined later
// and dc_set_location() is typically called periodically, this is ok)
let mut msg = dc_msg_new(context, Viewtype::Text);
msg.hidden = true;
msg.param.set_int(Param::Cmd, 9);
// TODO: handle cleanup on error
chat::send_msg(context, chat_id as u32, &mut msg).unwrap();
}
Ok(())
},
)
.unwrap(); // TODO: Better error handling
}
if 0 != continue_streaming { if 0 != continue_streaming {
schedule_MAYBE_SEND_LOCATIONS(context, 0x1); schedule_MAYBE_SEND_LOCATIONS(context, 0x1);
} }