fix: get_contacts logic was broken

This commit is contained in:
dignifiedquire
2019-07-12 11:17:49 +02:00
parent 08d8eebc37
commit 9d2ee5c0f7
2 changed files with 44 additions and 16 deletions

View File

@@ -13,6 +13,8 @@ use crate::peerstate::*;
use crate::types::*;
use crate::x::*;
const DC_GCL_VERIFIED_ONLY: u32 = 0x01;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct dc_contact_t<'a> {
@@ -523,23 +525,9 @@ pub fn dc_get_contacts(
let mut add_self = false;
let ret = unsafe { dc_array_new(100) };
if 0 == listflags & 0x1 || query.is_null() {
add_self = true;
context.sql.query_map(
"SELECT id FROM contacts WHERE addr!=?1 AND id>?2 AND origin>=?3 AND blocked=0 ORDER BY LOWER(name||addr),id;",
params![self_addr, 9, 0x100],
|row| row.get::<_, i32>(0),
|ids| {
for id in ids {
unsafe { dc_array_add_id(ret, id? as u32) };
}
Ok(())
}
).unwrap(); // TODO: better error handling
} else {
if (listflags & DC_GCL_VERIFIED_ONLY) > 0 || !query.is_null() {
let s3strLikeCmd = format!("%{}%", if !query.is_null() { as_str(query) } else { "" });
eprintln!("query '{}'", &s3strLikeCmd);
context
.sql
.query_map(
@@ -583,6 +571,20 @@ pub fn dc_get_contacts(
add_self = true;
}
unsafe { free(self_name2 as *mut _) };
} else {
add_self = true;
context.sql.query_map(
"SELECT id FROM contacts WHERE addr!=?1 AND id>?2 AND origin>=?3 AND blocked=0 ORDER BY LOWER(name||addr),id;",
params![self_addr, 9, 0x100],
|row| row.get::<_, i32>(0),
|ids| {
for id in ids {
unsafe { dc_array_add_id(ret, id? as u32) };
}
Ok(())
}
).unwrap(); // TODO: better error handling
}
if 0 != listflags & 0x2 && add_self {

View File

@@ -10,6 +10,7 @@ use deltachat::constants::*;
use deltachat::context::*;
use deltachat::dc_array::*;
use deltachat::dc_configure::*;
use deltachat::dc_contact::*;
use deltachat::dc_imex::*;
use deltachat::dc_location::*;
use deltachat::dc_lot::*;
@@ -945,6 +946,31 @@ fn test_stress_tests() {
}
}
#[test]
fn test_get_contacts() {
unsafe {
let context = create_test_context();
let contacts = dc_get_contacts(&context.ctx, 0, to_cstring("some2").as_ptr());
assert_eq!(dc_array_get_cnt(contacts), 0);
dc_array_unref(contacts);
let id = dc_create_contact(
&context.ctx,
to_cstring("bob").as_ptr(),
to_cstring("bob@mail.de").as_ptr(),
);
assert_ne!(id, 0);
let contacts = dc_get_contacts(&context.ctx, 0, to_cstring("bob").as_ptr());
assert_eq!(dc_array_get_cnt(contacts), 1);
dc_array_unref(contacts);
let contacts = dc_get_contacts(&context.ctx, 0, to_cstring("alice").as_ptr());
assert_eq!(dc_array_get_cnt(contacts), 0);
dc_array_unref(contacts);
}
}
#[test]
fn test_arr_to_string() {
let arr2: [uint32_t; 4] = [