use std::collections::{HashMap, HashSet};
use super::types::{Delivery, InboxMessage, PREFIX, decode, message_path};
use crate::conversation::Error;
use crate::conversation::ids::parse_conversation_id;
use crate::conversation::sessions::{continues, sessions_path};
use crate::memory::mounts::Mount;
use crate::state::State;
use crate::store::error::MemoryError;
use crate::store::grammar::Actor;
use crate::store::time::Stamp;
pub const LIST_LIMIT: usize = 50;
const LIST_MAX: usize = 500;
const DEFAULT_ACTOR: &str = "mailbox:local";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ListStatus {
Unread,
Open,
Scheduled,
Closed,
All,
}
impl ListStatus {
pub const ALL: [Self; 5] = [
Self::Unread,
Self::Open,
Self::Scheduled,
Self::Closed,
Self::All,
];
pub fn as_str(self) -> &'static str {
match self {
Self::Unread => "unread",
Self::Open => "open",
Self::Scheduled => "scheduled",
Self::Closed => "closed",
Self::All => "all",
}
}
pub fn parse(text: &str) -> Option<Self> {
Self::ALL.into_iter().find(|status| status.as_str() == text)
}
}
#[derive(Debug)]
pub struct Receipt {
pub message: InboxMessage,
pub version: u64,
}
#[derive(Clone)]
pub(super) struct Reach {
pub scope: String,
pub path: String,
pub read_only: bool,
pub edit_only: bool,
}
pub struct Mailbox<'a> {
pub(super) store: &'a dyn State,
pub(super) reach: Vec<Reach>,
pub(super) session: Option<String>,
pub(super) actor: Actor,
clock: Option<Stamp>,
}
impl<'a> Mailbox<'a> {
fn new(store: &'a dyn State, reach: Vec<Reach>, session: Option<&str>) -> Self {
Self {
store,
reach,
session: session.map(str::to_string),
actor: Actor::parse(DEFAULT_ACTOR).expect("the default actor is grammatical"),
clock: None,
}
}
pub fn of_mounts(store: &'a dyn State, mounts: &[Mount], session: Option<&str>) -> Self {
let mut reach: Vec<Reach> = Vec::new();
for mount in mounts {
if !reach.iter().any(|known| known.scope == mount.scope) {
reach.push(Reach {
scope: mount.scope.clone(),
path: mount.path.clone(),
read_only: mount.read_only,
edit_only: mount.edit_only,
});
}
}
Self::new(store, reach, session)
}
pub fn over_scopes(store: &'a dyn State, scopes: Vec<String>) -> Self {
let reach = scopes
.into_iter()
.map(|scope| Reach {
scope,
path: String::new(),
read_only: true,
edit_only: false,
})
.collect();
Self::new(store, reach, None)
}
pub fn with_actor(mut self, actor: Actor) -> Self {
self.actor = actor;
self
}
#[cfg(test)]
pub fn at(mut self, now: Stamp) -> Self {
self.clock = Some(now);
self
}
pub fn session(&self) -> Option<&str> {
self.session.as_deref()
}
pub fn as_session(&self, session: Option<&str>) -> Self {
Self {
store: self.store,
reach: self.reach.clone(),
session: session.map(str::to_string),
actor: self.actor.clone(),
clock: self.clock,
}
}
pub fn now(&self) -> Stamp {
self.clock.unwrap_or_else(Stamp::now)
}
pub(super) fn mounted(&self, scope: &str, write: bool) -> Result<&Reach, Error> {
let Some(reach) = self.reach.iter().find(|reach| reach.scope == scope) else {
return Err(Error::Value(format!("scope is not mounted: {scope}")));
};
if write && reach.read_only {
return Err(MemoryError::read_only(&reach.path).into());
}
Ok(reach)
}
pub fn view(&self, scope: &str, id: &str) -> Result<Receipt, Error> {
self.mounted(scope, false)?;
let path = message_path(id).map_err(Error::Value)?;
let document = self
.store
.read(scope, &path)?
.filter(|document| !document.envelope.redacted)
.ok_or_else(|| MemoryError::not_found(scope, &path))?;
let message = decode(&document.content).map_err(Error::Value)?;
if message.scope != scope || message_path(&message.id).map_err(Error::Value)? != path {
let mismatch = "message envelope does not match its storage address";
return Err(Error::Value(mismatch.to_string()));
}
Ok(Receipt {
message,
version: document.envelope.version,
})
}
pub fn history(&self, scope: &str, id: &str) -> Result<Vec<InboxMessage>, Error> {
let receipt = self.view(scope, id)?;
let path = message_path(id).map_err(Error::Value)?;
let rows = self
.store
.versions(scope, &path, receipt.version as usize)?;
rows.iter()
.filter(|row| !row.redacted)
.map(|row| decode(&row.content).map_err(Error::Value))
.collect()
}
pub fn all(&self, scope: Option<&str>) -> Result<Vec<Receipt>, Error> {
let scopes: Vec<&str> = match scope {
Some(scope) => vec![scope],
None => self
.reach
.iter()
.map(|reach| reach.scope.as_str())
.collect(),
};
let mut found = Vec::new();
for selected in scopes {
self.mounted(selected, false)?;
for entry in self.store.list_documents(selected, PREFIX)? {
if entry.redacted {
continue;
}
match self.view(selected, &entry.path[PREFIX.len()..]) {
Ok(receipt) => found.push(receipt),
Err(Error::Memory(error)) if error.code == "memory_document_not_found" => {}
Err(error) => return Err(error),
}
}
}
found.sort_by(|a, b| {
(a.message.created_at.instant(), &a.message.id)
.cmp(&(b.message.created_at.instant(), &b.message.id))
});
Ok(found)
}
pub fn lineage(&self, scope: &str) -> Result<HashSet<String>, Error> {
self.mounted(scope, false)?;
let mut pending: Vec<String> = self.session.iter().cloned().collect();
let mut found = HashSet::new();
while let Some(current) = pending.pop() {
if !found.insert(current.clone()) {
continue;
}
if let Some(document) = self.store.read(scope, &sessions_path(¤t))?
&& !document.envelope.redacted
{
for parent in continues(&document.content) {
parse_conversation_id(&parent)?;
pending.push(parent);
}
}
let prefix = format!("message-links/{current}/");
for entry in self.store.list_documents(scope, &prefix)? {
if !entry.redacted {
let parent = entry.path[prefix.len()..].to_string();
parse_conversation_id(&parent)?;
pending.push(parent);
}
}
}
Ok(found)
}
pub fn list(
&self,
scope: Option<&str>,
status: ListStatus,
limit: usize,
addressed: bool,
wake_only: bool,
) -> Result<Vec<Receipt>, Error> {
if !(1..=LIST_MAX).contains(&limit) {
return Err(Error::Value("limit must be between 1 and 500".to_string()));
}
let now = self.now();
let mut lineage: HashMap<String, HashSet<String>> = HashMap::new();
let mut found = Vec::new();
for receipt in self.all(scope)? {
let item = &receipt.message;
if wake_only
&& (item.delivery != Delivery::Wake
|| item.wake_accepted
|| !item.unread(now)
|| item.claimed(now)
|| item
.wake_until
.is_some_and(|until| until.instant() > now.instant()))
{
continue;
}
if addressed && let Some(conversation) = &item.conversation {
if !lineage.contains_key(&item.scope) {
let reach = self.lineage(&item.scope)?;
lineage.insert(item.scope.clone(), reach);
}
if !lineage[&item.scope].contains(conversation) {
continue;
}
}
let matches = match status {
ListStatus::All => true,
ListStatus::Unread => item.unread(now),
ListStatus::Open => item.available(now),
ListStatus::Scheduled => {
item.status == "open" && item.due_at.instant() > now.instant()
}
ListStatus::Closed => item.status != "open",
};
if matches {
found.push(receipt);
}
}
found.sort_by_key(|receipt| {
let item = &receipt.message;
(
item.due_at.instant(),
item.created_at.instant(),
item.id.clone(),
)
});
found.truncate(limit);
Ok(found)
}
}