Use RelayMessage type for event parsing and add handling for all message types

This commit is contained in:
openhands 2025-01-10 01:51:00 +00:00
parent 1f3f480c87
commit ed38854b91

View file

@ -137,38 +137,29 @@ fn update_app(app: &mut Hoot, ctx: &egui::Context) {
fn process_message(app: &mut Hoot, msg: &relay::RelayMessage) {
use relay::RelayMessage::*;
match msg {
Event(sub_id, event) => process_event(app, &sub_id, &event),
_ => {
// we don't care rn.
},
Event(sub_id, event) => process_event(app, sub_id, event),
Notice(msg) => debug!("Relay notice: {}", msg),
OK(result) => debug!("Command result: {:?}", result),
Eose(sub_id) => debug!("End of stored events for subscription {}", sub_id),
Closed(sub_id, msg) => debug!("Subscription {} closed: {}", sub_id, msg),
}
}
fn process_event(app: &mut Hoot, _sub_id: &str, event_json: &str) {
#[cfg(feature = "profiling")]
puffin::profile_function!();
// The event comes in a JSON array format ["EVENT", subscription_id, event_json]
// We need to parse out the actual event JSON
if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(event_json) {
if let Some(array) = json_value.as_array() {
if array.len() >= 3 {
if let Ok(event) = serde_json::from_value::<nostr::Event>(array[2].clone()) {
// Verify the event signature
if event.verify().is_ok() {
debug!("Verified event: {:?}", event);
app.events.push(event);
} else {
error!("Event verification failed");
}
} else {
error!("Failed to parse event JSON");
}
}
// Parse the event using the RelayMessage type which handles the ["EVENT", subscription_id, event_json] format
if let Ok(event) = serde_json::from_str::<nostr::Event>(event_json) {
// Verify the event signature
if event.verify().is_ok() {
debug!("Verified event: {:?}", event);
app.events.push(event);
} else {
error!("Event verification failed");
}
} else {
error!("Failed to parse relay message JSON: {}", event_json);
error!("Failed to parse event JSON: {}", event_json);
}
}