1 parent
d55640d37a
commit
fff8521a17
67 files changed
+11016
-16763
No files matched your search
@@ -0,0 +1,46 @@
|
||||
use rusqlite::Connection;
|
||||
|
||||
pub fn list(conn: &Connection) -> Result<Vec<String>, String> {
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT email FROM trusted_senders ORDER BY lower(email)")
|
||||
.map_err(|e| format!("Failed to prepare trusted sender list: {e}"))?;
|
||||
let rows = stmt
|
||||
.query_map([], |row| row.get::<_, String>(0))
|
||||
.map_err(|e| format!("Failed to query trusted senders: {e}"))?;
|
||||
rows.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|e| format!("Failed to read trusted sender: {e}"))
|
||||
}
|
||||
|
||||
pub fn add(conn: &Connection, email: &str) -> Result<(), String> {
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO trusted_senders(email) VALUES(?1)",
|
||||
[email],
|
||||
)
|
||||
.map_err(|e| format!("Failed to trust sender: {e}"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove(conn: &Connection, email: &str) -> Result<(), String> {
|
||||
conn.execute(
|
||||
"DELETE FROM trusted_senders WHERE email = ?1 COLLATE NOCASE",
|
||||
[email],
|
||||
)
|
||||
.map_err(|e| format!("Failed to remove trusted sender: {e}"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::db::pool::open_test_db;
|
||||
|
||||
#[test]
|
||||
fn trusted_senders_roundtrip_case_insensitive() {
|
||||
let conn = open_test_db();
|
||||
add(&conn, "Alice@Example.com").unwrap();
|
||||
add(&conn, "alice@example.com").unwrap();
|
||||
assert_eq!(list(&conn).unwrap().len(), 1);
|
||||
remove(&conn, "ALICE@example.com").unwrap();
|
||||
assert!(list(&conn).unwrap().is_empty());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user