forked from janek/mostr
1
0
Fork 0

refactor: put kinds and helpers in own module

This commit is contained in:
xeruf 2024-08-02 14:43:39 +03:00
parent aa468f80c5
commit 8c2c279238
4 changed files with 51 additions and 41 deletions

42
src/kinds.rs Normal file
View File

@ -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)
}

View File

@ -15,14 +15,13 @@ use log::{debug, error, info, trace, warn};
use nostr_sdk::prelude::*;
use xdg::BaseDirectories;
use crate::kinds::{build_tracking, TRACKING_KIND};
use crate::task::State;
use crate::tasks::Tasks;
mod task;
mod tasks;
const TASK_KIND: u64 = 1621;
const TRACKING_KIND: u64 = 1650;
mod kinds;
type Events = Vec<Event>;
@ -315,7 +314,7 @@ async fn main() {
}
},
Some(',') => tasks.add_note(arg),
Some(',') => tasks.make_note(arg),
Some('>') => {
tasks.update_state(arg, State::Done);

View File

@ -8,7 +8,7 @@ use itertools::Itertools;
use log::{debug, error, info, trace, warn};
use nostr_sdk::{Alphabet, Event, EventBuilder, EventId, Kind, Tag, Timestamp};
use crate::EventSender;
use crate::kinds::is_hashtag;
#[derive(Debug, Clone, PartialEq)]
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 {
state: State,
name: Option<String>,

View File

@ -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::Tag::Hashtag;
use crate::{EventSender, TASK_KIND, TRACKING_KIND};
use crate::task::{is_hashtag, State, Task};
use crate::EventSender;
use crate::kinds::*;
use crate::task::{State, Task};
type TaskMap = HashMap<EventId, Task>;
#[derive(Debug, Clone)]
@ -447,13 +448,7 @@ impl Tasks {
self.flush();
return;
}
self.submit(
EventBuilder::new(
Kind::from(TRACKING_KIND),
"",
id.iter().map(|id| Tag::event(id.clone())),
)
);
self.submit(build_tracking(id));
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");
self.flush();
@ -567,7 +562,7 @@ impl Tasks {
.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 {
None => warn!("Cannot add note '{}' without active task", note),
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>(
iter: impl Iterator<Item=&'a Task>,
include_last_id: bool,