feat(search): expand default search scope and support specific field filtering

This commit is contained in:
rustmailer
2026-03-19 15:44:20 +08:00
parent 2228e98410
commit 884fdeba10
24 changed files with 236 additions and 150 deletions
+16
View File
@@ -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());
}
-2
View File
@@ -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::{
+18 -8
View File
@@ -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(())
}