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

feat: add bot field to contact #4821

Merged
merged 2 commits into from
Oct 15, 2023
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
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 {
Septias marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2333,6 +2333,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 @@ -2350,6 +2352,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 @@ -113,7 +113,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 @@ -390,6 +394,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 @@ -418,6 +425,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 @@ -361,6 +361,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 @@ -731,6 +731,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
Loading