Skip to content

fix: Emit WebxdcInstanceDeleted events when deleting a chat (#6670) #6718

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,14 @@ impl ChatId {
Sync => chat.get_sync_id(context).await?,
};

context
let webxdc_ids = context
.sql
.transaction(|transaction| {
let mut stmt = transaction.prepare("SELECT id FROM msgs WHERE chat_id=? AND type=?")?;
let mut webxdc_ids = Vec::new();
for id in stmt.query_map((self, Viewtype::Webxdc), |row| row.get(0))? {
webxdc_ids.push(id?);
}
transaction.execute(
"UPDATE imap SET target=? WHERE rfc724_mid IN (SELECT rfc724_mid FROM msgs WHERE chat_id=?)",
(delete_msgs_target, self,),
Expand All @@ -819,10 +824,13 @@ impl ChatId {
transaction.execute("DELETE FROM msgs WHERE chat_id=?", (self,))?;
transaction.execute("DELETE FROM chats_contacts WHERE chat_id=?", (self,))?;
transaction.execute("DELETE FROM chats WHERE id=?", (self,))?;
Ok(())
Ok(webxdc_ids)
})
.await?;

for msg_id in webxdc_ids {
context.emit_event(EventType::WebxdcInstanceDeleted { msg_id });
}
context.emit_event(EventType::ChatDeleted { chat_id: self });
context.emit_msgs_changed_without_ids();

Expand Down
31 changes: 31 additions & 0 deletions src/webxdc/webxdc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,37 @@ async fn test_webxdc_delete_event() -> Result<()> {
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_chat_delete() -> Result<()> {
let alice = &TestContext::new_alice().await;

let chat_id = create_group_chat(alice, ProtectionStatus::Unprotected, "foo").await?;
send_webxdc_instance(alice, chat_id).await?;

let chat_id = create_group_chat(alice, ProtectionStatus::Unprotected, "bar").await?;
let instance = send_webxdc_instance(alice, chat_id).await?;
alice.send_text(chat_id, "wtf").await;

chat_id.delete(alice).await?;
let EventType::WebxdcInstanceDeleted { msg_id } = alice
.evtracker
.get_matching(|evt| matches!(evt, EventType::WebxdcInstanceDeleted { .. }))
.await
else {
unreachable!();
};
assert_eq!(msg_id, instance.id);
assert!(alice
.evtracker
.get_matching_opt(alice, |evt| matches!(
evt,
EventType::WebxdcInstanceDeleted { .. }
))
.await
.is_none());
Ok(())
}

Copy link
Contributor

Choose a reason for hiding this comment

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

would make sense to also make a test that checks that WebxdcInstanceDeleted is only emitted for deleted webxdc messages and one that makes sure only that it only emits the events from the chat where the message was deleted from.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I extended the test, now it creates two chats with webxdc messages and also sends a text message to the chat aimed for deletion

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn change_logging_webxdc() -> Result<()> {
let alice = TestContext::new_alice().await;
Expand Down
Loading