refactor(chat): first round of method renaming and restructuring

This commit is contained in:
dignifiedquire
2019-08-15 23:36:24 +02:00
parent ddfd067e97
commit 001880e1f0
19 changed files with 1335 additions and 1418 deletions

View File

@@ -322,7 +322,11 @@ pub unsafe extern "C" fn dc_create_chat_by_msg_id(context: *mut dc_context_t, ms
assert!(!context.is_null());
let context = &*context;
chat::dc_create_chat_by_msg_id(context, msg_id)
log_err_default(
chat::create_by_msg_id(context, msg_id),
context,
"Failed to create chat",
)
}
#[no_mangle]
@@ -333,7 +337,11 @@ pub unsafe extern "C" fn dc_create_chat_by_contact_id(
assert!(!context.is_null());
let context = &*context;
chat::dc_create_chat_by_contact_id(context, contact_id)
log_err_default(
chat::create_by_contact_id(context, contact_id),
context,
"Failed to create chat",
)
}
#[no_mangle]
@@ -344,7 +352,11 @@ pub unsafe extern "C" fn dc_get_chat_id_by_contact_id(
assert!(!context.is_null());
let context = &*context;
chat::dc_get_chat_id_by_contact_id(context, contact_id)
log_err_default(
chat::get_by_contact_id(context, contact_id),
context,
"Failed to get chat",
)
}
#[no_mangle]
@@ -357,7 +369,11 @@ pub unsafe extern "C" fn dc_prepare_msg(
assert!(!msg.is_null());
let context = &*context;
chat::dc_prepare_msg(context, chat_id, msg)
log_err_default(
chat::prepare_msg(context, chat_id, msg),
context,
"Failed to prepare message",
)
}
#[no_mangle]
@@ -370,7 +386,11 @@ pub unsafe extern "C" fn dc_send_msg(
assert!(!msg.is_null());
let context = &*context;
chat::dc_send_msg(context, chat_id, msg)
log_err_default(
chat::send_msg(context, chat_id, msg),
context,
"Failed to send message",
)
}
#[no_mangle]
@@ -384,7 +404,11 @@ pub unsafe extern "C" fn dc_send_text_msg(
let context = &*context;
let text_to_send = dc_tools::to_string_lossy(text_to_send);
chat::dc_send_text_msg(context, chat_id, text_to_send)
log_err_default(
chat::send_text_msg(context, chat_id, text_to_send),
context,
"Failed to send text message",
)
}
#[no_mangle]
@@ -396,7 +420,7 @@ pub unsafe extern "C" fn dc_set_draft(
assert!(!context.is_null());
let context = &*context;
chat::dc_set_draft(context, chat_id, msg)
chat::set_draft(context, chat_id, msg)
}
#[no_mangle]
@@ -407,7 +431,7 @@ pub unsafe extern "C" fn dc_get_draft<'a>(
assert!(!context.is_null());
let context = &*context;
chat::dc_get_draft(context, chat_id)
chat::get_draft(context, chat_id)
}
#[no_mangle]
@@ -420,7 +444,7 @@ pub unsafe extern "C" fn dc_get_chat_msgs(
assert!(!context.is_null());
let context = &*context;
chat::dc_get_chat_msgs(context, chat_id, flags, marker1before)
chat::get_chat_msgs(context, chat_id, flags, marker1before)
}
#[no_mangle]
@@ -428,7 +452,7 @@ pub unsafe extern "C" fn dc_get_msg_cnt(context: *mut dc_context_t, chat_id: u32
assert!(!context.is_null());
let context = &*context;
chat::dc_get_msg_cnt(context, chat_id)
chat::get_msg_cnt(context, chat_id) as libc::c_int
}
#[no_mangle]
@@ -439,7 +463,7 @@ pub unsafe extern "C" fn dc_get_fresh_msg_cnt(
assert!(!context.is_null());
let context = &*context;
chat::dc_get_fresh_msg_cnt(context, chat_id)
chat::get_fresh_msg_cnt(context, chat_id) as libc::c_int
}
#[no_mangle]
@@ -457,7 +481,11 @@ pub unsafe extern "C" fn dc_marknoticed_chat(context: *mut dc_context_t, chat_id
assert!(!context.is_null());
let context = &*context;
chat::dc_marknoticed_chat(context, chat_id);
log_err(
chat::marknoticed_chat(context, chat_id),
context,
"Failed marknoticed chat",
);
}
#[no_mangle]
@@ -465,7 +493,11 @@ pub unsafe extern "C" fn dc_marknoticed_all_chats(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;
chat::dc_marknoticed_all_chats(context);
log_err(
chat::marknoticed_all_chats(context),
context,
"Failed marknoticed all chats",
);
}
fn from_prim<S, T>(s: S) -> Option<T>
@@ -493,7 +525,7 @@ pub unsafe extern "C" fn dc_get_chat_media(
let or_msg_type3 =
from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3));
chat::dc_get_chat_media(context, chat_id, msg_type, or_msg_type2, or_msg_type3)
chat::get_chat_media(context, chat_id, msg_type, or_msg_type2, or_msg_type3)
}
#[no_mangle]
@@ -514,7 +546,7 @@ pub unsafe extern "C" fn dc_get_next_media(
let or_msg_type3 =
from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3));
chat::dc_get_next_media(context, msg_id, dir, msg_type, or_msg_type2, or_msg_type3)
chat::get_next_media(context, msg_id, dir, msg_type, or_msg_type2, or_msg_type3)
}
#[no_mangle]
@@ -526,7 +558,19 @@ pub unsafe extern "C" fn dc_archive_chat(
assert!(!context.is_null());
let context = &*context;
chat::dc_archive_chat(context, chat_id, archive);
let archive = if archive == 0 {
false
} else if archive == 1 {
true
} else {
return;
};
log_err(
chat::archive(context, chat_id, archive),
context,
"Failed archive chat",
);
}
#[no_mangle]
@@ -535,7 +579,11 @@ pub unsafe extern "C" fn dc_delete_chat(context: *mut dc_context_t, chat_id: u32
let context = &*context;
// TODO: update to indicate public api success/failure of deletion
chat::dc_delete_chat(context, chat_id);
log_err(
chat::delete(context, chat_id),
context,
"Failed chat delete",
);
}
#[no_mangle]
@@ -546,7 +594,7 @@ pub unsafe extern "C" fn dc_get_chat_contacts(
assert!(!context.is_null());
let context = &*context;
dc_array_t::from(chat::dc_get_chat_contacts(context, chat_id)).into_raw()
dc_array_t::from(chat::get_chat_contacts(context, chat_id)).into_raw()
}
#[no_mangle]
@@ -570,7 +618,7 @@ pub unsafe extern "C" fn dc_get_chat<'a>(
assert!(!context.is_null());
let context = &*context;
match chat::dc_get_chat(context, chat_id) {
match chat::Chat::load_from_db(context, chat_id) {
Ok(chat) => Box::into_raw(Box::new(chat)),
Err(_) => std::ptr::null_mut(),
}
@@ -586,7 +634,17 @@ pub unsafe extern "C" fn dc_create_group_chat(
assert!(!name.is_null());
let context = &*context;
chat::dc_create_group_chat(context, verified, name)
let verified = if let Some(s) = contact::VerifiedStatus::from_i32(verified) {
s
} else {
return 0;
};
log_err_default(
chat::create_group_chat(context, verified, name),
context,
"Failed to create group chat",
)
}
#[no_mangle]
@@ -598,7 +656,7 @@ pub unsafe extern "C" fn dc_is_contact_in_chat(
assert!(!context.is_null());
let context = &*context;
chat::dc_is_contact_in_chat(context, chat_id, contact_id)
chat::is_contact_in_chat(context, chat_id, contact_id)
}
#[no_mangle]
@@ -610,7 +668,7 @@ pub unsafe extern "C" fn dc_add_contact_to_chat(
assert!(!context.is_null());
let context = &*context;
chat::dc_add_contact_to_chat(context, chat_id, contact_id)
chat::add_contact_to_chat(context, chat_id, contact_id)
}
#[no_mangle]
@@ -622,7 +680,7 @@ pub unsafe extern "C" fn dc_remove_contact_from_chat(
assert!(!context.is_null());
let context = &*context;
chat::dc_remove_contact_from_chat(context, chat_id, contact_id)
chat::remove_contact_from_chat(context, chat_id, contact_id)
}
#[no_mangle]
@@ -636,7 +694,7 @@ pub unsafe extern "C" fn dc_set_chat_name(
assert!(chat_id > constants::DC_CHAT_ID_LAST_SPECIAL as u32);
let context = &*context;
chat::dc_set_chat_name(context, chat_id, name)
chat::set_chat_name(context, chat_id, name)
}
#[no_mangle]
@@ -649,7 +707,7 @@ pub unsafe extern "C" fn dc_set_chat_profile_image(
assert!(chat_id > constants::DC_CHAT_ID_LAST_SPECIAL as u32);
let context = &*context;
chat::dc_set_chat_profile_image(context, chat_id, image)
chat::set_chat_profile_image(context, chat_id, image)
}
#[no_mangle]
@@ -701,7 +759,7 @@ pub unsafe extern "C" fn dc_forward_msgs(
assert!(chat_id > constants::DC_CHAT_ID_LAST_SPECIAL as u32);
let context = &*context;
chat::dc_forward_msgs(context, msg_ids, msg_cnt, chat_id)
chat::forward_msgs(context, msg_ids, msg_cnt, chat_id)
}
#[no_mangle]
@@ -1275,7 +1333,7 @@ pub unsafe extern "C" fn dc_chat_get_id(chat: *mut dc_chat_t) -> u32 {
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_get_id(chat)
chat.get_id()
}
#[no_mangle]
@@ -1283,7 +1341,7 @@ pub unsafe extern "C" fn dc_chat_get_type(chat: *mut dc_chat_t) -> libc::c_int {
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_get_type(chat) as libc::c_int
chat.get_type() as libc::c_int
}
#[no_mangle]
@@ -1291,7 +1349,7 @@ pub unsafe extern "C" fn dc_chat_get_name(chat: *mut dc_chat_t) -> *mut libc::c_
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_get_name(chat)
chat.get_name()
}
#[no_mangle]
@@ -1299,7 +1357,7 @@ pub unsafe extern "C" fn dc_chat_get_subtitle(chat: *mut dc_chat_t) -> *mut libc
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_get_subtitle(chat)
chat.get_subtitle()
}
#[no_mangle]
@@ -1307,7 +1365,7 @@ pub unsafe extern "C" fn dc_chat_get_profile_image(chat: *mut dc_chat_t) -> *mut
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_get_profile_image(chat)
chat.get_profile_image()
}
#[no_mangle]
@@ -1315,7 +1373,7 @@ pub unsafe extern "C" fn dc_chat_get_color(chat: *mut dc_chat_t) -> u32 {
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_get_color(chat)
chat.get_color()
}
#[no_mangle]
@@ -1323,7 +1381,7 @@ pub unsafe extern "C" fn dc_chat_get_archived(chat: *mut dc_chat_t) -> libc::c_i
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_get_archived(chat) as libc::c_int
chat.is_archived() as libc::c_int
}
#[no_mangle]
@@ -1331,7 +1389,7 @@ pub unsafe extern "C" fn dc_chat_is_unpromoted(chat: *mut dc_chat_t) -> libc::c_
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_is_unpromoted(chat)
chat.is_unpromoted() as libc::c_int
}
#[no_mangle]
@@ -1339,7 +1397,7 @@ pub unsafe extern "C" fn dc_chat_is_self_talk(chat: *mut dc_chat_t) -> libc::c_i
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_is_self_talk(chat)
chat.is_self_talk() as libc::c_int
}
#[no_mangle]
@@ -1347,7 +1405,7 @@ pub unsafe extern "C" fn dc_chat_is_verified(chat: *mut dc_chat_t) -> libc::c_in
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_is_verified(chat)
chat.is_verified() as libc::c_int
}
#[no_mangle]
@@ -1355,7 +1413,7 @@ pub unsafe extern "C" fn dc_chat_is_sending_locations(chat: *mut dc_chat_t) -> l
assert!(!chat.is_null());
let chat = &*chat;
chat::dc_chat_is_sending_locations(chat) as libc::c_int
chat.is_sending_locations() as libc::c_int
}
// dc_msg_t
@@ -1836,3 +1894,23 @@ fn as_opt_str<'a>(s: *const libc::c_char) -> Option<&'a str> {
Some(dc_tools::as_str(s))
}
fn log_err<T>(res: Result<T, error::Error>, context: &context::Context, msg: &str) {
if let Err(err) = res {
error!(context, 0, "{}: {}", msg, err);
}
}
fn log_err_default<T: Default>(
res: Result<T, error::Error>,
context: &context::Context,
msg: &str,
) -> T {
match res {
Ok(t) => t,
Err(err) => {
error!(context, 0, "{}: {}", msg, err);
Default::default()
}
}
}