mirror of
https://github.com/chatmail/core.git
synced 2026-05-05 22:36:30 +03:00
fix: Fetch existing messages for bots as InFresh (#4976)
Before, if `Config::FetchExistingMsgs` is set, existing messages were received with the `InSeen` state set, but for bots they must be `InFresh` and also `IncomingMsg` events should be emitted for them so that they are processed by bots as it happens with new messages.
This commit is contained in:
@@ -481,8 +481,9 @@ char* dc_get_blobdir (const dc_context_t* context);
|
|||||||
* - `bot` = Set to "1" if this is a bot.
|
* - `bot` = Set to "1" if this is a bot.
|
||||||
* Prevents adding the "Device messages" and "Saved messages" chats,
|
* Prevents adding the "Device messages" and "Saved messages" chats,
|
||||||
* adds Auto-Submitted header to outgoing messages,
|
* adds Auto-Submitted header to outgoing messages,
|
||||||
* accepts contact requests automatically (calling dc_accept_chat() is not needed for bots)
|
* accepts contact requests automatically (calling dc_accept_chat() is not needed),
|
||||||
* and does not cut large incoming text messages.
|
* does not cut large incoming text messages,
|
||||||
|
* handles existing messages the same way as new ones if `fetch_existing_msgs=1`.
|
||||||
* - `last_msg_id` = database ID of the last message processed by the bot.
|
* - `last_msg_id` = database ID of the last message processed by the bot.
|
||||||
* This ID and IDs below it are guaranteed not to be returned
|
* This ID and IDs below it are guaranteed not to be returned
|
||||||
* by dc_get_next_msgs() and dc_wait_next_msgs().
|
* by dc_get_next_msgs() and dc_wait_next_msgs().
|
||||||
@@ -493,8 +494,8 @@ char* dc_get_blobdir (const dc_context_t* context);
|
|||||||
* For most bots calling `dc_markseen_msgs()` is the
|
* For most bots calling `dc_markseen_msgs()` is the
|
||||||
* recommended way to update this value
|
* recommended way to update this value
|
||||||
* even for self-sent messages.
|
* even for self-sent messages.
|
||||||
* - `fetch_existing_msgs` = 1=fetch most recent existing messages on configure (default),
|
* - `fetch_existing_msgs` = 0=do not fetch existing messages on configure (default),
|
||||||
* 0=do not fetch existing messages on configure.
|
* 1=fetch most recent existing messages on configure.
|
||||||
* In both cases, existing recipients are added to the contact database.
|
* In both cases, existing recipients are added to the contact database.
|
||||||
* - `disable_idle` = 1=disable IMAP IDLE even if the server supports it,
|
* - `disable_idle` = 1=disable IMAP IDLE even if the server supports it,
|
||||||
* 0=use IMAP IDLE if the server supports it.
|
* 0=use IMAP IDLE if the server supports it.
|
||||||
|
|||||||
@@ -696,6 +696,9 @@ async fn add_parts(
|
|||||||
prevent_rename: bool,
|
prevent_rename: bool,
|
||||||
verified_encryption: VerifiedEncryption,
|
verified_encryption: VerifiedEncryption,
|
||||||
) -> Result<ReceivedMsg> {
|
) -> Result<ReceivedMsg> {
|
||||||
|
let is_bot = context.get_config_bool(Config::Bot).await?;
|
||||||
|
// Bots handle existing messages the same way as new ones.
|
||||||
|
let fetching_existing_messages = fetching_existing_messages && !is_bot;
|
||||||
let rfc724_mid_orig = &mime_parser
|
let rfc724_mid_orig = &mime_parser
|
||||||
.get_rfc724_mid()
|
.get_rfc724_mid()
|
||||||
.unwrap_or(rfc724_mid.to_string());
|
.unwrap_or(rfc724_mid.to_string());
|
||||||
@@ -788,9 +791,6 @@ async fn add_parts(
|
|||||||
info!(context, "Message is an MDN (TRASH).",);
|
info!(context, "Message is an MDN (TRASH).",);
|
||||||
}
|
}
|
||||||
|
|
||||||
// signals whether the current user is a bot
|
|
||||||
let is_bot = context.get_config_bool(Config::Bot).await?;
|
|
||||||
|
|
||||||
let create_blocked_default = if is_bot {
|
let create_blocked_default = if is_bot {
|
||||||
Blocked::Not
|
Blocked::Not
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3276,6 +3276,46 @@ async fn test_send_as_bot() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn test_bot_recv_existing_msg() -> Result<()> {
|
||||||
|
let mut tcm = TestContextManager::new();
|
||||||
|
let bob = &tcm.bob().await;
|
||||||
|
bob.set_config(Config::Bot, Some("1")).await.unwrap();
|
||||||
|
bob.set_config(Config::FetchExistingMsgs, Some("1"))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let fetching_existing_messages = true;
|
||||||
|
let msg = receive_imf_from_inbox(
|
||||||
|
bob,
|
||||||
|
"first@example.org",
|
||||||
|
b"From: Alice <alice@example.org>\n\
|
||||||
|
To: Bob <bob@example.net>\n\
|
||||||
|
Chat-Version: 1.0\n\
|
||||||
|
Message-ID: <first@example.org>\n\
|
||||||
|
Date: Sun, 14 Nov 2021 00:10:00 +0000\n\
|
||||||
|
Content-Type: text/plain\n\
|
||||||
|
\n\
|
||||||
|
hello\n",
|
||||||
|
false,
|
||||||
|
None,
|
||||||
|
fetching_existing_messages,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
.unwrap();
|
||||||
|
let msg = Message::load_from_db(bob, msg.msg_ids[0]).await?;
|
||||||
|
assert_eq!(msg.state, MessageState::InFresh);
|
||||||
|
let event = bob
|
||||||
|
.evtracker
|
||||||
|
.get_matching(|ev| matches!(ev, EventType::IncomingMsg { .. }))
|
||||||
|
.await;
|
||||||
|
let EventType::IncomingMsg { chat_id, msg_id } = event else {
|
||||||
|
unreachable!();
|
||||||
|
};
|
||||||
|
assert_eq!(chat_id, msg.chat_id);
|
||||||
|
assert_eq!(msg_id, msg.id);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn test_no_private_reply_to_blocked_account() -> Result<()> {
|
async fn test_no_private_reply_to_blocked_account() -> Result<()> {
|
||||||
let mut tcm = TestContextManager::new();
|
let mut tcm = TestContextManager::new();
|
||||||
|
|||||||
Reference in New Issue
Block a user