use anyhow::Result;
use async_trait::async_trait;
use crate::domain::invitation_code::InvitationCode;
use crate::domain::{Group, Message, Organization};
#[derive(Debug, Clone, Default)]
pub struct MessageFilter {
pub from: Option<String>,
pub to: Option<String>,
pub target_type: Option<String>,
pub since: Option<i64>,
pub limit: usize,
}
impl MessageFilter {
pub fn new() -> Self {
Self {
limit: 100,
..Default::default()
}
}
pub fn from(mut self, agent_id: impl Into<String>) -> Self {
self.from = Some(agent_id.into());
self
}
pub fn to(mut self, target_id: impl Into<String>) -> Self {
self.to = Some(target_id.into());
self
}
pub fn target_type(mut self, type_: impl Into<String>) -> Self {
self.target_type = Some(type_.into());
self
}
pub fn since(mut self, timestamp: i64) -> Self {
self.since = Some(timestamp);
self
}
pub fn limit(mut self, n: usize) -> Self {
self.limit = n;
self
}
}
#[async_trait]
pub trait Store: Send + Sync {
async fn save_organization(&self, org: &Organization) -> Result<()>;
async fn load_organization(&self) -> Result<Organization>;
async fn save_group(&self, group: &Group) -> Result<()>;
async fn load_groups(&self) -> Result<Vec<Group>>;
async fn delete_group(&self, group_id: &str) -> Result<()>;
async fn save_message(&self, message: &Message) -> Result<()>;
async fn save_messages(&self, messages: &[Message]) -> Result<()> {
for msg in messages {
self.save_message(msg).await?;
}
Ok(())
}
async fn load_messages(&self, filter: MessageFilter) -> Result<Vec<Message>>;
async fn load_messages_by_agent(&self, agent_id: &str, limit: usize) -> Result<Vec<Message>> {
let from_filter = MessageFilter::new().limit(limit).from(agent_id);
let from_msgs = self.load_messages(from_filter).await?;
let to_filter = MessageFilter::new()
.limit(limit)
.to(agent_id)
.target_type("direct"); let to_msgs = self.load_messages(to_filter).await?;
let mut all_msgs = from_msgs;
for msg in to_msgs {
if !all_msgs.iter().any(|m| m.id == msg.id) {
all_msgs.push(msg);
}
}
all_msgs.sort_by_key(|msg| std::cmp::Reverse(msg.timestamp));
all_msgs.truncate(limit);
Ok(all_msgs)
}
async fn load_messages_by_group(&self, group_id: &str, limit: usize) -> Result<Vec<Message>> {
let filter = MessageFilter::new()
.to(group_id)
.target_type("group")
.limit(limit);
self.load_messages(filter).await
}
async fn save_user(&self, _user: &crate::domain::user::User) -> Result<()> {
Ok(())
}
async fn load_user_by_username(
&self,
_username: &str,
) -> Result<Option<crate::domain::user::User>> {
Ok(None)
}
async fn load_users(&self) -> Result<Vec<crate::domain::user::User>> {
Ok(vec![])
}
async fn save_invitation_code(&self, _code: &InvitationCode) -> Result<()> {
Ok(())
}
async fn load_invitation_code_by_code(&self, _code: &str) -> Result<Option<InvitationCode>> {
Ok(None)
}
async fn load_invitation_codes(&self) -> Result<Vec<InvitationCode>> {
Ok(vec![])
}
async fn update_invitation_code(&self, _code: &InvitationCode) -> Result<()> {
Ok(())
}
async fn load_invitation_codes_by_creator(
&self,
_creator_id: &str,
) -> Result<Vec<InvitationCode>> {
Ok(vec![])
}
}