text
stringlengths
0
357
|| chat_id<=DC_CHAT_ID_LAST_SPECIAL || locations==NULL) {
goto cleanup;
}
stmt_test = dc_sqlite3_prepare(context->sql,
"SELECT id FROM locations WHERE timestamp=? AND from_id=?");
stmt_insert = dc_sqlite3_prepare(context->sql,
"INSERT INTO locations "
" (timestamp,from_id,chat_id, latitude,longitude,accuracy, independent)"
" VALUES (?,?,?, ?,?,?, ?);");
for (int i=0; i<dc_array_get_cnt(locations); i++)
{
dc_location_t* location = dc_array_get_ptr(locations, i);
sqlite3_reset (stmt_test);
sqlite3_bind_int64(stmt_test, 1, location->timestamp);
sqlite3_bind_int (stmt_test, 2, contact_id);
if (independent || sqlite3_step(stmt_test)!=SQLITE_ROW)
{
sqlite3_reset (stmt_insert);
sqlite3_bind_int64 (stmt_insert, 1, location->timestamp);
sqlite3_bind_int (stmt_insert, 2, contact_id);
sqlite3_bind_int (stmt_insert, 3, chat_id);
sqlite3_bind_double(stmt_insert, 4, location->latitude);
sqlite3_bind_double(stmt_insert, 5, location->longitude);
sqlite3_bind_double(stmt_insert, 6, location->accuracy);
sqlite3_bind_double(stmt_insert, 7, independent);
sqlite3_step(stmt_insert);
}
if (location->timestamp > newest_timestamp) {
newest_timestamp = location->timestamp;
newest_location_id = dc_sqlite3_get_rowid2(context->sql, "locations",
"timestamp", location->timestamp,
"from_id", contact_id);
}
}
cleanup:
sqlite3_finalize(stmt_test);
sqlite3_finalize(stmt_insert);
return newest_location_id;
}
/*******************************************************************************
* job to send locations out to all chats that want them
******************************************************************************/
#define MAYBE_SEND_LOCATIONS_WAIT_SECONDS 60
static void schedule_MAYBE_SEND_LOCATIONS(dc_context_t* context, int flags)
{
#define FORCE_SCHEDULE 0x01
if ((flags&FORCE_SCHEDULE)
|| !dc_job_action_exists(context, DC_JOB_MAYBE_SEND_LOCATIONS)) {
dc_job_add(context, DC_JOB_MAYBE_SEND_LOCATIONS, 0, NULL,
MAYBE_SEND_LOCATIONS_WAIT_SECONDS);
}
}
void dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(dc_context_t* context, dc_job_t* job)
{
sqlite3_stmt* stmt_chats = NULL;
sqlite3_stmt* stmt_locations = NULL;
time_t now = time(NULL);
int continue_streaming = 1;
dc_log_info(context, 0, " ----------------- MAYBE_SEND_LOCATIONS -------------- ");
stmt_chats = dc_sqlite3_prepare(context->sql,
"SELECT id, locations_send_begin, locations_last_sent "
" FROM chats "
" WHERE locations_send_until>?;"); // this should be the same condition as for the return value dc_set_location()
sqlite3_bind_int64(stmt_chats, 1, now);
while (sqlite3_step(stmt_chats)==SQLITE_ROW)
{
uint32_t chat_id = sqlite3_column_int (stmt_chats, 0);
time_t locations_send_begin = sqlite3_column_int64(stmt_chats, 1);
time_t locations_last_sent = sqlite3_column_int64(stmt_chats, 2);
continue_streaming = 1;
// be a bit tolerant as the timer may not align exactly with time(NULL)
if (now-locations_last_sent < (MAYBE_SEND_LOCATIONS_WAIT_SECONDS-3)) {
continue;
}
if (stmt_locations==NULL) {
stmt_locations = dc_sqlite3_prepare(context->sql,
"SELECT id "
" FROM locations "
" WHERE from_id=? "
" AND timestamp>=? "
" AND timestamp>? "