use super::{Echo, Target};
use crate::AppModel;
use crate::boundary::Action;
use std::path::Path;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct HeldSend {
pub(crate) text: String,
pub(crate) at_unix: i64,
}
impl Echo {
pub(crate) fn is_pending_mint(&self, agent: &str) -> bool {
matches!(&self.target, Target::Conversation(name) if name == agent)
}
}
impl AppModel {
pub(crate) fn hold_send(&mut self, ws: &Path, agent: &str, text: &str) -> bool {
let at_unix = self.now_unix();
let Some(echo) = self
.started
.as_mut()
.filter(|echo| echo.ws == ws && echo.is_pending_mint(agent))
else {
return false;
};
echo.held.push(HeldSend {
text: text.to_owned(),
at_unix,
});
true
}
pub(in crate::app) fn release_held(&mut self, agent: &str) {
let Some(echo) = self.started.as_mut() else {
return;
};
let held = std::mem::take(&mut echo.held);
let ws = echo.ws.clone();
for send in held {
let workspace = self.derived.ws_name(&ws);
self.post_act(&Action::Message {
workspace,
agent: agent.to_owned(),
content: send.text,
});
}
}
pub(crate) fn adopted_names(&self) -> Option<(String, String)> {
let echo = self.started.as_ref()?;
let name = echo.born.clone()?;
match &echo.target {
Target::Agent(id) => Some((name, id.clone())),
Target::Conversation(_) => None,
}
}
}
#[cfg(test)]
mod tests;