fix: assign messages to chats based on not fully downloaded references

This commit is contained in:
link2xt
2024-04-11 05:39:51 +00:00
parent 9731ec419e
commit 08cbc54c00
2 changed files with 38 additions and 14 deletions

View File

@@ -538,3 +538,40 @@ def test_reactions_for_a_reordering_move(acfactory):
assert len(contacts) == 1
assert contacts[0].get_snapshot().address == ac1.get_config("addr")
assert list(reactions.reactions_by_contact.values())[0] == [react_str]
def test_download_limit_chat_assignment(acfactory, tmp_path):
download_limit = 300000
alice, bob, carol = acfactory.get_online_accounts(3)
for account in bob, carol:
chat = account.create_chat(alice)
chat.send_text("Hello Alice!")
assert alice.get_message_by_id(alice.wait_for_incoming_msg_event().msg_id).get_snapshot().text == "Hello Alice!"
bob_addr = bob.get_config("addr")
alice_contact_bob = alice.create_contact(bob_addr, "Bob")
carol_addr = carol.get_config("addr")
alice_contact_carol = alice.create_contact(carol_addr, "Carol")
alice_group = alice.create_group("test group")
alice_group.add_contact(alice_contact_bob)
alice_group.add_contact(alice_contact_carol)
bob.set_config("download_limit", str(download_limit))
alice_group.send_text("hi")
snapshot = bob.get_message_by_id(bob.wait_for_incoming_msg_event().msg_id).get_snapshot()
assert snapshot.text == "hi"
bob_group = snapshot.chat
path = tmp_path / "large"
path.write_bytes(os.urandom(download_limit + 1))
for i in range(10):
logging.info("Sending message %s", i)
alice_group.send_file(str(path))
snapshot = bob.get_message_by_id(bob.wait_for_incoming_msg_event().msg_id).get_snapshot()
assert snapshot.download_state == DownloadState.AVAILABLE
assert snapshot.chat == bob_group

View File

@@ -209,24 +209,11 @@ impl ChatId {
}
/// Returns [`ChatId`] of a chat that `msg` belongs to.
///
/// Checks that `msg` is assigned to the right chat.
pub(crate) fn lookup_by_message(msg: &Message) -> Option<Self> {
if msg.chat_id == DC_CHAT_ID_TRASH {
return None;
}
if msg.download_state != DownloadState::Done
// TODO (2023-09-12): Added for backward compatibility with versions that did not have
// `DownloadState::Undecipherable`. Remove eventually with the comment in
// `MimeMessage::from_bytes()`.
|| msg
.error
.as_ref()
.filter(|e| e.starts_with("Decrypting failed:"))
.is_some()
{
// If `msg` is not fully downloaded or undecipherable, it may have been assigned to the
// wrong chat (they often get assigned to the 1:1 chat with the sender).
if msg.download_state == DownloadState::Undecipherable {
return None;
}
Some(msg.chat_id)