From cf5342c3671d6b4b8289a32d6658e76ec25340c8 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Fri, 9 Oct 2020 19:58:56 +0200 Subject: [PATCH] Allow drafts without text if there is a quote --- src/chat.rs | 13 +++++-------- src/dc_tools.rs | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index c929a5306..43b40286b 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -315,14 +315,11 @@ impl ChatId { async fn do_set_draft(self, context: &Context, msg: &mut Message) -> Result<(), Error> { match msg.viewtype { Viewtype::Unknown => bail!("Can not set draft of unknown type."), - Viewtype::Text => match msg.text.as_ref() { - Some(text) => { - if text.is_empty() { - bail!("No text in draft"); - } + Viewtype::Text => { + if msg.text.is_none_or_empty() && msg.in_reply_to.is_none_or_empty() { + bail!("No text and no quote in draft"); } - None => bail!("No text in draft"), - }, + } _ => { let blob = msg .param @@ -346,7 +343,7 @@ impl ChatId { msg.text.as_deref().unwrap_or(""), msg.param.to_string(), 1, - msg.in_reply_to, + msg.in_reply_to.as_deref().unwrap_or_default(), ], ) .await?; diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 0551418dc..a1bcc23bb 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -709,6 +709,21 @@ pub(crate) fn improve_single_line_input(input: impl AsRef) -> String { .to_string() } +pub(crate) trait IsNoneOrEmpty { + fn is_none_or_empty(&self) -> bool; +} +impl IsNoneOrEmpty for Option +where + T: AsRef, +{ + fn is_none_or_empty(&self) -> bool { + match self { + Some(s) if !s.as_ref().is_empty() => false, + _ => true, + } + } +} + #[cfg(test)] mod tests { #![allow(clippy::indexing_slicing)]