chore: follow up on #172 (#173)

* clean up

* wip

* clean up

* remove unused picture field
This commit is contained in:
reya
2025-10-01 13:45:13 +07:00
committed by GitHub
parent 0db48bc003
commit ebcc60cd92
12 changed files with 273 additions and 333 deletions

View File

@@ -545,7 +545,7 @@ impl ChatSpace {
// Load all chat rooms
registry.update(cx, |this, cx| {
this.set_identity(public_key, cx);
this.set_signer_pubkey(public_key, cx);
this.load_rooms(window, cx);
});
}
@@ -1481,8 +1481,8 @@ impl Render for ChatSpace {
let registry = Registry::read_global(cx);
// Only render titlebar child elements if user is logged in
if registry.identity.is_some() {
let profile = registry.identity(cx);
if let Some(public_key) = registry.signer_pubkey() {
let profile = registry.get_person(&public_key, cx);
let left_side = self
.render_titlebar_left_side(window, cx)

View File

@@ -19,7 +19,6 @@ use registry::room::Room;
use registry::Registry;
use settings::AppSettings;
use smallvec::{smallvec, SmallVec};
use smol::Timer;
use theme::ActiveTheme;
use ui::avatar::Avatar;
use ui::button::{Button, ButtonVariants};
@@ -237,7 +236,7 @@ impl Compose {
});
});
} else {
self.set_error(Some(t!("compose.contact_existed").into()), cx);
self.set_error(t!("compose.contact_existed"), cx);
}
}
@@ -283,7 +282,7 @@ impl Compose {
}
Ok(Err(e)) => {
this.update(cx, |this, cx| {
this.set_error(Some(e.to_string().into()), cx);
this.set_error(e.to_string(), cx);
})
.ok();
}
@@ -312,47 +311,38 @@ impl Compose {
fn submit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let registry = Registry::global(cx);
let public_keys: Vec<PublicKey> = self.selected(cx);
let receivers: Vec<PublicKey> = self.selected(cx);
let subject_input = self.title_input.read(cx).value();
let subject = (!subject_input.is_empty()).then(|| subject_input.to_string());
if !self.user_input.read(cx).value().is_empty() {
self.add_and_select_contact(window, cx);
return;
};
if public_keys.is_empty() {
self.set_error(Some(t!("compose.receiver_required").into()), cx);
return;
};
cx.spawn_in(window, async move |this, cx| {
let result = Room::new(subject, receivers).await;
// Convert selected pubkeys into Nostr tags
let mut tags: Tags = Tags::from_list(
public_keys
.iter()
.map(|pubkey| Tag::public_key(pubkey.to_owned()))
.collect(),
);
this.update_in(cx, |this, window, cx| {
match result {
Ok(room) => {
registry.update(cx, |this, cx| {
this.push_room(cx.new(|_| room), cx);
});
// Add subject if it is present
if !self.title_input.read(cx).value().is_empty() {
tags.push(Tag::custom(
TagKind::Subject,
vec![self.title_input.read(cx).value().to_string()],
));
}
// Create a new room
let room = Room::new(public_keys[0], tags, cx);
// Insert the new room into the registry
registry.update(cx, |this, cx| {
this.push_room(cx.new(|_| room), cx);
});
// Close the current modal
window.close_modal(cx);
window.close_modal(cx);
}
Err(e) => {
this.set_error(e.to_string(), cx);
}
};
})
.ok();
})
.detach();
}
fn set_error(&mut self, error: impl Into<Option<SharedString>>, cx: &mut Context<Self>) {
fn set_error(&mut self, error: impl Into<SharedString>, cx: &mut Context<Self>) {
// Unlock the user input
self.user_input.update(cx, |this, cx| {
this.set_loading(false, cx);
@@ -360,15 +350,19 @@ impl Compose {
// Update error message
self.error_message.update(cx, |this, cx| {
*this = error.into();
*this = Some(error.into());
cx.notify();
});
// Dismiss error after 2 seconds
cx.spawn(async move |this, cx| {
Timer::after(Duration::from_secs(2)).await;
cx.background_executor().timer(Duration::from_secs(2)).await;
this.update(cx, |this, cx| {
this.set_error(None, cx);
this.error_message.update(cx, |this, cx| {
*this = None;
cx.notify();
});
})
.ok();
})

View File

@@ -1,5 +1,6 @@
use common::display::RenderedProfile;
use gpui::http_client::Url;
use gpui::prelude::FluentBuilder;
use gpui::{
div, px, relative, rems, App, AppContext, Context, Entity, InteractiveElement, IntoElement,
ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Window,
@@ -111,9 +112,6 @@ impl Preferences {
impl Render for Preferences {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let input_state = self.media_input.downgrade();
let profile = Registry::read_global(cx).identity(cx);
let auto_auth = AppSettings::get_auto_auth(cx);
let backup = AppSettings::get_backup_messages(cx);
let screening = AppSettings::get_screening(cx);
@@ -121,6 +119,9 @@ impl Render for Preferences {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let hide = AppSettings::get_hide_user_avatars(cx);
let registry = Registry::read_global(cx);
let input_state = self.media_input.downgrade();
v_flex()
.child(
v_flex()
@@ -133,48 +134,54 @@ impl Render for Preferences {
.font_semibold()
.child(shared_t!("preferences.account_header")),
)
.child(
h_flex()
.w_full()
.justify_between()
.child(
h_flex()
.id("user")
.gap_2()
.child(Avatar::new(profile.avatar(proxy)).size(rems(2.4)))
.child(
div()
.flex_1()
.text_sm()
.child(
div()
.font_semibold()
.line_height(relative(1.3))
.child(profile.display_name()),
)
.child(
div()
.text_xs()
.text_color(cx.theme().text_muted)
.line_height(relative(1.3))
.child(shared_t!("preferences.account_btn")),
),
)
.on_click(cx.listener(move |this, _e, window, cx| {
this.open_edit_profile(window, cx);
})),
)
.child(
Button::new("relays")
.label("Messaging Relays")
.xsmall()
.ghost_alt()
.rounded()
.on_click(cx.listener(move |this, _e, window, cx| {
this.open_relays(window, cx);
})),
),
),
.when_some(registry.signer_pubkey(), |this, public_key| {
let profile = registry.get_person(&public_key, cx);
this.child(
h_flex()
.w_full()
.justify_between()
.child(
h_flex()
.id("user")
.gap_2()
.child(Avatar::new(profile.avatar(proxy)).size(rems(2.4)))
.child(
div()
.flex_1()
.text_sm()
.child(
div()
.font_semibold()
.line_height(relative(1.3))
.child(profile.display_name()),
)
.child(
div()
.text_xs()
.text_color(cx.theme().text_muted)
.line_height(relative(1.3))
.child(shared_t!(
"preferences.account_btn"
)),
),
)
.on_click(cx.listener(move |this, _e, window, cx| {
this.open_edit_profile(window, cx);
})),
)
.child(
Button::new("relays")
.label("Messaging Relays")
.xsmall()
.ghost_alt()
.rounded()
.on_click(cx.listener(move |this, _e, window, cx| {
this.open_relays(window, cx);
})),
),
)
}),
)
.child(
v_flex()

View File

@@ -37,33 +37,35 @@ pub struct Screening {
impl Screening {
pub fn new(public_key: PublicKey, window: &mut Window, cx: &mut Context<Self>) -> Self {
let registry = Registry::read_global(cx);
let identity = registry.identity(cx).public_key();
let profile = registry.get_person(&public_key, cx);
let mut tasks = smallvec![];
let contact_check: Task<(bool, Vec<Profile>)> = cx.background_spawn(async move {
let client = nostr_client();
let contact_check: Task<Result<(bool, Vec<Profile>), Error>> =
cx.background_spawn(async move {
let client = nostr_client();
let signer = client.signer().await?;
let signer_pubkey = signer.get_public_key().await?;
// Check if user is in contact list
let contacts = client.database().contacts_public_keys(identity).await;
let followed = contacts.unwrap_or_default().contains(&public_key);
// Check if user is in contact list
let contacts = client.database().contacts_public_keys(signer_pubkey).await;
let followed = contacts.unwrap_or_default().contains(&public_key);
// Check mutual contacts
let contact_list = Filter::new().kind(Kind::ContactList).pubkey(public_key);
let mut mutual_contacts = vec![];
// Check mutual contacts
let contact_list = Filter::new().kind(Kind::ContactList).pubkey(public_key);
let mut mutual_contacts = vec![];
if let Ok(events) = client.database().query(contact_list).await {
for event in events.into_iter().filter(|ev| ev.pubkey != identity) {
if let Ok(metadata) = client.database().metadata(event.pubkey).await {
let profile = Profile::new(event.pubkey, metadata.unwrap_or_default());
mutual_contacts.push(profile);
if let Ok(events) = client.database().query(contact_list).await {
for event in events.into_iter().filter(|ev| ev.pubkey != signer_pubkey) {
if let Ok(metadata) = client.database().metadata(event.pubkey).await {
let profile = Profile::new(event.pubkey, metadata.unwrap_or_default());
mutual_contacts.push(profile);
}
}
}
}
(followed, mutual_contacts)
});
Ok((followed, mutual_contacts))
});
let activity_check = cx.background_spawn(async move {
let client = nostr_client();
@@ -93,14 +95,14 @@ impl Screening {
tasks.push(
// Run the contact check in the background
cx.spawn_in(window, async move |this, cx| {
let (followed, mutual_contacts) = contact_check.await;
this.update(cx, |this, cx| {
this.followed = followed;
this.mutual_contacts = mutual_contacts;
cx.notify();
})
.ok();
if let Ok((followed, mutual_contacts)) = contact_check.await {
this.update(cx, |this, cx| {
this.followed = followed;
this.mutual_contacts = mutual_contacts;
cx.notify();
})
.ok();
}
}),
);

View File

@@ -11,7 +11,6 @@ use gpui::{
};
use i18n::{shared_t, t};
use nostr_sdk::prelude::*;
use registry::Registry;
use smallvec::{smallvec, SmallVec};
use theme::ActiveTheme;
use ui::button::{Button, ButtonVariants};
@@ -70,7 +69,6 @@ pub struct SetupRelay {
impl SetupRelay {
pub fn new(kind: Kind, window: &mut Window, cx: &mut Context<Self>) -> Self {
let identity = Registry::read_global(cx).identity(cx).public_key();
let input = cx.new(|cx| InputState::new(window, cx).placeholder("wss://example.com"));
let mut subscriptions = smallvec![];
@@ -78,7 +76,10 @@ impl SetupRelay {
let load_relay = cx.background_spawn(async move {
let client = nostr_client();
let filter = Filter::new().kind(kind).author(identity).limit(1);
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new().kind(kind).author(public_key).limit(1);
if let Some(event) = client.database().query(filter).await?.first() {
let relays: Vec<RelayUrl> = event

View File

@@ -138,7 +138,8 @@ impl Sidebar {
}
}
async fn request_metadata(client: &Client, public_key: PublicKey) -> Result<(), Error> {
async fn request_metadata(public_key: PublicKey) -> Result<(), Error> {
let client = nostr_client();
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
let kinds = vec![Kind::Metadata, Kind::ContactList, Kind::RelayList];
let filter = Filter::new().author(public_key).kinds(kinds).limit(10);
@@ -152,23 +153,21 @@ impl Sidebar {
Ok(())
}
async fn create_temp_room(identity: PublicKey, public_key: PublicKey) -> Result<Room, Error> {
let client = nostr_client();
let keys = Keys::generate();
let builder = EventBuilder::private_msg_rumor(public_key, "");
let event = builder.build(identity).sign(&keys).await?;
async fn create_temp_room(receiver: PublicKey) -> Result<Room, Error> {
// Request to get user's metadata
Self::request_metadata(client, public_key).await?;
Self::request_metadata(receiver).await?;
// Create a temporary room
let room = Room::from(&event).current_user(identity);
let room = Room::new(None, vec![receiver]).await?;
Ok(room)
}
async fn nip50(identity: PublicKey, query: &str) -> BTreeSet<Room> {
async fn nip50(query: &str) -> Result<BTreeSet<Room>, Error> {
let client = nostr_client();
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let timeout = Duration::from_secs(2);
let mut rooms: BTreeSet<Room> = BTreeSet::new();
@@ -184,18 +183,18 @@ impl Sidebar {
// Process to verify the search results
for event in events.into_iter().unique_by(|event| event.pubkey) {
// Skip if author is match current user
if event.pubkey == identity {
if event.pubkey == public_key {
continue;
}
// Return a temporary room
if let Ok(room) = Self::create_temp_room(identity, event.pubkey).await {
if let Ok(room) = Self::create_temp_room(event.pubkey).await {
rooms.insert(room);
}
}
}
rooms
Ok(rooms)
}
fn debounced_search(&self, window: &mut Window, cx: &mut Context<Self>) -> Task<()> {
@@ -214,15 +213,11 @@ impl Sidebar {
window: &mut Window,
cx: &mut Context<Self>,
) {
let identity = Registry::read_global(cx).identity(cx).public_key();
let query = query.to_owned();
let query_cloned = query.clone();
let task = smol::future::or(
Tokio::spawn(cx, async move {
let rooms = Self::nip50(identity, &query).await;
Some(rooms)
}),
Tokio::spawn(cx, async move { Self::nip50(&query).await.ok() }),
Tokio::spawn(cx, async move {
let _ = rx.recv().await.is_ok();
None
@@ -269,12 +264,11 @@ impl Sidebar {
}
fn search_by_nip05(&mut self, query: &str, window: &mut Window, cx: &mut Context<Self>) {
let identity = Registry::read_global(cx).identity(cx).public_key();
let address = query.to_owned();
let task = Tokio::spawn(cx, async move {
if let Ok(profile) = common::nip05::nip05_profile(&address).await {
Self::create_temp_room(identity, profile.public_key).await
Self::create_temp_room(profile.public_key).await
} else {
Err(anyhow!(t!("sidebar.addr_error")))
}
@@ -323,10 +317,9 @@ impl Sidebar {
return;
};
let identity = Registry::read_global(cx).identity(cx).public_key();
let task: Task<Result<Room, Error>> = cx.background_spawn(async move {
// Create a gift wrap event to represent as room
Self::create_temp_room(identity, public_key).await
Self::create_temp_room(public_key).await
});
cx.spawn_in(window, async move |this, cx| {