refactor(task): cache parent id

This commit is contained in:
xeruf 2024-07-29 16:13:40 +03:00
parent 14575813c7
commit 1824de8d69
3 changed files with 27 additions and 15 deletions

10
Cargo.lock generated
View File

@ -681,6 +681,15 @@ version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3"
[[package]]
name = "itertools"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.11" version = "1.0.11"
@ -765,6 +774,7 @@ dependencies = [
name = "mostr" name = "mostr"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"itertools",
"nostr-sdk", "nostr-sdk",
"once_cell", "once_cell",
"tokio", "tokio",

View File

@ -16,3 +16,4 @@ nostr-sdk = "0.30"
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] } tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
once_cell = "1.19.0" once_cell = "1.19.0"
xdg = "2.5.2" xdg = "2.5.2"
itertools = "0.12.1"

View File

@ -2,6 +2,8 @@ use std::collections::{BTreeSet, HashSet};
use std::fmt; use std::fmt;
use std::ops::Div; use std::ops::Div;
use itertools::Either::{Left, Right};
use itertools::Itertools;
use nostr_sdk::{Alphabet, Event, EventBuilder, EventId, Kind, Tag, Timestamp}; use nostr_sdk::{Alphabet, Event, EventBuilder, EventId, Kind, Tag, Timestamp};
use crate::EventSender; use crate::EventSender;
@ -13,18 +15,22 @@ pub(crate) struct Task {
pub(crate) props: BTreeSet<Event>, pub(crate) props: BTreeSet<Event>,
/// Cached sorted tags of the event /// Cached sorted tags of the event
pub(crate) tags: Option<BTreeSet<Tag>>, pub(crate) tags: Option<BTreeSet<Tag>>,
parents: Vec<EventId>,
} }
impl Task { impl Task {
pub(crate) fn new(event: Event) -> Task { pub(crate) fn new(event: Event) -> Task {
let (parents, tags) = event.tags.iter().partition_map(|tag| {
match tag {
Tag::Event { event_id, .. } => return Left(event_id),
_ => Right(tag.clone())
}
});
Task { Task {
children: Default::default(), children: Default::default(),
props: Default::default(), props: Default::default(),
tags: if event.tags.is_empty() { tags: Some(tags).filter(|t: &BTreeSet<Tag>| !t.is_empty()),
None parents,
} else {
Some(event.tags.iter().cloned().collect())
},
event, event,
} }
} }
@ -34,13 +40,7 @@ impl Task {
} }
pub(crate) fn parent_id(&self) -> Option<EventId> { pub(crate) fn parent_id(&self) -> Option<EventId> {
for tag in self.event.tags.iter() { self.parents.first().cloned()
match tag {
Tag::Event { event_id, .. } => return Some(*event_id),
_ => {}
}
}
None
} }
pub(crate) fn get_title(&self) -> String { pub(crate) fn get_title(&self) -> String {
@ -147,7 +147,8 @@ impl Task {
"name" => Some(self.event.content.clone()), "name" => Some(self.event.content.clone()),
"time" => Some(format!("{}m", self.time_tracked().div(60))), "time" => Some(format!("{}m", self.time_tracked().div(60))),
"hashtags" => self.filter_tags(|tag| tag.single_letter_tag().is_some_and(|sltag| sltag.character == Alphabet::T)), "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)), "tags" => self.filter_tags(|_| true),
"alltags" => Some(format!("{:?}", self.tags)),
"props" => Some(format!( "props" => Some(format!(
"{:?}", "{:?}",
self.props self.props