Merge pull request #334 from link2xt/dc_get_chat_contacts_vec

Return Vec from dc_get_chat_contacts
This commit is contained in:
Jikstra
2019-08-13 02:03:58 +02:00
committed by GitHub
6 changed files with 35 additions and 64 deletions

View File

@@ -545,7 +545,7 @@ pub unsafe extern "C" fn dc_get_chat_contacts(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
dc_chat::dc_get_chat_contacts(context, chat_id) dc_array_t::from(dc_chat::dc_get_chat_contacts(context, chat_id)).into_raw()
} }
#[no_mangle] #[no_mangle]
@@ -817,7 +817,7 @@ pub unsafe extern "C" fn dc_get_contacts(
}; };
match Contact::get_all(context, flags, query) { match Contact::get_all(context, flags, query) {
Ok(contacts) => contacts, Ok(contacts) => dc_array_t::from(contacts).into_raw(),
Err(_) => std::ptr::null_mut(), Err(_) => std::ptr::null_mut(),
} }
} }

View File

@@ -301,13 +301,12 @@ unsafe fn log_msglist(context: &Context, msglist: *mut dc_array_t) {
} }
} }
unsafe fn log_contactlist(context: &Context, contacts: *mut dc_array_t) { unsafe fn log_contactlist(context: &Context, contacts: &Vec<u32>) {
if !dc_array_search_id(contacts, 1 as uint32_t, 0 as *mut size_t) { let mut contacts = contacts.clone();
dc_array_add_id(contacts, 1 as uint32_t); if !contacts.contains(&1) {
contacts.push(1);
} }
let cnt = dc_array_get_cnt(contacts); for contact_id in contacts {
for i in 0..cnt {
let contact_id = dc_array_get_id(contacts, i as size_t);
let line; let line;
let mut line2 = "".to_string(); let mut line2 = "".to_string();
if let Ok(contact) = Contact::get_by_id(context, contact_id) { if let Ok(contact) = Contact::get_by_id(context, contact_id) {
@@ -835,16 +834,14 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
ensure!(!sel_chat.is_null(), "No chat selected."); ensure!(!sel_chat.is_null(), "No chat selected.");
let contacts = dc_get_chat_contacts(context, dc_chat_get_id(sel_chat)); let contacts = dc_get_chat_contacts(context, dc_chat_get_id(sel_chat));
ensure!(!contacts.is_null(), "Failed to retreive contacts");
info!(context, 0, "Memberlist:"); info!(context, 0, "Memberlist:");
log_contactlist(context, contacts); log_contactlist(context, &contacts);
println!( println!(
"{} contacts\nLocation streaming: {}", "{} contacts\nLocation streaming: {}",
dc_array_get_cnt(contacts), contacts.len(),
dc_is_sending_locations_to_chat(context, dc_chat_get_id(sel_chat)), dc_is_sending_locations_to_chat(context, dc_chat_get_id(sel_chat)),
); );
dc_array_unref(contacts);
} }
"getlocations" => { "getlocations" => {
let contact_id = arg1.parse().unwrap_or_default(); let contact_id = arg1.parse().unwrap_or_default();
@@ -1064,13 +1061,8 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
}, },
Some(arg1), Some(arg1),
)?; )?;
if !contacts.is_null() { log_contactlist(context, &contacts);
log_contactlist(context, contacts); println!("{} contacts.", contacts.len());
println!("{} contacts.", dc_array_get_cnt(contacts) as libc::c_int,);
dc_array_unref(contacts);
} else {
bail!("");
}
} }
"addcontact" => { "addcontact" => {
ensure!(!arg1.is_empty(), "Arguments [<name>] <addr> expected."); ensure!(!arg1.is_empty(), "Arguments [<name>] <addr> expected.");

View File

@@ -472,7 +472,7 @@ impl<'a> Contact<'a> {
context: &Context, context: &Context,
listflags: u32, listflags: u32,
query: Option<impl AsRef<str>>, query: Option<impl AsRef<str>>,
) -> Result<*mut dc_array_t> { ) -> Result<Vec<u32>> {
let self_addr = context let self_addr = context
.get_config(Config::ConfiguredAddr) .get_config(Config::ConfiguredAddr)
.unwrap_or_default(); .unwrap_or_default();
@@ -548,7 +548,7 @@ impl<'a> Contact<'a> {
ret.push(DC_CONTACT_ID_SELF as u32); ret.push(DC_CONTACT_ID_SELF as u32);
} }
Ok(dc_array_t::from(ret).into_raw()) Ok(ret)
} }
pub fn get_blocked_cnt(context: &Context) -> usize { pub fn get_blocked_cnt(context: &Context) -> usize {

View File

@@ -1406,12 +1406,12 @@ pub fn dc_delete_chat(context: &Context, chat_id: u32) -> bool {
true true
} }
pub fn dc_get_chat_contacts(context: &Context, chat_id: u32) -> *mut dc_array_t { pub fn dc_get_chat_contacts(context: &Context, chat_id: u32) -> Vec<u32> {
/* Normal chats do not include SELF. Group chats do (as it may happen that one is deleted from a /* Normal chats do not include SELF. Group chats do (as it may happen that one is deleted from a
groupchat but the chats stays visible, moreover, this makes displaying lists easier) */ groupchat but the chats stays visible, moreover, this makes displaying lists easier) */
if chat_id == 1 { if chat_id == 1 {
return std::ptr::null_mut(); return Vec::new();
} }
// we could also create a list for all contacts in the deaddrop by searching contacts belonging to chats with // we could also create a list for all contacts in the deaddrop by searching contacts belonging to chats with
@@ -1423,19 +1423,11 @@ pub fn dc_get_chat_contacts(context: &Context, chat_id: u32) -> *mut dc_array_t
"SELECT cc.contact_id FROM chats_contacts cc \ "SELECT cc.contact_id FROM chats_contacts cc \
LEFT JOIN contacts c ON c.id=cc.contact_id WHERE cc.chat_id=? \ LEFT JOIN contacts c ON c.id=cc.contact_id WHERE cc.chat_id=? \
ORDER BY c.id=1, LOWER(c.name||c.addr), c.id;", ORDER BY c.id=1, LOWER(c.name||c.addr), c.id;",
params![chat_id as i32], params![chat_id],
|row| row.get::<_, i32>(0), |row| row.get::<_, u32>(0),
|ids| { |ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
let mut ret = Vec::new();
for id in ids {
ret.push(id? as u32);
}
Ok(dc_array_t::from(ret).into_raw())
},
) )
.unwrap_or_else(|_| std::ptr::null_mut()) .unwrap_or_default()
} }
pub unsafe fn dc_get_chat(context: &Context, chat_id: uint32_t) -> *mut Chat { pub unsafe fn dc_get_chat(context: &Context, chat_id: uint32_t) -> *mut Chat {
@@ -2099,7 +2091,6 @@ pub fn dc_get_chat_contact_cnt(context: &Context, chat_id: u32) -> libc::c_int {
pub unsafe fn dc_chat_get_profile_image(chat: *const Chat) -> *mut libc::c_char { pub unsafe fn dc_chat_get_profile_image(chat: *const Chat) -> *mut libc::c_char {
let mut image_rel: *mut libc::c_char = 0 as *mut libc::c_char; let mut image_rel: *mut libc::c_char = 0 as *mut libc::c_char;
let mut image_abs: *mut libc::c_char = 0 as *mut libc::c_char; let mut image_abs: *mut libc::c_char = 0 as *mut libc::c_char;
let mut contacts: *mut dc_array_t = 0 as *mut dc_array_t;
if !(chat.is_null() || (*chat).magic != DC_CHAT_MAGIC) { if !(chat.is_null() || (*chat).magic != DC_CHAT_MAGIC) {
image_rel = (*chat) image_rel = (*chat)
@@ -2110,9 +2101,9 @@ pub unsafe fn dc_chat_get_profile_image(chat: *const Chat) -> *mut libc::c_char
if !image_rel.is_null() && 0 != *image_rel.offset(0isize) as libc::c_int { if !image_rel.is_null() && 0 != *image_rel.offset(0isize) as libc::c_int {
image_abs = dc_get_abs_path((*chat).context, image_rel) image_abs = dc_get_abs_path((*chat).context, image_rel)
} else if (*chat).type_0 == DC_CHAT_TYPE_SINGLE { } else if (*chat).type_0 == DC_CHAT_TYPE_SINGLE {
contacts = dc_get_chat_contacts((*chat).context, (*chat).id); let contacts = dc_get_chat_contacts((*chat).context, (*chat).id);
if !(*contacts).is_empty() { if !contacts.is_empty() {
if let Ok(contact) = Contact::get_by_id((*chat).context, (*contacts).get_id(0)) { if let Ok(contact) = Contact::get_by_id((*chat).context, contacts[0]) {
if let Some(img) = contact.get_profile_image() { if let Some(img) = contact.get_profile_image() {
image_abs = img.strdup(); image_abs = img.strdup();
} }
@@ -2122,20 +2113,18 @@ pub unsafe fn dc_chat_get_profile_image(chat: *const Chat) -> *mut libc::c_char
} }
free(image_rel as *mut libc::c_void); free(image_rel as *mut libc::c_void);
dc_array_unref(contacts);
image_abs image_abs
} }
pub unsafe fn dc_chat_get_color(chat: *const Chat) -> uint32_t { pub unsafe fn dc_chat_get_color(chat: *const Chat) -> uint32_t {
let mut color: uint32_t = 0i32 as uint32_t; let mut color: uint32_t = 0i32 as uint32_t;
let mut contacts: *mut dc_array_t = 0 as *mut dc_array_t;
if !(chat.is_null() || (*chat).magic != DC_CHAT_MAGIC) { if !(chat.is_null() || (*chat).magic != DC_CHAT_MAGIC) {
if (*chat).type_0 == DC_CHAT_TYPE_SINGLE { if (*chat).type_0 == DC_CHAT_TYPE_SINGLE {
contacts = dc_get_chat_contacts((*chat).context, (*chat).id); let contacts = dc_get_chat_contacts((*chat).context, (*chat).id);
if !(*contacts).is_empty() { if !contacts.is_empty() {
if let Ok(contact) = Contact::get_by_id((*chat).context, (*contacts).get_id(0)) { if let Ok(contact) = Contact::get_by_id((*chat).context, contacts[0]) {
color = contact.get_color(); color = contact.get_color();
} }
} }
@@ -2144,8 +2133,6 @@ pub unsafe fn dc_chat_get_color(chat: *const Chat) -> uint32_t {
} }
} }
dc_array_unref(contacts);
color color
} }

View File

@@ -7,7 +7,6 @@ use crate::aheader::EncryptPreference;
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;
use crate::context::Context; use crate::context::Context;
use crate::dc_array::*;
use crate::dc_chat::*; use crate::dc_chat::*;
use crate::dc_configure::*; use crate::dc_configure::*;
use crate::dc_e2ee::*; use crate::dc_e2ee::*;
@@ -296,14 +295,12 @@ unsafe fn send_handshake_msg(
} }
unsafe fn chat_id_2_contact_id(context: &Context, contact_chat_id: uint32_t) -> uint32_t { unsafe fn chat_id_2_contact_id(context: &Context, contact_chat_id: uint32_t) -> uint32_t {
let mut contact_id: uint32_t = 0i32 as uint32_t; let contacts = dc_get_chat_contacts(context, contact_chat_id);
let contacts: *mut dc_array_t = dc_get_chat_contacts(context, contact_chat_id); if contacts.len() == 1 {
if !(dc_array_get_cnt(contacts) != 1) { contacts[0]
contact_id = dc_array_get_id(contacts, 0i32 as size_t) } else {
0
} }
dc_array_unref(contacts);
contact_id
} }
unsafe fn fingerprint_equals_sender( unsafe fn fingerprint_equals_sender(
@@ -317,8 +314,8 @@ unsafe fn fingerprint_equals_sender(
let mut fingerprint_equal: libc::c_int = 0i32; let mut fingerprint_equal: libc::c_int = 0i32;
let contacts = dc_get_chat_contacts(context, contact_chat_id); let contacts = dc_get_chat_contacts(context, contact_chat_id);
if !(dc_array_get_cnt(contacts) != 1) { if contacts.len() == 1 {
if let Ok(contact) = Contact::load_from_db(context, dc_array_get_id(contacts, 0)) { if let Ok(contact) = Contact::load_from_db(context, contacts[0]) {
if let Some(peerstate) = Peerstate::from_addr(context, &context.sql, contact.get_addr()) if let Some(peerstate) = Peerstate::from_addr(context, &context.sql, contact.get_addr())
{ {
let fingerprint_normalized = dc_normalize_fingerprint(as_str(fingerprint)); let fingerprint_normalized = dc_normalize_fingerprint(as_str(fingerprint));
@@ -332,7 +329,6 @@ unsafe fn fingerprint_equals_sender(
return 0; return 0;
} }
} }
dc_array_unref(contacts);
fingerprint_equal fingerprint_equal
} }

View File

@@ -10,7 +10,6 @@ use deltachat::config;
use deltachat::constants::*; use deltachat::constants::*;
use deltachat::contact::*; use deltachat::contact::*;
use deltachat::context::*; use deltachat::context::*;
use deltachat::dc_array::*;
use deltachat::dc_chat::*; use deltachat::dc_chat::*;
use deltachat::dc_configure::*; use deltachat::dc_configure::*;
use deltachat::dc_imex::*; use deltachat::dc_imex::*;
@@ -809,19 +808,16 @@ fn test_get_contacts() {
unsafe { unsafe {
let context = create_test_context(); let context = create_test_context();
let contacts = Contact::get_all(&context.ctx, 0, Some("some2")).unwrap(); let contacts = Contact::get_all(&context.ctx, 0, Some("some2")).unwrap();
assert_eq!(dc_array_get_cnt(contacts), 0); assert_eq!(contacts.len(), 0);
dc_array_unref(contacts);
let id = Contact::create(&context.ctx, "bob", "bob@mail.de").unwrap(); let id = Contact::create(&context.ctx, "bob", "bob@mail.de").unwrap();
assert_ne!(id, 0); assert_ne!(id, 0);
let contacts = Contact::get_all(&context.ctx, 0, Some("bob")).unwrap(); let contacts = Contact::get_all(&context.ctx, 0, Some("bob")).unwrap();
assert_eq!(dc_array_get_cnt(contacts), 1); assert_eq!(contacts.len(), 1);
dc_array_unref(contacts);
let contacts = Contact::get_all(&context.ctx, 0, Some("alice")).unwrap(); let contacts = Contact::get_all(&context.ctx, 0, Some("alice")).unwrap();
assert_eq!(dc_array_get_cnt(contacts), 0); assert_eq!(contacts.len(), 0);
dc_array_unref(contacts);
} }
} }