generate nostr events from compose window

This commit is contained in:
Jack Chakany 2024-09-24 11:28:53 -04:00
parent 8c80d252d0
commit c7d3eedf71
3 changed files with 86 additions and 43 deletions

View file

@ -4,11 +4,11 @@ use nostr::{EventBuilder, Event, PublicKey, Kind, Tag, Keys};
pub const MAIL_EVENT_KIND: u16 = 1059;
pub struct MailMessage {
to: Vec<PublicKey>,
cc: Vec<PublicKey>,
bcc: Vec<PublicKey>,
subject: String,
content: String,
pub to: Vec<PublicKey>,
pub cc: Vec<PublicKey>,
pub bcc: Vec<PublicKey>,
pub subject: String,
pub content: String,
}
impl MailMessage {

View file

@ -1,5 +1,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // for windows release
use std::collections::HashMap;
use eframe::egui::{self, FontDefinitions, Sense, Vec2b};
use egui::FontFamily::Proportional;
use egui_extras::{Column, TableBuilder};
@ -67,6 +68,7 @@ pub enum Page {
// for storing the state of different components and such.
#[derive(Default)]
pub struct HootState {
pub compose_window: HashMap<egui::Id, ui::compose_window::ComposeWindowState>,
pub onboarding: ui::onboarding::OnboardingState,
pub settings: ui::settings::SettingsState,
}
@ -79,7 +81,6 @@ pub struct Hoot {
relays: relay::RelayPool,
ndb: nostrdb::Ndb,
events: Vec<nostr::Event>,
pub windows: Vec<Box<ui::compose_window::ComposeWindow>>,
account_manager: account_manager::AccountManager,
}
@ -169,16 +170,20 @@ fn render_app(app: &mut Hoot, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| {
// todo: fix
for window in &mut app.windows {
window.show(ui);
for window_id in app.state.compose_window.clone().into_keys() {
ui::compose_window::ComposeWindow::show(app, ui, window_id);
}
if app.page == Page::Inbox {
ui.label("hello there!");
if ui.button("Compose").clicked() {
let mut new_window = Box::new(ui::compose_window::ComposeWindow::new());
new_window.show(ui);
app.windows.push(new_window);
let state = ui::compose_window::ComposeWindowState {
subject: String::new(),
to_field: String::new(),
content: String::new(),
selected_account: None,
};
app.state.compose_window.insert(egui::Id::new(rand::random::<u32>()), state);
}
if ui.button("Send Test Event").clicked() {
@ -287,7 +292,6 @@ impl Hoot {
relays: relay::RelayPool::new(),
ndb,
events: Vec::new(),
windows: Vec::new(),
account_manager: account_manager::AccountManager::new(),
}
}

View file

@ -1,43 +1,82 @@
use tracing::{info, error};
use eframe::egui::{self, RichText};
use rand::random;
use nostr::{Keys, PublicKey};
use crate::mail_event::MailMessage;
pub struct ComposeWindow {
title: Option<RichText>,
id: egui::Id,
subject: String,
to_field: String,
content: String,
#[derive(Debug, Clone)]
pub struct ComposeWindowState {
pub subject: String,
pub to_field: String,
pub content: String,
pub selected_account: Option<Keys>,
}
impl Default for ComposeWindow {
fn default() -> Self {
Self::new()
}
}
pub struct ComposeWindow {}
impl ComposeWindow {
pub fn new() -> Self {
Self {
title: None,
id: egui::Id::new(random::<u32>()),
subject: String::from("New Message"),
to_field: String::new(),
content: String::new(),
}
}
pub fn show(&mut self, ui: &mut egui::Ui) {
egui::Window::new(&self.subject)
.id(self.id)
pub fn show(app: &mut crate::Hoot, ui: &mut egui::Ui, id: egui::Id) {
let state = app.state.compose_window.get_mut(&id).expect("no state found for id");
egui::Window::new(&state.subject)
.id(id)
.show(ui.ctx(), |ui| {
ui.label("Hello!");
ui.vertical(|ui| {
ui.text_edit_singleline(&mut self.to_field);
ui.text_edit_singleline(&mut self.subject);
ui.add_sized(
ui.available_size(),
egui::TextEdit::multiline(&mut self.content),
);
ui.horizontal(|ui| {
ui.label("To:");
ui.text_edit_singleline(&mut state.to_field);
});
{
// god this is such a fucking mess
let accounts = app.account_manager.loaded_keys.clone();
use nostr::ToBech32;
let mut formatted_key = String::new();
if state.selected_account.is_some() {
formatted_key = state.selected_account.clone().unwrap().public_key().to_bech32().unwrap();
}
egui::ComboBox::from_label("Select Keys to Send With")
.selected_text(format!("{}", formatted_key))
.show_ui(ui, |ui| {
for key in accounts {
ui.selectable_value(&mut state.selected_account, Some(key.clone()), key.public_key().to_bech32().unwrap());
}
});
}
ui.horizontal(|ui| {
ui.label("Subject:");
ui.text_edit_singleline(&mut state.subject);
});
ui.label("Body:");
ui.text_edit_multiline(&mut state.content);
if ui.button("Send").clicked() {
if state.selected_account.is_none() {
error!("No Account Selected!");
return;
}
// convert to field into PublicKey object
let to_field = state.to_field.clone();
let mut recipient_keys: Vec<PublicKey> = Vec::new();
for key_string in to_field.split_whitespace() {
let new_key = PublicKey::from_hex(key_string).expect("could not parse key"); // TODO: fail gracefully.
recipient_keys.push(new_key);
}
let mut msg = MailMessage {
to: recipient_keys,
cc: vec![],
bcc: vec![],
subject: state.subject.clone(),
content: state.content.clone(),
};
let events_to_send = msg.to_events(&state.selected_account.clone().unwrap());
info!("new events! {:?}", events_to_send);
// send over wire
}
});
});
}