// // Copyright (c) 2025-2026 rustmailer.com (https://rustmailer.com) // // This file is part of the Bichon Email Archiving Project // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . use poem_openapi::{Enum, Object}; use serde::{Deserialize, Serialize}; use std::collections::HashSet; use crate::{ modules::{ error::{code::ErrorCode, BichonResult}, rest::response::DataPage, store::{ envelope::Envelope, tantivy::{ attachment::ATTACHMENT_MANAGER, envelope::ENVELOPE_MANAGER, model::AttachmentModel, }, }, }, raise_error, }; #[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize, Object)] pub struct EmailSearchFilter { pub text: Option, pub subject: Option, pub body: Option, pub from: Option, pub to: Option, pub cc: Option, pub bcc: Option, pub since: Option, pub before: Option, pub account_ids: Option>, pub mailbox_ids: Option>, pub min_size: Option, pub max_size: Option, pub message_id: Option, pub has_attachment: Option, pub attachment_name: Option, pub tags: Option>, pub attachment_extension: Option, pub attachment_category: Option, pub attachment_content_type: Option, } #[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize, Enum)] pub enum SortBy { #[default] DATE, SIZE, } #[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize, Object)] pub struct EmailSearchRequest { filter: EmailSearchFilter, page: u64, page_size: u64, sort_by: Option, desc: Option, } impl EmailSearchRequest { pub fn validate(&self) -> BichonResult<()> { if self.page == 0 || self.page_size == 0 { return Err(raise_error!( "Both page and page_size must be greater than 0.".into(), ErrorCode::InvalidParameter )); } if self.page_size > 500 { return Err(raise_error!( "The page_size exceeds the maximum allowed limit of 500.".into(), ErrorCode::InvalidParameter )); } Ok(()) } } pub async fn search_messages_impl( accounts: Option>, request: EmailSearchRequest, ) -> BichonResult> { request.validate()?; ENVELOPE_MANAGER .search( accounts, request.filter, request.page, request.page_size, request.desc.unwrap_or(true), request.sort_by.unwrap_or(SortBy::DATE), ) .await } #[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize, Object)] pub struct AttachmentSearchFilter { pub text: Option, pub subject: Option, pub from: Option, pub since: Option, pub before: Option, pub account_ids: Option>, pub mailbox_ids: Option>, pub min_size: Option, pub max_size: Option, pub attachment_name: Option, pub content_hash: Option, pub tags: Option>, pub attachment_extension: Option, pub attachment_category: Option, pub attachment_content_type: Option, pub is_ocr: Option, pub is_message: Option, pub has_text: Option, pub min_page_count: Option, pub max_page_count: Option, } #[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize, Object)] pub struct AttachmentSearchRequest { filter: AttachmentSearchFilter, page: u64, page_size: u64, sort_by: Option, desc: Option, } impl AttachmentSearchRequest { pub fn validate(&self) -> BichonResult<()> { if self.page == 0 || self.page_size == 0 { return Err(raise_error!( "Both page and page_size must be greater than 0.".into(), ErrorCode::InvalidParameter )); } if self.page_size > 500 { return Err(raise_error!( "The page_size exceeds the maximum allowed limit of 500.".into(), ErrorCode::InvalidParameter )); } Ok(()) } } pub async fn search_attachment_impl( accounts: Option>, request: AttachmentSearchRequest, ) -> BichonResult> { request.validate()?; ATTACHMENT_MANAGER .search( accounts, request.filter, request.page, request.page_size, request.desc.unwrap_or(true), request.sort_by.unwrap_or(SortBy::DATE), ) .await }