mostr/src/task.rs

273 lines
9 KiB
Rust
Raw Normal View History

2024-12-06 12:42:39 +01:00
mod state;
#[cfg(test)]
mod tests;
2024-07-30 17:13:29 +03:00
use fmt::Display;
use std::cmp::Ordering;
2024-12-06 12:42:39 +01:00
use std::collections::btree_set::Iter;
use std::collections::BTreeSet;
2024-07-19 21:06:03 +03:00
use std::fmt;
use std::hash::{Hash, Hasher};
2024-12-06 12:42:39 +01:00
use std::iter::{once, Chain, Once};
2024-11-25 02:29:23 +01:00
use std::str::FromStr;
2024-08-09 20:53:30 +03:00
use std::string::ToString;
2024-12-05 14:39:47 +01:00
use crate::hashtag::{is_hashtag, Hashtag};
use crate::helpers::{format_timestamp_local, some_non_empty};
use crate::kinds::{match_event_tag, Prio, PRIO, PROCEDURE_KIND, PROCEDURE_KIND_ID, TASK_KIND};
use crate::tasks::now;
2024-12-06 12:42:39 +01:00
pub use crate::task::state::State;
pub use crate::task::state::StateChange;
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-11-25 02:29:23 +01:00
use nostr_sdk::{Alphabet, Event, EventId, Kind, PublicKey, SingleLetterTag, Tag, TagKind, Timestamp};
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";
pub static MARKER_PROPERTY: &str = "property";
2024-08-09 20:53:30 +03:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Task {
/// Event that defines this task
2024-12-06 12:42:39 +01:00
pub(super) event: Event, // TODO make private
/// Cached sorted tags of the event with references removed
tags: Option<BTreeSet<Tag>>,
2024-08-09 20:53:30 +03:00
/// Task references derived from the event tags
refs: Vec<(String, 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)
}
}
impl Hash for Task {
fn hash<H: Hasher>(&self, state: &mut H) {
self.event.id.hash(state);
}
}
impl Task {
pub(crate) fn new(event: Event) -> Task {
let (refs, tags) = event.tags.iter().partition_map(|tag| if let Some(et) = match_event_tag(tag) {
Left((et.marker.as_ref().map_or(MARKER_PARENT.to_string(), |m| m.to_string()), et.id))
} else {
Right(tag.clone())
2024-07-29 16:13:40 +03:00
});
// Separate refs for dependencies
Task {
2024-07-24 16:03:34 +03:00
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-12-06 12:42:39 +01:00
/// All Events including the task and its props in chronological order
pub(crate) fn all_events(&self) -> impl DoubleEndedIterator<Item=&Event> {
once(&self.event).chain(self.props.iter().rev())
}
pub(crate) fn get_id(&self) -> EventId {
self.event.id
}
2024-11-25 14:07:08 +01:00
pub(crate) fn get_participants(&self) -> impl Iterator<Item=PublicKey> + '_ {
2024-11-25 02:29:23 +01:00
self.tags()
.filter(|t| t.kind() == TagKind::p())
2024-11-25 14:07:08 +01:00
.filter_map(|t| t.content()
2024-11-25 02:29:23 +01:00
.and_then(|c| PublicKey::from_str(c).inspect_err(|e| warn!("Unparseable pubkey in {:?}", t)).ok()))
}
pub(crate) fn get_assignee(&self) -> Option<PublicKey> {
2024-11-25 14:07:08 +01:00
self.get_participants().next()
}
pub(crate) fn get_owner(&self) -> PublicKey {
self.get_assignee()
2024-11-25 14:07:08 +01:00
.unwrap_or_else(|| self.event.pubkey)
}
2024-11-25 02:29:23 +01:00
2024-08-29 22:15:30 +03:00
/// Trimmed event content or stringified id
pub(crate) fn get_title(&self) -> String {
some_non_empty(self.event.content.trim())
.unwrap_or_else(|| self.get_id().to_string())
}
2024-12-06 12:42:39 +01:00
/// Title with leading hashtags removed
pub(crate) fn get_filter_title(&self) -> String {
self.event.content.trim().trim_start_matches('#').to_string()
}
2024-12-06 12:42:39 +01:00
pub(crate) fn find_refs<'a>(&'a self, marker: &'a str) -> impl Iterator<Item=&'a EventId> {
self.refs.iter().filter_map(move |(str, id)|
Some(id).filter(|_| str == marker))
}
pub(crate) fn parent_id(&self) -> Option<&EventId> {
self.find_refs(MARKER_PARENT).next()
}
pub(crate) fn find_dependents(&self) -> Vec<&EventId> {
self.find_refs(MARKER_DEPENDS).collect()
}
2024-11-20 23:10:28 +01:00
fn description_events(&self) -> impl DoubleEndedIterator<Item=&Event> + '_ {
2024-08-25 14:46:07 +03:00
self.props.iter().filter(|event| event.kind == Kind::TextNote)
}
2024-08-08 13:52:02 +03:00
2024-11-20 23:10:28 +01:00
/// Description items, ordered newest to oldest
pub(crate) fn descriptions(&self) -> impl DoubleEndedIterator<Item=&String> + '_ {
self.description_events()
.filter_map(|e| Some(&e.content).take_if(|s| !s.trim().is_empty()))
}
pub(crate) fn is_task_kind(&self) -> bool {
self.event.kind == TASK_KIND
}
/// Whether this is an actionable task - false if stateless activity
2024-08-18 22:24:14 +03:00
pub(crate) fn is_task(&self) -> bool {
self.is_task_kind() ||
2024-10-12 14:17:46 +02:00
self.props.iter().any(|event| State::try_from(event.kind).is_ok())
2024-08-18 22:24:14 +03:00
}
pub(crate) fn priority(&self) -> Option<Prio> {
self.priority_raw().and_then(|s| s.parse().ok())
}
pub(crate) fn priority_raw(&self) -> Option<&str> {
self.props.iter()
.chain(once(&self.event))
.find_map(|p| {
p.tags.iter().find_map(|t|
t.content().take_if(|_| { t.kind().to_string() == PRIO }))
})
}
2024-12-06 12:42:39 +01:00
fn states(&self) -> impl DoubleEndedIterator<Item=StateChange> + '_ {
self.props.iter().filter_map(|event| {
2024-12-06 12:42:39 +01:00
event.kind.try_into().ok().map(|s| StateChange {
name: some_non_empty(&event.content),
2024-07-19 21:06:03 +03:00
state: s,
2024-08-25 14:46:07 +03:00
time: event.created_at,
2024-07-19 21:06:03 +03:00
})
})
}
2024-11-23 08:47:54 +01:00
pub fn last_state_update(&self) -> Timestamp {
self.state().map(|s| s.time).unwrap_or(self.event.created_at)
}
2024-12-05 14:39:47 +01:00
2024-12-06 12:42:39 +01:00
pub fn state_at(&self, time: Timestamp) -> Option<StateChange> {
// TODO do not iterate constructed state objects
2024-11-23 08:47:54 +01:00
let state = self.states().take_while_inclusive(|ts| ts.time > time);
state.last().map(|ts| {
2024-11-23 08:47:54 +01:00
if ts.time <= time {
ts
} else {
self.default_state()
}
})
}
2024-11-23 08:47:54 +01:00
/// Returns the current state if this is a task rather than an activity
2024-12-06 12:42:39 +01:00
pub fn state(&self) -> Option<StateChange> {
2024-11-23 08:47:54 +01:00
let now = now();
self.state_at(now)
}
pub(crate) fn pure_state(&self) -> State {
2024-12-06 12:42:39 +01:00
State::from(self.state())
}
2024-12-06 12:42:39 +01:00
pub(crate) fn state_or_default(&self) -> StateChange {
2024-08-01 14:07:40 +03:00
self.state().unwrap_or_else(|| self.default_state())
}
/// Returns None for activities.
pub(crate) fn state_label(&self) -> Option<ColoredString> {
self.state()
.or_else(|| Some(self.default_state()).filter(|_| self.is_task()))
.map(|state| state.get_colored_label())
}
2024-12-06 12:42:39 +01:00
fn default_state(&self) -> StateChange {
StateChange {
name: None,
state: State::Open,
time: self.event.created_at,
}
}
pub(crate) fn list_hashtags(&self) -> impl Iterator<Item=Hashtag> + '_ {
2024-11-21 09:17:56 +01:00
self.tags().filter_map(|t| Hashtag::try_from(t).ok())
}
2024-11-25 02:29:23 +01:00
/// Tags of this task that are not event references, newest to oldest
fn tags(&self) -> impl Iterator<Item=&Tag> {
2024-11-25 02:29:23 +01:00
self.props.iter()
.flat_map(|e| e.tags.iter()
2024-12-05 14:39:47 +01:00
.filter(|t| t.single_letter_tag().is_none_or(|s| s.character != Alphabet::E)))
2024-11-25 02:29:23 +01:00
.chain(self.tags.iter().flatten())
}
fn join_tags<P>(&self, predicate: P) -> String
2024-07-30 09:02:56 +03:00
where
P: FnMut(&&Tag) -> bool,
{
self.tags()
.filter(predicate)
.map(|t| t.content().unwrap().to_string())
.sorted_unstable()
.dedup()
.join(" ")
}
pub(crate) fn get(&self, property: &str) -> Option<String> {
match property {
// Static
2024-12-06 12:42:39 +01:00
"id" => Some(self.get_id().to_string()),
"parentid" => self.parent_id().map(|i| i.to_string()),
"name" => Some(self.event.content.clone()),
"key" | "pubkey" => Some(self.event.pubkey.to_string()),
"created" => Some(format_timestamp_local(&self.event.created_at)),
2024-08-20 13:49:53 +03:00
"kind" => Some(self.event.kind.to_string()),
// Dynamic
2024-11-09 20:41:22 +01:00
"priority" => self.priority_raw().map(|c| c.to_string()),
"status" => self.state_label().map(|c| c.to_string()),
2024-11-20 23:10:28 +01:00
"desc" => self.descriptions().next().cloned(),
"description" => Some(self.descriptions().rev().join(" ")),
"hashtags" => Some(self.join_tags(|tag| { is_hashtag(tag) })),
"tags" => Some(self.join_tags(|_| true)), // TODO test these!
2024-07-29 16:13:40 +03:00
"alltags" => Some(format!("{:?}", self.tags)),
"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()
.map(|e| format!("{} kind {} \"{}\"", e.created_at, e.kind, e.content))
.collect_vec()
2024-07-25 10:55:29 +03:00
)),
"descriptions" => Some(format!("{:?}", self.descriptions().collect_vec())),
_ => {
2024-07-29 21:06:23 +03:00
warn!("Unknown task property {}", property);
None
}
}
}
}