mostr/src/task.rs

218 lines
6.2 KiB
Rust
Raw Normal View History

2024-07-30 17:13:29 +03:00
use fmt::Display;
2024-07-24 16:03:34 +03:00
use std::collections::{BTreeSet, HashSet};
2024-07-19 21:06:03 +03:00
use std::fmt;
2024-07-29 16:13:40 +03:00
use itertools::Either::{Left, Right};
use itertools::Itertools;
2024-07-29 21:06:23 +03:00
use log::{debug, error, info, trace, warn};
use nostr_sdk::{Event, EventBuilder, EventId, Kind, Tag, TagStandard, Timestamp};
2024-08-06 23:01:59 +03:00
use crate::helpers::some_non_empty;
use crate::kinds::is_hashtag;
2024-07-24 16:03:34 +03:00
2024-07-26 21:45:29 +03:00
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Task {
pub(crate) event: Event,
2024-07-24 16:03:34 +03:00
pub(crate) children: HashSet<EventId>,
pub(crate) props: BTreeSet<Event>,
2024-07-25 22:40:35 +03:00
/// Cached sorted tags of the event
2024-07-25 22:50:57 +03:00
pub(crate) tags: Option<BTreeSet<Tag>>,
2024-07-29 16:13:40 +03:00
parents: Vec<EventId>,
}
impl Task {
pub(crate) fn new(event: Event) -> Task {
2024-08-06 11:34:18 +03:00
let (parents, tags) = event.tags.iter().partition_map(|tag| match tag.as_standardized() {
Some(TagStandard::Event { event_id, .. }) => return Left(event_id),
2024-07-30 09:02:56 +03:00
_ => Right(tag.clone()),
2024-07-29 16:13:40 +03:00
});
Task {
2024-07-24 16:03:34 +03:00
children: Default::default(),
props: Default::default(),
2024-07-29 16:13:40 +03:00
tags: Some(tags).filter(|t: &BTreeSet<Tag>| !t.is_empty()),
parents,
2024-07-25 22:40:35 +03:00
event,
}
}
pub(crate) fn get_id(&self) -> &EventId {
&self.event.id
}
2024-08-01 14:07:40 +03:00
pub(crate) fn parent_id(&self) -> Option<&EventId> {
self.parents.first()
}
pub(crate) fn get_title(&self) -> String {
Some(self.event.content.trim().to_string())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| self.get_id().to_string())
}
pub(crate) fn description_events(&self) -> impl Iterator<Item=&Event> + '_ {
self.props.iter().filter_map(|event| {
if event.kind == Kind::TextNote {
Some(event)
} else {
None
}
})
}
2024-08-08 13:52:02 +03:00
pub(crate) fn descriptions(&self) -> impl Iterator<Item=&String> + '_ {
self.description_events().map(|e| &e.content)
}
2024-07-31 20:08:33 +03:00
fn states(&self) -> impl Iterator<Item=TaskState> + '_ {
self.props.iter().filter_map(|event| {
2024-07-26 11:07:47 +03:00
event.kind.try_into().ok().map(|s| TaskState {
name: some_non_empty(&event.content),
2024-07-19 21:06:03 +03:00
state: s,
time: event.created_at.clone(),
})
})
}
2024-07-26 11:07:47 +03:00
pub(crate) fn state(&self) -> Option<TaskState> {
self.states().max_by_key(|t| t.time)
}
pub(crate) fn pure_state(&self) -> State {
self.state().map_or(State::Open, |s| s.state)
}
2024-08-01 14:07:40 +03:00
pub(crate) fn state_or_default(&self) -> TaskState {
self.state().unwrap_or_else(|| self.default_state())
}
fn default_state(&self) -> TaskState {
TaskState {
name: None,
state: State::Open,
time: self.event.created_at,
}
}
2024-07-29 16:13:40 +03:00
fn filter_tags<P>(&self, predicate: P) -> Option<String>
2024-07-30 09:02:56 +03:00
where
P: FnMut(&&Tag) -> bool,
{
self.tags.as_ref().map(|tags| {
tags.into_iter()
.filter(predicate)
.map(|t| format!("{}", t.content().unwrap()))
.collect::<Vec<String>>()
.join(" ")
})
}
pub(crate) fn get(&self, property: &str) -> Option<String> {
match property {
"id" => Some(self.event.id.to_string()),
"parentid" => self.parent_id().map(|i| i.to_string()),
2024-08-01 14:07:40 +03:00
"state" => Some(self.state_or_default().get_label()),
"name" => Some(self.event.content.clone()),
2024-07-30 09:09:54 +03:00
"desc" => self.descriptions().last().cloned(),
"description" => Some(self.descriptions().join(" ")),
2024-08-02 14:31:00 +03:00
"hashtags" => self.filter_tags(|tag| { is_hashtag(tag) }),
2024-07-29 16:13:40 +03:00
"tags" => self.filter_tags(|_| true),
"alltags" => Some(format!("{:?}", self.tags)),
2024-07-25 10:55:29 +03:00
"props" => Some(format!(
"{:?}",
self.props
.iter()
.map(|e| format!("{} kind {} \"{}\"", e.created_at, e.kind, e.content))
.collect_vec()
2024-07-25 10:55:29 +03:00
)),
2024-07-30 09:02:56 +03:00
"descriptions" => Some(format!(
"{:?}",
self.descriptions().collect_vec()
2024-07-30 09:02:56 +03:00
)),
_ => {
2024-07-29 21:06:23 +03:00
warn!("Unknown task property {}", property);
None
}
}
}
}
2024-07-26 11:07:47 +03:00
pub(crate) struct TaskState {
state: State,
2024-07-26 21:45:29 +03:00
name: Option<String>,
pub(crate) time: Timestamp,
}
2024-07-26 11:07:47 +03:00
impl TaskState {
pub(crate) fn get_label_for(state: &State, comment: &str) -> String {
some_non_empty(comment).unwrap_or_else(|| state.to_string())
}
2024-07-26 11:07:47 +03:00
pub(crate) fn get_label(&self) -> String {
self.name.clone().unwrap_or_else(|| self.state.to_string())
}
pub(crate) fn matches_label(&self, label: &str) -> bool {
self.name.as_ref().is_some_and(|n| n.eq_ignore_ascii_case(label))
|| self.state.to_string().eq_ignore_ascii_case(label)
}
2024-07-26 11:07:47 +03:00
}
2024-07-30 17:13:29 +03:00
impl Display for TaskState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2024-07-29 14:16:37 +03:00
let state_str = self.state.to_string();
write!(
f,
2024-07-29 14:16:37 +03:00
"{}",
self.name
.as_ref()
2024-07-29 14:16:37 +03:00
.map(|s| s.trim())
.filter(|s| !s.eq_ignore_ascii_case(&state_str))
.map_or(state_str, |s| format!("{}: {}", self.state, s))
)
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum State {
Closed,
Open,
Active,
Done,
}
2024-07-26 11:07:47 +03:00
impl TryFrom<Kind> for State {
type Error = ();
fn try_from(value: Kind) -> Result<Self, Self::Error> {
match value.as_u32() {
1630 => Ok(State::Open),
1631 => Ok(State::Done),
1632 => Ok(State::Closed),
1633 => Ok(State::Active),
_ => Err(()),
}
}
}
impl State {
pub(crate) fn is_open(&self) -> bool {
match self {
State::Open | State::Active => true,
_ => false,
}
}
2024-08-06 11:34:18 +03:00
pub(crate) fn kind(&self) -> u16 {
match self {
2024-07-30 20:25:27 +03:00
State::Open => 1630,
State::Done => 1631,
State::Closed => 1632,
State::Active => 1633,
}
}
}
2024-07-30 20:25:27 +03:00
impl From<State> for Kind {
fn from(value: State) -> Self {
Kind::from(value.kind())
}
}
2024-07-30 17:13:29 +03:00
impl Display for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}