Skip to content

Commit

Permalink
feat: add bot field to contact (#4821)
Browse files Browse the repository at this point in the history
closes #4647
  • Loading branch information
Septias authored and link2xt committed Oct 24, 2023
1 parent ac557f7 commit e7244f7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ impl ContactId {
pub const fn to_u32(&self) -> u32 {
self.0
}

/// Mark contact as bot.
pub(crate) async fn mark_bot(&self, context: &Context, is_bot: bool) -> Result<()> {
context
.sql
.execute("UPDATE contacts SET is_bot=? WHERE id=?;", (is_bot, self.0))
.await?;
Ok(())
}
}

impl fmt::Display for ContactId {
Expand Down Expand Up @@ -223,6 +232,9 @@ pub struct Contact {

/// Last seen message signature for this contact, to be displayed in the profile.
status: String,

/// If the contact is a bot.
is_bot: bool,
}

/// Possible origins of a contact.
Expand Down Expand Up @@ -366,7 +378,7 @@ impl Contact {
.sql
.query_row_optional(
"SELECT c.name, c.addr, c.origin, c.blocked, c.last_seen,
c.authname, c.param, c.status
c.authname, c.param, c.status, c.is_bot
FROM contacts c
WHERE c.id=?;",
(contact_id,),
Expand All @@ -379,6 +391,7 @@ impl Contact {
let authname: String = row.get(5)?;
let param: String = row.get(6)?;
let status: Option<String> = row.get(7)?;
let is_bot: bool = row.get(8)?;
let contact = Self {
id: contact_id,
name,
Expand All @@ -389,6 +402,7 @@ impl Contact {
origin,
param: param.parse().unwrap_or_default(),
status: status.unwrap_or_default(),
is_bot,
};
Ok(contact)
},
Expand Down Expand Up @@ -498,6 +512,11 @@ impl Contact {
Ok(())
}

/// Returns whether contact is a bot.
pub fn is_bot(&self) -> bool {
self.is_bot
}

/// Check if an e-mail address belongs to a known and unblocked contact.
///
/// Known and unblocked contacts will be returned by `get_contacts()`.
Expand Down
4 changes: 4 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,8 @@ mod tests {
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 who is not the bot anymore.
receive_imf(
Expand All @@ -2358,6 +2360,8 @@ mod tests {
let msg = alice.get_last_msg().await;
assert_eq!(msg.get_text(), "hello again".to_string());
assert!(!msg.is_bot());
let contact = Contact::get_by_id(&alice, msg.from_id).await?;
assert!(!contact.is_bot());

Ok(())
}
Expand Down
8 changes: 8 additions & 0 deletions src/mimeparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ pub(crate) struct MimeMessage {
/// for e.g. late-parsing HTML.
pub decoded_data: Vec<u8>,

/// Hop info for debugging.
pub(crate) hop_info: String,

/// Whether the contact sending this should be marked as bot.
pub(crate) is_bot: bool,
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -373,6 +377,9 @@ impl MimeMessage {
signatures.clear();
}

// Auto-submitted is also set by holiday-notices so we also check `chat-version`
let is_bot = headers.contains_key("auto-submitted") && headers.contains_key("chat-version");

let mut parser = MimeMessage {
parts: Vec::new(),
headers,
Expand Down Expand Up @@ -401,6 +408,7 @@ impl MimeMessage {
is_mime_modified: false,
decoded_data: Vec::new(),
hop_info,
is_bot,
};

match partial {
Expand Down
2 changes: 2 additions & 0 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ pub(crate) async fn receive_imf_inner(
.handle_reports(context, from_id, sent_timestamp, &mime_parser.parts)
.await;

from_id.mark_bot(context, mime_parser.is_bot).await?;

Ok(Some(received_msg))
}

Expand Down
9 changes: 9 additions & 0 deletions src/sql/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,15 @@ CREATE INDEX smtp_messageid ON imap(rfc724_mid);
.await?;
}

// Add is_bot column to contacts table with default false.
if dbversion < 102 {
sql.execute_migration(
"ALTER TABLE contacts ADD COLUMN is_bot INTEGER NOT NULL DEFAULT 0",
102,
)
.await?;
}

let new_version = sql
.get_raw_config_int(VERSION_CFG)
.await?
Expand Down

0 comments on commit e7244f7

Please sign in to comment.