forked from janek/mostr
1
0
Fork 0

refactor: use static Kinds rather than their ids

This commit is contained in:
xeruf 2024-08-29 22:12:55 +03:00
parent 003d1d6120
commit c2b106ea69
4 changed files with 54 additions and 46 deletions

View File

@ -5,26 +5,27 @@ use nostr_sdk::TagStandard::Hashtag;
use crate::task::{MARKER_PARENT, State}; use crate::task::{MARKER_PARENT, State};
pub const METADATA_KIND: u16 = 0; pub const TASK_KIND: Kind = Kind::GitIssue;
pub const NOTE_KIND: u16 = 1; pub const PROCEDURE_KIND_ID: u16 = 1639;
pub const TASK_KIND: u16 = 1621; pub const PROCEDURE_KIND: Kind = Kind::Regular(PROCEDURE_KIND_ID);
pub const TRACKING_KIND: u16 = 1650; pub const TRACKING_KIND: Kind = Kind::Regular(1650);
pub const KINDS: [u16; 3] = [ pub const BASIC_KINDS: [Kind; 4] = [
METADATA_KIND, Kind::Metadata,
NOTE_KIND, Kind::TextNote,
TASK_KIND, TASK_KIND,
Kind::Bookmarks,
]; ];
pub const PROP_KINDS: [u16; 6] = [ pub const PROP_KINDS: [Kind; 6] = [
TRACKING_KIND, TRACKING_KIND,
State::Open as u16, Kind::GitStatusOpen,
State::Done as u16, Kind::GitStatusApplied,
State::Closed as u16, Kind::GitStatusClosed,
State::Pending as u16, Kind::GitStatusDraft,
State::Procedure as u16 PROCEDURE_KIND,
]; ];
// TODO: use formatting - bold / heading / italics - and generate from code
/// Helper for available properties. /// Helper for available properties.
/// TODO: use formatting - bold / heading / italics - and generate from code
pub const PROPERTY_COLUMNS: &str = pub const PROPERTY_COLUMNS: &str =
"# Available Properties "# Available Properties
Immutable: Immutable:
@ -66,7 +67,7 @@ pub(crate) fn build_task(name: &str, tags: Vec<Tag>, kind: Option<(&str, Kind)>)
info!("Created {}task \"{name}\" with tags [{}]", info!("Created {}task \"{name}\" with tags [{}]",
kind.map(|k| k.0).unwrap_or_default(), kind.map(|k| k.0).unwrap_or_default(),
tags.iter().map(format_tag).join(", ")); tags.iter().map(format_tag).join(", "));
EventBuilder::new(kind.map(|k| k.1).unwrap_or(Kind::from(TASK_KIND)), name, tags) EventBuilder::new(kind.map(|k| k.1).unwrap_or(TASK_KIND), name, tags)
} }
pub(crate) fn build_prop( pub(crate) fn build_prop(

View File

@ -28,7 +28,7 @@ use tokio::time::timeout;
use xdg::BaseDirectories; use xdg::BaseDirectories;
use crate::helpers::*; use crate::helpers::*;
use crate::kinds::{KINDS, PROP_KINDS, PROPERTY_COLUMNS, TRACKING_KIND}; use crate::kinds::{BASIC_KINDS, PROP_KINDS, PROPERTY_COLUMNS, TRACKING_KIND};
use crate::task::{MARKER_DEPENDS, State}; use crate::task::{MARKER_DEPENDS, State};
use crate::tasks::{PropertyCollection, StateFilter, Tasks}; use crate::tasks::{PropertyCollection, StateFilter, Tasks};
@ -95,9 +95,9 @@ impl EventSender {
} }
let mut queue = self.queue.borrow_mut(); let mut queue = self.queue.borrow_mut();
Ok(event_builder.to_event(&self.keys).inspect(|event| { Ok(event_builder.to_event(&self.keys).inspect(|event| {
if event.kind.as_u16() == TRACKING_KIND { if event.kind == TRACKING_KIND {
queue.retain(|e| { queue.retain(|e| {
e.kind.as_u16() != TRACKING_KIND e.kind != TRACKING_KIND
}); });
} }
queue.push(event.clone()); queue.push(event.clone());
@ -115,7 +115,7 @@ impl EventSender {
} }
/// Sends all pending events if there is a non-tracking event /// Sends all pending events if there is a non-tracking event
fn flush(&self) { fn flush(&self) {
if self.queue.borrow().iter().any(|event| event.kind.as_u16() != TRACKING_KIND) { if self.queue.borrow().iter().any(|event| event.kind != TRACKING_KIND) {
self.force_flush() self.force_flush()
} }
} }
@ -240,14 +240,10 @@ async fn main() -> Result<()> {
let mut notifications = client.notifications(); let mut notifications = client.notifications();
client.connect().await; client.connect().await;
let sub1 = client.subscribe(vec![ let sub1 = client.subscribe(vec![Filter::new().kinds(BASIC_KINDS)], None).await;
Filter::new().kinds(KINDS.into_iter().map(Kind::from))
], None).await;
info!("Subscribed to tasks with {:?}", sub1); info!("Subscribed to tasks with {:?}", sub1);
let sub2 = client.subscribe(vec![ let sub2 = client.subscribe(vec![Filter::new().kinds(PROP_KINDS)], None).await;
Filter::new().kinds(PROP_KINDS.into_iter().map(Kind::from))
], None).await;
info!("Subscribed to updates with {:?}", sub2); info!("Subscribed to updates with {:?}", sub2);
let metadata = var("USER").ok().map( let metadata = var("USER").ok().map(

View File

@ -11,7 +11,7 @@ use log::{debug, error, info, trace, warn};
use nostr_sdk::{Event, EventId, Kind, Tag, TagStandard, Timestamp}; use nostr_sdk::{Event, EventId, Kind, Tag, TagStandard, Timestamp};
use crate::helpers::{format_timestamp_local, some_non_empty}; use crate::helpers::{format_timestamp_local, some_non_empty};
use crate::kinds::{is_hashtag, TASK_KIND}; use crate::kinds::{is_hashtag, PROCEDURE_KIND, PROCEDURE_KIND_ID, TASK_KIND};
pub static MARKER_PARENT: &str = "parent"; pub static MARKER_PARENT: &str = "parent";
pub static MARKER_DEPENDS: &str = "depends"; pub static MARKER_DEPENDS: &str = "depends";
@ -91,7 +91,7 @@ impl Task {
} }
pub(crate) fn is_task(&self) -> bool { pub(crate) fn is_task(&self) -> bool {
self.event.kind.as_u16() == TASK_KIND || self.event.kind == TASK_KIND ||
self.states().next().is_some() self.states().next().is_some()
} }
@ -219,7 +219,6 @@ impl Display for TaskState {
} }
} }
pub const PROCEDURE_KIND: u16 = 1639;
#[derive(Debug, Copy, Clone, PartialEq, Ord, PartialOrd, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Ord, PartialOrd, Eq)]
pub(crate) enum State { pub(crate) enum State {
/// Actionable /// Actionable
@ -231,7 +230,7 @@ pub(crate) enum State {
/// Temporarily not actionable /// Temporarily not actionable
Pending, Pending,
/// Actionable ordered task list /// Actionable ordered task list
Procedure = PROCEDURE_KIND as isize, Procedure = PROCEDURE_KIND_ID as isize,
} }
impl TryFrom<&str> for State { impl TryFrom<&str> for State {
type Error = (); type Error = ();
@ -251,13 +250,18 @@ impl TryFrom<Kind> for State {
type Error = (); type Error = ();
fn try_from(value: Kind) -> Result<Self, Self::Error> { fn try_from(value: Kind) -> Result<Self, Self::Error> {
match value.as_u16() { match value {
1630 => Ok(State::Open), Kind::GitStatusOpen => Ok(State::Open),
1631 => Ok(State::Done), Kind::GitStatusApplied => Ok(State::Done),
1632 => Ok(State::Closed), Kind::GitStatusClosed => Ok(State::Closed),
1633 => Ok(State::Pending), Kind::GitStatusDraft => Ok(State::Pending),
PROCEDURE_KIND => Ok(State::Procedure), _ => {
_ => Err(()), if value == PROCEDURE_KIND {
Ok(State::Procedure)
} else {
Err(())
}
}
} }
} }
} }

View File

@ -800,19 +800,26 @@ impl Tasks {
} }
pub(crate) fn add(&mut self, event: Event) { pub(crate) fn add(&mut self, event: Event) {
match event.kind.as_u16() { match event.kind {
TASK_KIND => self.add_task(event), Kind::GitIssue => self.add_task(event),
TRACKING_KIND => Kind::Metadata =>
match self.history.get_mut(&event.pubkey) {
Some(c) => { c.insert(event.created_at, event); }
None => { self.history.insert(event.pubkey, BTreeMap::from([(event.created_at, event)])); }
},
METADATA_KIND =>
match Metadata::from_json(event.content()) { match Metadata::from_json(event.content()) {
Ok(metadata) => { self.users.insert(event.pubkey, metadata); } Ok(metadata) => { self.users.insert(event.pubkey, metadata); }
Err(e) => warn!("Cannot parse metadata: {} from {:?}", e, event) Err(e) => warn!("Cannot parse metadata: {} from {:?}", e, event)
} }
_ => self.add_prop(event), Kind::Bookmarks => {
//referenced_events(event)
}
_ => {
if event.kind == TRACKING_KIND {
match self.history.get_mut(&event.pubkey) {
Some(c) => { c.insert(event.created_at, event); }
None => { self.history.insert(event.pubkey, BTreeMap::from([(event.created_at, event)])); }
}
} else {
self.add_prop(event)
}
}
} }
} }
@ -834,7 +841,7 @@ impl Tasks {
t.props.insert(event.clone()); t.props.insert(event.clone());
}); });
if !found { if !found {
if event.kind.as_u16() == NOTE_KIND { if event.kind.as_u16() == 1 {
self.add_task(event); self.add_task(event);
return; return;
} }