mirror of
https://github.com/aljazceru/blastr.git
synced 2025-12-18 22:44:22 +01:00
Don't broadcast events more than once
This commit is contained in:
@@ -15,6 +15,7 @@ cfg-if = "0.1.2"
|
|||||||
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" }
|
nostr = { git = "https://github.com/rust-nostr/nostr", rev = "c333544b1e58b89acb786695d5ca08759ebf5e69" }
|
||||||
|
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
|
||||||
# logging them with `console.error`. This is great for development, but requires
|
# logging them with `console.error`. This is great for development, but requires
|
||||||
|
|||||||
140
src/lib.rs
140
src/lib.rs
@@ -1,9 +1,10 @@
|
|||||||
pub(crate) use crate::nostr::{
|
pub(crate) use crate::nostr::{
|
||||||
try_queue_client_msg, 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};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use worker::*;
|
use worker::*;
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
@@ -18,6 +19,11 @@ fn log_request(req: &Request) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
pub struct PublishedNote {
|
||||||
|
date: String,
|
||||||
|
}
|
||||||
|
|
||||||
/// Main function for the Cloudflare Worker that triggers off of a HTTP req
|
/// Main function for the Cloudflare Worker that triggers off of a HTTP req
|
||||||
#[event(fetch)]
|
#[event(fetch)]
|
||||||
pub async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
|
pub async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
|
||||||
@@ -40,15 +46,60 @@ pub async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
|
|||||||
match req.text().await {
|
match req.text().await {
|
||||||
Ok(request_text) => {
|
Ok(request_text) => {
|
||||||
if let Ok(client_msg) = ClientMessage::from_json(request_text) {
|
if let Ok(client_msg) = ClientMessage::from_json(request_text) {
|
||||||
let nostr_queues = vec![
|
match client_msg {
|
||||||
ctx.env.queue(NOSTR_QUEUE).expect("get queue"),
|
ClientMessage::Event(event) => {
|
||||||
ctx.env.queue(NOSTR_QUEUE_2).expect("get queue"),
|
console_log!("got an event from client: {}", event.id);
|
||||||
ctx.env.queue(NOSTR_QUEUE_3).expect("get queue"),
|
|
||||||
ctx.env.queue(NOSTR_QUEUE_4).expect("get queue"),
|
// check if we've already published it before
|
||||||
ctx.env.queue(NOSTR_QUEUE_5).expect("get queue"),
|
let published_notes = ctx.kv("PUBLISHED_NOTES")?;
|
||||||
ctx.env.queue(NOSTR_QUEUE_6).expect("get queue"),
|
match published_notes
|
||||||
];
|
.get(event.id.to_string().as_str())
|
||||||
try_queue_client_msg(client_msg, nostr_queues).await
|
.json::<PublishedNote>()
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
Some(_) => {
|
||||||
|
console_log!("event already published: {}", event.id);
|
||||||
|
return fetch();
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// broadcast it to all queues
|
||||||
|
let nostr_queues = vec![
|
||||||
|
ctx.env.queue(NOSTR_QUEUE).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_2).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_3).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_4).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_5).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_6).expect("get queue"),
|
||||||
|
];
|
||||||
|
try_queue_event(*event.clone(), nostr_queues).await;
|
||||||
|
console_log!("queued up nostr event: {}", event.id);
|
||||||
|
match published_notes
|
||||||
|
.put(
|
||||||
|
event.id.to_string().as_str(),
|
||||||
|
PublishedNote {
|
||||||
|
date: Date::now().to_string(),
|
||||||
|
},
|
||||||
|
)?
|
||||||
|
.execute()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => {
|
||||||
|
console_log!("saved published note: {}", event.id);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
console_log!(
|
||||||
|
"could not save published note: {} - {e:?}",
|
||||||
|
event.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
console_log!("ignoring other nostr client message types");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -74,15 +125,66 @@ pub async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
if let Ok(client_msg) = ClientMessage::from_json(msg.text().unwrap()) {
|
if let Ok(client_msg) = ClientMessage::from_json(msg.text().unwrap()) {
|
||||||
let nostr_queues = vec![
|
match client_msg {
|
||||||
ctx.env.queue(NOSTR_QUEUE).expect("get queue"),
|
ClientMessage::Event(event) => {
|
||||||
ctx.env.queue(NOSTR_QUEUE_2).expect("get queue"),
|
console_log!("got an event from client: {}", event.id);
|
||||||
ctx.env.queue(NOSTR_QUEUE_3).expect("get queue"),
|
|
||||||
ctx.env.queue(NOSTR_QUEUE_4).expect("get queue"),
|
// check if we've already published it before
|
||||||
ctx.env.queue(NOSTR_QUEUE_5).expect("get queue"),
|
let published_notes =
|
||||||
ctx.env.queue(NOSTR_QUEUE_6).expect("get queue"),
|
ctx.kv("PUBLISHED_NOTES").expect("get kv");
|
||||||
];
|
match published_notes
|
||||||
try_queue_client_msg(client_msg, nostr_queues).await
|
.get(event.id.to_string().as_str())
|
||||||
|
.json::<PublishedNote>()
|
||||||
|
.await
|
||||||
|
.expect("lookup value")
|
||||||
|
{
|
||||||
|
Some(_) => {
|
||||||
|
console_log!(
|
||||||
|
"event already published: {}",
|
||||||
|
event.id
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// broadcast it to all queues
|
||||||
|
let nostr_queues = vec![
|
||||||
|
ctx.env.queue(NOSTR_QUEUE).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_2).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_3).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_4).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_5).expect("get queue"),
|
||||||
|
ctx.env.queue(NOSTR_QUEUE_6).expect("get queue"),
|
||||||
|
];
|
||||||
|
try_queue_event(*event.clone(), nostr_queues).await;
|
||||||
|
console_log!("queued up nostr event: {}", event.id);
|
||||||
|
match published_notes
|
||||||
|
.put(
|
||||||
|
event.id.to_string().as_str(),
|
||||||
|
PublishedNote {
|
||||||
|
date: Date::now().to_string(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.expect("saved note")
|
||||||
|
.execute()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => {
|
||||||
|
console_log!("saved published note: {}", event.id);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
console_log!(
|
||||||
|
"could not save published note: {} - {e:?}",
|
||||||
|
event.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
console_log!("ignoring other nostr client message types");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WebsocketEvent::Close(_) => {
|
WebsocketEvent::Close(_) => {
|
||||||
|
|||||||
21
src/nostr.rs
21
src/nostr.rs
@@ -22,22 +22,13 @@ const RELAYS: [&str; 8] = [
|
|||||||
"wss://nostr.wine",
|
"wss://nostr.wine",
|
||||||
];
|
];
|
||||||
|
|
||||||
pub async fn try_queue_client_msg(client_msg: ClientMessage, nostr_queues: Vec<Queue>) {
|
pub async fn try_queue_event(event: Event, nostr_queues: Vec<Queue>) {
|
||||||
match client_msg {
|
for nostr_queue in nostr_queues.iter() {
|
||||||
ClientMessage::Event(event) => {
|
match queue_nostr_event_with_queue(nostr_queue, event.clone()).await {
|
||||||
console_log!("got an event from client: {}", event.id);
|
Ok(_) => {}
|
||||||
for nostr_queue in nostr_queues.iter() {
|
Err(Error::WorkerError(e)) => {
|
||||||
match queue_nostr_event_with_queue(nostr_queue, *event.clone()).await {
|
console_log!("worker error: {e}");
|
||||||
Ok(_) => {}
|
|
||||||
Err(Error::WorkerError(e)) => {
|
|
||||||
console_log!("worker error: {e}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
console_log!("queued up nostr event: {}", event.id)
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
console_log!("ignoring other nostr client message types");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ routes = [
|
|||||||
{ pattern = "nostr.mutinywallet.com/event", zone_id = "2b9268714ce8d1c4431e8046d4ba55d3" }
|
{ pattern = "nostr.mutinywallet.com/event", zone_id = "2b9268714ce8d1c4431e8046d4ba55d3" }
|
||||||
]
|
]
|
||||||
|
|
||||||
|
kv_namespaces = [
|
||||||
|
{ binding = "PUBLISHED_NOTES", id = "afa24a392a5a41f6b1655507dfd9b97a", preview_id = "0b334aece8d74c3ab90e3e99db569ce8" }
|
||||||
|
]
|
||||||
|
|
||||||
[vars]
|
[vars]
|
||||||
WORKERS_RS_VERSION = "0.0.11"
|
WORKERS_RS_VERSION = "0.0.11"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user