diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index 97fe035c3..8ff305b95 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -3151,6 +3151,20 @@ void dc_accounts_maybe_network (dc_accounts_t* accounts); void dc_accounts_maybe_network_lost (dc_accounts_t* accounts); +/** + * Perform a Background fetch for all accounts in parallel with a timeout. + * Pauses the scheduler, fetches messages from imap and then resumes the scheduler. + * + * dc_accounts_background_fetch_with_timeout() was created for the iOS Background fetch. + * + * @memberof dc_accounts_t + * @param timeout The timeout in seconds + * @return Return 1 on success and 0 on failure (like timeout) + * But note that this only indicates that the fetch of all accounts was done before the timeout. + * To know wether it worked you need to look for the events. + */ +int dc_accounts_background_fetch_with_timeout (dc_accounts_t* accounts, uint64_t timeout); + /** * Create the event emitter that is used to receive events. * diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 5d68a940d..3793ab546 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -4898,6 +4898,34 @@ pub unsafe extern "C" fn dc_accounts_maybe_network_lost(accounts: *mut dc_accoun block_on(async move { accounts.write().await.maybe_network_lost().await }); } +#[no_mangle] +pub unsafe extern "C" fn dc_accounts_background_fetch_with_timeout( + accounts: *mut dc_accounts_t, + timeout_in_seconds: u64, +) -> libc::c_int { + if accounts.is_null() || timeout_in_seconds <= 2 { + eprintln!("ignoring careless call to dc_accounts_background_fetch_with_timeout()"); + return 0; + } + + let accounts = &*accounts; + block_on(async move { + let accounts = accounts.write().await; + match accounts + .background_fetch_with_timeout(Duration::from_secs(timeout_in_seconds)) + .await + { + Ok(()) => 1, + Err(err) => { + accounts.emit_event(EventType::Error(format!( + "Failed to do background fetch: {err:#}" + ))); + 0 + } + } + }) +} + #[no_mangle] pub unsafe extern "C" fn dc_accounts_get_event_emitter( accounts: *mut dc_accounts_t,