From b42d8799b4bb25873ad176bde27af0edfdff2634 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Tue, 12 Nov 2019 12:22:02 +0100 Subject: [PATCH] add dc_add_device_msg_once() to fii --- deltachat-ffi/deltachat.h | 22 ++++++++++++++++++++++ deltachat-ffi/src/lib.rs | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index fa13b6e5f..91001b744 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -1113,6 +1113,28 @@ void dc_set_draft (dc_context_t* context, uint32_t ch uint32_t dc_add_device_msg (dc_context_t* context, dc_msg_t* msg); +/** + * Add a message only one time to the device-chat. + * The device-message is defined by a name. + * If a message with the same name was added before, + * the message is not added again. + * Use dc_add_device_msg() to add device-messages unconditionally. + * + * Sends the event #DC_EVENT_MSGS_CHANGED on success. + * + * @memberof dc_context_t + * @param context The context as created by dc_context_new(). + * @param label A unique name for the message to add. + * The label is typically not displayed to the user and + * must be created from the characters `A-Z`, `a-z`, `0-9`, `_` or `-`. + * @param msg Message to be added to the device-chat. + * The message appears to the user as an incoming message. + * @return The ID of the added message, + * this might be the id of an older message with the same name. + */ +uint32_t dc_add_device_msg_once (dc_context_t* context, const char* label, dc_msg_t* msg); + + /** * Get draft for a chat, if any. * See dc_set_draft() for more details about drafts. diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 6c52f09ca..b7895549d 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -828,6 +828,27 @@ pub unsafe extern "C" fn dc_add_device_msg(context: *mut dc_context_t, msg: *mut .unwrap_or(0) } +#[no_mangle] +pub unsafe extern "C" fn dc_add_device_msg_once( + context: *mut dc_context_t, + label: *const libc::c_char, + msg: *mut dc_msg_t, +) -> u32 { + if context.is_null() || label.is_null() || msg.is_null() { + eprintln!("ignoring careless call to dc_add_device_msg_once()"); + return 0; + } + let ffi_context = &mut *context; + let ffi_msg = &mut *msg; + ffi_context + .with_inner(|ctx| { + chat::add_device_msg_once(ctx, &to_string_lossy(label), &mut ffi_msg.message) + .unwrap_or_log_default(ctx, "Failed to add device message once") + }) + .map(|msg_id| msg_id.to_u32()) + .unwrap_or(0) +} + #[no_mangle] pub unsafe extern "C" fn dc_get_draft(context: *mut dc_context_t, chat_id: u32) -> *mut dc_msg_t { if context.is_null() {