From 54e870a93a4a900b34117f5bb43db992c7f14677 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Fri, 26 Jul 2024 21:15:50 +0300 Subject: [PATCH] feat(task): more accessors and modifiers --- src/task.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/task.rs b/src/task.rs index 442f739..6180475 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,7 +1,9 @@ use std::collections::{BTreeSet, HashSet}; use std::fmt; -use nostr_sdk::{Event, EventId, Kind, Tag, Timestamp}; +use nostr_sdk::{Event, EventBuilder, EventId, Kind, Tag, Timestamp}; + +use crate::EventSender; pub(crate) struct Task { pub(crate) event: Event, @@ -10,6 +12,7 @@ pub(crate) struct Task { /// Cached sorted tags of the event pub(crate) tags: Option>, } + impl Task { pub(crate) fn new(event: Event) -> Task { Task { @@ -24,6 +27,10 @@ impl Task { } } + pub(crate) fn get_id(&self) -> &EventId { + &self.event.id + } + pub(crate) fn parent_id(&self) -> Option { for tag in self.event.tags.iter() { match tag { @@ -66,6 +73,23 @@ impl Task { self.state().map_or(State::Open, |s| s.state) } + pub(crate) fn set_state( + &mut self, + sender: &EventSender, + state: State, + comment: &str, + ) -> Option { + sender + .submit(EventBuilder::new( + state.kind(), + comment, + vec![Tag::event(self.event.id)], + )) + .inspect(|e| { + self.props.insert(e.clone()); + }) + } + fn default_state(&self) -> TaskState { TaskState { name: None, @@ -136,6 +160,11 @@ impl TaskState { 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.state == State::Active + || self.name.as_ref().is_some_and(|n| n == label) + || self.state.to_string() == label + } } impl fmt::Display for TaskState { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {