From 1534a07deda2c36cd69923abc9698695dfc0d2a1 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 18 Nov 2019 17:14:31 +0100 Subject: [PATCH] add dc_has_device_msg() --- deltachat-ffi/deltachat.h | 15 +++++++++++++++ deltachat-ffi/src/lib.rs | 17 +++++++++++++++++ src/chat.rs | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index bb7807d16..22a544b3c 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -1171,6 +1171,21 @@ uint32_t dc_add_device_msg_unlabelled(dc_context_t* context, dc_msg_t* ms void dc_skip_device_msg (dc_context_t* context, const char* label); + +/** + * Check if a device-message was ever added or skipped. + * Device-messages can be added or skipped + * using dc_add_device_msg_once() or dc_skip_device_msg(). + * + * @memberof dc_context_t + * @param context The context as created by dc_context_new(). + * @param label Label of the message to check. + * @return 1=A message with this label was added or skipped at some point, + * 0=A message with this label was never added nor skipped. + */ +int dc_has_device_msg (dc_context_t* context, const char* label); + + /** * 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 a8722ef47..c0bbb5dff 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -870,6 +870,23 @@ pub unsafe extern "C" fn dc_skip_device_msg( .unwrap_or(()) } +#[no_mangle] +pub unsafe extern "C" fn dc_has_device_msg( + context: *mut dc_context_t, + label: *const libc::c_char, +) -> libc::c_int { + if context.is_null() || label.is_null() { + eprintln!("ignoring careless call to dc_has_device_msg()"); + return 0; + } + let ffi_context = &mut *context; + ffi_context + .with_inner(|ctx| { + chat::has_device_msg(ctx, &to_string_lossy(label)).unwrap_or(false) as libc::c_int + }) + .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() { diff --git a/src/chat.rs b/src/chat.rs index 2ae1fa16a..6bb0b40dc 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -2021,12 +2021,7 @@ fn add_device_msg_maybe_labelled( } pub fn skip_device_msg(context: &Context, label: &str) -> Result<(), Error> { - ensure!(!label.is_empty(), "cannot skip empty label"); - if let Ok(()) = context.sql.query_row( - "SELECT label FROM devmsglabels WHERE label=?", - params![label], - |_| Ok(()), - ) { + if has_device_msg(context, label)? { info!(context, "device-message {} already added", label); return Ok(()); } @@ -2039,6 +2034,19 @@ pub fn skip_device_msg(context: &Context, label: &str) -> Result<(), Error> { Ok(()) } +pub fn has_device_msg(context: &Context, label: &str) -> Result { + ensure!(!label.is_empty(), "empty label"); + if let Ok(()) = context.sql.query_row( + "SELECT label FROM devmsglabels WHERE label=?", + params![label], + |_| Ok(()), + ) { + return Ok(true); + } + + Ok(false) +} + pub fn add_info_msg(context: &Context, chat_id: u32, text: impl AsRef) { let rfc724_mid = dc_create_outgoing_rfc724_mid(None, "@device"); @@ -2251,6 +2259,22 @@ mod tests { assert!(!msg_id.as_ref().unwrap().is_unset()); } + #[test] + fn test_has_device_msg() { + let t = test_context(Some(Box::new(logging_cb))); + skip_device_msg(&t.ctx, "some-label").ok(); + assert!(has_device_msg(&t.ctx, "some-label").unwrap()); + + let mut msg = Message::new(Viewtype::Text); + msg.text = Some("message text".to_string()); + add_device_msg_once(&t.ctx, "another-label", &mut msg).ok(); + assert!(has_device_msg(&t.ctx, "another-label").unwrap()); + + assert!(!has_device_msg(&t.ctx, "unused-label").unwrap()); + + assert!(has_device_msg(&t.ctx, "").is_err()); + } + fn chatlist_len(ctx: &Context, listflags: usize) -> usize { Chatlist::try_load(ctx, listflags, None, None) .unwrap()