refactor(chat): some minor cleanup and api improvements

This commit is contained in:
dignifiedquire
2019-08-16 01:03:58 +02:00
parent 9b1a74cc22
commit 13d8306a7b
3 changed files with 115 additions and 110 deletions

View File

@@ -642,6 +642,8 @@ pub unsafe extern "C" fn dc_remove_contact_from_chat(
let context = &*context; let context = &*context;
chat::remove_contact_from_chat(context, chat_id, contact_id) chat::remove_contact_from_chat(context, chat_id, contact_id)
.map(|_| 1)
.unwrap_or_log_default(context, "Failed to remove contact")
} }
#[no_mangle] #[no_mangle]
@@ -656,6 +658,8 @@ pub unsafe extern "C" fn dc_set_chat_name(
let context = &*context; let context = &*context;
chat::set_chat_name(context, chat_id, as_str(name)) chat::set_chat_name(context, chat_id, as_str(name))
.map(|_| 1)
.unwrap_or_log_default(context, "Failed to set chat name")
} }
#[no_mangle] #[no_mangle]
@@ -669,6 +673,8 @@ pub unsafe extern "C" fn dc_set_chat_profile_image(
let context = &*context; let context = &*context;
chat::set_chat_profile_image(context, chat_id, as_str(image)) chat::set_chat_profile_image(context, chat_id, as_str(image))
.map(|_| 1)
.unwrap_or_log_default(context, "Failed to set profile image")
} }
#[no_mangle] #[no_mangle]

View File

