chore: Move NostrEvent to crate::cli::types
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
@@ -14,16 +14,16 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
use std::{fs, str::FromStr};
|
use std::fs;
|
||||||
|
|
||||||
use clap::{ArgGroup, Args};
|
use clap::{ArgGroup, Args};
|
||||||
use futures::future;
|
use futures::future;
|
||||||
use nostr::{
|
use nostr::{
|
||||||
event::{Event, EventBuilder, EventId, Kind},
|
event::{Event, EventBuilder, Kind},
|
||||||
filter::Filter,
|
filter::Filter,
|
||||||
nips::{
|
nips::{
|
||||||
nip01::{Coordinate, Metadata},
|
nip01::{Coordinate, Metadata},
|
||||||
nip19::{self, FromBech32, ToBech32},
|
nip19::ToBech32,
|
||||||
},
|
},
|
||||||
types::RelayUrl,
|
types::RelayUrl,
|
||||||
};
|
};
|
||||||
@@ -32,7 +32,7 @@ use super::{CliOptions, CommandRunner};
|
|||||||
use crate::{
|
use crate::{
|
||||||
cli::{
|
cli::{
|
||||||
CliConfig,
|
CliConfig,
|
||||||
types::{NaddrOrSet, OptionNaddrOrSetVecExt, RelayOrSetVecExt},
|
types::{NaddrOrSet, NostrEvent, OptionNaddrOrSetVecExt, RelayOrSetVecExt},
|
||||||
},
|
},
|
||||||
error::{N34Error, N34Result},
|
error::{N34Error, N34Result},
|
||||||
nostr_utils::{
|
nostr_utils::{
|
||||||
@@ -47,43 +47,6 @@ const NPUB_LEN: usize = 63;
|
|||||||
/// The max date "9999-01-01 at 00:00 UTC"
|
/// The max date "9999-01-01 at 00:00 UTC"
|
||||||
const MAX_DATE: i64 = 253370764800;
|
const MAX_DATE: i64 = 253370764800;
|
||||||
|
|
||||||
/// Parses and represents a Nostr `nevent1` or `note1`.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
struct NostrEvent {
|
|
||||||
/// Unique identifier for the event.
|
|
||||||
event_id: EventId,
|
|
||||||
/// List of relay URLs associated with the event. Empty if parsing a
|
|
||||||
/// `note1`.
|
|
||||||
relays: Vec<RelayUrl>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NostrEvent {
|
|
||||||
/// Create a new [`NostrEvent`] instance
|
|
||||||
fn new(event_id: EventId, relays: Vec<RelayUrl>) -> Self {
|
|
||||||
Self { event_id, relays }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for NostrEvent {
|
|
||||||
type Err = String;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
let str_event = s.trim().trim_start_matches("nostr:");
|
|
||||||
if str_event.starts_with("nevent1") {
|
|
||||||
let event = nip19::Nip19Event::from_bech32(str_event).map_err(|e| e.to_string())?;
|
|
||||||
Ok(Self::new(event.event_id, event.relays))
|
|
||||||
} else if str_event.starts_with("note1") {
|
|
||||||
Ok(Self::new(
|
|
||||||
EventId::from_bech32(str_event).map_err(|e| e.to_string())?,
|
|
||||||
Vec::new(),
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
Err("Invalid event id, must starts with `note1` or `nevent1`".to_owned())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Arguments for the `reply` command
|
/// Arguments for the `reply` command
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
#[clap(
|
#[clap(
|
||||||
|
|||||||
@@ -17,8 +17,12 @@
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use nostr::{
|
use nostr::{
|
||||||
event::Kind,
|
event::{EventId, Kind},
|
||||||
nips::{self, nip01::Coordinate, nip19::Nip19Coordinate},
|
nips::{
|
||||||
|
self,
|
||||||
|
nip01::Coordinate,
|
||||||
|
nip19::{self, FromBech32, Nip19Coordinate},
|
||||||
|
},
|
||||||
types::RelayUrl,
|
types::RelayUrl,
|
||||||
};
|
};
|
||||||
use tokio::runtime::Handle;
|
use tokio::runtime::Handle;
|
||||||
@@ -47,6 +51,16 @@ pub enum RelayOrSet {
|
|||||||
Set(String),
|
Set(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses and represents a Nostr `nevent1` or `note1`.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct NostrEvent {
|
||||||
|
/// Unique identifier for the event.
|
||||||
|
pub event_id: EventId,
|
||||||
|
/// List of relay URLs associated with the event. Empty if parsing a
|
||||||
|
/// `note1`.
|
||||||
|
pub relays: Vec<RelayUrl>,
|
||||||
|
}
|
||||||
|
|
||||||
impl NaddrOrSet {
|
impl NaddrOrSet {
|
||||||
/// Returns the naddr if `Naddr` or try to get the relays from the set.
|
/// Returns the naddr if `Naddr` or try to get the relays from the set.
|
||||||
/// Returns error if the set naddrs are empty or the set not found.
|
/// Returns error if the set naddrs are empty or the set not found.
|
||||||
@@ -86,6 +100,13 @@ impl RelayOrSet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl NostrEvent {
|
||||||
|
/// Create a new [`NostrEvent`] instance
|
||||||
|
fn new(event_id: EventId, relays: Vec<RelayUrl>) -> Self {
|
||||||
|
Self { event_id, relays }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for NaddrOrSet {
|
impl FromStr for NaddrOrSet {
|
||||||
type Err = String;
|
type Err = String;
|
||||||
|
|
||||||
@@ -132,24 +153,23 @@ impl FromStr for RelayOrSet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_nip5_repo(nip5: &str, repo_id: &str) -> Result<NaddrOrSet, String> {
|
impl FromStr for NostrEvent {
|
||||||
let (username, domain) = nip5.split_once("@").unwrap_or(("_", nip5));
|
type Err = String;
|
||||||
|
|
||||||
let nip5_profile = tokio::task::block_in_place(|| {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
Handle::current().block_on(async {
|
let str_event = s.trim().trim_start_matches("nostr:");
|
||||||
nips::nip05::profile(format!("{username}@{domain}"), None)
|
if str_event.starts_with("nevent1") {
|
||||||
.await
|
let event = nip19::Nip19Event::from_bech32(str_event).map_err(|e| e.to_string())?;
|
||||||
.map_err(|err| err.to_string())
|
Ok(Self::new(event.event_id, event.relays))
|
||||||
})
|
} else if str_event.starts_with("note1") {
|
||||||
})?;
|
Ok(Self::new(
|
||||||
|
EventId::from_bech32(str_event).map_err(|e| e.to_string())?,
|
||||||
Ok(NaddrOrSet::Naddr(
|
Vec::new(),
|
||||||
Nip19Coordinate::new(
|
))
|
||||||
Coordinate::new(Kind::GitRepoAnnouncement, nip5_profile.public_key).identifier(repo_id),
|
} else {
|
||||||
nip5_profile.relays,
|
Err("Invalid event id, must starts with `note1` or `nevent1`".to_owned())
|
||||||
)
|
}
|
||||||
.expect("The relays is `RelayUrl`"),
|
}
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[easy_ext::ext(NaddrOrSetVecExt)]
|
#[easy_ext::ext(NaddrOrSetVecExt)]
|
||||||
@@ -192,3 +212,23 @@ impl Option<Vec<NaddrOrSet>> {
|
|||||||
.transpose()
|
.transpose()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_nip5_repo(nip5: &str, repo_id: &str) -> Result<NaddrOrSet, String> {
|
||||||
|
let (username, domain) = nip5.split_once("@").unwrap_or(("_", nip5));
|
||||||
|
|
||||||
|
let nip5_profile = tokio::task::block_in_place(|| {
|
||||||
|
Handle::current().block_on(async {
|
||||||
|
nips::nip05::profile(format!("{username}@{domain}"), None)
|
||||||
|
.await
|
||||||
|
.map_err(|err| err.to_string())
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(NaddrOrSet::Naddr(
|
||||||
|
Nip19Coordinate::new(
|
||||||
|
Coordinate::new(Kind::GitRepoAnnouncement, nip5_profile.public_key).identifier(repo_id),
|
||||||
|
nip5_profile.relays,
|
||||||
|
)
|
||||||
|
.expect("The relays is `RelayUrl`"),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user