commit 7a7a04da39c84281f3e174ceabae21450997e162
parent 85ce25f48d869e94e572c7fa9b4b5c0259e445ae
Author: William Casarin <jb55@jb55.com>
Date: Sun, 17 Dec 2023 13:26:21 -0800
nip19: add a few utils for getting filters and relays
A few utility functions for getting filters and relays from nip19
objects. We will use this for finding notes on other relays.
Diffstat:
1 file changed, 32 insertions(+), 0 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -65,6 +65,38 @@ fn render_note<'a>(_app_ctx: &Context, note: &'a Note) -> Vec<u8> {
.into()
}
+fn nip19_to_filters(nip19: &Nip19) -> Result<Vec<Filter>, Error> {
+ match nip19 {
+ Nip19::Event(ev) => {
+ let mut filters = vec![Filter::new().id(ev.event_id).limit(1)];
+ if let Some(author) = ev.author {
+ filters.push(Filter::new().author(author).kind(Kind::Metadata).limit(1))
+ }
+ Ok(filters)
+ }
+ Nip19::EventId(evid) => Ok(vec![Filter::new().id(*evid).limit(1)]),
+ Nip19::Profile(prof) => Ok(vec![Filter::new()
+ .author(prof.public_key)
+ .kind(Kind::Metadata)
+ .limit(1)]),
+ Nip19::Pubkey(pk) => Ok(vec![Filter::new()
+ .author(*pk)
+ .kind(Kind::Metadata)
+ .limit(1)]),
+ Nip19::Secret(_sec) => Err(Error::InvalidNip19),
+ }
+}
+
+fn nip19_relays(nip19: &Nip19) -> Vec<String> {
+ let mut relays: Vec<String> = vec![];
+ match nip19 {
+ Nip19::Event(ev) => relays.extend(ev.relays.clone()),
+ Nip19::Profile(p) => relays.extend(p.relays.clone()),
+ _ => (),
+ }
+ relays
+}
+
async fn serve(
ctx: &Context,
r: Request<hyper::body::Incoming>,