forked from janek/mostr
refactor: put kinds and helpers in own module
This commit is contained in:
parent
aa468f80c5
commit
8c2c279238
|
@ -0,0 +1,42 @@
|
||||||
|
use itertools::Itertools;
|
||||||
|
use log::info;
|
||||||
|
use nostr_sdk::{Alphabet, EventBuilder, EventId, GenericTagValue, Kind, Tag};
|
||||||
|
|
||||||
|
pub const TASK_KIND: u64 = 1621;
|
||||||
|
pub const TRACKING_KIND: u64 = 1650;
|
||||||
|
|
||||||
|
pub(crate) fn build_tracking<I>(id: I) -> EventBuilder
|
||||||
|
where I: IntoIterator<Item=EventId> {
|
||||||
|
EventBuilder::new(
|
||||||
|
Kind::from(TRACKING_KIND),
|
||||||
|
"",
|
||||||
|
id.into_iter().map(|id| Tag::event(id)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build_task(name: &str, tags: Vec<Tag>) -> EventBuilder {
|
||||||
|
info!("Created task \"{name}\" with tags [{}]", tags.iter().map(|tag| format_tag(tag)).join(", "));
|
||||||
|
EventBuilder::new(Kind::from(TASK_KIND), name, tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_tag(tag: &Tag) -> String {
|
||||||
|
tag.content().map(|c| {
|
||||||
|
match c {
|
||||||
|
GenericTagValue::PublicKey(key) => format!("Key: {}", key.to_string()[..8].to_string()),
|
||||||
|
GenericTagValue::EventId(id) => format!("Parent: {}", id.to_string()[..8].to_string()),
|
||||||
|
GenericTagValue::String(str) => {
|
||||||
|
if is_hashtag(tag) {
|
||||||
|
format!("#{str}")
|
||||||
|
} else {
|
||||||
|
str
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).unwrap_or_else(|| format!("Kind {}", tag.kind()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn is_hashtag(tag: &Tag) -> bool {
|
||||||
|
tag.single_letter_tag()
|
||||||
|
.is_some_and(|sltag| sltag.character == Alphabet::T)
|
||||||
|
}
|
||||||
|
|
|
@ -15,14 +15,13 @@ use log::{debug, error, info, trace, warn};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use xdg::BaseDirectories;
|
use xdg::BaseDirectories;
|
||||||
|
|
||||||
|
use crate::kinds::{build_tracking, TRACKING_KIND};
|
||||||
use crate::task::State;
|
use crate::task::State;
|
||||||
use crate::tasks::Tasks;
|
use crate::tasks::Tasks;
|
||||||
|
|
||||||
mod task;
|
mod task;
|
||||||
mod tasks;
|
mod tasks;
|
||||||
|
mod kinds;
|
||||||
const TASK_KIND: u64 = 1621;
|
|
||||||
const TRACKING_KIND: u64 = 1650;
|
|
||||||
|
|
||||||
type Events = Vec<Event>;
|
type Events = Vec<Event>;
|
||||||
|
|
||||||
|
@ -315,7 +314,7 @@ async fn main() {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
Some(',') => tasks.add_note(arg),
|
Some(',') => tasks.make_note(arg),
|
||||||
|
|
||||||
Some('>') => {
|
Some('>') => {
|
||||||
tasks.update_state(arg, State::Done);
|
tasks.update_state(arg, State::Done);
|
||||||
|
|
|
@ -8,7 +8,7 @@ use itertools::Itertools;
|
||||||
use log::{debug, error, info, trace, warn};
|
use log::{debug, error, info, trace, warn};
|
||||||
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::kinds::is_hashtag;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub(crate) struct Task {
|
pub(crate) struct Task {
|
||||||
|
@ -132,11 +132,6 @@ impl Task {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn is_hashtag(tag: &Tag) -> bool {
|
|
||||||
tag.single_letter_tag()
|
|
||||||
.is_some_and(|sltag| sltag.character == Alphabet::T)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct TaskState {
|
pub(crate) struct TaskState {
|
||||||
state: State,
|
state: State,
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
|
|
36
src/tasks.rs
36
src/tasks.rs
|
@ -11,8 +11,9 @@ use log::{debug, error, info, trace, warn};
|
||||||
use nostr_sdk::{Event, EventBuilder, EventId, GenericTagValue, Kind, PublicKey, Tag, Timestamp};
|
use nostr_sdk::{Event, EventBuilder, EventId, GenericTagValue, Kind, PublicKey, Tag, Timestamp};
|
||||||
use nostr_sdk::Tag::Hashtag;
|
use nostr_sdk::Tag::Hashtag;
|
||||||
|
|
||||||
use crate::{EventSender, TASK_KIND, TRACKING_KIND};
|
use crate::EventSender;
|
||||||
use crate::task::{is_hashtag, State, Task};
|
use crate::kinds::*;
|
||||||
|
use crate::task::{State, Task};
|
||||||
|
|
||||||
type TaskMap = HashMap<EventId, Task>;
|
type TaskMap = HashMap<EventId, Task>;
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -447,13 +448,7 @@ impl Tasks {
|
||||||
self.flush();
|
self.flush();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.submit(
|
self.submit(build_tracking(id));
|
||||||
EventBuilder::new(
|
|
||||||
Kind::from(TRACKING_KIND),
|
|
||||||
"",
|
|
||||||
id.iter().map(|id| Tag::event(id.clone())),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
if !id.and_then(|id| self.tasks.get(&id)).is_some_and(|t| t.parent_id() == self.position.as_ref()) {
|
if !id.and_then(|id| self.tasks.get(&id)).is_some_and(|t| t.parent_id() == self.position.as_ref()) {
|
||||||
debug!("Flushing Tasks because of move");
|
debug!("Flushing Tasks because of move");
|
||||||
self.flush();
|
self.flush();
|
||||||
|
@ -567,7 +562,7 @@ impl Tasks {
|
||||||
.map(|id| self.set_state_for(id, comment, state));
|
.map(|id| self.set_state_for(id, comment, state));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_note(&mut self, note: &str) {
|
pub(crate) fn make_note(&mut self, note: &str) {
|
||||||
match self.position {
|
match self.position {
|
||||||
None => warn!("Cannot add note '{}' without active task", note),
|
None => warn!("Cannot add note '{}' without active task", note),
|
||||||
Some(id) => {
|
Some(id) => {
|
||||||
|
@ -593,27 +588,6 @@ fn display_time(format: &str, secs: u64) -> String {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_task(name: &str, tags: Vec<Tag>) -> EventBuilder {
|
|
||||||
info!("Created task \"{name}\" with tags [{}]", tags.iter().map(|tag| format_tag(tag)).join(", "));
|
|
||||||
EventBuilder::new(Kind::from(TASK_KIND), name, tags)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn format_tag(tag: &Tag) -> String {
|
|
||||||
tag.content().map(|c| {
|
|
||||||
match c {
|
|
||||||
GenericTagValue::PublicKey(key) => format!("Key: {}", key.to_string()[..8].to_string()),
|
|
||||||
GenericTagValue::EventId(id) => format!("Parent: {}", id.to_string()[..8].to_string()),
|
|
||||||
GenericTagValue::String(str) => {
|
|
||||||
if is_hashtag(tag) {
|
|
||||||
format!("#{str}")
|
|
||||||
} else {
|
|
||||||
str
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).unwrap_or_else(|| format!("Kind {}", tag.kind()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn join_tasks<'a>(
|
pub(crate) fn join_tasks<'a>(
|
||||||
iter: impl Iterator<Item=&'a Task>,
|
iter: impl Iterator<Item=&'a Task>,
|
||||||
include_last_id: bool,
|
include_last_id: bool,
|
||||||
|
|
Loading…
Reference in New Issue