forked from janek/mostr
1
0
Fork 0
mostr/src/task.rs

166 lines
4.4 KiB
Rust
Raw Normal View History

use crate::make_event;
2024-07-19 18:06:03 +00:00
use nostr_sdk::{Event, EventId, Kind, Tag, Timestamp};
use std::fmt;
pub(crate) struct Task {
pub(crate) event: Event,
pub(crate) children: Vec<EventId>,
pub(crate) props: Vec<Event>,
}
impl Task {
pub(crate) fn new(event: Event) -> Task {
Task {
event,
children: Vec::new(),
props: Vec::new(),
}
}
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
}
fn descriptions(&self) -> impl Iterator<Item = String> + '_ {
self.props.iter().filter_map(|event| {
if event.kind == Kind::TextNote {
Some(event.content.clone())
} else {
None
}
})
}
fn states(&self) -> impl Iterator<Item = TaskState> + '_ {
self.props.iter().filter_map(|event| {
match event.kind.as_u32() {
1630 => Some(State::Open),
1631 => Some(State::Done),
1632 => Some(State::Closed),
1633 => Some(State::Active),
_ => None,
}
2024-07-19 18:06:03 +00:00
.map(|s| TaskState {
name: if event.content.is_empty() {
None
} else {
Some(event.content.clone())
},
state: s,
time: event.created_at.clone(),
})
})
}
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)
}
pub(crate) fn update_state(&mut self, state: State, comment: &str) -> Event {
let event = make_event(
2024-07-18 22:37:39 +00:00
state.kind(),
comment,
&[Tag::event(self.event.id)],
);
self.props.push(event.clone());
event
2024-07-18 22:37:39 +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.
/// 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;
}
}
}
}
total
}
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-18 22:37:39 +00:00
"time" => Some(self.time_tracked().to_string()), // TODO: format properly
"desc" | "description" => self.descriptions().fold(None, |total, s| {
Some(match total {
None => s,
Some(i) => i + " " + &s,
})
}),
_ => {
eprintln!("Unknown column {}", property);
None
}
}
}
}
struct TaskState {
name: Option<String>,
state: State,
time: Timestamp,
}
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,
}
impl State {
fn kind(&self) -> Kind {
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)
}
}