mod numbers;
pub(in crate::store) mod publish;
mod stage;
mod trail;
pub(crate) use self::trail::TrailAt;
use super::FileStore;
use super::time::Stamp;
use crate::conversation::ids::parse_conversation_id;
use crate::conversation::rows::{CONVERSATIONS, REDACTIONS, parse_redaction};
use crate::conversation::search::held_ids;
use crate::conversation::{Error, Redaction};
use std::path::Path;
pub(crate) fn selection(
through: Option<i64>,
turns: Option<&[i64]>,
) -> Result<(Option<u64>, Option<Vec<u64>>), Error> {
if through.is_some() && turns.is_some() {
return Err(Error::Value(
"give through= or turns=, not both".to_string(),
));
}
if let Some(through) = through.filter(|through| *through < 1) {
return Err(Error::Value(format!("through must be >= 1, got {through}")));
}
let Some(turns) = turns else {
return Ok((through.map(|through| through as u64), None));
};
let mut named: Vec<i64> = turns.to_vec();
named.sort_unstable();
named.dedup();
if let Some(first) = named.first().filter(|first| **first < 1) {
return Err(Error::Value(format!(
"turn numbers start at 1, got {first}"
)));
}
Ok((
None,
Some(named.into_iter().map(|turn| turn as u64).collect()),
))
}
fn selected(number: u64, through: Option<u64>, named: Option<&[u64]>) -> bool {
match named {
Some(named) => named.binary_search(&number).is_ok(),
None => through.is_none_or(|through| number <= through),
}
}
fn rows<T>(
file: &Path,
conversation_id: &str,
parse: impl Fn(&str, usize, &str) -> Result<T, crate::conversation::Refused>,
) -> Result<Vec<T>, Error> {
let text = std::fs::read_to_string(file)?;
text.lines()
.enumerate()
.map(|(index, line)| parse(line, index + 1, conversation_id).map_err(Error::from))
.collect()
}
impl FileStore {
pub fn redact_turns(
&self,
conversation_id: &str,
through: Option<i64>,
turns: Option<&[i64]>,
actor: Option<&str>,
) -> Result<usize, Error> {
let _held = self.lock()?;
parse_conversation_id(conversation_id)?;
let (through, named) = selection(through, turns)?;
self.settle(conversation_id)?;
self.erase_selected(conversation_id, through, named.as_deref(), actor)
}
pub fn turn_redactions(
&self,
ids: Option<&[String]>,
since: Option<Stamp>,
limit: i64,
) -> Result<Vec<Redaction>, Error> {
let _held = self.lock()?;
if let Some(ids) = ids {
for conversation_id in ids {
parse_conversation_id(conversation_id)?;
}
}
if limit < 1 {
return Err(Error::Value(format!("limit must be >= 1, got {limit}")));
}
let parent = self.root.join(CONVERSATIONS);
let held;
let ids = match ids {
Some([]) => return Ok(Vec::new()),
Some(ids) => ids,
None => {
held = held_ids(&parent)?;
&held
}
};
let mut acts: Vec<(Redaction, usize)> = Vec::new();
for conversation_id in ids {
self.settle(conversation_id)?;
let trail = self.conversation_file(conversation_id, REDACTIONS)?;
if trail.is_file() {
let read = rows(&trail, conversation_id, parse_redaction)?;
acts.extend(read.into_iter().enumerate().map(|(at, act)| (act, at)));
}
}
acts.retain(|(act, _)| {
since.is_none_or(|since| act.created_at.instant() >= since.instant())
});
acts.sort_by(|(a, at_a), (b, at_b)| {
(b.created_at.instant(), &b.conversation_id, at_b).cmp(&(
a.created_at.instant(),
&a.conversation_id,
at_a,
))
});
acts.truncate(limit as usize);
Ok(acts.into_iter().map(|(act, _)| act).collect())
}
}