@@ -755,35 +755,28 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
ensure!(!arg1.is_empty(), "Argument <contact-id> missing."); ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
let contact_id_1: libc::c_int = arg1.parse()?; let contact_id_1: libc::c_int = arg1.parse()?;
if 0 != chat::remove_contact_from_chat( chat::remove_contact_from_chat(
context, context,
sel_chat.as_ref().unwrap().get_id(), sel_chat.as_ref().unwrap().get_id(),
contact_id_1 as uint32_t, contact_id_1 as uint32_t,
) { )?;
println!("Contact added to chat."); println!("Contact added to chat.");
} else {
bail!("Cannot remove member from chat.");
}
} }
"groupname" => { "groupname" => {
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
ensure!(!arg1.is_empty(), "Argument <name> missing."); ensure!(!arg1.is_empty(), "Argument <name> missing.");
if 0 != chat::set_chat_name(context, sel_chat.as_ref().unwrap().get_id(), arg1) { chat::set_chat_name(context, sel_chat.as_ref().unwrap().get_id(), arg1)?;
println!("Chat name set"); println!("Chat name set");
} else {
bail!("Failed to set chat name");
}
} }
"groupimage" => { "groupimage" => {
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
ensure!(!arg1.is_empty(), "Argument <image> missing."); ensure!(!arg1.is_empty(), "Argument <image> missing.");
if 0 != chat::set_chat_profile_image(context, sel_chat.as_ref().unwrap().get_id(), arg1) chat::set_chat_profile_image(context, sel_chat.as_ref().unwrap().get_id(), arg1)?;
{
println!("Chat image set"); println!("Chat image set");
} else {
bail!("Failed to set chat image");
}
} }
"chatinfo" => { "chatinfo" => {
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");

View File

@@ -579,7 +579,7 @@ pub fn create_by_msg_id(context: &Context, msg_id: u32) -> Result<u32, Error> {
if dc_msg_load_from_db(msg, context, msg_id) { if dc_msg_load_from_db(msg, context, msg_id) {
if let Ok(chat) = Chat::load_from_db(context, unsafe { (*msg).chat_id }) { if let Ok(chat) = Chat::load_from_db(context, unsafe { (*msg).chat_id }) {
if chat.id > 9 { if chat.id > DC_CHAT_ID_LAST_SPECIAL as u32 {
chat_id = chat.id; chat_id = chat.id;
if chat.blocked != Blocked::Not { if chat.blocked != Blocked::Not {
unblock(context, chat.id); unblock(context, chat.id);
@@ -1458,7 +1458,7 @@ pub unsafe fn add_contact_to_chat_ex(
let mut success: libc::c_int = 0; let mut success: libc::c_int = 0;
let contact = Contact::get_by_id(context, contact_id); let contact = Contact::get_by_id(context, contact_id);
if contact.is_err() || chat_id <= 9 { if contact.is_err() || chat_id <= DC_CHAT_ID_LAST_SPECIAL as u32 {
return 0; return 0;
} }
let mut msg = dc_msg_new_untyped(context); let mut msg = dc_msg_new_untyped(context);
@@ -1469,7 +1469,7 @@ pub unsafe fn add_contact_to_chat_ex(
/*this also makes sure, not contacts are added to special or normal chats*/ /*this also makes sure, not contacts are added to special or normal chats*/
let chat = Chat::load_from_db(context, chat_id); let chat = Chat::load_from_db(context, chat_id);
if !(0 == real_group_exists(context, chat_id) if !(!real_group_exists(context, chat_id)
|| !Contact::real_exists_by_id(context, contact_id) || !Contact::real_exists_by_id(context, contact_id)
&& contact_id != DC_CONTACT_ID_SELF as u32 && contact_id != DC_CONTACT_ID_SELF as u32
|| chat.is_err()) || chat.is_err())
@@ -1550,11 +1550,10 @@ pub unsafe fn add_contact_to_chat_ex(
success success
} }
// TODO should return bool /rtn fn real_group_exists(context: &Context, chat_id: u32) -> bool {
fn real_group_exists(context: &Context, chat_id: u32) -> libc::c_int {
// check if a group or a verified group exists under the given ID // check if a group or a verified group exists under the given ID
if !context.sql.is_open() || chat_id <= 9 { if !context.sql.is_open() || chat_id <= DC_CHAT_ID_LAST_SPECIAL as u32 {
return 02; return false;
} }
context context
@@ -1563,7 +1562,7 @@ fn real_group_exists(context: &Context, chat_id: u32) -> libc::c_int {
"SELECT id FROM chats WHERE id=? AND (type=120 OR type=130);", "SELECT id FROM chats WHERE id=? AND (type=120 OR type=130);",
params![chat_id as i32], params![chat_id as i32],
) )
.unwrap_or_default() as libc::c_int .unwrap_or_default()
} }
pub fn reset_gossiped_timestamp(context: &Context, chat_id: u32) { pub fn reset_gossiped_timestamp(context: &Context, chat_id: u32) {
@@ -1600,25 +1599,29 @@ pub fn set_gossiped_timestamp(context: &Context, chat_id: u32, timestamp: i64) {
} }
} }
// TODO should return bool /rtn
pub unsafe fn remove_contact_from_chat( pub unsafe fn remove_contact_from_chat(
context: &Context, context: &Context,
chat_id: u32, chat_id: u32,
contact_id: u32, contact_id: u32,
) -> libc::c_int { ) -> Result<(), Error> {
let mut success = 0; ensure!(
chat_id > DC_CHAT_ID_LAST_SPECIAL as u32,
if chat_id <= 9 || contact_id <= 9 && contact_id != DC_CONTACT_ID_SELF as u32 { "bad chat_id = {} <= 9",
return 0; chat_id
} );
ensure!(
contact_id != DC_CONTACT_ID_SELF as u32,
"Cannot remove self"
);
let mut msg = dc_msg_new_untyped(context); let mut msg = dc_msg_new_untyped(context);
let mut success = false;
/* we do not check if "contact_id" exists but just delete all records with the id from chats_contacts */ /* we do not check if "contact_id" exists but just delete all records with the id from chats_contacts */
/* this allows to delete pending references to deleted contacts. Of course, this should _not_ happen. */ /* this allows to delete pending references to deleted contacts. Of course, this should _not_ happen. */
let chat = Chat::load_from_db(context, chat_id); let chat = Chat::load_from_db(context, chat_id);
if !(0 == real_group_exists(context, chat_id) || chat.is_err()) { if !(!real_group_exists(context, chat_id) || chat.is_err()) {
let chat = chat.unwrap(); let chat = chat.unwrap();
if !(is_contact_in_chat(context, chat_id, 1 as u32) == 1) { if !(is_contact_in_chat(context, chat_id, 1 as u32) == 1) {
log_event!( log_event!(
@@ -1667,14 +1670,18 @@ pub unsafe fn remove_contact_from_chat(
.is_ok() .is_ok()
{ {
context.call_cb(Event::CHAT_MODIFIED, chat_id as uintptr_t, 0 as uintptr_t); context.call_cb(Event::CHAT_MODIFIED, chat_id as uintptr_t, 0 as uintptr_t);
success = 1; success = true;
} }
} }
} }
dc_msg_unref(msg); dc_msg_unref(msg);
success if !success {
bail!("Failed to remove contact");
}
Ok(())
} }
fn set_group_explicitly_left(context: &Context, grpid: impl AsRef<str>) -> Result<(), Error> { fn set_group_explicitly_left(context: &Context, grpid: impl AsRef<str>) -> Result<(), Error> {
@@ -1690,7 +1697,6 @@ fn set_group_explicitly_left(context: &Context, grpid: impl AsRef<str>) -> Resul
Ok(()) Ok(())
} }
// TODO should return bool /rtn
pub fn is_group_explicitly_left(context: &Context, grpid: impl AsRef<str>) -> Result<bool, Error> { pub fn is_group_explicitly_left(context: &Context, grpid: impl AsRef<str>) -> Result<bool, Error> {
context.sql.exists( context.sql.exists(
"SELECT id FROM leftgrps WHERE grpid=?;", "SELECT id FROM leftgrps WHERE grpid=?;",
@@ -1698,26 +1704,23 @@ pub fn is_group_explicitly_left(context: &Context, grpid: impl AsRef<str>) -> Re
) )
} }
// TODO should return bool /rtn
pub unsafe fn set_chat_name( pub unsafe fn set_chat_name(
context: &Context, context: &Context,
chat_id: u32, chat_id: u32,
new_name: impl AsRef<str>, new_name: impl AsRef<str>,
) -> libc::c_int { ) -> Result<(), Error> {
/* the function only sets the names of group chats; normal chats get their names from the contacts */ /* the function only sets the names of group chats; normal chats get their names from the contacts */
let mut success: libc::c_int = 0i32; let mut success = false;
ensure!(!new_name.as_ref().is_empty(), "Invalid name");
ensure!(chat_id > DC_CHAT_ID_LAST_SPECIAL as u32, "Invalid chat ID");
let chat = Chat::load_from_db(context, chat_id)?;
let mut msg = dc_msg_new_untyped(context); let mut msg = dc_msg_new_untyped(context);
if new_name.as_ref().is_empty() || chat_id <= DC_CHAT_ID_LAST_SPECIAL as u32 { if real_group_exists(context, chat_id) {
return 0;
}
let chat = Chat::load_from_db(context, chat_id);
if !(0i32 == real_group_exists(context, chat_id) || chat.is_err()) {
let chat = chat.unwrap();
if &chat.name == new_name.as_ref() { if &chat.name == new_name.as_ref() {
success = 1; success = true;
} else if !(is_contact_in_chat(context, chat_id, 1) == 1) { } else if !(is_contact_in_chat(context, chat_id, 1) == 1) {
log_event!( log_event!(
context, context,
@@ -1763,36 +1766,36 @@ pub unsafe fn set_chat_name(
chat_id as uintptr_t, chat_id as uintptr_t,
0i32 as uintptr_t, 0i32 as uintptr_t,
); );
success = 1i32; success = true;
} }
} }
} }
dc_msg_unref(msg); dc_msg_unref(msg);
success if !success {
bail!("Failed to set name");
}
Ok(())
} }
// TODO should return bool /rtn
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub unsafe fn set_chat_profile_image( pub unsafe fn set_chat_profile_image(
context: &Context, context: &Context,
chat_id: u32, chat_id: u32,
new_image: impl AsRef<str>, new_image: impl AsRef<str>,
) -> libc::c_int { ) -> Result<(), Error> {
ensure!(chat_id > DC_CHAT_ID_LAST_SPECIAL as u32, "Invalid chat ID");
let mut OK_TO_CONTINUE = true; let mut OK_TO_CONTINUE = true;
let mut success: libc::c_int = 0i32; let mut success = false;
if chat_id <= 9 {
return 0;
}
let mut chat = Chat::load_from_db(context, chat_id)?;
let mut msg = dc_msg_new_untyped(context); let mut msg = dc_msg_new_untyped(context);
if !(chat_id <= 9i32 as libc::c_uint) {
let mut new_image_rel = None; let mut new_image_rel = None;
let chat = Chat::load_from_db(context, chat_id);
if !(0i32 == real_group_exists(context, chat_id) || chat.is_err()) { if real_group_exists(context, chat_id) {
let mut chat = chat.unwrap();
if !(is_contact_in_chat(context, chat_id, 1i32 as u32) == 1i32) { if !(is_contact_in_chat(context, chat_id, 1i32 as u32) == 1i32) {
log_event!( log_event!(
context, context,
@@ -1845,15 +1848,18 @@ pub unsafe fn set_chat_profile_image(
chat_id as uintptr_t, chat_id as uintptr_t,
0i32 as uintptr_t, 0i32 as uintptr_t,
); );
success = 1i32; success = true;
}
} }
} }
} }
dc_msg_unref(msg); dc_msg_unref(msg);
success if !success {
bail!("Failed to set profile image");
}
Ok(())
} }
pub unsafe fn forward_msgs( pub unsafe fn forward_msgs(
@@ -1862,7 +1868,7 @@ pub unsafe fn forward_msgs(
msg_cnt: libc::c_int, msg_cnt: libc::c_int,
chat_id: u32, chat_id: u32,
) { ) {
if msg_ids.is_null() || msg_cnt <= 0 || chat_id <= 9 { if msg_ids.is_null() || msg_cnt <= 0 || chat_id <= DC_CHAT_ID_LAST_SPECIAL as u32 {
return; return;
} }