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};
pub const METADATA_KIND: u16 = 0;
pub const NOTE_KIND: u16 = 1;
pub const TASK_KIND: u16 = 1621;
pub const TRACKING_KIND: u16 = 1650;
pub const KINDS: [u16; 3] = [
METADATA_KIND,
NOTE_KIND,
pub const TASK_KIND: Kind = Kind::GitIssue;
pub const PROCEDURE_KIND_ID: u16 = 1639;
pub const PROCEDURE_KIND: Kind = Kind::Regular(PROCEDURE_KIND_ID);
pub const TRACKING_KIND: Kind = Kind::Regular(1650);
pub const BASIC_KINDS: [Kind; 4] = [
Kind::Metadata,
Kind::TextNote,
TASK_KIND,
Kind::Bookmarks,
];
pub const PROP_KINDS: [u16; 6] = [
pub const PROP_KINDS: [Kind; 6] = [
TRACKING_KIND,
State::Open as u16,
State::Done as u16,
State::Closed as u16,
State::Pending as u16,
State::Procedure as u16
Kind::GitStatusOpen,
Kind::GitStatusApplied,
Kind::GitStatusClosed,
Kind::GitStatusDraft,
PROCEDURE_KIND,
];
// TODO: use formatting - bold / heading / italics - and generate from code
/// Helper for available properties.
/// TODO: use formatting - bold / heading / italics - and generate from code
pub const PROPERTY_COLUMNS: &str =
"# Available Properties
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 [{}]",
kind.map(|k| k.0).unwrap_or_default(),
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(

View File

@ -28,7 +28,7 @@ use tokio::time::timeout;
use xdg::BaseDirectories;
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::tasks::{PropertyCollection, StateFilter, Tasks};
@ -95,9 +95,9 @@ impl EventSender {
}
let mut queue = self.queue.borrow_mut();
Ok(event_builder.to_event(&self.keys).inspect(|event| {
if event.kind.as_u16() == TRACKING_KIND {
if event.kind == TRACKING_KIND {
queue.retain(|e| {
e.kind.as_u16() != TRACKING_KIND
e.kind != TRACKING_KIND
});
}
queue.push(event.clone());
@ -115,7 +115,7 @@ impl EventSender {
}
/// Sends all pending events if there is a non-tracking event
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()
}
}
@ -240,14 +240,10 @@ async fn main() -> Result<()> {
let mut notifications = client.notifications();
client.connect().await;
let sub1 = client.subscribe(vec![
Filter::new().kinds(KINDS.into_iter().map(Kind::from))
], None).await;
let sub1 = client.subscribe(vec![Filter::new().kinds(BASIC_KINDS)], None).await;
info!("Subscribed to tasks with {:?}", sub1);
let sub2 = client.subscribe(vec![
Filter::new().kinds(PROP_KINDS.into_iter().map(Kind::from))
], None).await;
let sub2 = client.subscribe(vec![Filter::new().kinds(PROP_KINDS)], None).await;
info!("Subscribed to updates with {:?}", sub2);
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 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_DEPENDS: &str = "depends";
@ -91,7 +91,7 @@ impl Task {
}
pub(crate) fn is_task(&self) -> bool {
self.event.kind.as_u16() == TASK_KIND ||
self.event.kind == TASK_KIND ||
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)]
pub(crate) enum State {
/// Actionable
@ -231,7 +230,7 @@ pub(crate) enum State {
/// Temporarily not actionable
Pending,
/// Actionable ordered task list
Procedure = PROCEDURE_KIND as isize,
Procedure = PROCEDURE_KIND_ID as isize,
}
impl TryFrom<&str> for State {
type Error = ();
@ -251,13 +250,18 @@ impl TryFrom<Kind> for State {
type Error = ();
fn try_from(value: Kind) -> Result<Self, Self::Error> {
match value.as_u16() {
1630 => Ok(State::Open),
1631 => Ok(State::Done),
1632 => Ok(State::Closed),
1633 => Ok(State::Pending),
PROCEDURE_KIND => Ok(State::Procedure),
_ => Err(()),
match value {
Kind::GitStatusOpen => Ok(State::Open),
Kind::GitStatusApplied => Ok(State::Done),
Kind::GitStatusClosed => Ok(State::Closed),
Kind::GitStatusDraft => Ok(State::Pending),
_ => {
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) {
match event.kind.as_u16() {
TASK_KIND => self.add_task(event),
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)])); }
},
METADATA_KIND =>
match event.kind {
Kind::GitIssue => self.add_task(event),
Kind::Metadata =>
match Metadata::from_json(event.content()) {
Ok(metadata) => { self.users.insert(event.pubkey, metadata); }
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());
});
if !found {
if event.kind.as_u16() == NOTE_KIND {
if event.kind.as_u16() == 1 {
self.add_task(event);
return;
}