2024-07-30 17:13:29 +03:00
|
|
|
use fmt::Display;
|
2024-08-08 21:10:17 +03:00
|
|
|
use std::cmp::Ordering;
|
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-08-09 20:53:30 +03:00
|
|
|
use std::string::ToString;
|
2024-07-19 01:23:25 +03:00
|
|
|
|
2024-08-10 21:25:46 +03:00
|
|
|
use colored::{ColoredString, Colorize};
|
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};
|
2024-08-10 21:25:46 +03:00
|
|
|
use nostr_sdk::{Event, EventId, Kind, Tag, TagStandard, Timestamp};
|
2024-07-26 21:15:50 +03:00
|
|
|
|
2024-08-18 21:38:20 +03:00
|
|
|
use crate::helpers::{local_datetimestamp, some_non_empty};
|
2024-08-18 22:24:14 +03:00
|
|
|
use crate::kinds::{is_hashtag, PROCEDURE_KIND, TASK_KIND};
|
2024-07-24 16:03:34 +03:00
|
|
|
|
2024-08-09 20:53:30 +03:00
|
|
|
pub static MARKER_PARENT: &str = "parent";
|
|
|
|
pub static MARKER_DEPENDS: &str = "depends";
|
|
|
|
|
2024-08-08 21:10:17 +03:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2024-07-19 01:23:25 +03:00
|
|
|
pub(crate) struct Task {
|
2024-08-08 21:10:17 +03:00
|
|
|
/// Event that defines this task
|
2024-07-19 01:23:25 +03:00
|
|
|
pub(crate) event: Event,
|
2024-08-08 21:10:17 +03:00
|
|
|
/// Cached sorted tags of the event with references remove - do not modify!
|
2024-07-25 22:50:57 +03:00
|
|
|
pub(crate) tags: Option<BTreeSet<Tag>>,
|
2024-08-09 20:53:30 +03:00
|
|
|
/// Task references derived from the event tags
|
|
|
|
refs: Vec<(String, EventId)>,
|
2024-08-08 21:10:17 +03:00
|
|
|
|
|
|
|
/// Reference to children, populated dynamically
|
|
|
|
pub(crate) children: HashSet<EventId>,
|
|
|
|
/// Events belonging to this task, such as state updates and notes
|
|
|
|
pub(crate) props: BTreeSet<Event>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd<Self> for Task {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
self.event.partial_cmp(&other.event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Task {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
self.event.cmp(&other.event)
|
|
|
|
}
|
2024-07-19 01:23:25 +03:00
|
|
|
}
|
2024-07-26 21:15:50 +03:00
|
|
|
|
2024-07-19 01:23:25 +03:00
|
|
|
impl Task {
|
|
|
|
pub(crate) fn new(event: Event) -> Task {
|
2024-08-08 21:10:17 +03:00
|
|
|
let (refs, tags) = event.tags.iter().partition_map(|tag| match tag.as_standardized() {
|
2024-08-09 20:53:30 +03:00
|
|
|
Some(TagStandard::Event { event_id, marker, .. }) => Left((marker.as_ref().map_or(MARKER_PARENT.to_string(), |m| m.to_string()), event_id.clone())),
|
2024-07-30 09:02:56 +03:00
|
|
|
_ => Right(tag.clone()),
|
2024-07-29 16:13:40 +03:00
|
|
|
});
|
2024-08-08 21:10:17 +03:00
|
|
|
// Separate refs for dependencies
|
2024-07-19 01:23:25 +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()),
|
2024-08-09 20:53:30 +03:00
|
|
|
refs,
|
2024-07-25 22:40:35 +03:00
|
|
|
event,
|
2024-07-19 01:23:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 21:15:50 +03:00
|
|
|
pub(crate) fn get_id(&self) -> &EventId {
|
|
|
|
&self.event.id
|
|
|
|
}
|
|
|
|
|
2024-08-12 23:02:50 +03:00
|
|
|
pub(crate) fn find_refs<'a>(&'a self, marker: &'a str) -> impl Iterator<Item=&'a EventId> {
|
2024-08-09 20:53:30 +03:00
|
|
|
self.refs.iter().filter_map(move |(str, id)| Some(id).filter(|_| str == marker))
|
|
|
|
}
|
|
|
|
|
2024-08-01 14:07:40 +03:00
|
|
|
pub(crate) fn parent_id(&self) -> Option<&EventId> {
|
2024-08-09 20:53:30 +03:00
|
|
|
self.find_refs(MARKER_PARENT).next()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get_dependendees(&self) -> Vec<&EventId> {
|
|
|
|
self.find_refs(MARKER_DEPENDS).collect()
|
2024-07-19 01:23:25 +03:00
|
|
|
}
|
|
|
|
|
2024-07-29 13:32:47 +03: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-08-08 00:18:34 +03:00
|
|
|
pub(crate) fn description_events(&self) -> impl Iterator<Item=&Event> + '_ {
|
2024-07-19 01:23:25 +03:00
|
|
|
self.props.iter().filter_map(|event| {
|
|
|
|
if event.kind == Kind::TextNote {
|
2024-08-08 00:18:34 +03:00
|
|
|
Some(event)
|
2024-07-19 01:23:25 +03:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-08-08 13:52:02 +03:00
|
|
|
|
2024-08-08 00:18:34 +03:00
|
|
|
pub(crate) fn descriptions(&self) -> impl Iterator<Item=&String> + '_ {
|
|
|
|
self.description_events().map(|e| &e.content)
|
|
|
|
}
|
2024-07-19 01:23:25 +03:00
|
|
|
|
2024-08-18 22:24:14 +03:00
|
|
|
pub(crate) fn is_task(&self) -> bool {
|
|
|
|
self.event.kind.as_u16() == TASK_KIND ||
|
|
|
|
self.states().next().is_some()
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:08:33 +03:00
|
|
|
fn states(&self) -> impl Iterator<Item=TaskState> + '_ {
|
2024-07-19 01:23:25 +03:00
|
|
|
self.props.iter().filter_map(|event| {
|
2024-07-26 11:07:47 +03:00
|
|
|
event.kind.try_into().ok().map(|s| TaskState {
|
2024-08-06 22:11:34 +03:00
|
|
|
name: some_non_empty(&event.content),
|
2024-07-19 21:06:03 +03:00
|
|
|
state: s,
|
|
|
|
time: event.created_at.clone(),
|
|
|
|
})
|
2024-07-19 01:23:25 +03:00
|
|
|
})
|
|
|
|
}
|
2024-08-06 22:11:34 +03:00
|
|
|
|
2024-07-26 11:07:47 +03:00
|
|
|
pub(crate) fn state(&self) -> Option<TaskState> {
|
2024-07-19 01:23:25 +03: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-08-01 14:07:40 +03:00
|
|
|
pub(crate) fn state_or_default(&self) -> TaskState {
|
|
|
|
self.state().unwrap_or_else(|| self.default_state())
|
2024-07-26 21:15:50 +03:00
|
|
|
}
|
|
|
|
|
2024-07-19 01:23:25 +03:00
|
|
|
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,
|
|
|
|
{
|
2024-07-29 14:13:32 +03:00
|
|
|
self.tags.as_ref().map(|tags| {
|
|
|
|
tags.into_iter()
|
|
|
|
.filter(predicate)
|
|
|
|
.map(|t| format!("{}", t.content().unwrap()))
|
|
|
|
.join(" ")
|
|
|
|
})
|
|
|
|
}
|
2024-07-19 01:23:25 +03:00
|
|
|
|
|
|
|
pub(crate) fn get(&self, property: &str) -> Option<String> {
|
|
|
|
match property {
|
2024-08-18 21:54:05 +03:00
|
|
|
// Static
|
2024-07-19 01:23:25 +03:00
|
|
|
"id" => Some(self.event.id.to_string()),
|
|
|
|
"parentid" => self.parent_id().map(|i| i.to_string()),
|
|
|
|
"name" => Some(self.event.content.clone()),
|
2024-08-18 21:54:05 +03:00
|
|
|
"pubkey" => Some(self.event.pubkey.to_string()),
|
|
|
|
"created" => Some(local_datetimestamp(&self.event.created_at)),
|
|
|
|
// Dynamic
|
|
|
|
"status" => Some(self.state_or_default().get_label()),
|
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-08-10 18:12:31 +03:00
|
|
|
"refs" => Some(format!("{:?}", self.refs.iter().map(|re| format!("{}: {}", re.0, re.1)).collect_vec())),
|
2024-07-25 10:55:29 +03:00
|
|
|
"props" => Some(format!(
|
|
|
|
"{:?}",
|
|
|
|
self.props
|
|
|
|
.iter()
|
2024-08-06 22:11:34 +03:00
|
|
|
.map(|e| format!("{} kind {} \"{}\"", e.created_at, e.kind, e.content))
|
2024-08-08 00:18:34 +03:00
|
|
|
.collect_vec()
|
2024-07-25 10:55:29 +03:00
|
|
|
)),
|
2024-07-30 09:02:56 +03:00
|
|
|
"descriptions" => Some(format!(
|
|
|
|
"{:?}",
|
2024-08-08 00:18:34 +03:00
|
|
|
self.descriptions().collect_vec()
|
2024-07-30 09:02:56 +03:00
|
|
|
)),
|
2024-07-19 01:23:25 +03:00
|
|
|
_ => {
|
2024-07-29 21:06:23 +03:00
|
|
|
warn!("Unknown task property {}", property);
|
2024-07-19 01:23:25 +03:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 11:07:47 +03:00
|
|
|
pub(crate) struct TaskState {
|
2024-08-10 21:25:46 +03:00
|
|
|
pub(crate) state: State,
|
2024-07-26 21:45:29 +03:00
|
|
|
name: Option<String>,
|
2024-07-30 08:23:32 +03:00
|
|
|
pub(crate) time: Timestamp,
|
2024-07-19 01:23:25 +03:00
|
|
|
}
|
2024-07-26 11:07:47 +03:00
|
|
|
impl TaskState {
|
2024-08-06 22:11:34 +03:00
|
|
|
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())
|
|
|
|
}
|
2024-08-10 21:25:46 +03:00
|
|
|
pub(crate) fn get_colored_label(&self) -> ColoredString {
|
|
|
|
self.state.colorize(&self.get_label())
|
|
|
|
}
|
2024-07-26 21:15:50 +03:00
|
|
|
pub(crate) fn matches_label(&self, label: &str) -> bool {
|
2024-08-06 22:11:34 +03:00
|
|
|
self.name.as_ref().is_some_and(|n| n.eq_ignore_ascii_case(label))
|
2024-07-29 09:43:01 +03:00
|
|
|
|| self.state.to_string().eq_ignore_ascii_case(label)
|
2024-07-26 21:15:50 +03:00
|
|
|
}
|
2024-07-26 11:07:47 +03:00
|
|
|
}
|
2024-07-30 17:13:29 +03:00
|
|
|
impl Display for TaskState {
|
2024-07-19 01:23:25 +03:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2024-07-29 14:16:37 +03:00
|
|
|
let state_str = self.state.to_string();
|
2024-07-19 01:23:25 +03:00
|
|
|
write!(
|
|
|
|
f,
|
2024-07-29 14:16:37 +03:00
|
|
|
"{}",
|
2024-07-19 01:23:25 +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))
|
2024-07-19 01:23:25 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-10 21:25:46 +03:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Ord, PartialOrd, Eq)]
|
2024-07-19 01:23:25 +03:00
|
|
|
pub(crate) enum State {
|
2024-08-18 21:37:39 +03:00
|
|
|
/// Actionable
|
|
|
|
Open = 1630,
|
|
|
|
/// Completed
|
2024-07-19 01:23:25 +03:00
|
|
|
Done,
|
2024-08-18 21:37:39 +03:00
|
|
|
/// Not Actionable (anymore)
|
2024-08-10 15:14:09 +03:00
|
|
|
Closed,
|
2024-08-18 21:37:39 +03:00
|
|
|
/// Temporarily not actionable
|
2024-08-10 15:14:09 +03:00
|
|
|
Pending,
|
2024-08-18 21:37:39 +03:00
|
|
|
/// Actionable ordered task list
|
|
|
|
Procedure = PROCEDURE_KIND as isize,
|
2024-07-19 01:23:25 +03:00
|
|
|
}
|
2024-08-10 18:16:21 +03:00
|
|
|
impl From<&str> for State {
|
|
|
|
fn from(value: &str) -> Self {
|
|
|
|
match value {
|
|
|
|
"Closed" => State::Closed,
|
|
|
|
"Done" => State::Done,
|
|
|
|
"Pending" => State::Pending,
|
|
|
|
"Proc" | "Procedure" | "List" => State::Procedure,
|
|
|
|
_ => State::Open,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-26 11:07:47 +03:00
|
|
|
impl TryFrom<Kind> for State {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn try_from(value: Kind) -> Result<Self, Self::Error> {
|
2024-08-08 21:10:17 +03:00
|
|
|
match value.as_u16() {
|
2024-07-26 11:07:47 +03:00
|
|
|
1630 => Ok(State::Open),
|
|
|
|
1631 => Ok(State::Done),
|
|
|
|
1632 => Ok(State::Closed),
|
2024-08-08 21:10:17 +03:00
|
|
|
1633 => Ok(State::Pending),
|
|
|
|
PROCEDURE_KIND => Ok(State::Procedure),
|
2024-07-26 11:07:47 +03:00
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-19 01:23:25 +03:00
|
|
|
impl State {
|
2024-07-30 17:11:43 +03:00
|
|
|
pub(crate) fn is_open(&self) -> bool {
|
|
|
|
match self {
|
2024-08-10 18:16:21 +03:00
|
|
|
State::Open | State::Pending | State::Procedure => true,
|
2024-07-30 17:11:43 +03:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-06 11:34:18 +03:00
|
|
|
pub(crate) fn kind(&self) -> u16 {
|
2024-07-19 01:23:25 +03:00
|
|
|
match self {
|
2024-07-30 20:25:27 +03:00
|
|
|
State::Open => 1630,
|
|
|
|
State::Done => 1631,
|
|
|
|
State::Closed => 1632,
|
2024-08-08 21:10:17 +03:00
|
|
|
State::Pending => 1633,
|
|
|
|
State::Procedure => PROCEDURE_KIND,
|
2024-07-19 01:23:25 +03:00
|
|
|
}
|
|
|
|
}
|
2024-08-10 21:25:46 +03:00
|
|
|
|
|
|
|
pub(crate) fn colorize(&self, str: &str) -> ColoredString {
|
|
|
|
match self {
|
|
|
|
State::Open => str.green(),
|
|
|
|
State::Done => str.bright_black(),
|
|
|
|
State::Closed => str.magenta(),
|
|
|
|
State::Pending => str.yellow(),
|
|
|
|
State::Procedure => str.blue(),
|
|
|
|
}
|
|
|
|
}
|
2024-07-19 01:23:25 +03:00
|
|
|
}
|
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 {
|
2024-07-19 01:23:25 +03:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|