wip: refactor
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["crates/*"]
|
members = ["crates/*"]
|
||||||
default-members = ["crates/ui"]
|
default-members = ["crates/app"]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
|
|||||||
@@ -20,7 +20,9 @@ keyring.workspace = true
|
|||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
serde_json.workspace = true
|
serde_json.workspace = true
|
||||||
|
itertools.workspace = true
|
||||||
|
chrono.workspace = true
|
||||||
|
dirs.workspace = true
|
||||||
|
|
||||||
client = { version = "0.1.0", path = "../client" }
|
|
||||||
rust-embed = "8.5.0"
|
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
|
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
|
||||||
|
rust-embed = "8.5.0"
|
||||||
3
crates/app/src/constants.rs
Normal file
3
crates/app/src/constants.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub const KEYRING_SERVICE: &str = "Coop Safe Storage";
|
||||||
|
pub const APP_NAME: &str = "coop";
|
||||||
|
pub const FAKE_SIG: &str = "f9e79d141c004977192d05a86f81ec7c585179c371f7350a5412d33575a2a356433f58e405c2296ed273e2fe0aafa25b641e39cc4e1f3f261ebf55bce0cbac83";
|
||||||
156
crates/app/src/main.rs
Normal file
156
crates/app/src/main.rs
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
use asset::Assets;
|
||||||
|
use components::Root;
|
||||||
|
use constants::{APP_NAME, FAKE_SIG};
|
||||||
|
use dirs::config_dir;
|
||||||
|
use gpui::*;
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
use std::{fs, str::FromStr, sync::Arc, time::Duration};
|
||||||
|
use tokio::sync::OnceCell;
|
||||||
|
|
||||||
|
use states::user::UserState;
|
||||||
|
use ui::app::AppView;
|
||||||
|
|
||||||
|
pub mod asset;
|
||||||
|
pub mod constants;
|
||||||
|
pub mod states;
|
||||||
|
pub mod ui;
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
|
actions!(main_menu, [Quit]);
|
||||||
|
|
||||||
|
pub static CLIENT: OnceCell<Client> = OnceCell::const_new();
|
||||||
|
|
||||||
|
pub async fn get_client() -> &'static Client {
|
||||||
|
CLIENT
|
||||||
|
.get_or_init(|| async {
|
||||||
|
// Setup app data folder
|
||||||
|
let config_dir = config_dir().expect("Config directory not found");
|
||||||
|
let _ = fs::create_dir_all(config_dir.join("Coop/"));
|
||||||
|
|
||||||
|
// Setup database
|
||||||
|
let lmdb = NostrLMDB::open(config_dir.join("Coop/nostr"))
|
||||||
|
.expect("Database is NOT initialized");
|
||||||
|
|
||||||
|
// Client options
|
||||||
|
let opts = Options::new()
|
||||||
|
.gossip(true)
|
||||||
|
.max_avg_latency(Duration::from_secs(2));
|
||||||
|
|
||||||
|
// Setup Nostr Client
|
||||||
|
let client = ClientBuilder::default().database(lmdb).opts(opts).build();
|
||||||
|
|
||||||
|
// Add some bootstrap relays
|
||||||
|
let _ = client.add_relay("wss://relay.damus.io").await;
|
||||||
|
let _ = client.add_relay("wss://relay.primal.net").await;
|
||||||
|
|
||||||
|
let _ = client.add_discovery_relay("wss://directory.yabu.me").await;
|
||||||
|
let _ = client.add_discovery_relay("wss://user.kindpag.es/").await;
|
||||||
|
|
||||||
|
// Connect to all relays
|
||||||
|
client.connect().await;
|
||||||
|
|
||||||
|
// Return client
|
||||||
|
client
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
// Initialize nostr client
|
||||||
|
let _client = get_client().await;
|
||||||
|
|
||||||
|
App::new()
|
||||||
|
.with_assets(Assets)
|
||||||
|
.with_http_client(Arc::new(reqwest_client::ReqwestClient::new()))
|
||||||
|
.run(move |cx| {
|
||||||
|
// Initialize components
|
||||||
|
components::init(cx);
|
||||||
|
|
||||||
|
// Set quit action
|
||||||
|
cx.on_action(quit);
|
||||||
|
|
||||||
|
// Set app state
|
||||||
|
UserState::set_global(cx);
|
||||||
|
|
||||||
|
// Refresh
|
||||||
|
cx.refresh();
|
||||||
|
|
||||||
|
// Handle notifications
|
||||||
|
cx.foreground_executor()
|
||||||
|
.spawn(async move {
|
||||||
|
let client = get_client().await;
|
||||||
|
|
||||||
|
// Generate a fake signature for rumor event.
|
||||||
|
// TODO: Find better way to save unsigned event to database.
|
||||||
|
let fake_sig = Signature::from_str(FAKE_SIG).unwrap();
|
||||||
|
|
||||||
|
client
|
||||||
|
.handle_notifications(|notification| async {
|
||||||
|
#[allow(clippy::collapsible_match)]
|
||||||
|
if let RelayPoolNotification::Message { message, .. } = notification {
|
||||||
|
if let RelayMessage::Event { event, .. } = message {
|
||||||
|
if event.kind == Kind::GiftWrap {
|
||||||
|
if let Ok(UnwrappedGift { rumor, .. }) =
|
||||||
|
client.unwrap_gift_wrap(&event).await
|
||||||
|
{
|
||||||
|
println!("rumor: {}", rumor.as_json());
|
||||||
|
let mut rumor_clone = rumor.clone();
|
||||||
|
|
||||||
|
// Compute event id if not exist
|
||||||
|
rumor_clone.ensure_id();
|
||||||
|
|
||||||
|
let ev = Event::new(
|
||||||
|
rumor_clone.id.expect("System error"),
|
||||||
|
rumor_clone.pubkey,
|
||||||
|
rumor_clone.created_at,
|
||||||
|
rumor_clone.kind,
|
||||||
|
rumor_clone.tags,
|
||||||
|
rumor_clone.content,
|
||||||
|
fake_sig,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Save rumor to database to further query
|
||||||
|
if let Err(e) = client.database().save_event(&ev).await
|
||||||
|
{
|
||||||
|
println!("Error: {}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if event.kind == Kind::Metadata {
|
||||||
|
// TODO: handle metadata
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(false)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
// Set window size
|
||||||
|
let bounds = Bounds::centered(None, size(px(900.0), px(680.0)), cx);
|
||||||
|
|
||||||
|
let opts = WindowOptions {
|
||||||
|
window_bounds: Some(WindowBounds::Windowed(bounds)),
|
||||||
|
window_decorations: Some(WindowDecorations::Client),
|
||||||
|
titlebar: Some(TitlebarOptions {
|
||||||
|
title: Some(SharedString::new_static(APP_NAME)),
|
||||||
|
appears_transparent: true,
|
||||||
|
traffic_light_position: Some(point(px(9.0), px(9.0))),
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
cx.open_window(opts, |cx| {
|
||||||
|
let app_view = cx.new_view(AppView::new);
|
||||||
|
cx.new_view(|cx| Root::new(app_view.into(), cx))
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn quit(_: &Quit, cx: &mut AppContext) {
|
||||||
|
cx.quit();
|
||||||
|
}
|
||||||
2
crates/app/src/states/mod.rs
Normal file
2
crates/app/src/states/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod room;
|
||||||
|
pub mod user;
|
||||||
35
crates/app/src/states/room.rs
Normal file
35
crates/app/src/states/room.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
use gpui::*;
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct RoomLastMessage {
|
||||||
|
pub content: Option<String>,
|
||||||
|
pub time: Timestamp,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, IntoElement)]
|
||||||
|
pub struct Room {
|
||||||
|
members: Vec<PublicKey>,
|
||||||
|
last_message: Option<RoomLastMessage>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Room {
|
||||||
|
pub fn new(members: Vec<PublicKey>, last_message: Option<RoomLastMessage>) -> Self {
|
||||||
|
Self {
|
||||||
|
members,
|
||||||
|
last_message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RenderOnce for Room {
|
||||||
|
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
|
||||||
|
div().child("TODO")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Rooms {
|
||||||
|
pub rooms: Vec<Room>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Global for Rooms {}
|
||||||
19
crates/app/src/states/user.rs
Normal file
19
crates/app/src/states/user.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
use gpui::*;
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct UserState {
|
||||||
|
pub current_user: Option<PublicKey>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Global for UserState {}
|
||||||
|
|
||||||
|
impl UserState {
|
||||||
|
pub fn set_global(cx: &mut AppContext) {
|
||||||
|
cx.set_global(Self::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
Self { current_user: None }
|
||||||
|
}
|
||||||
|
}
|
||||||
189
crates/app/src/ui/app.rs
Normal file
189
crates/app/src/ui/app.rs
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
use components::{
|
||||||
|
dock::{DockArea, DockItem, PanelStyle},
|
||||||
|
theme::{ActiveTheme, Theme},
|
||||||
|
Root, TitleBar,
|
||||||
|
};
|
||||||
|
use gpui::*;
|
||||||
|
use itertools::Itertools;
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
use std::{cmp::Reverse, sync::Arc, time::Duration};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
get_client,
|
||||||
|
states::{
|
||||||
|
room::{Room, RoomLastMessage, Rooms},
|
||||||
|
user::UserState,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
block::{welcome::WelcomeBlock, BlockContainer},
|
||||||
|
onboarding::Onboarding,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct DockAreaTab {
|
||||||
|
id: &'static str,
|
||||||
|
version: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const DOCK_AREA: DockAreaTab = DockAreaTab {
|
||||||
|
id: "dock",
|
||||||
|
version: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct AppView {
|
||||||
|
onboarding: View<Onboarding>,
|
||||||
|
dock_area: View<DockArea>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AppView {
|
||||||
|
pub fn new(cx: &mut ViewContext<'_, Self>) -> AppView {
|
||||||
|
// Sync theme with system
|
||||||
|
cx.observe_window_appearance(|_, cx| {
|
||||||
|
Theme::sync_system_appearance(cx);
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
// Observe UserState
|
||||||
|
// If current user is present, fetching all gift wrap events
|
||||||
|
cx.observe_global::<UserState>(|_v, cx| {
|
||||||
|
let app_state = cx.global::<UserState>();
|
||||||
|
let view_id = cx.parent_view_id();
|
||||||
|
let mut async_cx = cx.to_async();
|
||||||
|
|
||||||
|
if let Some(public_key) = app_state.current_user {
|
||||||
|
cx.foreground_executor()
|
||||||
|
.spawn(async move {
|
||||||
|
let client = get_client().await;
|
||||||
|
let filter = Filter::new().pubkey(public_key).kind(Kind::GiftWrap);
|
||||||
|
|
||||||
|
let mut rumors: Vec<UnsignedEvent> = Vec::new();
|
||||||
|
|
||||||
|
if let Ok(mut rx) = client
|
||||||
|
.stream_events(vec![filter], Some(Duration::from_secs(30)))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
while let Some(event) = rx.next().await {
|
||||||
|
if let Ok(UnwrappedGift { rumor, .. }) =
|
||||||
|
client.unwrap_gift_wrap(&event).await
|
||||||
|
{
|
||||||
|
rumors.push(rumor);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let items = rumors
|
||||||
|
.into_iter()
|
||||||
|
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||||
|
.filter(|ev| ev.pubkey != public_key)
|
||||||
|
.unique_by(|ev| ev.pubkey)
|
||||||
|
.map(|item| {
|
||||||
|
Room::new(
|
||||||
|
vec![item.pubkey, public_key],
|
||||||
|
Some(RoomLastMessage {
|
||||||
|
content: Some(item.content),
|
||||||
|
time: item.created_at,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
_ = async_cx.update_global::<Rooms, _>(|state, cx| {
|
||||||
|
state.rooms = items;
|
||||||
|
cx.notify(view_id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
// Onboarding
|
||||||
|
let onboarding = cx.new_view(Onboarding::new);
|
||||||
|
|
||||||
|
// Dock
|
||||||
|
let dock_area = cx.new_view(|cx| {
|
||||||
|
DockArea::new(DOCK_AREA.id, Some(DOCK_AREA.version), cx).panel_style(PanelStyle::TabBar)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Set dock layout
|
||||||
|
Self::init_layout(dock_area.downgrade(), cx);
|
||||||
|
|
||||||
|
AppView {
|
||||||
|
onboarding,
|
||||||
|
dock_area,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_layout(dock_area: WeakView<DockArea>, cx: &mut WindowContext) {
|
||||||
|
let dock_item = Self::init_dock_items(&dock_area, cx);
|
||||||
|
|
||||||
|
let left_panels = DockItem::split_with_sizes(
|
||||||
|
Axis::Vertical,
|
||||||
|
vec![DockItem::tabs(vec![], None, &dock_area, cx)],
|
||||||
|
vec![None, None],
|
||||||
|
&dock_area,
|
||||||
|
cx,
|
||||||
|
);
|
||||||
|
|
||||||
|
_ = dock_area.update(cx, |view, cx| {
|
||||||
|
view.set_version(DOCK_AREA.version, cx);
|
||||||
|
view.set_left_dock(left_panels, Some(px(260.)), true, cx);
|
||||||
|
view.set_root(dock_item, cx);
|
||||||
|
view.set_dock_collapsible(
|
||||||
|
Edges {
|
||||||
|
left: false,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
cx,
|
||||||
|
);
|
||||||
|
// TODO: support right dock?
|
||||||
|
// TODO: support bottom dock?
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_dock_items(dock_area: &WeakView<DockArea>, cx: &mut WindowContext) -> DockItem {
|
||||||
|
DockItem::split_with_sizes(
|
||||||
|
Axis::Vertical,
|
||||||
|
vec![DockItem::tabs(
|
||||||
|
vec![
|
||||||
|
Arc::new(BlockContainer::panel::<WelcomeBlock>(cx)),
|
||||||
|
// TODO: add chat block
|
||||||
|
],
|
||||||
|
None,
|
||||||
|
dock_area,
|
||||||
|
cx,
|
||||||
|
)],
|
||||||
|
vec![None],
|
||||||
|
dock_area,
|
||||||
|
cx,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for AppView {
|
||||||
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
|
let modal_layer = Root::render_modal_layer(cx);
|
||||||
|
let notification_layer = Root::render_notification_layer(cx);
|
||||||
|
let mut content = div();
|
||||||
|
|
||||||
|
if cx.global::<UserState>().current_user.is_none() {
|
||||||
|
content = content.child(self.onboarding.clone())
|
||||||
|
} else {
|
||||||
|
content = content
|
||||||
|
.size_full()
|
||||||
|
.flex()
|
||||||
|
.flex_col()
|
||||||
|
.child(TitleBar::new())
|
||||||
|
.child(self.dock_area.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
div()
|
||||||
|
.bg(cx.theme().background)
|
||||||
|
.text_color(cx.theme().foreground)
|
||||||
|
.size_full()
|
||||||
|
.child(content)
|
||||||
|
.children(modal_layer)
|
||||||
|
.child(div().absolute().top_8().children(notification_layer))
|
||||||
|
}
|
||||||
|
}
|
||||||
160
crates/app/src/ui/block/sidebar.rs
Normal file
160
crates/app/src/ui/block/sidebar.rs
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
use components::{theme::ActiveTheme, StyledExt};
|
||||||
|
use gpui::*;
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
use prelude::FluentBuilder;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::Block;
|
||||||
|
use crate::{state::get_client, utils::ago};
|
||||||
|
|
||||||
|
#[derive(Clone, IntoElement)]
|
||||||
|
struct Room {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
public_key: PublicKey,
|
||||||
|
message_at: Timestamp,
|
||||||
|
metadata: Model<Option<Metadata>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Room {
|
||||||
|
pub fn new(public_key: PublicKey, created_at: Timestamp, cx: &mut WindowContext) -> Self {
|
||||||
|
let metadata = cx.new_model(|_| None);
|
||||||
|
let async_metadata = metadata.clone();
|
||||||
|
|
||||||
|
let mut async_cx = cx.to_async();
|
||||||
|
|
||||||
|
cx.foreground_executor()
|
||||||
|
.spawn(async move {
|
||||||
|
let client = get_client().await;
|
||||||
|
let metadata = client
|
||||||
|
.fetch_metadata(public_key, Some(Duration::from_secs(2)))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
async_metadata
|
||||||
|
.update(&mut async_cx, |a, b| {
|
||||||
|
*a = Some(metadata);
|
||||||
|
b.notify()
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
public_key,
|
||||||
|
metadata,
|
||||||
|
message_at: created_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RenderOnce for Room {
|
||||||
|
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
||||||
|
let ago = ago(self.message_at.as_u64());
|
||||||
|
let metadata = match self.metadata.read(cx) {
|
||||||
|
Some(metadata) => div()
|
||||||
|
.flex()
|
||||||
|
.gap_2()
|
||||||
|
.when_some(metadata.picture.clone(), |parent, picture| {
|
||||||
|
parent.child(
|
||||||
|
img(picture)
|
||||||
|
.size_6()
|
||||||
|
.rounded_full()
|
||||||
|
.object_fit(ObjectFit::Cover),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.when_some(metadata.display_name.clone(), |parent, display_name| {
|
||||||
|
parent.child(display_name).font_medium()
|
||||||
|
}),
|
||||||
|
None => div()
|
||||||
|
.flex()
|
||||||
|
.gap_2()
|
||||||
|
.child(div().size_6().rounded_full().bg(cx.theme().muted))
|
||||||
|
.child("Unnamed"),
|
||||||
|
};
|
||||||
|
|
||||||
|
div()
|
||||||
|
.flex()
|
||||||
|
.justify_between()
|
||||||
|
.items_center()
|
||||||
|
.px_2()
|
||||||
|
.text_sm()
|
||||||
|
.child(metadata)
|
||||||
|
.child(ago)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Rooms {
|
||||||
|
rooms: Vec<Room>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Rooms {
|
||||||
|
pub fn new(items: Vec<UnsignedEvent>, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||||
|
let rooms: Vec<Room> = items
|
||||||
|
.iter()
|
||||||
|
.map(|item| Room::new(item.pubkey, item.created_at, cx))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Self { rooms }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for Rooms {
|
||||||
|
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
|
div().flex().flex_col().gap_2().children(self.rooms.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Sidebar {
|
||||||
|
rooms: Model<Option<View<Rooms>>>,
|
||||||
|
focus_handle: FocusHandle,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sidebar {
|
||||||
|
pub fn view(cx: &mut WindowContext) -> View<Self> {
|
||||||
|
cx.new_view(Self::new)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||||
|
let rooms = cx.new_model(|_| None);
|
||||||
|
let async_rooms = rooms.clone();
|
||||||
|
|
||||||
|
let mut async_cx = cx.to_async();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
rooms,
|
||||||
|
focus_handle: cx.focus_handle(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Block for Sidebar {
|
||||||
|
fn title() -> &'static str {
|
||||||
|
"Sidebar"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_view(cx: &mut WindowContext) -> View<impl FocusableView> {
|
||||||
|
Self::view(cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn zoomable() -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FocusableView for Sidebar {
|
||||||
|
fn focus_handle(&self, _: &gpui::AppContext) -> gpui::FocusHandle {
|
||||||
|
self.focus_handle.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for Sidebar {
|
||||||
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
|
let mut content = div();
|
||||||
|
|
||||||
|
if let Some(rooms) = self.rooms.read(cx).as_ref() {
|
||||||
|
content = content.child(rooms.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
div().pt_4().child(content)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
use components::{
|
||||||
|
theme::{ActiveTheme, Colorize},
|
||||||
|
StyledExt,
|
||||||
|
};
|
||||||
use gpui::*;
|
use gpui::*;
|
||||||
|
|
||||||
use super::Block;
|
use super::Block;
|
||||||
@@ -7,15 +11,15 @@ pub struct WelcomeBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl WelcomeBlock {
|
impl WelcomeBlock {
|
||||||
|
pub fn view(cx: &mut WindowContext) -> View<Self> {
|
||||||
|
cx.new_view(Self::new)
|
||||||
|
}
|
||||||
|
|
||||||
fn new(cx: &mut ViewContext<Self>) -> Self {
|
fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
focus_handle: cx.focus_handle(),
|
focus_handle: cx.focus_handle(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn view(cx: &mut WindowContext) -> View<Self> {
|
|
||||||
cx.new_view(Self::new)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Block for WelcomeBlock {
|
impl Block for WelcomeBlock {
|
||||||
@@ -39,7 +43,15 @@ impl FocusableView for WelcomeBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Render for WelcomeBlock {
|
impl Render for WelcomeBlock {
|
||||||
fn render(&mut self, _cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
||||||
div().child("Welcome")
|
div()
|
||||||
|
.size_full()
|
||||||
|
.flex()
|
||||||
|
.items_center()
|
||||||
|
.justify_center()
|
||||||
|
.child("coop on nostr.")
|
||||||
|
.text_color(cx.theme().muted.darken(0.1))
|
||||||
|
.font_black()
|
||||||
|
.text_sm()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
use ::client::NostrClient;
|
|
||||||
use components::{
|
use components::{
|
||||||
input::{InputEvent, TextInput},
|
input::{InputEvent, TextInput},
|
||||||
label::Label,
|
label::Label,
|
||||||
@@ -7,7 +6,7 @@ use gpui::*;
|
|||||||
use keyring::Entry;
|
use keyring::Entry;
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
|
|
||||||
use crate::{constants::KEYRING_SERVICE, state::AppState};
|
use crate::{constants::KEYRING_SERVICE, get_client, states::user::UserState};
|
||||||
|
|
||||||
pub struct Onboarding {
|
pub struct Onboarding {
|
||||||
input: View<TextInput>,
|
input: View<TextInput>,
|
||||||
@@ -23,7 +22,6 @@ impl Onboarding {
|
|||||||
|
|
||||||
cx.subscribe(&input, move |_, text_input, input_event, cx| {
|
cx.subscribe(&input, move |_, text_input, input_event, cx| {
|
||||||
let mut async_cx = cx.to_async();
|
let mut async_cx = cx.to_async();
|
||||||
let client = cx.global::<NostrClient>().client;
|
|
||||||
let view_id = cx.parent_view_id();
|
let view_id = cx.parent_view_id();
|
||||||
|
|
||||||
if let InputEvent::PressEnter = input_event {
|
if let InputEvent::PressEnter = input_event {
|
||||||
@@ -32,6 +30,8 @@ impl Onboarding {
|
|||||||
if let Ok(keys) = Keys::parse(content) {
|
if let Ok(keys) = Keys::parse(content) {
|
||||||
cx.foreground_executor()
|
cx.foreground_executor()
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
|
let client = get_client().await;
|
||||||
|
|
||||||
let public_key = keys.public_key();
|
let public_key = keys.public_key();
|
||||||
let secret = keys.secret_key().to_secret_hex();
|
let secret = keys.secret_key().to_secret_hex();
|
||||||
|
|
||||||
@@ -46,8 +46,8 @@ impl Onboarding {
|
|||||||
client.set_signer(keys).await;
|
client.set_signer(keys).await;
|
||||||
|
|
||||||
// Update view
|
// Update view
|
||||||
async_cx.update_global(|app_state: &mut AppState, cx| {
|
async_cx.update_global(|state: &mut UserState, cx| {
|
||||||
app_state.signer = Some(public_key);
|
state.current_user = Some(public_key);
|
||||||
cx.notify(view_id);
|
cx.notify(view_id);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -64,11 +64,18 @@ impl Onboarding {
|
|||||||
impl Render for Onboarding {
|
impl Render for Onboarding {
|
||||||
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
|
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
div()
|
div()
|
||||||
.size_1_3()
|
.size_full()
|
||||||
.flex()
|
.flex()
|
||||||
.flex_col()
|
.items_center()
|
||||||
.gap_1()
|
.justify_center()
|
||||||
.child(Label::new("Private Key").text_sm())
|
.child(
|
||||||
.child(self.input.clone())
|
div()
|
||||||
|
.size_1_3()
|
||||||
|
.flex()
|
||||||
|
.flex_col()
|
||||||
|
.gap_1()
|
||||||
|
.child(Label::new("Private Key").text_sm())
|
||||||
|
.child(self.input.clone()),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use chrono::{Local, TimeZone};
|
||||||
use keyring_search::{Limit, List, Search};
|
use keyring_search::{Limit, List, Search};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
|
|
||||||
@@ -13,3 +14,16 @@ pub fn get_all_accounts_from_keyring() -> Vec<PublicKey> {
|
|||||||
|
|
||||||
accounts
|
accounts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ago(time: u64) -> String {
|
||||||
|
let now = Local::now();
|
||||||
|
let input_time = Local.timestamp_opt(time as i64, 0).unwrap();
|
||||||
|
let diff = (now - input_time).num_hours();
|
||||||
|
|
||||||
|
if diff < 24 {
|
||||||
|
let duration = now.signed_duration_since(input_time);
|
||||||
|
format!("{} ago", duration.num_hours())
|
||||||
|
} else {
|
||||||
|
input_time.format("%b %d").to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "client"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
gpui.workspace = true
|
|
||||||
nostr-sdk.workspace = true
|
|
||||||
dirs.workspace = true
|
|
||||||
tokio.workspace = true
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
use gpui::Global;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use state::get_client;
|
|
||||||
|
|
||||||
pub mod state;
|
|
||||||
|
|
||||||
pub struct NostrClient {
|
|
||||||
pub client: &'static Client,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Global for NostrClient {}
|
|
||||||
|
|
||||||
impl NostrClient {
|
|
||||||
pub async fn init() -> Self {
|
|
||||||
// Initialize nostr client
|
|
||||||
let client = get_client().await;
|
|
||||||
|
|
||||||
Self { client }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
use dirs::config_dir;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use std::{fs, time::Duration};
|
|
||||||
use tokio::sync::OnceCell;
|
|
||||||
|
|
||||||
pub static CLIENT: OnceCell<Client> = OnceCell::const_new();
|
|
||||||
|
|
||||||
pub async fn get_client() -> &'static Client {
|
|
||||||
CLIENT
|
|
||||||
.get_or_init(|| async {
|
|
||||||
// Setup app data folder
|
|
||||||
let config_dir = config_dir().unwrap();
|
|
||||||
let _ = fs::create_dir_all(config_dir.join("Coop/"));
|
|
||||||
|
|
||||||
// Setup database
|
|
||||||
let lmdb = NostrLMDB::open(config_dir.join("Coop/nostr"))
|
|
||||||
.expect("Database is NOT initialized");
|
|
||||||
|
|
||||||
// Setup Nostr Client
|
|
||||||
let opts = Options::new().gossip(true).timeout(Duration::from_secs(5));
|
|
||||||
let client = ClientBuilder::default().database(lmdb).opts(opts).build();
|
|
||||||
|
|
||||||
// Add some bootstrap relays
|
|
||||||
let _ = client.add_relay("wss://relay.damus.io").await;
|
|
||||||
let _ = client.add_relay("wss://relay.primal.net").await;
|
|
||||||
let _ = client.add_relay("wss://nos.lol").await;
|
|
||||||
let _ = client.add_relay("wss://directory.yabu.me").await;
|
|
||||||
|
|
||||||
let _ = client.add_discovery_relay("wss://user.kindpag.es/").await;
|
|
||||||
let _ = client.add_discovery_relay("wss://purplepag.es").await;
|
|
||||||
|
|
||||||
// Connect to all relays
|
|
||||||
client.connect().await;
|
|
||||||
|
|
||||||
// Return client
|
|
||||||
client
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
pub const KEYRING_SERVICE: &str = "Coop Safe Storage";
|
|
||||||
pub const APP_NAME: &str = "coop";
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
use asset::Assets;
|
|
||||||
use client::NostrClient;
|
|
||||||
use constants::{APP_NAME, KEYRING_SERVICE};
|
|
||||||
use gpui::*;
|
|
||||||
use keyring::Entry;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use state::AppState;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use utils::get_all_accounts_from_keyring;
|
|
||||||
use views::app::AppView;
|
|
||||||
|
|
||||||
pub mod asset;
|
|
||||||
pub mod constants;
|
|
||||||
pub mod state;
|
|
||||||
pub mod utils;
|
|
||||||
pub mod views;
|
|
||||||
|
|
||||||
actions!(main_menu, [Quit]);
|
|
||||||
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() {
|
|
||||||
tracing_subscriber::fmt::init();
|
|
||||||
|
|
||||||
// Initialize nostr client
|
|
||||||
let nostr = NostrClient::init().await;
|
|
||||||
// Initialize app state
|
|
||||||
let app_state = AppState::new();
|
|
||||||
|
|
||||||
App::new()
|
|
||||||
.with_assets(Assets)
|
|
||||||
.with_http_client(Arc::new(reqwest_client::ReqwestClient::new()))
|
|
||||||
.run(move |cx| {
|
|
||||||
// Initialize components
|
|
||||||
components::init(cx);
|
|
||||||
|
|
||||||
// Set global state
|
|
||||||
cx.set_global(nostr);
|
|
||||||
cx.set_global(app_state);
|
|
||||||
|
|
||||||
// Set quit action
|
|
||||||
cx.on_action(quit);
|
|
||||||
|
|
||||||
// Refresh
|
|
||||||
cx.refresh();
|
|
||||||
|
|
||||||
// Login
|
|
||||||
let async_cx = cx.to_async();
|
|
||||||
cx.foreground_executor()
|
|
||||||
.spawn(async move {
|
|
||||||
let accounts = get_all_accounts_from_keyring();
|
|
||||||
|
|
||||||
if let Some(account) = accounts.first() {
|
|
||||||
let client = async_cx
|
|
||||||
.read_global(|nostr: &NostrClient, _cx| nostr.client)
|
|
||||||
.unwrap();
|
|
||||||
let entry =
|
|
||||||
Entry::new(KEYRING_SERVICE, account.to_bech32().unwrap().as_ref())
|
|
||||||
.unwrap();
|
|
||||||
let password = entry.get_password().unwrap();
|
|
||||||
let keys = Keys::parse(password).unwrap();
|
|
||||||
|
|
||||||
client.set_signer(keys).await;
|
|
||||||
|
|
||||||
async_cx
|
|
||||||
.update_global(|app_state: &mut AppState, _cx| {
|
|
||||||
app_state.signer = Some(*account);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
// Set window size
|
|
||||||
let bounds = Bounds::centered(None, size(px(900.0), px(680.0)), cx);
|
|
||||||
|
|
||||||
cx.open_window(
|
|
||||||
WindowOptions {
|
|
||||||
window_bounds: Some(WindowBounds::Windowed(bounds)),
|
|
||||||
window_decorations: Some(WindowDecorations::Client),
|
|
||||||
titlebar: Some(TitlebarOptions {
|
|
||||||
title: Some(SharedString::new_static(APP_NAME)),
|
|
||||||
appears_transparent: true,
|
|
||||||
traffic_light_position: Some(point(px(9.0), px(9.0))),
|
|
||||||
}),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
|cx| cx.new_view(AppView::new),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn quit(_: &Quit, cx: &mut AppContext) {
|
|
||||||
cx.quit();
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
use gpui::Global;
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
|
|
||||||
pub struct AppState {
|
|
||||||
pub signer: Option<PublicKey>,
|
|
||||||
// TODO: add more app state
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Global for AppState {}
|
|
||||||
|
|
||||||
impl AppState {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self { signer: None }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for AppState {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use components::{
|
|
||||||
dock::{DockArea, DockItem},
|
|
||||||
theme::{ActiveTheme, Theme},
|
|
||||||
TitleBar,
|
|
||||||
};
|
|
||||||
use gpui::*;
|
|
||||||
|
|
||||||
use super::{
|
|
||||||
block::{welcome::WelcomeBlock, BlockContainer},
|
|
||||||
onboarding::Onboarding,
|
|
||||||
};
|
|
||||||
use crate::state::AppState;
|
|
||||||
|
|
||||||
pub struct DockAreaTab {
|
|
||||||
id: &'static str,
|
|
||||||
version: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const DOCK_AREA: DockAreaTab = DockAreaTab {
|
|
||||||
id: "dock",
|
|
||||||
version: 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct AppView {
|
|
||||||
onboarding: View<Onboarding>,
|
|
||||||
dock_area: View<DockArea>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AppView {
|
|
||||||
pub fn new(cx: &mut ViewContext<'_, Self>) -> AppView {
|
|
||||||
// Sync theme with system
|
|
||||||
cx.observe_window_appearance(|_, cx| {
|
|
||||||
Theme::sync_system_appearance(cx);
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
// Onboarding
|
|
||||||
let onboarding = cx.new_view(Onboarding::new);
|
|
||||||
|
|
||||||
// Dock
|
|
||||||
let dock_area = cx.new_view(|cx| DockArea::new(DOCK_AREA.id, Some(DOCK_AREA.version), cx));
|
|
||||||
let weak_dock_area = dock_area.downgrade();
|
|
||||||
|
|
||||||
// Set dock layout
|
|
||||||
Self::init_layout(weak_dock_area, cx);
|
|
||||||
|
|
||||||
AppView {
|
|
||||||
onboarding,
|
|
||||||
dock_area,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn init_layout(dock_area: WeakView<DockArea>, cx: &mut WindowContext) {
|
|
||||||
let dock_item = Self::init_dock_items(&dock_area, cx);
|
|
||||||
let left_panels =
|
|
||||||
DockItem::split_with_sizes(Axis::Vertical, vec![], vec![None, None], &dock_area, cx);
|
|
||||||
|
|
||||||
_ = dock_area.update(cx, |view, cx| {
|
|
||||||
view.set_version(DOCK_AREA.version, cx);
|
|
||||||
view.set_left_dock(left_panels, Some(px(260.)), true, cx);
|
|
||||||
view.set_root(dock_item, cx);
|
|
||||||
// TODO: support right dock?
|
|
||||||
// TODO: support bottom dock?
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn init_dock_items(dock_area: &WeakView<DockArea>, cx: &mut WindowContext) -> DockItem {
|
|
||||||
DockItem::split_with_sizes(
|
|
||||||
Axis::Vertical,
|
|
||||||
vec![DockItem::tabs(
|
|
||||||
vec![
|
|
||||||
Arc::new(BlockContainer::panel::<WelcomeBlock>(cx)),
|
|
||||||
Arc::new(BlockContainer::panel::<WelcomeBlock>(cx)),
|
|
||||||
// TODO: add chat block
|
|
||||||
],
|
|
||||||
None,
|
|
||||||
dock_area,
|
|
||||||
cx,
|
|
||||||
)],
|
|
||||||
vec![None],
|
|
||||||
dock_area,
|
|
||||||
cx,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for AppView {
|
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
|
||||||
let mut content = div();
|
|
||||||
|
|
||||||
if cx.global::<AppState>().signer.is_none() {
|
|
||||||
content = content.child(self.onboarding.clone())
|
|
||||||
} else {
|
|
||||||
content = content
|
|
||||||
.size_full()
|
|
||||||
.flex()
|
|
||||||
.flex_col()
|
|
||||||
.child(TitleBar::new())
|
|
||||||
.child(self.dock_area.clone())
|
|
||||||
}
|
|
||||||
|
|
||||||
div()
|
|
||||||
.bg(cx.theme().background)
|
|
||||||
.text_color(cx.theme().foreground)
|
|
||||||
.size_full()
|
|
||||||
.child(content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user