Merge pull request #517 from deltachat/fix_get_draft

Fix get_draft to just return null and not produce errors if there's no
This commit is contained in:
Simon Laux
2019-09-14 23:39:09 +02:00
committed by GitHub
3 changed files with 69 additions and 13 deletions

View File

@@ -916,12 +916,13 @@ fn get_draft_msg_id(context: &Context, chat_id: u32) -> u32 {
.unwrap_or_default() as u32
}
pub unsafe fn get_draft(context: &Context, chat_id: u32) -> Result<Message, Error> {
pub fn get_draft(context: &Context, chat_id: u32) -> Result<Option<Message>, Error> {
ensure!(chat_id > DC_CHAT_ID_LAST_SPECIAL, "Invalid chat ID");
let draft_msg_id = get_draft_msg_id(context, chat_id);
ensure!(draft_msg_id != 0, "Invalid draft message ID");
dc_msg_load_from_db(context, draft_msg_id)
if draft_msg_id == 0 {
return Ok(None);
}
Ok(Some(dc_msg_load_from_db(context, draft_msg_id)?))
}
pub fn get_chat_msgs(context: &Context, chat_id: u32, flags: u32, marker1before: u32) -> Vec<u32> {
@@ -1862,3 +1863,49 @@ pub fn add_device_msg(context: &Context, chat_id: u32, text: impl AsRef<str>) {
msg_id as uintptr_t,
);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::*;
#[test]
fn test_get_draft_no_draft() {
let t = dummy_context();
let chat_id = create_by_contact_id(&t.ctx, DC_CONTACT_ID_SELF).unwrap();
let draft = get_draft(&t.ctx, chat_id).unwrap();
assert!(draft.is_none());
}
#[test]
fn test_get_draft_special_chat_id() {
let t = dummy_context();
let draft = get_draft(&t.ctx, DC_CHAT_ID_LAST_SPECIAL);
assert!(draft.is_err());
}
#[test]
fn test_get_draft_no_chat() {
// This is a weird case, maybe this should be an error but we
// do not get this info from the database currently.
let t = dummy_context();
let draft = get_draft(&t.ctx, 42).unwrap();
assert!(draft.is_none());
}
#[test]
fn test_get_draft() {
unsafe {
let t = dummy_context();
let chat_id = create_by_contact_id(&t.ctx, DC_CONTACT_ID_SELF).unwrap();
let mut msg = dc_msg_new(Viewtype::Text);
dc_msg_set_text(&mut msg, b"hello\x00" as *const u8 as *const libc::c_char);
set_draft(&t.ctx, chat_id, Some(&mut msg));
let draft = get_draft(&t.ctx, chat_id).unwrap().unwrap();
let msg_text = dc_msg_get_text(&msg);
let draft_text = dc_msg_get_text(&draft);
assert_eq!(as_str(msg_text), as_str(draft_text));
}
}
}