2024-07-24 13:03:34 +00:00
|
|
|
use std::collections::{BTreeSet, HashSet};
|
2024-07-19 18:06:03 +00:00
|
|
|
use std::fmt;
|
2024-07-28 08:37:36 +00:00
|
|
|
use std::ops::Div;
|
2024-07-18 22:23:25 +00:00
|
|
|
|
2024-07-29 11:13:32 +00:00
|
|
|
use nostr_sdk::{Alphabet, Event, EventBuilder, EventId, Kind, Tag, Timestamp};
|
2024-07-26 18:15:50 +00:00
|
|
|
|
|
|
|
use crate::EventSender;
|
2024-07-24 13:03:34 +00:00
|
|
|
|
2024-07-26 18:45:29 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2024-07-18 22:23:25 +00:00
|
|
|
pub(crate) struct Task {
|
|
|
|
pub(crate) event: Event,
|
2024-07-24 13:03:34 +00:00
|
|
|
pub(crate) children: HashSet<EventId>,
|
|
|
|
pub(crate) props: BTreeSet<Event>,
|
2024-07-25 19:40:35 +00:00
|
|
|
/// Cached sorted tags of the event
|
2024-07-25 19:50:57 +00:00
|
|
|
pub(crate) tags: Option<BTreeSet<Tag>>,
|
2024-07-18 22:23:25 +00:00
|
|
|
}
|
2024-07-26 18:15:50 +00:00
|
|
|
|
2024-07-18 22:23:25 +00:00
|
|
|
impl Task {
|
|
|
|
pub(crate) fn new(event: Event) -> Task {
|
|
|
|
Task {
|
2024-07-24 13:03:34 +00:00
|
|
|
children: Default::default(),
|
|
|
|
props: Default::default(),
|
2024-07-26 09:38:27 +00:00
|
|
|
tags: if event.tags.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(event.tags.iter().cloned().collect())
|
|
|
|
},
|
2024-07-25 19:40:35 +00:00
|
|
|
event,
|
2024-07-18 22:23:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 18:15:50 +00:00
|
|
|
pub(crate) fn get_id(&self) -> &EventId {
|
|
|
|
&self.event.id
|
|
|
|
}
|
|
|
|
|
2024-07-18 22:23:25 +00:00
|
|
|
pub(crate) fn parent_id(&self) -> Option<EventId> {
|
|
|
|
for tag in self.event.tags.iter() {
|
|
|
|
match tag {
|
|
|
|
Tag::Event { event_id, .. } => return Some(*event_id),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-07-29 10:32:47 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-07-26 09:38:27 +00:00
|
|
|
fn descriptions(&self) -> impl Iterator<Item = &String> + '_ {
|
2024-07-18 22:23:25 +00:00
|
|
|
self.props.iter().filter_map(|event| {
|
|
|
|
if event.kind == Kind::TextNote {
|
2024-07-26 09:38:27 +00:00
|
|
|
Some(&event.content)
|
2024-07-18 22:23:25 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn states(&self) -> impl Iterator<Item = TaskState> + '_ {
|
|
|
|
self.props.iter().filter_map(|event| {
|
2024-07-26 08:07:47 +00:00
|
|
|
event.kind.try_into().ok().map(|s| TaskState {
|
2024-07-19 18:06:03 +00:00
|
|
|
name: if event.content.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(event.content.clone())
|
|
|
|
},
|
|
|
|
state: s,
|
|
|
|
time: event.created_at.clone(),
|
|
|
|
})
|
2024-07-18 22:23:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:07:47 +00:00
|
|
|
pub(crate) fn state(&self) -> Option<TaskState> {
|
2024-07-18 22:23:25 +00:00
|
|
|
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-07-26 18:15:50 +00:00
|
|
|
pub(crate) fn set_state(
|
|
|
|
&mut self,
|
|
|
|
sender: &EventSender,
|
|
|
|
state: State,
|
|
|
|
comment: &str,
|
|
|
|
) -> Option<Event> {
|
|
|
|
sender
|
|
|
|
.submit(EventBuilder::new(
|
|
|
|
state.kind(),
|
|
|
|
comment,
|
|
|
|
vec![Tag::event(self.event.id)],
|
|
|
|
))
|
|
|
|
.inspect(|e| {
|
|
|
|
self.props.insert(e.clone());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-18 22:23:25 +00:00
|
|
|
fn default_state(&self) -> TaskState {
|
|
|
|
TaskState {
|
|
|
|
name: None,
|
|
|
|
state: State::Open,
|
|
|
|
time: self.event.created_at,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 22:37:39 +00:00
|
|
|
/// Total time this task has been active.
|
2024-07-19 06:44:12 +00:00
|
|
|
/// TODO: Consider caching
|
2024-07-18 22:37:39 +00:00
|
|
|
pub(crate) fn time_tracked(&self) -> u64 {
|
|
|
|
let mut total = 0;
|
|
|
|
let mut start: Option<Timestamp> = None;
|
|
|
|
for state in self.states() {
|
|
|
|
match state.state {
|
|
|
|
State::Active => start = start.or(Some(state.time)),
|
|
|
|
_ => {
|
|
|
|
if let Some(stamp) = start {
|
|
|
|
total += (state.time - stamp).as_u64();
|
|
|
|
start = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-29 11:01:33 +00:00
|
|
|
if let Some(start) = start {
|
|
|
|
total += (Timestamp::now() - start).as_u64();
|
|
|
|
}
|
2024-07-18 22:37:39 +00:00
|
|
|
total
|
2024-07-18 22:23:25 +00:00
|
|
|
}
|
2024-07-29 11:13:32 +00:00
|
|
|
|
|
|
|
fn filter_tags<P>(&self, predicate: P) -> Option<String>
|
|
|
|
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(" ")
|
|
|
|
})
|
|
|
|
}
|
2024-07-18 22:23:25 +00:00
|
|
|
|
|
|
|
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()),
|
|
|
|
"state" => self.state().map(|s| s.to_string()),
|
|
|
|
"name" => Some(self.event.content.clone()),
|
2024-07-28 08:37:36 +00:00
|
|
|
"time" => Some(format!("{}m", self.time_tracked().div(60))),
|
2024-07-29 11:13:32 +00:00
|
|
|
"hashtags" => self.filter_tags(|tag| tag.single_letter_tag().is_some_and(|sltag| sltag.character == Alphabet::T)),
|
|
|
|
"tags" => self.filter_tags(|tag| !tag.single_letter_tag().is_some_and(|sltag| sltag.character == Alphabet::E)),
|
2024-07-25 07:55:29 +00:00
|
|
|
"props" => Some(format!(
|
|
|
|
"{:?}",
|
|
|
|
self.props
|
|
|
|
.iter()
|
|
|
|
.map(|e| format!("{} kind {} '{}'", e.created_at, e.kind, e.content))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
)),
|
2024-07-29 11:13:32 +00:00
|
|
|
"descriptions" => Some(format!("{:?}", self.descriptions().collect::<Vec<&String>>())),
|
|
|
|
"desc" | "description" => self.descriptions().last().cloned(),
|
2024-07-18 22:23:25 +00:00
|
|
|
_ => {
|
2024-07-29 11:13:32 +00:00
|
|
|
eprintln!("Unknown task property {}", property);
|
2024-07-18 22:23:25 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:07:47 +00:00
|
|
|
pub(crate) struct TaskState {
|
2024-07-18 22:23:25 +00:00
|
|
|
state: State,
|
2024-07-26 18:45:29 +00:00
|
|
|
name: Option<String>,
|
2024-07-18 22:23:25 +00:00
|
|
|
time: Timestamp,
|
|
|
|
}
|
2024-07-26 08:07:47 +00:00
|
|
|
impl TaskState {
|
|
|
|
pub(crate) fn get_label(&self) -> String {
|
|
|
|
self.name.clone().unwrap_or_else(|| self.state.to_string())
|
|
|
|
}
|
2024-07-26 18:15:50 +00:00
|
|
|
pub(crate) fn matches_label(&self, label: &str) -> bool {
|
|
|
|
self.state == State::Active
|
2024-07-29 06:43:01 +00:00
|
|
|
|| 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 18:15:50 +00:00
|
|
|
}
|
2024-07-26 08:07:47 +00:00
|
|
|
}
|
2024-07-18 22:23:25 +00:00
|
|
|
impl fmt::Display for TaskState {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}{}",
|
|
|
|
self.state,
|
|
|
|
self.name
|
|
|
|
.as_ref()
|
|
|
|
.map_or(String::new(), |s| format!(": {}", s))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
|
|
pub(crate) enum State {
|
|
|
|
Closed,
|
|
|
|
Open,
|
|
|
|
Active,
|
|
|
|
Done,
|
|
|
|
}
|
2024-07-26 08:07:47 +00: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(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-18 22:23:25 +00:00
|
|
|
impl State {
|
2024-07-24 13:03:34 +00:00
|
|
|
pub(crate) fn kind(&self) -> Kind {
|
2024-07-18 22:23:25 +00:00
|
|
|
match self {
|
|
|
|
State::Open => Kind::from(1630),
|
|
|
|
State::Done => Kind::from(1631),
|
|
|
|
State::Closed => Kind::from(1632),
|
|
|
|
State::Active => Kind::from(1633),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl fmt::Display for State {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|