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

76
Cargo.lock generated
View File

@@ -482,9 +482,9 @@ dependencies = [
[[package]]
name = "aws-lc-sys"
version = "0.32.1"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ba2e2516bdf37af57fc6ff047855f54abad0066e5c4fdaaeb76dabb2e05bcf5"
checksum = "a2b715a6010afb9e457ca2b7c9d2b9c344baa8baed7b38dc476034c171b32575"
dependencies = [
"bindgen 0.72.1",
"cc",
@@ -661,14 +661,15 @@ checksum = "6099cdc01846bc367c4e7dd630dc5966dccf36b652fae7a74e17b640411a91b2"
[[package]]
name = "blade-graphics"
version = "0.6.0"
source = "git+https://github.com/kvark/blade?rev=bfa594ea697d4b6326ea29f747525c85ecf933b9#bfa594ea697d4b6326ea29f747525c85ecf933b9"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4deb8f595ce7f00dee3543ebf6fd9a20ea86fc421ab79600dac30876250bdae"
dependencies = [
"ash",
"ash-window",
"bitflags 2.9.4",
"bytemuck",
"codespan-reporting 0.11.1",
"codespan-reporting",
"glow",
"gpu-alloc",
"gpu-alloc-ash",
@@ -686,6 +687,7 @@ dependencies = [
"objc2-metal",
"objc2-quartz-core",
"objc2-ui-kit",
"once_cell",
"raw-window-handle",
"slab",
"wasm-bindgen",
@@ -695,7 +697,8 @@ dependencies = [
[[package]]
name = "blade-macros"
version = "0.3.0"
source = "git+https://github.com/kvark/blade?rev=bfa594ea697d4b6326ea29f747525c85ecf933b9#bfa594ea697d4b6326ea29f747525c85ecf933b9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27142319e2f4c264581067eaccb9f80acccdde60d8b4bf57cc50cd3152f109ca"
dependencies = [
"proc-macro2",
"quote",
@@ -704,8 +707,9 @@ dependencies = [
[[package]]
name = "blade-util"
version = "0.2.0"
source = "git+https://github.com/kvark/blade?rev=bfa594ea697d4b6326ea29f747525c85ecf933b9#bfa594ea697d4b6326ea29f747525c85ecf933b9"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a6be3a82c001ba7a17b6f8e413ede5d1004e6047213f8efaf0ffc15b5c4904c"
dependencies = [
"blade-graphics",
"bytemuck",
@@ -1105,16 +1109,6 @@ dependencies = [
"objc",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"termcolor",
"unicode-width 0.1.14",
]
[[package]]
name = "codespan-reporting"
version = "0.12.0"
@@ -1123,13 +1117,13 @@ checksum = "fe6d2e5af09e8c8ad56c969f2157a3d4238cebc7c55f0a517728c38f7b200f81"
dependencies = [
"serde",
"termcolor",
"unicode-width 0.2.1",
"unicode-width",
]
[[package]]
name = "collections"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"indexmap",
"rustc-hash 2.1.1",
@@ -1570,7 +1564,7 @@ dependencies = [
[[package]]
name = "derive_refineable"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"proc-macro2",
"quote",
@@ -2463,9 +2457,9 @@ dependencies = [
[[package]]
name = "glow"
version = "0.14.2"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51fa363f025f5c111e03f13eda21162faeacb6911fe8caa0c0349f9cf0c4483"
checksum = "c5e5ea60d70410161c8bf5da3fdfeaa1c72ed2c15f8bbb9d19fe3a4fad085f08"
dependencies = [
"js-sys",
"slotmap",
@@ -2506,7 +2500,7 @@ dependencies = [
[[package]]
name = "gpui"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"anyhow",
"as-raw-xcb-connection",
@@ -2600,7 +2594,7 @@ dependencies = [
[[package]]
name = "gpui_macros"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"heck 0.5.0",
"proc-macro2",
@@ -2612,7 +2606,7 @@ dependencies = [
[[package]]
name = "gpui_tokio"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"anyhow",
"gpui",
@@ -2832,7 +2826,7 @@ dependencies = [
[[package]]
name = "http_client"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"anyhow",
"bytes",
@@ -2852,7 +2846,7 @@ dependencies = [
[[package]]
name = "http_client_tls"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"rustls",
"rustls-platform-verifier",
@@ -3657,7 +3651,7 @@ dependencies = [
[[package]]
name = "media"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"anyhow",
"bindgen 0.71.1",
@@ -3793,7 +3787,7 @@ dependencies = [
"bit-set",
"bitflags 2.9.4",
"cfg_aliases",
"codespan-reporting 0.12.0",
"codespan-reporting",
"half",
"hashbrown 0.15.5",
"hexf-parse",
@@ -4500,7 +4494,7 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "perf"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"collections",
"serde",
@@ -5108,7 +5102,7 @@ dependencies = [
[[package]]
name = "refineable"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"derive_refineable",
"workspace-hack",
@@ -5262,7 +5256,7 @@ dependencies = [
[[package]]
name = "reqwest_client"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"anyhow",
"bytes",
@@ -5317,7 +5311,7 @@ dependencies = [
[[package]]
name = "rope"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"arrayvec",
"log",
@@ -5812,7 +5806,7 @@ checksum = "0f7d95a54511e0c7be3f51e8867aa8cf35148d7b9445d44de2f943e2b206e749"
[[package]]
name = "semantic_version"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"anyhow",
"serde",
@@ -6264,7 +6258,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "sum_tree"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"arrayvec",
"log",
@@ -7222,12 +7216,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1d386ff53b415b7fe27b50bb44679e2cc4660272694b7b6f3326d8480823a94"
[[package]]
name = "unicode-width"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "unicode-width"
version = "0.2.1"
@@ -7310,7 +7298,7 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "util"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"anyhow",
"async-fs",
@@ -7345,7 +7333,7 @@ dependencies = [
[[package]]
name = "util_macros"
version = "0.1.0"
source = "git+https://github.com/zed-industries/zed#5922f4adce07781467c0e3d172a11cf22fcfdab5"
source = "git+https://github.com/zed-industries/zed#1659fb81e7ead8b050a9fe10bcea6e46c5ded6b0"
dependencies = [
"perf",
"quote",

View File

@@ -1,4 +1,3 @@
use std::collections::HashSet;
use std::hash::{DefaultHasher, Hash, Hasher};
use itertools::Itertools;
@@ -7,26 +6,14 @@ use nostr_sdk::prelude::*;
pub trait EventUtils {
fn uniq_id(&self) -> u64;
fn all_pubkeys(&self) -> Vec<PublicKey>;
fn compare_pubkeys(&self, other: &[PublicKey]) -> bool;
}
impl EventUtils for Event {
fn uniq_id(&self) -> u64 {
let mut hasher = DefaultHasher::new();
let mut pubkeys: Vec<PublicKey> = vec![];
// Add all public keys from event
pubkeys.push(self.pubkey);
pubkeys.extend(self.tags.public_keys().collect::<Vec<_>>());
// Generate unique hash
pubkeys
.into_iter()
.unique()
.sorted()
.collect::<Vec<_>>()
.hash(&mut hasher);
let mut pubkeys: Vec<PublicKey> = self.all_pubkeys();
pubkeys.sort();
pubkeys.hash(&mut hasher);
hasher.finish()
}
@@ -34,15 +21,7 @@ impl EventUtils for Event {
let mut public_keys: Vec<PublicKey> = self.tags.public_keys().copied().collect();
public_keys.push(self.pubkey);
public_keys
}
fn compare_pubkeys(&self, other: &[PublicKey]) -> bool {
let pubkeys = self.all_pubkeys();
let a: HashSet<_> = pubkeys.iter().collect();
let b: HashSet<_> = other.iter().collect();
a == b
public_keys.into_iter().unique().collect()
}
}
@@ -72,12 +51,4 @@ impl EventUtils for UnsignedEvent {
public_keys
}
fn compare_pubkeys(&self, other: &[PublicKey]) -> bool {
let pubkeys = self.all_pubkeys();
let a: HashSet<_> = pubkeys.iter().collect();
let b: HashSet<_> = other.iter().collect();
a == b
}
}

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| {

View File

@@ -147,28 +147,40 @@ impl Ingester {
/// A simple storage to store all states that using across the application.
#[derive(Debug)]
pub struct AppState {
/// The timestamp when the application was initialized.
pub init_at: Timestamp,
/// The timestamp when the application was last used.
pub last_used_at: Option<Timestamp>,
/// Whether this is the first run of the application.
pub is_first_run: AtomicBool,
/// Subscription ID for listening to gift wrap events from relays.
pub gift_wrap_sub_id: SubscriptionId,
pub gift_wrap_processing: AtomicBool,
/// Auto-close options for relay subscriptions
pub auto_close_opts: Option<SubscribeAutoCloseOptions>,
/// Whether gift wrap processing is in progress.
pub gift_wrap_processing: AtomicBool,
/// Tracking events sent by Coop in the current session
pub sent_ids: RwLock<HashSet<EventId>>,
/// Tracking events seen on which relays in the current session
pub seen_on_relays: RwLock<HashMap<EventId, HashSet<RelayUrl>>>,
/// Tracking events that have been resent by Coop in the current session
pub resent_ids: RwLock<Vec<Output<EventId>>>,
/// Temporarily store events that need to be resent later
pub resend_queue: RwLock<HashMap<EventId, RelayUrl>>,
/// Signal channel for communication between Nostr and GPUI
pub signal: Signal,
/// Ingester channel for processing public keys
pub ingester: Ingester,
}

View File

@@ -44,8 +44,8 @@ pub struct Registry {
/// Status of the unwrapping process
pub unwrapping_status: Entity<UnwrappingStatus>,
/// Public Key of the current user
pub identity: Option<PublicKey>,
/// Public key of the currently activated signer
signer_pubkey: Option<PublicKey>,
/// Tasks for asynchronous operations
_tasks: SmallVec<[Task<()>; 1]>,
@@ -106,21 +106,19 @@ impl Registry {
unwrapping_status,
rooms: vec![],
persons: HashMap::new(),
identity: None,
signer_pubkey: None,
_tasks: tasks,
}
}
/// Returns the identity of the user.
///
/// WARNING: This method will panic if user is not logged in.
pub fn identity(&self, cx: &App) -> Profile {
self.get_person(&self.identity.unwrap(), cx)
/// Returns the public key of the currently activated signer.
pub fn signer_pubkey(&self) -> Option<PublicKey> {
self.signer_pubkey
}
/// Sets the identity of the user.
pub fn set_identity(&mut self, identity: PublicKey, cx: &mut Context<Self>) {
self.identity = Some(identity);
/// Update the public key of the currently activated signer.
pub fn set_signer_pubkey(&mut self, public_key: PublicKey, cx: &mut Context<Self>) {
self.signer_pubkey = Some(public_key);
cx.notify();
}
@@ -254,7 +252,7 @@ impl Registry {
self.set_unwrapping_status(UnwrappingStatus::default(), cx);
// Clear the current identity
self.identity = None;
self.signer_pubkey = None;
// Clear all current rooms
self.rooms.clear();
@@ -276,7 +274,7 @@ impl Registry {
let contacts = client.database().contacts_public_keys(public_key).await?;
// Get messages sent by the user
let send = Filter::new()
let sent = Filter::new()
.kind(Kind::PrivateDirectMessage)
.author(public_key);
@@ -285,9 +283,9 @@ impl Registry {
.kind(Kind::PrivateDirectMessage)
.pubkey(public_key);
let send_events = client.database().query(send).await?;
let sent_events = client.database().query(sent).await?;
let recv_events = client.database().query(recv).await?;
let events = send_events.merge(recv_events);
let events = sent_events.merge(recv_events);
let mut rooms: HashSet<Room> = HashSet::new();
@@ -297,12 +295,16 @@ impl Registry {
.sorted_by_key(|event| Reverse(event.created_at))
.filter(|ev| ev.tags.public_keys().peekable().peek().is_some())
{
if rooms.iter().any(|room| room.id == event.uniq_id()) {
// Parse the room from the nostr event
let room = Room::from(&event);
// Skip if the room is already in the set
if rooms.iter().any(|r| r.id == room.id) {
continue;
}
// Get all public keys from the event's tags
let mut public_keys = event.all_pubkeys();
let mut public_keys: Vec<PublicKey> = room.members().to_vec();
public_keys.retain(|pk| pk != &public_key);
// Bypass screening flag
@@ -323,9 +325,6 @@ impl Registry {
// If current user has sent a message at least once, mark as ongoing
let is_ongoing = client.database().count(filter).await.unwrap_or(1) >= 1;
// Create a new room
let room = Room::from(&event).current_user(public_key);
if is_ongoing || bypassed {
rooms.insert(room.kind(RoomKind::Ongoing));
} else {
@@ -419,7 +418,7 @@ impl Registry {
/// Updates room ordering based on the most recent messages.
pub fn event_to_message(
&mut self,
gift_wrap_id: EventId,
gift_wrap: EventId,
event: Event,
window: &mut Window,
cx: &mut Context<Self>,
@@ -427,7 +426,7 @@ impl Registry {
let id = event.uniq_id();
let author = event.pubkey;
let Some(identity) = self.identity else {
let Some(public_key) = self.signer_pubkey else {
return;
};
@@ -441,13 +440,13 @@ impl Registry {
}
// Set this room is ongoing if the new message is from current user
if author == identity {
if author == public_key {
this.set_ongoing(cx);
}
// Emit the new message to the room
cx.defer_in(window, move |this, _window, cx| {
this.emit_message(gift_wrap_id, event, cx);
this.emit_message(gift_wrap, event, cx);
});
});
@@ -458,10 +457,8 @@ impl Registry {
});
}
} else {
let room = Room::from(&event).current_user(identity);
// Push the new room to the front of the list
self.add_room(cx.new(|_| room), cx);
self.add_room(cx.new(|_| Room::from(&event)), cx);
// Notify the UI about the new room
cx.defer_in(window, move |_this, _window, cx| {

View File

@@ -88,8 +88,6 @@ pub struct Room {
pub created_at: Timestamp,
/// Subject of the room
pub subject: Option<String>,
/// Picture of the room
pub picture: Option<String>,
/// All members of the room
pub members: Vec<PublicKey>,
/// Kind
@@ -130,32 +128,18 @@ impl From<&Event> for Room {
let created_at = val.created_at;
// Get the members from the event's tags and event's pubkey
let members = val
.all_pubkeys()
.into_iter()
.unique()
.sorted()
.collect_vec();
let members = val.all_pubkeys();
// Get the subject from the event's tags
let subject = if let Some(tag) = val.tags.find(TagKind::Subject) {
tag.content().map(|s| s.to_owned())
} else {
None
};
// Get the picture from the event's tags
let picture = if let Some(tag) = val.tags.find(TagKind::custom("picture")) {
tag.content().map(|s| s.to_owned())
} else {
None
};
// Get subject from tags
let subject = val
.tags
.find(TagKind::Subject)
.and_then(|tag| tag.content().map(|s| s.to_owned()));
Room {
id,
created_at,
subject,
picture,
members,
kind: RoomKind::default(),
}
@@ -168,32 +152,18 @@ impl From<&UnsignedEvent> for Room {
let created_at = val.created_at;
// Get the members from the event's tags and event's pubkey
let members = val
.all_pubkeys()
.into_iter()
.unique()
.sorted()
.collect_vec();
let members = val.all_pubkeys();
// Get the subject from the event's tags
let subject = if let Some(tag) = val.tags.find(TagKind::Subject) {
tag.content().map(|s| s.to_owned())
} else {
None
};
// Get the picture from the event's tags
let picture = if let Some(tag) = val.tags.find(TagKind::custom("picture")) {
tag.content().map(|s| s.to_owned())
} else {
None
};
// Get subject from tags
let subject = val
.tags
.find(TagKind::Subject)
.and_then(|tag| tag.content().map(|s| s.to_owned()));
Room {
id,
created_at,
subject,
picture,
members,
kind: RoomKind::default(),
}
@@ -201,32 +171,39 @@ impl From<&UnsignedEvent> for Room {
}
impl Room {
/// Constructs a new room instance with a given receiver.
pub fn new(receiver: PublicKey, tags: Tags, cx: &App) -> Self {
let identity = Registry::read_global(cx).identity(cx);
/// Constructs a new room instance for a private message with the given receiver and tags.
pub async fn new(subject: Option<String>, receivers: Vec<PublicKey>) -> Result<Self, Error> {
let client = nostr_client();
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let mut event = EventBuilder::private_msg_rumor(receiver, "")
if receivers.is_empty() {
return Err(anyhow!("You need to add at least one receiver"));
};
// Convert receiver's public keys into tags
let mut tags: Tags = Tags::from_list(
receivers
.iter()
.map(|pubkey| Tag::public_key(pubkey.to_owned()))
.collect(),
);
// Add subject if it is present
if let Some(subject) = subject {
tags.push(Tag::from_standardized_without_cell(TagStandard::Subject(
subject,
)));
}
let mut event = EventBuilder::new(Kind::PrivateDirectMessage, "")
.tags(tags)
.build(identity.public_key());
.build(public_key);
// Ensure event ID is generated
// Generate event ID
event.ensure_id();
Room::from(&event).current_user(identity.public_key())
}
/// Constructs a new room instance from an nostr event.
pub fn from(event: impl Into<Room>) -> Self {
event.into()
}
/// Call this function to ensure the current user is always at the bottom of the members list
pub fn current_user(mut self, public_key: PublicKey) -> Self {
let (not_match, matches): (Vec<PublicKey>, Vec<PublicKey>) =
self.members.iter().partition(|&key| key != &public_key);
self.members = not_match;
self.members.extend(matches);
self
Ok(Room::from(&event))
}
/// Sets the kind of the room and returns the modified room
@@ -255,13 +232,12 @@ impl Room {
cx.notify();
}
/// Updates the picture of the room
pub fn set_picture(&mut self, picture: String, cx: &mut Context<Self>) {
self.picture = Some(picture);
cx.notify();
/// Returns the members of the room
pub fn members(&self) -> &Vec<PublicKey> {
&self.members
}
/// Checks if the room is a group chat
/// Checks if the room has more than two members (group)
pub fn is_group(&self) -> bool {
self.members.len() > 2
}
@@ -277,20 +253,27 @@ impl Room {
/// Gets the display image for the room
pub fn display_image(&self, proxy: bool, cx: &App) -> SharedUri {
if let Some(picture) = self.picture.as_ref() {
SharedUri::from(picture)
} else if !self.is_group() {
self.first_member(cx).avatar(proxy)
if !self.is_group() {
self.display_member(cx).avatar(proxy)
} else {
SharedUri::from("brand/group.png")
}
}
/// Get the first member of the room.
/// Get a single member to represent the room
///
/// First member is always different from the current user.
fn first_member(&self, cx: &App) -> Profile {
/// This member is always different from the current user.
fn display_member(&self, cx: &App) -> Profile {
let registry = Registry::read_global(cx);
if let Some(public_key) = registry.signer_pubkey() {
for member in self.members() {
if member != &public_key {
return registry.get_person(member, cx);
}
}
}
registry.get_person(&self.members[0], cx)
}
@@ -299,11 +282,11 @@ impl Room {
let registry = Registry::read_global(cx);
if self.is_group() {
let profiles = self
let profiles: Vec<Profile> = self
.members
.iter()
.map(|pk| registry.get_person(pk, cx))
.collect::<Vec<_>>();
.map(|public_key| registry.get_person(public_key, cx))
.collect();
let mut name = profiles
.iter()
@@ -318,7 +301,7 @@ impl Room {
SharedString::from(name)
} else {
self.first_member(cx).display_name()
self.display_member(cx).display_name()
}
}
@@ -455,9 +438,8 @@ impl Room {
/// Create a new message event (unsigned)
pub fn create_message(&self, content: &str, replies: &[EventId], cx: &App) -> UnsignedEvent {
let public_key = Registry::read_global(cx).identity(cx).public_key();
let public_key = Registry::read_global(cx).signer_pubkey().unwrap();
let subject = self.subject.clone();
let picture = self.picture.clone();
let mut tags = vec![];
@@ -470,22 +452,17 @@ impl Room {
// Add subject tag if it's present
if let Some(subject) = subject {
tags.push(Tag::from_standardized(TagStandard::Subject(
subject.to_string(),
tags.push(Tag::from_standardized_without_cell(TagStandard::Subject(
subject,
)));
}
// Add picture tag if it's present
if let Some(picture) = picture {
tags.push(Tag::custom(TagKind::custom("picture"), vec![picture]));
}
// Add reply/quote tag
if replies.len() == 1 {
tags.push(Tag::event(replies[0]))
} else {
for id in replies {
tags.push(Tag::from_standardized(TagStandard::Quote {
tags.push(Tag::from_standardized_without_cell(TagStandard::Quote {
event_id: id.to_owned(),
relay_url: None,
public_key: None,
@@ -677,7 +654,7 @@ impl Room {
client.connect_relay(url).await?;
}
relay_urls.extend(urls);
relay_urls.extend(urls.into_iter().take(3).unique());
} else {
return Err(anyhow!("Not found"));
}

View File

@@ -331,8 +331,6 @@ compose:
en: "Your recently contacts will appear here."
contact_existed:
en: "Contact already added"
receiver_required:
en: "You need to add at least 1 receiver"
description:
en: "Start a conversation with someone using their npub or NIP-05 (like foo@bar.com)."
subject_label: