diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 299d92ba2..1eb0bf9cc 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -966,7 +966,7 @@ pub unsafe extern "C" fn dc_get_msg_cnt(context: *mut dc_context_t, chat_id: u32 } let ffi_context = &*context; ffi_context - .with_inner(|ctx| chat::get_msg_cnt(ctx, ChatId::new(chat_id)) as libc::c_int) + .with_inner(|ctx| ChatId::new(chat_id).get_msg_cnt(ctx) as libc::c_int) .unwrap_or(0) } @@ -981,7 +981,7 @@ pub unsafe extern "C" fn dc_get_fresh_msg_cnt( } let ffi_context = &*context; ffi_context - .with_inner(|ctx| chat::get_fresh_msg_cnt(ctx, ChatId::new(chat_id)) as libc::c_int) + .with_inner(|ctx| ChatId::new(chat_id).get_fresh_msg_cnt(ctx) as libc::c_int) .unwrap_or(0) } diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index d03a943fb..adefb5926 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -514,7 +514,7 @@ pub fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::Error> { chat_prefix(&chat), chat.get_id(), chat.get_name(), - chat::get_fresh_msg_cnt(context, chat.get_id()), + chat.get_id().get_fresh_msg_cnt(context), ); let lot = chatlist.get_summary(context, i, Some(&chat)); let statestr = if chat.is_archived() { @@ -602,10 +602,7 @@ pub fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::Error> { log_msg(context, "Draft", &draft); } - println!( - "{} messages.", - chat::get_msg_cnt(context, sel_chat.get_id()) - ); + println!("{} messages.", sel_chat.get_id().get_msg_cnt(context)); chat::marknoticed_chat(context, sel_chat.get_id())?; } "createchat" => { diff --git a/src/chat.rs b/src/chat.rs index 226bfd100..e5f11b6b5 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -327,6 +327,33 @@ impl ChatId { Ok(()) } + /// Returns number of messages in a chat. + pub fn get_msg_cnt(self, context: &Context) -> usize { + context + .sql + .query_get_value::<_, i32>( + context, + "SELECT COUNT(*) FROM msgs WHERE chat_id=?;", + params![self], + ) + .unwrap_or_default() as usize + } + + pub fn get_fresh_msg_cnt(self, context: &Context) -> usize { + context + .sql + .query_get_value::<_, i32>( + context, + "SELECT COUNT(*) + FROM msgs + WHERE state=10 + AND hidden=0 + AND chat_id=?;", + params![self], + ) + .unwrap_or_default() as usize + } + /// Bad evil escape hatch. /// /// Avoid using this, eventually types should be cleaned up enough @@ -1453,33 +1480,6 @@ pub fn get_chat_msgs( } } -/// Returns number of messages in a chat. -pub fn get_msg_cnt(context: &Context, chat_id: ChatId) -> usize { - context - .sql - .query_get_value::<_, i32>( - context, - "SELECT COUNT(*) FROM msgs WHERE chat_id=?;", - params![chat_id], - ) - .unwrap_or_default() as usize -} - -pub fn get_fresh_msg_cnt(context: &Context, chat_id: ChatId) -> usize { - context - .sql - .query_get_value::<_, i32>( - context, - "SELECT COUNT(*) - FROM msgs - WHERE state=10 - AND hidden=0 - AND chat_id=?;", - params![chat_id], - ) - .unwrap_or_default() as usize -} - pub fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<(), Error> { if !context.sql.exists( "SELECT id FROM msgs WHERE chat_id=? AND state=?;", @@ -2535,7 +2535,7 @@ mod tests { assert_eq!(msg2.text.as_ref().unwrap(), "second message"); // check device chat - assert_eq!(get_msg_cnt(&t.ctx, msg2.chat_id), 2); + assert_eq!(msg2.chat_id.get_msg_cnt(&t.ctx), 2); } #[test] @@ -2568,7 +2568,7 @@ mod tests { // check device chat let chat_id = msg1.chat_id; - assert_eq!(get_msg_cnt(&t.ctx, chat_id), 1); + assert_eq!(chat_id.get_msg_cnt(&t.ctx), 1); assert!(!chat_id.is_special()); let chat = Chat::load_from_db(&t.ctx, chat_id); assert!(chat.is_ok());