Return Vec from dc_get_chat_contacts

This commit is contained in:
Alexander Krotov
2019-08-11 20:59:54 +03:00
parent 39fbff5fb6
commit fb7bbac524
6 changed files with 35 additions and 64 deletions

View File

@@ -472,7 +472,7 @@ impl<'a> Contact<'a> {
context: &Context,
listflags: u32,
query: Option<impl AsRef<str>>,
) -> Result<*mut dc_array_t> {
) -> Result<Vec<u32>> {
let self_addr = context
.get_config(Config::ConfiguredAddr)
.unwrap_or_default();
@@ -548,7 +548,7 @@ impl<'a> Contact<'a> {
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 {

View File

@@ -1406,12 +1406,12 @@ pub fn dc_delete_chat(context: &Context, chat_id: u32) -> bool {
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
groupchat but the chats stays visible, moreover, this makes displaying lists easier) */
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
@@ -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 \
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;",
params![chat_id as i32],
|row| row.get::<_, i32>(0),
|ids| {
let mut ret = Vec::new();
for id in ids {
ret.push(id? as u32);
}
Ok(dc_array_t::from(ret).into_raw())
},
params![chat_id],
|row| row.get::<_, u32>(0),
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
)
.unwrap_or_else(|_| std::ptr::null_mut())
.unwrap_or_default()
}
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 {
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 contacts: *mut dc_array_t = 0 as *mut dc_array_t;
if !(chat.is_null() || (*chat).magic != DC_CHAT_MAGIC) {
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 {
image_abs = dc_get_abs_path((*chat).context, image_rel)
} else if (*chat).type_0 == DC_CHAT_TYPE_SINGLE {
contacts = dc_get_chat_contacts((*chat).context, (*chat).id);
if !(*contacts).is_empty() {
if let Ok(contact) = Contact::get_by_id((*chat).context, (*contacts).get_id(0)) {
let contacts = dc_get_chat_contacts((*chat).context, (*chat).id);
if !contacts.is_empty() {
if let Ok(contact) = Contact::get_by_id((*chat).context, contacts[0]) {
if let Some(img) = contact.get_profile_image() {
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);
dc_array_unref(contacts);
image_abs
}
pub unsafe fn dc_chat_get_color(chat: *const Chat) -> 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).type_0 == DC_CHAT_TYPE_SINGLE {
contacts = dc_get_chat_contacts((*chat).context, (*chat).id);
if !(*contacts).is_empty() {
if let Ok(contact) = Contact::get_by_id((*chat).context, (*contacts).get_id(0)) {
let contacts = dc_get_chat_contacts((*chat).context, (*chat).id);
if !contacts.is_empty() {
if let Ok(contact) = Contact::get_by_id((*chat).context, contacts[0]) {
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
}

View File

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