add dc_has_device_msg()

This commit is contained in:
B. Petersen
2019-11-18 17:14:31 +01:00
parent a4e92694ba
commit 1534a07ded
3 changed files with 62 additions and 6 deletions

View File

@@ -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.

View File

@@ -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() {

View File

@@ -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<bool, Error> {
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<str>) {
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()