feat: sharpen chat experiences (#9)
* feat: add global account and refactor chat registry * chore: improve last seen * chore: reduce string alloc * wip: refactor room * chore: fix edit profile panel * chore: refactor open window in main * chore: refactor sidebar * chore: refactor room
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
use asset::Assets;
|
||||
use chats::registry::ChatRegistry;
|
||||
use common::{
|
||||
constants::{ALL_MESSAGES_SUB_ID, APP_ID, APP_NAME, KEYRING_SERVICE, NEW_MESSAGE_SUB_ID},
|
||||
profile::NostrProfile,
|
||||
use common::constants::{
|
||||
ALL_MESSAGES_SUB_ID, APP_ID, APP_NAME, KEYRING_SERVICE, NEW_MESSAGE_SUB_ID,
|
||||
};
|
||||
use futures::{select, FutureExt};
|
||||
use gpui::{
|
||||
@@ -14,16 +13,16 @@ use gpui::{point, SharedString, TitlebarOptions};
|
||||
#[cfg(target_os = "linux")]
|
||||
use gpui::{WindowBackgroundAppearance, WindowDecorations};
|
||||
use log::{error, info};
|
||||
use nostr_sdk::SubscriptionId;
|
||||
use nostr_sdk::{
|
||||
pool::prelude::ReqExitPolicy, Client, Event, Filter, Keys, Kind, Metadata, PublicKey,
|
||||
RelayMessage, RelayPoolNotification, SubscribeAutoCloseOptions,
|
||||
pool::prelude::ReqExitPolicy, Client, Event, Filter, Keys, Kind, PublicKey, RelayMessage,
|
||||
RelayPoolNotification, SubscribeAutoCloseOptions,
|
||||
};
|
||||
use nostr_sdk::{prelude::NostrEventsDatabaseExt, FromBech32, SubscriptionId};
|
||||
use smol::Timer;
|
||||
use state::get_client;
|
||||
use std::{collections::HashSet, mem, sync::Arc, time::Duration};
|
||||
use ui::{theme::Theme, Root};
|
||||
use views::{app, onboarding, startup};
|
||||
use views::{app, onboarding};
|
||||
|
||||
mod asset;
|
||||
mod views;
|
||||
@@ -45,7 +44,7 @@ fn main() {
|
||||
// Enable logging
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let (event_tx, event_rx) = smol::channel::bounded::<Signal>(2048);
|
||||
let (event_tx, event_rx) = smol::channel::bounded::<Signal>(1024);
|
||||
let (batch_tx, batch_rx) = smol::channel::bounded::<Vec<PublicKey>>(100);
|
||||
|
||||
// Initialize nostr client
|
||||
@@ -176,31 +175,17 @@ fn main() {
|
||||
// Handle re-open window
|
||||
app.on_reopen(move |cx| {
|
||||
let client = get_client();
|
||||
let (tx, rx) = oneshot::channel::<Option<NostrProfile>>();
|
||||
let (tx, rx) = oneshot::channel::<bool>();
|
||||
|
||||
cx.background_spawn(async move {
|
||||
if let Ok(signer) = client.signer().await {
|
||||
if let Ok(public_key) = signer.get_public_key().await {
|
||||
let metadata =
|
||||
if let Ok(Some(metadata)) = client.database().metadata(public_key).await {
|
||||
metadata
|
||||
} else {
|
||||
Metadata::new()
|
||||
};
|
||||
|
||||
_ = tx.send(Some(NostrProfile::new(public_key, metadata)));
|
||||
} else {
|
||||
_ = tx.send(None);
|
||||
}
|
||||
} else {
|
||||
_ = tx.send(None);
|
||||
}
|
||||
let is_login = client.signer().await.is_ok();
|
||||
_ = tx.send(is_login);
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.spawn(|mut cx| async move {
|
||||
if let Ok(result) = rx.await {
|
||||
_ = restore_window(result, &mut cx).await;
|
||||
if let Ok(is_login) = rx.await {
|
||||
_ = restore_window(is_login, &mut cx).await;
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
@@ -223,115 +208,90 @@ fn main() {
|
||||
items: vec![MenuItem::action("Quit", Quit)],
|
||||
}]);
|
||||
|
||||
// Open window with default options
|
||||
cx.open_window(
|
||||
WindowOptions {
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
titlebar: Some(TitlebarOptions {
|
||||
title: Some(SharedString::new_static(APP_NAME)),
|
||||
traffic_light_position: Some(point(px(9.0), px(9.0))),
|
||||
appears_transparent: true,
|
||||
}),
|
||||
window_bounds: Some(WindowBounds::Windowed(Bounds::centered(
|
||||
None,
|
||||
size(px(900.0), px(680.0)),
|
||||
cx,
|
||||
))),
|
||||
#[cfg(target_os = "linux")]
|
||||
window_background: WindowBackgroundAppearance::Transparent,
|
||||
#[cfg(target_os = "linux")]
|
||||
window_decorations: Some(WindowDecorations::Client),
|
||||
kind: WindowKind::Normal,
|
||||
..Default::default()
|
||||
},
|
||||
|window, cx| {
|
||||
window.set_window_title(APP_NAME);
|
||||
window.set_app_id(APP_ID);
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
window
|
||||
.observe_window_appearance(|window, cx| {
|
||||
Theme::sync_system_appearance(Some(window), cx);
|
||||
})
|
||||
.detach();
|
||||
|
||||
let handle = window.window_handle();
|
||||
let root = cx.new(|cx| Root::new(startup::init(window, cx).into(), window, cx));
|
||||
|
||||
let task = cx.read_credentials(KEYRING_SERVICE);
|
||||
let (tx, rx) = oneshot::channel::<Option<NostrProfile>>();
|
||||
|
||||
// Read credential in OS Keyring
|
||||
cx.background_spawn(async {
|
||||
let profile = if let Ok(Some((npub, secret))) = task.await {
|
||||
let public_key = PublicKey::from_bech32(&npub).unwrap();
|
||||
let secret_hex = String::from_utf8(secret).unwrap();
|
||||
let keys = Keys::parse(&secret_hex).unwrap();
|
||||
|
||||
// Update nostr signer
|
||||
_ = client.set_signer(keys).await;
|
||||
|
||||
// Get user's metadata
|
||||
let metadata = if let Ok(Some(metadata)) =
|
||||
client.database().metadata(public_key).await
|
||||
{
|
||||
metadata
|
||||
} else {
|
||||
Metadata::new()
|
||||
// Spawn a task to handle events from nostr channel
|
||||
cx.spawn(|cx| async move {
|
||||
while let Ok(signal) = event_rx.recv().await {
|
||||
cx.update(|cx| {
|
||||
if let Some(chats) = ChatRegistry::global(cx) {
|
||||
match signal {
|
||||
Signal::Eose => chats.update(cx, |this, cx| this.load_chat_rooms(cx)),
|
||||
Signal::Event(event) => {
|
||||
chats.update(cx, |this, cx| this.push_message(event, cx))
|
||||
}
|
||||
};
|
||||
|
||||
Some(NostrProfile::new(public_key, metadata))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
_ = tx.send(profile)
|
||||
})
|
||||
.detach();
|
||||
|
||||
// Set root view based on credential status
|
||||
cx.spawn(|mut cx| async move {
|
||||
if let Ok(Some(profile)) = rx.await {
|
||||
_ = cx.update_window(handle, |_, window, cx| {
|
||||
window.replace_root(cx, |window, cx| {
|
||||
Root::new(app::init(profile, window, cx).into(), window, cx)
|
||||
});
|
||||
});
|
||||
} else {
|
||||
_ = cx.update_window(handle, |_, window, cx| {
|
||||
window.replace_root(cx, |window, cx| {
|
||||
Root::new(onboarding::init(window, cx).into(), window, cx)
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
.ok();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.spawn(|cx| async move {
|
||||
while let Ok(signal) = event_rx.recv().await {
|
||||
cx.update(|cx| {
|
||||
match signal {
|
||||
Signal::Eose => {
|
||||
if let Some(chats) = ChatRegistry::global(cx) {
|
||||
chats.update(cx, |this, cx| this.load_chat_rooms(cx))
|
||||
}
|
||||
}
|
||||
Signal::Event(event) => {
|
||||
if let Some(chats) = ChatRegistry::global(cx) {
|
||||
chats.update(cx, |this, cx| this.push_message(event, cx))
|
||||
}
|
||||
}
|
||||
};
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
// Set up the window options
|
||||
let window_opts = WindowOptions {
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
titlebar: Some(TitlebarOptions {
|
||||
title: Some(SharedString::new_static(APP_NAME)),
|
||||
traffic_light_position: Some(point(px(9.0), px(9.0))),
|
||||
appears_transparent: true,
|
||||
}),
|
||||
window_bounds: Some(WindowBounds::Windowed(Bounds::centered(
|
||||
None,
|
||||
size(px(900.0), px(680.0)),
|
||||
cx,
|
||||
))),
|
||||
#[cfg(target_os = "linux")]
|
||||
window_background: WindowBackgroundAppearance::Transparent,
|
||||
#[cfg(target_os = "linux")]
|
||||
window_decorations: Some(WindowDecorations::Client),
|
||||
kind: WindowKind::Normal,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
root
|
||||
},
|
||||
)
|
||||
.expect("System error. Please re-open the app.");
|
||||
// Create a task to read credentials from the keyring service
|
||||
let task = cx.read_credentials(KEYRING_SERVICE);
|
||||
let (tx, rx) = oneshot::channel::<bool>();
|
||||
|
||||
// Read credential in OS Keyring
|
||||
cx.background_spawn(async {
|
||||
let is_ready = if let Ok(Some((_, secret))) = task.await {
|
||||
let result = async {
|
||||
let secret_hex = String::from_utf8(secret)?;
|
||||
let keys = Keys::parse(&secret_hex)?;
|
||||
|
||||
// Update nostr signer
|
||||
client.set_signer(keys).await;
|
||||
|
||||
Ok::<_, anyhow::Error>(true)
|
||||
}
|
||||
.await;
|
||||
|
||||
result.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
_ = tx.send(is_ready)
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.spawn(|cx| async move {
|
||||
if let Ok(is_ready) = rx.await {
|
||||
if is_ready {
|
||||
// Open a App window
|
||||
cx.open_window(window_opts, |window, cx| {
|
||||
cx.new(|cx| Root::new(app::init(window, cx).into(), window, cx))
|
||||
})
|
||||
.expect("Failed to open window");
|
||||
} else {
|
||||
// Open a Onboarding window
|
||||
cx.open_window(window_opts, |window, cx| {
|
||||
cx.new(|cx| Root::new(onboarding::init(window, cx).into(), window, cx))
|
||||
})
|
||||
.expect("Failed to open window");
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -347,7 +307,7 @@ async fn sync_metadata(client: &Client, buffer: HashSet<PublicKey>) {
|
||||
}
|
||||
}
|
||||
|
||||
async fn restore_window(profile: Option<NostrProfile>, cx: &mut AsyncApp) -> anyhow::Result<()> {
|
||||
async fn restore_window(is_login: bool, cx: &mut AsyncApp) -> anyhow::Result<()> {
|
||||
let opts = cx
|
||||
.update(|cx| WindowOptions {
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
@@ -370,7 +330,7 @@ async fn restore_window(profile: Option<NostrProfile>, cx: &mut AsyncApp) -> any
|
||||
})
|
||||
.expect("Failed to set window options.");
|
||||
|
||||
if let Some(profile) = profile {
|
||||
if is_login {
|
||||
_ = cx.open_window(opts, |window, cx| {
|
||||
window.set_window_title(APP_NAME);
|
||||
window.set_app_id(APP_ID);
|
||||
@@ -382,7 +342,7 @@ async fn restore_window(profile: Option<NostrProfile>, cx: &mut AsyncApp) -> any
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.new(|cx| Root::new(app::init(profile, window, cx).into(), window, cx))
|
||||
cx.new(|cx| Root::new(app::init(window, cx).into(), window, cx))
|
||||
});
|
||||
} else {
|
||||
_ = cx.open_window(opts, |window, cx| {
|
||||
|
||||
Reference in New Issue
Block a user