mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-19 03:33:53 +00:00
Carry through with post
This commit is contained in:
parent
243a9a2af4
commit
87a91c670e
@ -1,7 +1,9 @@
|
||||
use super::Minion;
|
||||
use crate::{BusMessage, Error};
|
||||
use nostr_types::{IdHex, PublicKeyHex};
|
||||
use tracing::warn;
|
||||
use futures::SinkExt;
|
||||
use nostr_types::{ClientMessage, Event, IdHex, PublicKeyHex};
|
||||
use tracing::{info, warn};
|
||||
use tungstenite::protocol::Message as WsMessage;
|
||||
|
||||
impl Minion {
|
||||
pub(super) async fn handle_bus_message(
|
||||
@ -20,6 +22,14 @@ impl Minion {
|
||||
"follow_event_reactions" => {
|
||||
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!(
|
||||
"{} Unrecognized bus message kind received by minion: {}",
|
||||
|
@ -7,7 +7,9 @@ use crate::error::Error;
|
||||
use crate::globals::{Globals, GLOBALS};
|
||||
use crate::settings::Settings;
|
||||
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 std::collections::HashMap;
|
||||
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(())
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ use crate::globals::{Globals, GLOBALS};
|
||||
use eframe::egui;
|
||||
use egui::{Align, Color32, Context, Layout, RichText, ScrollArea, TextStyle, Ui, Vec2};
|
||||
use nostr_types::{EventKind, Id};
|
||||
use tracing::info;
|
||||
|
||||
pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Frame, ui: &mut Ui) {
|
||||
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);
|
||||
|
||||
if ui.button("Send").clicked() && !app.draft.is_empty() {
|
||||
info!("Would send: {}", app.draft);
|
||||
|
||||
// We need our private key
|
||||
// Then we need to create a TextNote event
|
||||
// Then we need to send it to multiple relays
|
||||
// NOT a one-liner
|
||||
|
||||
let tx = GLOBALS.to_overlord.clone();
|
||||
let _ = tx.send(BusMessage {
|
||||
target: "overlord".to_string(),
|
||||
kind: "post_textnote".to_string(),
|
||||
json_payload: serde_json::to_string(&app.draft).unwrap(),
|
||||
});
|
||||
app.draft = "".to_owned();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user