Email blocklisting (#718)
* Interface complete for the email blacklisting * Everything seems to work * Neutralize language * fix clippy warnings * Add missing spaces * Added matching test * Correct primary key datatype for postgresql * Address review comments * Add placeholder when empty. Fix missing 'i'
This commit is contained in:
@@ -28,6 +28,7 @@ webfinger = "0.4.1"
|
||||
whatlang = "0.7.1"
|
||||
shrinkwraprs = "0.2.1"
|
||||
diesel-derive-newtype = "0.1.2"
|
||||
glob = "0.3.0"
|
||||
|
||||
[dependencies.chrono]
|
||||
features = ["serde"]
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, TextExpressionMethods};
|
||||
use glob::Pattern;
|
||||
|
||||
use schema::email_blocklist;
|
||||
use {Connection, Error, Result};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable)]
|
||||
#[table_name = "email_blocklist"]
|
||||
pub struct BlocklistedEmail {
|
||||
pub id: i32,
|
||||
pub email_address: String,
|
||||
pub note: String,
|
||||
pub notify_user: bool,
|
||||
pub notification_text: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable, FromForm)]
|
||||
#[table_name = "email_blocklist"]
|
||||
pub struct NewBlocklistedEmail {
|
||||
pub email_address: String,
|
||||
pub note: String,
|
||||
pub notify_user: bool,
|
||||
pub notification_text: String,
|
||||
}
|
||||
|
||||
impl BlocklistedEmail {
|
||||
insert!(email_blocklist, NewBlocklistedEmail);
|
||||
get!(email_blocklist);
|
||||
find_by!(email_blocklist, find_by_id, id as i32);
|
||||
pub fn delete_entries(conn: &Connection, ids: Vec<i32>) -> Result<bool> {
|
||||
use diesel::delete;
|
||||
for i in ids {
|
||||
let be: BlocklistedEmail = BlocklistedEmail::find_by_id(&conn, i)?;
|
||||
delete(&be).execute(conn)?;
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
pub fn find_for_domain(conn: &Connection, domain: &str) -> Result<Vec<BlocklistedEmail>> {
|
||||
let effective = format!("%@{}", domain);
|
||||
email_blocklist::table
|
||||
.filter(email_blocklist::email_address.like(effective))
|
||||
.load::<BlocklistedEmail>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
pub fn matches_blocklist(conn: &Connection, email: &str) -> Result<Option<BlocklistedEmail>> {
|
||||
let mut result = email_blocklist::table.load::<BlocklistedEmail>(conn)?;
|
||||
for i in result.drain(..) {
|
||||
if let Ok(x) = Pattern::new(&i.email_address) {
|
||||
if x.matches(email) {
|
||||
return Ok(Some(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<BlocklistedEmail>> {
|
||||
email_blocklist::table
|
||||
.offset(min.into())
|
||||
.limit((max - min).into())
|
||||
.load::<BlocklistedEmail>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
pub fn count(conn: &Connection) -> Result<i64> {
|
||||
email_blocklist::table
|
||||
.count()
|
||||
.get_result(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
pub fn pattern_errors(pat: &str) -> Option<glob::PatternError> {
|
||||
let c = Pattern::new(pat);
|
||||
c.err()
|
||||
}
|
||||
pub fn new(
|
||||
conn: &Connection,
|
||||
pattern: &str,
|
||||
note: &str,
|
||||
show_notification: bool,
|
||||
notification_text: &str,
|
||||
) -> Result<BlocklistedEmail> {
|
||||
let c = NewBlocklistedEmail {
|
||||
email_address: pattern.to_owned(),
|
||||
note: note.to_owned(),
|
||||
notify_user: show_notification,
|
||||
notification_text: notification_text.to_owned(),
|
||||
};
|
||||
BlocklistedEmail::insert(conn, c)
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use diesel::Connection;
|
||||
use instance::tests as instance_tests;
|
||||
use tests::rockets;
|
||||
use Connection as Conn;
|
||||
pub(crate) fn fill_database(conn: &Conn) -> Vec<BlocklistedEmail> {
|
||||
instance_tests::fill_database(conn);
|
||||
let domainblock =
|
||||
BlocklistedEmail::new(conn, "*@bad-actor.com", "Mean spammers", false, "").unwrap();
|
||||
let userblock = BlocklistedEmail::new(
|
||||
conn,
|
||||
"spammer@lax-administration.com",
|
||||
"Decent enough domain, but this user is a problem.",
|
||||
true,
|
||||
"Stop it please",
|
||||
)
|
||||
.unwrap();
|
||||
vec![domainblock, userblock]
|
||||
}
|
||||
#[test]
|
||||
fn test_match() {
|
||||
let r = rockets();
|
||||
let conn = &*r.conn;
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let various = fill_database(conn);
|
||||
let match1 = "user1@bad-actor.com";
|
||||
let match2 = "spammer@lax-administration.com";
|
||||
let no_match = "happy-user@lax-administration.com";
|
||||
assert_eq!(
|
||||
BlocklistedEmail::matches_blocklist(conn, match1)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.id,
|
||||
various[0].id
|
||||
);
|
||||
assert_eq!(
|
||||
BlocklistedEmail::matches_blocklist(conn, match2)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.id,
|
||||
various[1].id
|
||||
);
|
||||
assert_eq!(
|
||||
BlocklistedEmail::matches_blocklist(conn, no_match)
|
||||
.unwrap()
|
||||
.is_none(),
|
||||
true
|
||||
);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ extern crate plume_common;
|
||||
#[macro_use]
|
||||
extern crate plume_macro;
|
||||
extern crate reqwest;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
extern crate rocket_i18n;
|
||||
extern crate scheduled_thread_pool;
|
||||
@@ -32,6 +33,7 @@ extern crate serde_derive;
|
||||
extern crate serde_json;
|
||||
#[macro_use]
|
||||
extern crate tantivy;
|
||||
extern crate glob;
|
||||
extern crate url;
|
||||
extern crate walkdir;
|
||||
extern crate webfinger;
|
||||
@@ -53,6 +55,7 @@ pub type Connection = diesel::PgConnection;
|
||||
/// All the possible errors that can be encoutered in this crate
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Blocklisted(bool, String),
|
||||
Db(diesel::result::Error),
|
||||
Inbox(Box<InboxError<Error>>),
|
||||
InvalidValue,
|
||||
@@ -351,6 +354,7 @@ mod tests {
|
||||
pub mod admin;
|
||||
pub mod api_tokens;
|
||||
pub mod apps;
|
||||
pub mod blocklisted_emails;
|
||||
pub mod blog_authors;
|
||||
pub mod blogs;
|
||||
pub mod comment_seers;
|
||||
|
||||
@@ -73,6 +73,15 @@ table! {
|
||||
user_id -> Int4,
|
||||
}
|
||||
}
|
||||
table! {
|
||||
email_blocklist(id){
|
||||
id -> Int4,
|
||||
email_address -> VarChar,
|
||||
note -> Text,
|
||||
notify_user -> Bool,
|
||||
notification_text -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
follows (id) {
|
||||
|
||||
@@ -49,7 +49,10 @@ use safe_string::SafeString;
|
||||
use schema::users;
|
||||
use search::Searcher;
|
||||
use timeline::Timeline;
|
||||
use {ap_url, Connection, Error, PlumeRocket, Result, ITEMS_PER_PAGE};
|
||||
use {
|
||||
ap_url, blocklisted_emails::BlocklistedEmail, Connection, Error, PlumeRocket, Result,
|
||||
ITEMS_PER_PAGE,
|
||||
};
|
||||
|
||||
pub type CustomPerson = CustomObject<ApSignature, Person>;
|
||||
|
||||
@@ -992,6 +995,10 @@ impl NewUser {
|
||||
) -> Result<User> {
|
||||
let (pub_key, priv_key) = gen_keypair();
|
||||
let instance = Instance::get_local()?;
|
||||
let blocklisted = BlocklistedEmail::matches_blocklist(conn, &email)?;
|
||||
if let Some(x) = blocklisted {
|
||||
return Err(Error::Blocklisted(x.notify_user, x.notification_text));
|
||||
}
|
||||
|
||||
let res = User::insert(
|
||||
conn,
|
||||
|
||||
Reference in New Issue
Block a user