Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Dont overwrite equal drafts #6212

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,25 @@ impl ChatId {
&& old_draft.chat_id == self
&& old_draft.state == MessageState::OutDraft
{
// Do not overwrite draft if text and file are the same
if old_draft.text == msg.text {
if msg.param.get(Param::File).is_some() {
let blob = msg
.param
.get_blob(Param::File, context, !msg.is_increation())
.await?
.context("no file stored in params")?;
Septias marked this conversation as resolved.
Show resolved Hide resolved
let old_blob = old_draft
.param
.get_blob(Param::File, context, false)
.await?
.context("no file stored in params")?;
Septias marked this conversation as resolved.
Show resolved Hide resolved
if blob == old_blob {
return Ok(false);
}
}
return Ok(false);
};
context
.sql
.execute(
Expand Down Expand Up @@ -7696,4 +7715,19 @@ mod tests {

Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_do_not_overwrite_draft() -> Result<()> {
let mut tcm = TestContextManager::new();
let alice = tcm.alice().await;
let mut msg = Message::new_text("This is a draft message".to_string());
let self_chat = alice.get_self_chat().await.id;
self_chat.set_draft(&alice, Some(&mut msg)).await.unwrap();
let draft1 = self_chat.get_draft(&alice).await?.unwrap();
tokio::time::sleep(Duration::from_millis(800)).await;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use SystemTime::shift?

self_chat.set_draft(&alice, Some(&mut msg)).await.unwrap();
let draft2 = self_chat.get_draft(&alice).await?.unwrap();
assert_eq!(draft1.timestamp_sort, draft2.timestamp_sort);
Ok(())
}
}
Loading