Carry through with post

This commit is contained in:
Mike Dilger 2022-12-28 07:48:01 +13:00
parent 243a9a2af4
commit 87a91c670e
3 changed files with 77 additions and 11 deletions

View File

@ -1,7 +1,9 @@
use super::Minion; use super::Minion;
use crate::{BusMessage, Error}; use crate::{BusMessage, Error};
use nostr_types::{IdHex, PublicKeyHex}; use futures::SinkExt;
use tracing::warn; use nostr_types::{ClientMessage, Event, IdHex, PublicKeyHex};
use tracing::{info, warn};
use tungstenite::protocol::Message as WsMessage;
impl Minion { impl Minion {
pub(super) async fn handle_bus_message( pub(super) async fn handle_bus_message(
@ -20,6 +22,14 @@ impl Minion {
"follow_event_reactions" => { "follow_event_reactions" => {
warn!("{}: follow event reactions unimplemented", &self.url); warn!("{}: follow event reactions unimplemented", &self.url);
} }
"post_event" => {
let event: Event = serde_json::from_str(&bus_message.json_payload)?;
let msg = ClientMessage::Event(Box::new(event));
let wire = serde_json::to_string(&msg)?;
let ws_sink = self.sink.as_mut().unwrap();
ws_sink.send(WsMessage::Text(wire)).await?;
info!("Posted event to {}", &self.url);
}
_ => { _ => {
warn!( warn!(
"{} Unrecognized bus message kind received by minion: {}", "{} Unrecognized bus message kind received by minion: {}",

View File

@ -7,7 +7,9 @@ use crate::error::Error;
use crate::globals::{Globals, GLOBALS}; use crate::globals::{Globals, GLOBALS};
use crate::settings::Settings; use crate::settings::Settings;
use minion::Minion; use minion::Minion;
use nostr_types::{Event, Nip05, PrivateKey, PublicKey, PublicKeyHex, Unixtime, Url}; use nostr_types::{
Event, EventKind, Nip05, PreEvent, PrivateKey, PublicKey, PublicKeyHex, Unixtime, Url,
};
use relay_picker::{BestRelay, RelayPicker}; use relay_picker::{BestRelay, RelayPicker};
use std::collections::HashMap; use std::collections::HashMap;
use tokio::sync::broadcast::Sender; use tokio::sync::broadcast::Sender;
@ -468,6 +470,10 @@ impl Overlord {
} }
} }
} }
"post_textnote" => {
let content: String = serde_json::from_str(&bus_message.json_payload)?;
self.post_textnote(content).await?;
}
_ => {} _ => {}
}, },
_ => {} _ => {}
@ -633,4 +639,56 @@ impl Overlord {
Ok(()) Ok(())
} }
async fn post_textnote(&mut self, content: String) -> Result<(), Error> {
let event = {
let public_key = match GLOBALS.signer.read().await.public_key() {
Some(pk) => pk,
None => {
warn!("No public key! Not posting");
return Ok(());
}
};
let pre_event = PreEvent {
pubkey: public_key,
created_at: Unixtime::now().unwrap(),
kind: EventKind::TextNote,
tags: vec![],
content,
ots: None,
};
GLOBALS.signer.read().await.sign_preevent(pre_event)?
};
let relays: Vec<DbRelay> = GLOBALS
.relays
.read()
.await
.iter()
.filter_map(|(_, r)| if r.post { Some(r.to_owned()) } else { None })
.collect();
for relay in relays {
// Start a minion for it, if there is none
if !self.urls_watching.contains(&Url(relay.url.clone())) {
self.start_minion(relay.url.clone()).await?;
}
// Send it the event to post
debug!("Asking {} to post", &relay.url);
let _ = self.to_minions.send(BusMessage {
target: relay.url.clone(),
kind: "post_event".to_string(),
json_payload: serde_json::to_string(&event).unwrap(),
});
}
// Process the message for ourself
crate::process::process_new_event(&event, false, None).await?;
Ok(())
}
} }

View File

@ -4,7 +4,6 @@ use crate::globals::{Globals, GLOBALS};
use eframe::egui; use eframe::egui;
use egui::{Align, Color32, Context, Layout, RichText, ScrollArea, TextStyle, Ui, Vec2}; use egui::{Align, Color32, Context, Layout, RichText, ScrollArea, TextStyle, Ui, Vec2};
use nostr_types::{EventKind, Id}; use nostr_types::{EventKind, Id};
use tracing::info;
pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Frame, ui: &mut Ui) { pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Frame, ui: &mut Ui) {
let feed = Globals::blocking_get_feed(true); let feed = Globals::blocking_get_feed(true);
@ -35,13 +34,12 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram
ui.text_edit_multiline(&mut app.draft); ui.text_edit_multiline(&mut app.draft);
if ui.button("Send").clicked() && !app.draft.is_empty() { if ui.button("Send").clicked() && !app.draft.is_empty() {
info!("Would send: {}", app.draft); let tx = GLOBALS.to_overlord.clone();
let _ = tx.send(BusMessage {
// We need our private key target: "overlord".to_string(),
// Then we need to create a TextNote event kind: "post_textnote".to_string(),
// Then we need to send it to multiple relays json_payload: serde_json::to_string(&app.draft).unwrap(),
// NOT a one-liner });
app.draft = "".to_owned(); app.draft = "".to_owned();
} }
} }