refactor chats (#15)

* refactor

* update

* update

* update

* remove nostrprofile struct

* update

* refactor contacts

* prevent double login
This commit is contained in:
reya
2025-04-10 08:10:53 +07:00
committed by GitHub
parent f7610cc9c9
commit 3246abace1
27 changed files with 1166 additions and 909 deletions

View File

@@ -24,10 +24,3 @@ uuid = "1.10"
once_cell = "1.19.0"
image = "0.25.1"
linkify = "0.10.0"
[dev-dependencies]
criterion = "0.5"
[[bench]]
name = "text_benchmark"
harness = false

View File

@@ -1,142 +0,0 @@
use common::profile::NostrProfile;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use gpui::SharedString;
use nostr_sdk::prelude::*;
use ui::text::render_plain_text_mut;
fn create_test_profiles() -> Vec<NostrProfile> {
let mut profiles = Vec::new();
// Create a few test profiles
for i in 0..5 {
let keypair = Keys::generate();
let profile = NostrProfile {
public_key: keypair.public_key(),
name: SharedString::from(format!("user{}", i)),
avatar: SharedString::from(format!("avatar{}", i)),
// Add other required fields based on NostrProfile definition
// This is a simplified version - adjust based on your actual NostrProfile struct
};
profiles.push(profile);
}
profiles
}
fn benchmark_plain_text(c: &mut Criterion) {
let profiles = create_test_profiles();
// Simple text without any links or entities
let simple_text = "This is a simple text message without any links or entities.";
// Text with URLs
let text_with_urls =
"Check out https://example.com and https://nostr.com for more information.";
// Text with nostr entities
let text_with_nostr = "I found this note nostr:note1qw5uy7hsqs4jcsvmjc2rj5t6f5uuenwg3yapm5l58srprspvshlspr4mh3 from npub1l2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqutajft";
// Mixed content with urls and nostr entities
let mixed_content = "Check out https://example.com and my profile nostr:npub1l2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqutajft along with this event nevent1qw5uy7hsqs4jcsvmjc2rj5t6f5uuenwg3yapm5l58srprspvshlspr4mh3";
// Long text with multiple links and entities
let long_text = "Here's a long message with multiple links like https://example1.com, https://example2.com, and https://example3.com. It also has nostr entities like npub1l2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqutajft, note1qw5uy7hsqs4jcsvmjc2rj5t6f5uuenwg3yapm5l58srprspvshlspr4mh3, and nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuerpd46hxtnfdupzp8xummjw3exgcnqvmpw35xjueqvdnyqystngfxk5hsnfd9h8jtr8a4klacnp".repeat(3);
// Benchmark with simple text
c.bench_function("render_plain_text_simple", |b| {
b.iter(|| {
let mut text = String::new();
let mut highlights = Vec::new();
let mut link_ranges = Vec::new();
let mut link_urls = Vec::new();
render_plain_text_mut(
black_box(simple_text),
black_box(&profiles),
&mut text,
&mut highlights,
&mut link_ranges,
&mut link_urls,
)
})
});
// Benchmark with URLs
c.bench_function("render_plain_text_urls", |b| {
b.iter(|| {
let mut text = String::new();
let mut highlights = Vec::new();
let mut link_ranges = Vec::new();
let mut link_urls = Vec::new();
render_plain_text_mut(
black_box(text_with_urls),
black_box(&profiles),
&mut text,
&mut highlights,
&mut link_ranges,
&mut link_urls,
)
})
});
// Benchmark with nostr entities
c.bench_function("render_plain_text_nostr", |b| {
b.iter(|| {
let mut text = String::new();
let mut highlights = Vec::new();
let mut link_ranges = Vec::new();
let mut link_urls = Vec::new();
render_plain_text_mut(
black_box(text_with_nostr),
black_box(&profiles),
&mut text,
&mut highlights,
&mut link_ranges,
&mut link_urls,
)
})
});
// Benchmark with mixed content
c.bench_function("render_plain_text_mixed", |b| {
b.iter(|| {
let mut text = String::new();
let mut highlights = Vec::new();
let mut link_ranges = Vec::new();
let mut link_urls = Vec::new();
render_plain_text_mut(
black_box(mixed_content),
black_box(&profiles),
&mut text,
&mut highlights,
&mut link_ranges,
&mut link_urls,
)
})
});
// Benchmark with long text
c.bench_function("render_plain_text_long", |b| {
b.iter(|| {
let mut text = String::new();
let mut highlights = Vec::new();
let mut link_ranges = Vec::new();
let mut link_urls = Vec::new();
render_plain_text_mut(
black_box(&long_text),
black_box(&profiles),
&mut text,
&mut highlights,
&mut link_ranges,
&mut link_urls,
)
})
});
}
criterion_group!(benches, benchmark_plain_text);
criterion_main!(benches);

View File

@@ -161,7 +161,7 @@ impl Element for Switch {
if !self.disabled
&& prev_checked
.borrow()
.map_or(false, |prev| prev != checked)
.is_some_and(|prev| prev != checked)
{
let dur = Duration::from_secs_f64(0.15);
cx.spawn(async move |cx| {

View File

@@ -1,4 +1,4 @@
use common::profile::NostrProfile;
use common::profile::SharedProfile;
use gpui::{
AnyElement, AnyView, App, ElementId, FontWeight, HighlightStyle, InteractiveText, IntoElement,
SharedString, StyledText, UnderlineStyle, Window,
@@ -43,7 +43,7 @@ pub struct RichText {
}
impl RichText {
pub fn new(content: String, profiles: &[NostrProfile]) -> Self {
pub fn new(content: String, profiles: &[Profile]) -> Self {
let mut text = String::new();
let mut highlights = Vec::new();
let mut link_ranges = Vec::new();
@@ -154,7 +154,7 @@ impl RichText {
pub fn render_plain_text_mut(
content: &str,
profiles: &[NostrProfile],
profiles: &[Profile],
text: &mut String,
highlights: &mut Vec<(Range<usize>, Highlight)>,
link_ranges: &mut Vec<Range<usize>>,
@@ -164,9 +164,9 @@ pub fn render_plain_text_mut(
text.push_str(content);
// Create a profile lookup using PublicKey directly
let profile_lookup: HashMap<&PublicKey, &NostrProfile> = profiles
let profile_lookup: HashMap<PublicKey, Profile> = profiles
.iter()
.map(|profile| (&profile.public_key, profile))
.map(|profile| (profile.public_key(), profile.clone()))
.collect();
// Process regular URLs using linkify
@@ -263,18 +263,18 @@ pub fn render_plain_text_mut(
let profile_match = if entity_without_prefix.starts_with("npub") {
PublicKey::from_bech32(entity_without_prefix)
.ok()
.and_then(|pubkey| profile_lookup.get(&pubkey).copied())
.and_then(|pubkey| profile_lookup.get(&pubkey).cloned())
} else if entity_without_prefix.starts_with("nprofile") {
Nip19Profile::from_bech32(entity_without_prefix)
.ok()
.and_then(|profile| profile_lookup.get(&profile.public_key).copied())
.and_then(|profile| profile_lookup.get(&profile.public_key).cloned())
} else {
None
};
if let Some(profile) = profile_match {
// Profile found - create a mention
let display_name = format!("@{}", profile.name);
let display_name = format!("@{}", profile.shared_name());
// Replace mention with profile name
text.replace_range(range.clone(), &display_name);