From 69fbb98f3ca66697d5eb09577c91fbcc981609eb Mon Sep 17 00:00:00 2001 From: Hocuri Date: Tue, 29 Oct 2024 16:22:52 +0100 Subject: [PATCH] api: Add Message::new_text() (#6123) This adds a function to `Message`: ```rust pub fn new_text(text: String) -> Self { Message { viewtype: Viewtype::Text, text, ..Default::default() } } ``` I keep expecting that a function like this must exist and being surprised that it doesn't. Open question is whether it should be `pub` or `pub(crate)` - I made it `pub` for now because it may be useful for others and we currently we aren't thinking about the Rust API that much, anyway, but I can make it `pub(crate)`, too (then it can't be used in deltachat-jsonrpc and deltachat-repl). I replaced some usages of Message::new(Viewtype::Text), but not all yet, I'm going to do this in a follow-up, which will remove another around 65 LOC. --- deltachat-jsonrpc/src/api.rs | 3 +- deltachat-repl/src/cmdline.rs | 6 +-- src/chat.rs | 72 +++++++++++------------------------ src/message.rs | 9 +++++ 4 files changed, 35 insertions(+), 55 deletions(-) diff --git a/deltachat-jsonrpc/src/api.rs b/deltachat-jsonrpc/src/api.rs index 3afe60fe74..92d4804e51 100644 --- a/deltachat-jsonrpc/src/api.rs +++ b/deltachat-jsonrpc/src/api.rs @@ -2118,8 +2118,7 @@ impl CommandApi { ) -> Result { let ctx = self.get_context(account_id).await?; - let mut msg = Message::new(Viewtype::Text); - msg.set_text(text); + let mut msg = Message::new_text(text); let message_id = deltachat::chat::send_msg(&ctx, ChatId::new(chat_id), &mut msg).await?; Ok(message_id.to_u32()) diff --git a/deltachat-repl/src/cmdline.rs b/deltachat-repl/src/cmdline.rs index dd18839849..2f636ee867 100644 --- a/deltachat-repl/src/cmdline.rs +++ b/deltachat-repl/src/cmdline.rs @@ -1004,8 +1004,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu ensure!(sel_chat.is_some(), "No chat selected."); if !arg1.is_empty() { - let mut draft = Message::new(Viewtype::Text); - draft.set_text(arg1.to_string()); + let mut draft = Message::new_text(arg1.to_string()); sel_chat .as_ref() .unwrap() @@ -1028,8 +1027,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu !arg1.is_empty(), "Please specify text to add as device message." ); - let mut msg = Message::new(Viewtype::Text); - msg.set_text(arg1.to_string()); + let mut msg = Message::new_text(arg1.to_string()); chat::add_device_msg(&context, None, Some(&mut msg)).await?; } "listmedia" => { diff --git a/src/chat.rs b/src/chat.rs index 97a435d7e7..aa5b548246 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -797,8 +797,7 @@ impl ChatId { context.scheduler.interrupt_inbox().await; if chat.is_self_talk() { - let mut msg = Message::new(Viewtype::Text); - msg.text = stock_str::self_deleted_msg_body(context).await; + let mut msg = Message::new_text(stock_str::self_deleted_msg_body(context).await); add_device_msg(context, None, Some(&mut msg)).await?; } chatlist_events::emit_chatlist_changed(context); @@ -3106,8 +3105,7 @@ pub async fn send_text_msg( chat_id ); - let mut msg = Message::new(Viewtype::Text); - msg.text = text_to_send; + let mut msg = Message::new_text(text_to_send); send_msg(context, chat_id, &mut msg).await } @@ -4780,8 +4778,7 @@ mod tests { async fn test_get_draft() { let t = TestContext::new().await; let chat_id = &t.get_self_chat().await.id; - let mut msg = Message::new(Viewtype::Text); - msg.set_text("hello".to_string()); + let mut msg = Message::new_text("hello".to_string()); chat_id.set_draft(&t, Some(&mut msg)).await.unwrap(); let draft = chat_id.get_draft(&t).await.unwrap().unwrap(); @@ -4795,13 +4792,11 @@ mod tests { let t = TestContext::new_alice().await; let chat_id = create_group_chat(&t, ProtectionStatus::Unprotected, "abc").await?; - let mut msg = Message::new(Viewtype::Text); - msg.set_text("hi!".to_string()); + let mut msg = Message::new_text("hi!".to_string()); chat_id.set_draft(&t, Some(&mut msg)).await?; assert!(chat_id.get_draft(&t).await?.is_some()); - let mut msg = Message::new(Viewtype::Text); - msg.set_text("another".to_string()); + let mut msg = Message::new_text("another".to_string()); chat_id.set_draft(&t, Some(&mut msg)).await?; assert!(chat_id.get_draft(&t).await?.is_some()); @@ -4815,8 +4810,7 @@ mod tests { async fn test_forwarding_draft_failing() -> Result<()> { let t = TestContext::new_alice().await; let chat_id = &t.get_self_chat().await.id; - let mut msg = Message::new(Viewtype::Text); - msg.set_text("hello".to_string()); + let mut msg = Message::new_text("hello".to_string()); chat_id.set_draft(&t, Some(&mut msg)).await?; assert_eq!(msg.id, chat_id.get_draft(&t).await?.unwrap().id); @@ -4829,8 +4823,7 @@ mod tests { async fn test_draft_stable_ids() -> Result<()> { let t = TestContext::new_alice().await; let chat_id = &t.get_self_chat().await.id; - let mut msg = Message::new(Viewtype::Text); - msg.set_text("hello".to_string()); + let mut msg = Message::new_text("hello".to_string()); assert_eq!(msg.id, MsgId::new_unset()); assert!(chat_id.get_draft_msg_id(&t).await?.is_none()); @@ -4876,11 +4869,7 @@ mod tests { let chat_id = create_group_chat(&t, ProtectionStatus::Unprotected, "abc").await?; let msgs: Vec = (1..=1000) - .map(|i| { - let mut msg = Message::new(Viewtype::Text); - msg.set_text(i.to_string()); - msg - }) + .map(|i| Message::new_text(i.to_string())) .collect(); let mut tasks = Vec::new(); for mut msg in msgs { @@ -4913,8 +4902,7 @@ mod tests { .await?; // save a draft - let mut draft = Message::new(Viewtype::Text); - draft.set_text("draft text".to_string()); + let mut draft = Message::new_text("draft text".to_string()); chat_id.set_draft(&t, Some(&mut draft)).await?; let test = Message::load_from_db(&t, draft.id).await?; @@ -4967,29 +4955,25 @@ mod tests { let one2one_msg = Message::load_from_db(&alice, one2one_msg_id).await?; // quoting messages in same chat is okay - let mut msg = Message::new(Viewtype::Text); - msg.set_text("baz".to_string()); + let mut msg = Message::new_text("baz".to_string()); msg.set_quote(&alice, Some(&grp_msg)).await?; let result = send_msg(&alice, grp_chat_id, &mut msg).await; assert!(result.is_ok()); - let mut msg = Message::new(Viewtype::Text); - msg.set_text("baz".to_string()); + let mut msg = Message::new_text("baz".to_string()); msg.set_quote(&alice, Some(&one2one_msg)).await?; let result = send_msg(&alice, one2one_chat_id, &mut msg).await; assert!(result.is_ok()); let one2one_quote_reply_msg_id = result.unwrap(); // quoting messages from groups to one-to-ones is okay ("reply privately") - let mut msg = Message::new(Viewtype::Text); - msg.set_text("baz".to_string()); + let mut msg = Message::new_text("baz".to_string()); msg.set_quote(&alice, Some(&grp_msg)).await?; let result = send_msg(&alice, one2one_chat_id, &mut msg).await; assert!(result.is_ok()); // quoting messages from one-to-one chats in groups is an error; usually this is also not allowed by UI at all ... - let mut msg = Message::new(Viewtype::Text); - msg.set_text("baz".to_string()); + let mut msg = Message::new_text("baz".to_string()); msg.set_quote(&alice, Some(&one2one_msg)).await?; let result = send_msg(&alice, grp_chat_id, &mut msg).await; assert!(result.is_err()); @@ -5481,13 +5465,11 @@ mod tests { let t = TestContext::new().await; // add two device-messages - let mut msg1 = Message::new(Viewtype::Text); - msg1.set_text("first message".to_string()); + let mut msg1 = Message::new_text("first message".to_string()); let msg1_id = add_device_msg(&t, None, Some(&mut msg1)).await; assert!(msg1_id.is_ok()); - let mut msg2 = Message::new(Viewtype::Text); - msg2.set_text("second message".to_string()); + let mut msg2 = Message::new_text("second message".to_string()); let msg2_id = add_device_msg(&t, None, Some(&mut msg2)).await; assert!(msg2_id.is_ok()); assert_ne!(msg1_id.as_ref().unwrap(), msg2_id.as_ref().unwrap()); @@ -5516,14 +5498,12 @@ mod tests { let t = TestContext::new().await; // add two device-messages with the same label (second attempt is not added) - let mut msg1 = Message::new(Viewtype::Text); - msg1.text = "first message".to_string(); + let mut msg1 = Message::new_text("first message".to_string()); let msg1_id = add_device_msg(&t, Some("any-label"), Some(&mut msg1)).await; assert!(msg1_id.is_ok()); assert!(!msg1_id.as_ref().unwrap().is_unset()); - let mut msg2 = Message::new(Viewtype::Text); - msg2.text = "second message".to_string(); + let mut msg2 = Message::new_text("second message".to_string()); let msg2_id = add_device_msg(&t, Some("any-label"), Some(&mut msg2)).await; assert!(msg2_id.is_ok()); assert!(msg2_id.as_ref().unwrap().is_unset()); @@ -5570,8 +5550,7 @@ mod tests { let res = add_device_msg(&t, Some("some-label"), None).await; assert!(res.is_ok()); - let mut msg = Message::new(Viewtype::Text); - msg.set_text("message text".to_string()); + let mut msg = Message::new_text("message text".to_string()); let msg_id = add_device_msg(&t, Some("some-label"), Some(&mut msg)).await; assert!(msg_id.is_ok()); @@ -5588,8 +5567,7 @@ mod tests { add_device_msg(&t, Some("some-label"), None).await.ok(); assert!(was_device_msg_ever_added(&t, "some-label").await.unwrap()); - let mut msg = Message::new(Viewtype::Text); - msg.set_text("message text".to_string()); + let mut msg = Message::new_text("message text".to_string()); add_device_msg(&t, Some("another-label"), Some(&mut msg)) .await .ok(); @@ -5606,8 +5584,7 @@ mod tests { async fn test_delete_device_chat() { let t = TestContext::new().await; - let mut msg = Message::new(Viewtype::Text); - msg.set_text("message text".to_string()); + let mut msg = Message::new_text("message text".to_string()); add_device_msg(&t, Some("some-label"), Some(&mut msg)) .await .ok(); @@ -5630,8 +5607,7 @@ mod tests { .await .unwrap(); - let mut msg = Message::new(Viewtype::Text); - msg.set_text("message text".to_string()); + let mut msg = Message::new_text("message text".to_string()); assert!(send_msg(&t, device_chat_id, &mut msg).await.is_err()); assert!(prepare_msg(&t, device_chat_id, &mut msg).await.is_err()); @@ -5642,8 +5618,7 @@ mod tests { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_delete_and_reset_all_device_msgs() { let t = TestContext::new().await; - let mut msg = Message::new(Viewtype::Text); - msg.set_text("message text".to_string()); + let mut msg = Message::new_text("message text".to_string()); let msg_id1 = add_device_msg(&t, Some("some-label"), Some(&mut msg)) .await .unwrap(); @@ -5675,8 +5650,7 @@ mod tests { async fn test_archive() { // create two chats let t = TestContext::new().await; - let mut msg = Message::new(Viewtype::Text); - msg.set_text("foo".to_string()); + let mut msg = Message::new_text("foo".to_string()); let msg_id = add_device_msg(&t, None, Some(&mut msg)).await.unwrap(); let chat_id1 = message::Message::load_from_db(&t, msg_id) .await diff --git a/src/message.rs b/src/message.rs index 6a0770c77a..3e8c3f70b0 100644 --- a/src/message.rs +++ b/src/message.rs @@ -491,6 +491,15 @@ impl Message { } } + /// Creates a new message with Viewtype::Text. + pub fn new_text(text: String) -> Self { + Message { + viewtype: Viewtype::Text, + text, + ..Default::default() + } + } + /// Loads message with given ID from the database. /// /// Returns an error if the message does not exist.