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)
| 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(), |
no test coverage detected