mirror of
https://github.com/aljazceru/blastr.git
synced 2025-12-17 05:54:26 +01:00
Merge pull request #5 from MutinyWallet/nip20
Send RelayMessages back to client
This commit is contained in:
@@ -14,7 +14,8 @@ cfg-if = "0.1.2"
|
|||||||
# Need the queue feature
|
# Need the queue feature
|
||||||
worker = { git = "https://github.com/cloudflare/workers-rs", rev = "b4b9cd1f15feac412b6f9e9e9209458cd3b98430", features = ["queue"] }
|
worker = { git = "https://github.com/cloudflare/workers-rs", rev = "b4b9cd1f15feac412b6f9e9e9209458cd3b98430", features = ["queue"] }
|
||||||
futures = "0.3.26"
|
futures = "0.3.26"
|
||||||
nostr = { git = "https://github.com/rust-nostr/nostr", rev = "c333544b1e58b89acb786695d5ca08759ebf5e69" }
|
# Needed for serde serializaters
|
||||||
|
nostr = { git = "https://github.com/benthecarman/nostr", rev = "6c238ac72651671bcfd6666ddcd731767d1643b8" }
|
||||||
serde = { version = "^1.0", features = ["derive"] }
|
serde = { version = "^1.0", features = ["derive"] }
|
||||||
|
|
||||||
# The `console_error_panic_hook` crate provides better debugging of panics by
|
# The `console_error_panic_hook` crate provides better debugging of panics by
|
||||||
|
|||||||
68
src/lib.rs
68
src/lib.rs
@@ -3,7 +3,7 @@ pub(crate) use crate::nostr::{
|
|||||||
try_queue_event, NOSTR_QUEUE, NOSTR_QUEUE_2, NOSTR_QUEUE_3, NOSTR_QUEUE_4, NOSTR_QUEUE_5,
|
try_queue_event, NOSTR_QUEUE, NOSTR_QUEUE_2, NOSTR_QUEUE_3, NOSTR_QUEUE_4, NOSTR_QUEUE_5,
|
||||||
NOSTR_QUEUE_6,
|
NOSTR_QUEUE_6,
|
||||||
};
|
};
|
||||||
use ::nostr::{ClientMessage, Event};
|
use ::nostr::{ClientMessage, Event, RelayMessage};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use worker::*;
|
use worker::*;
|
||||||
@@ -53,16 +53,21 @@ pub async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
|
|||||||
|
|
||||||
// check if we've already published it before
|
// check if we've already published it before
|
||||||
let published_notes = ctx.kv("PUBLISHED_NOTES")?;
|
let published_notes = ctx.kv("PUBLISHED_NOTES")?;
|
||||||
match published_notes
|
if published_notes
|
||||||
.get(event.id.to_string().as_str())
|
.get(event.id.to_string().as_str())
|
||||||
.json::<PublishedNote>()
|
.json::<PublishedNote>()
|
||||||
.await?
|
.await
|
||||||
|
.ok()
|
||||||
|
.flatten()
|
||||||
|
.is_some()
|
||||||
{
|
{
|
||||||
Some(_) => {
|
console_log!("event already published: {}", event.id);
|
||||||
console_log!("event already published: {}", event.id);
|
let relay_msg = RelayMessage::new_ok(
|
||||||
return empty_response();
|
event.id,
|
||||||
}
|
false,
|
||||||
None => {}
|
"event already published",
|
||||||
|
);
|
||||||
|
return relay_response(relay_msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
// broadcast it to all queues
|
// broadcast it to all queues
|
||||||
@@ -88,27 +93,37 @@ pub async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
|
|||||||
{
|
{
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
console_log!("saved published note: {}", event.id);
|
console_log!("saved published note: {}", event.id);
|
||||||
|
let relay_msg = RelayMessage::new_ok(event.id, true, "");
|
||||||
|
relay_response(relay_msg)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
console_log!(
|
console_log!(
|
||||||
"could not save published note: {} - {e:?}",
|
"could not save published note: {} - {e:?}",
|
||||||
event.id
|
event.id
|
||||||
);
|
);
|
||||||
|
let relay_msg = RelayMessage::new_ok(
|
||||||
|
event.id,
|
||||||
|
false,
|
||||||
|
"error: could not save published note",
|
||||||
|
);
|
||||||
|
relay_response(relay_msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
console_log!("ignoring other nostr client message types");
|
console_log!("ignoring other nostr client message types");
|
||||||
|
Response::error("Only Event types allowed", 400)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Response::error("Could not parse Client Message", 400)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
console_log!("could not get request text: {}", e);
|
console_log!("could not get request text: {}", e);
|
||||||
|
Response::error("Could not get request text", 400)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
empty_response()
|
|
||||||
})
|
})
|
||||||
.get("/", |req, ctx| {
|
.get("/", |req, ctx| {
|
||||||
// NIP 11
|
// NIP 11
|
||||||
@@ -140,20 +155,24 @@ pub async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
|
|||||||
// check if we've already published it before
|
// check if we've already published it before
|
||||||
let published_notes =
|
let published_notes =
|
||||||
ctx.kv("PUBLISHED_NOTES").expect("get kv");
|
ctx.kv("PUBLISHED_NOTES").expect("get kv");
|
||||||
match published_notes
|
if published_notes
|
||||||
.get(event.id.to_string().as_str())
|
.get(event.id.to_string().as_str())
|
||||||
.json::<PublishedNote>()
|
.json::<PublishedNote>()
|
||||||
.await
|
.await
|
||||||
.expect("lookup value")
|
.ok()
|
||||||
|
.flatten()
|
||||||
|
.is_some()
|
||||||
{
|
{
|
||||||
Some(_) => {
|
console_log!("event already published: {}", event.id);
|
||||||
console_log!(
|
let relay_msg = RelayMessage::new_ok(
|
||||||
"event already published: {}",
|
event.id,
|
||||||
event.id
|
false,
|
||||||
);
|
"event already published",
|
||||||
continue;
|
);
|
||||||
}
|
server
|
||||||
None => {}
|
.send_with_str(&relay_msg.as_json())
|
||||||
|
.expect("failed to send response");
|
||||||
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
// broadcast it to all queues
|
// broadcast it to all queues
|
||||||
@@ -188,6 +207,11 @@ pub async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let relay_msg = RelayMessage::new_ok(event.id, true, "");
|
||||||
|
server
|
||||||
|
.send_with_str(&relay_msg.as_json())
|
||||||
|
.expect("failed to send response");
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
console_log!("ignoring other nostr client message types");
|
console_log!("ignoring other nostr client message types");
|
||||||
@@ -244,6 +268,10 @@ pub fn queue_number(batch_name: &str) -> Result<u32> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn relay_response(msg: RelayMessage) -> worker::Result<Response> {
|
||||||
|
Response::from_json(&msg)?.with_cors(&cors())
|
||||||
|
}
|
||||||
|
|
||||||
fn empty_response() -> worker::Result<Response> {
|
fn empty_response() -> worker::Result<Response> {
|
||||||
Response::empty()?.with_cors(&cors())
|
Response::empty()?.with_cors(&cors())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user