mirror of
https://github.com/rustmailer/bichon.git
synced 2026-08-03 07:48:34 +02:00
feat(search): expand default search scope and support specific field filtering
This commit is contained in:
@@ -1168,6 +1168,18 @@ impl DuckDBManager {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(subject) = filter.subject {
|
||||
let pattern = format!("(?i){}", subject);
|
||||
base_sql.push_str(" AND regexp_matches(coalesce(e.subject, ''), ?)");
|
||||
args.push(pattern.into());
|
||||
}
|
||||
|
||||
if let Some(body) = filter.body {
|
||||
let pattern = format!("(?i){}", body);
|
||||
base_sql.push_str(" AND regexp_matches(coalesce(e.body, ''), ?)");
|
||||
args.push(pattern.into());
|
||||
}
|
||||
|
||||
if let Some(text) = filter.text {
|
||||
let pattern = format!("(?i){}", text);
|
||||
base_sql.push_str(
|
||||
@@ -1175,10 +1187,14 @@ impl DuckDBManager {
|
||||
AND (
|
||||
regexp_matches(coalesce(e.subject, ''), ?)
|
||||
OR regexp_matches(coalesce(e.body, ''), ?)
|
||||
OR regexp_matches(coalesce(e.sender, ''), ?)
|
||||
OR regexp_matches(array_to_string(e.recipients, ','), ?)
|
||||
)
|
||||
",
|
||||
);
|
||||
args.push(pattern.clone().into());
|
||||
args.push(pattern.clone().into());
|
||||
args.push(pattern.clone().into());
|
||||
args.push(pattern.into());
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
use mail_parser::{parsers::MessageStream, MessageParser, MimeHeaders};
|
||||
|
||||
use crate::{
|
||||
|
||||
@@ -34,6 +34,8 @@ use crate::{
|
||||
#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize, Object)]
|
||||
pub struct SearchFilter {
|
||||
pub text: Option<String>,
|
||||
pub subject: Option<String>,
|
||||
pub body: Option<String>,
|
||||
pub from: Option<String>,
|
||||
pub to: Option<String>,
|
||||
pub cc: Option<String>,
|
||||
@@ -80,15 +82,23 @@ impl SearchRequest {
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(ref pattern) = self.filter.text {
|
||||
if let Err(_) = duckdb()?.validate_regex(pattern) {
|
||||
return Err(raise_error!(
|
||||
"Invalid search pattern: The regular expression is not supported by DuckDB."
|
||||
.into(),
|
||||
ErrorCode::InvalidParameter
|
||||
));
|
||||
let validate = |pattern: &Option<String>| -> BichonResult<()> {
|
||||
if let Some(ref p) = pattern {
|
||||
if duckdb()?.validate_regex(p).is_err() {
|
||||
return Err(raise_error!(
|
||||
"Invalid search pattern: The regular expression is not supported by DuckDB.".into(),
|
||||
ErrorCode::InvalidParameter
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
|
||||
validate(&self.filter.text)?;
|
||||
validate(&self.filter.subject)?;
|
||||
validate(&self.filter.body)?;
|
||||
validate(&self.filter.from)?;
|
||||
validate(&self.filter.to)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user