MCPcopy Create free account
hub / github.com/chatmail/core / do_set_draft

Method do_set_draft

src/chat.rs:781–889  ·  view source on GitHub ↗

Set provided message as draft message for specified chat. Returns true if the draft was added or updated in place.

(self, context: &Context, msg: &mut Message)

Source from the content-addressed store, hash-verified

779 /// Set provided message as draft message for specified chat.
780 /// Returns true if the draft was added or updated in place.
781 async fn do_set_draft(self, context: &Context, msg: &mut Message) -> Result<bool> {
782 match msg.viewtype {
783 Viewtype::Unknown => bail!("Can not set draft of unknown type."),
784 Viewtype::Text => {
785 if msg.text.is_empty() && msg.in_reply_to.is_none_or_empty() {
786 bail!("No text and no quote in draft");
787 }
788 }
789 _ => {
790 if msg.viewtype == Viewtype::File
791 && let Some((better_type, _)) = message::guess_msgtype_from_suffix(msg)
792 // We do not do an automatic conversion to other viewtypes here so that
793 // users can send images as "files" to preserve the original quality
794 // (usually we compress images). The remaining conversions are done by
795 // `prepare_msg_blob()` later.
796 .filter(|&(vt, _)| vt == Viewtype::Webxdc || vt == Viewtype::Vcard)
797 {
798 msg.viewtype = better_type;
799 }
800 if msg.viewtype == Viewtype::Vcard {
801 let blob = msg
802 .param
803 .get_file_blob(context)?
804 .context("no file stored in params")?;
805 msg.try_set_vcard(context, &blob.to_abs_path()).await?;
806 }
807 }
808 }
809
810 // set back draft information to allow identifying the draft later on -
811 // no matter if message object is reused or reloaded from db
812 msg.state = MessageState::OutDraft;
813 msg.chat_id = self;
814
815 // if possible, replace existing draft and keep id
816 if !msg.id.is_special()
817 && let Some(old_draft) = self.get_draft(context).await?
818 && old_draft.id == msg.id
819 && old_draft.chat_id == self
820 && old_draft.state == MessageState::OutDraft
821 {
822 let affected_rows = context
823 .sql.execute(
824 "UPDATE msgs
825 SET timestamp=?1,type=?2,txt=?3,txt_normalized=?4,param=?5,mime_in_reply_to=?6
826 WHERE id=?7
827 AND (type <> ?2
828 OR txt <> ?3
829 OR txt_normalized <> ?4
830 OR param <> ?5
831 OR mime_in_reply_to <> ?6);",
832 (
833 time(),
834 msg.viewtype,
835 &msg.text,
836 normalize_text(&msg.text),
837 msg.param.to_string(),
838 msg.in_reply_to.as_deref().unwrap_or_default(),

Callers 1

set_draftMethod · 0.80

Calls 13

timeFunction · 0.85
normalize_textFunction · 0.85
is_none_or_emptyMethod · 0.80
get_file_blobMethod · 0.80
try_set_vcardMethod · 0.80
to_abs_pathMethod · 0.80
executeMethod · 0.80
transactionMethod · 0.80
is_emptyMethod · 0.45
filterMethod · 0.45
is_specialMethod · 0.45

Tested by

no test coverage detected