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: mark holiday notice messages as bot-generated #6374

Merged
merged 1 commit into from
Jan 1, 2025
Merged
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
23 changes: 23 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,29 @@ mod tests {
async fn test_is_bot() -> Result<()> {
let alice = TestContext::new_alice().await;

// Alice receives an auto-generated non-chat message.
//
// This could be a holiday notice,
// in which case the message should be marked as bot-generated,
// but the contact should not.
receive_imf(
&alice,
b"From: Claire <[email protected]>\n\
To: [email protected]\n\
Message-ID: <[email protected]>\n\
Auto-Submitted: auto-generated\n\
Date: Fri, 29 Jan 2021 21:37:55 +0000\n\
\n\
hello\n",
false,
)
.await?;
let msg = alice.get_last_msg().await;
assert_eq!(msg.get_text(), "hello".to_string());
assert!(msg.is_bot());
let contact = Contact::get_by_id(&alice, msg.from_id).await?;
assert!(!contact.is_bot());

// Alice receives a message from Bob the bot.
receive_imf(
&alice,
Expand Down
16 changes: 11 additions & 5 deletions src/mimeparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@ pub(crate) struct MimeMessage {
/// Hop info for debugging.
pub(crate) hop_info: String,

/// Whether the contact sending this should be marked as bot or non-bot.
/// Whether the message is auto-generated.
///
/// If chat message (with `Chat-Version` header) is auto-generated,
/// the contact sending this should be marked as bot.
///
/// If non-chat message is auto-generated,
/// it could be a holiday notice auto-reply,
/// in which case the message should be marked as bot-generated,
/// but the contact should not be.
pub(crate) is_bot: Option<bool>,

/// When the message was received, in secs since epoch.
Expand Down Expand Up @@ -563,10 +571,8 @@ impl MimeMessage {
};

if parser.mdn_reports.is_empty() && parser.webxdc_status_update.is_none() {
// "Auto-Submitted" is also set by holiday-notices so we also check "chat-version".
let is_bot = parser.headers.get("auto-submitted")
== Some(&"auto-generated".to_string())
&& parser.headers.contains_key("chat-version");
let is_bot =
parser.headers.get("auto-submitted") == Some(&"auto-generated".to_string());
Copy link
Collaborator

Choose a reason for hiding this comment

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

.as_deref() probably can be used for the lhs to avoid to_string()

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 tried and this does not work because of the cursed &String type inside the return type of get. as_deref() changes nothing, it is still Option<&String> afterwards.

parser.is_bot = Some(is_bot);
}
parser.maybe_remove_bad_parts();
Expand Down
6 changes: 5 additions & 1 deletion src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,11 @@ pub(crate) async fn receive_imf_inner(
.await;

if let Some(is_bot) = mime_parser.is_bot {
from_id.mark_bot(context, is_bot).await?;
// If the message is auto-generated and was generated by Delta Chat,
// mark the contact as a bot.
if mime_parser.get_header(HeaderDef::ChatVersion).is_some() {
from_id.mark_bot(context, is_bot).await?;
}
}

Ok(Some(received_msg))
Expand Down
Loading