23 Commits
Author SHA1 Message Date
reya c12856cda0 chore: bump version 2025-09-16 20:31:10 +07:00
reyaandGitHub c67b223a53 chore: add missing ui elements (#153)
* add empty state

* .

* update welcome panel
2025-09-16 19:59:03 +07:00
reyaandGitHub 9880a3ed3d chore: follow up on #151 (#152)
* improve ui

* .

* clean up
2025-09-15 20:53:25 +07:00
reyaandGitHub d13ffd5a54 feat: detect user dm relays when opening chat panel (#151)
* preconnect to user messaging relays

* .
2025-09-15 19:34:48 +07:00
reya cc79f0ed1c chore: clean up 2025-09-15 09:10:37 +07:00
reyaandGitHub 5127eaadbb feat: add seen-on-relays viewer per message (#149)
* chore: bump version

* add seen on

* seen on menu
2025-09-14 11:50:14 +07:00
reya d38e70ecbf chore: update deps 2025-09-13 07:51:33 +07:00
reyaandGitHub b142982ab1 chore: refactor event fetching (#148)
* use stream for nip65 and nip17 relays fetching

* .
2025-09-13 07:42:17 +07:00
reyaandGitHub 2ea2519e8b feat: resend failed messages (#147)
* .

* .

* fix

* fix

* update

* fix

* .

* .
2025-09-12 17:07:57 +07:00
reyaandGitHub 2ea5feaf4b chore: improve handling of user profiles (#146)
* resubscribe metadata for all pubkeys

* .
2025-09-10 10:06:45 +07:00
reya 4ec7530b91 chore: update deps 2025-09-10 08:21:43 +07:00
reya df82861101 chore: bump version 2025-09-10 07:25:15 +07:00
reyaandGitHub fc99ef4dfe chore: improve the activity check (#145)
* better check for activity

* .
2025-09-09 10:23:40 +07:00
reyaandGitHub d0f7a1abd3 feat: extended screening (#144)
* improve mutual contacts check

* .

* .
2025-09-08 17:11:29 +07:00
reyaandGitHub 71140beb52 feat: relay status viewer (#143)
* add relay status

* .
2025-09-07 14:54:28 +07:00
reyaandGitHub e177facef4 chore: better handle async tasks (#142)
* improve some codes

* .
2025-09-07 08:33:21 +07:00
reya 60bca49200 chore: update deps and refactor the event loop 2025-09-06 20:55:11 +07:00
reyaandGitHub ede41c41c3 chore: improve the event loop (#141)
* improve wait for signer

* refactor gift wrap processor

* .

* .

* .

* .

* .
2025-09-05 19:01:26 +07:00
reyaandGitHub 70e235dcc2 chore: minor ui components improvements (#140)
* improve ui

* .

* .
2025-09-04 07:30:03 +07:00
reya b11b0e0115 chore: update deps 2025-09-03 12:12:40 +07:00
reyaandGitHub d8edac0bb9 chore: fix rooms out of order while loading (#139)
* fix room out of order while loading

* .

* .
2025-09-03 09:16:36 +07:00
reyaandGitHub d392602ed6 feat: resend messages after authentication (#137)
* resend failed message

* update settings
2025-09-02 18:19:53 +07:00
reyaandGitHub 5a36354cc8 chore: fix handling of ongoing room kind incorrectly (#136) 2025-09-01 19:44:23 +07:00
52 changed files with 2637 additions and 1634 deletions
+1 -1
View File
@@ -157,7 +157,7 @@ jobs:
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.version.outputs.tag }}
name: Release ${{ steps.version.outputs.tag }}
name: ${{ steps.version.outputs.tag }}
draft: true
prerelease: false
generate_release_notes: true
Generated
+424 -317
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -4,7 +4,7 @@ members = ["crates/*"]
default-members = ["crates/coop"]
[workspace.package]
version = "0.2.5"
version = "0.2.8"
edition = "2021"
publish = false
@@ -42,6 +42,7 @@ itertools = "0.13.0"
log = "0.4"
oneshot = "0.1.10"
reqwest = { version = "0.12", features = ["multipart", "stream", "json"] }
flume = { version = "0.11.1", default-features = false, features = ["async", "select"] }
rust-embed = "8.5.0"
rust-i18n = "3"
serde = { version = "1.0", features = ["derive"] }
Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

-3
View File
@@ -1,3 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path fill="currentColor" fill-rule="evenodd" d="M2 12C2 6.477 6.477 2 12 2s10 4.477 10 10-4.477 10-10 10S2 17.523 2 12Zm6.22-1.53 3.25-3.25a.75.75 0 0 1 1.06 0l3.25 3.25a.75.75 0 1 1-1.06 1.06l-1.97-1.97v6.69a.75.75 0 0 1-1.5 0V9.56l-1.97 1.97a.75.75 0 0 1-1.06-1.06Z" clip-rule="evenodd"/>
</svg>

Before

Width:  |  Height:  |  Size: 397 B

-4
View File
@@ -1,4 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path fill="currentColor" d="M4 4.75A2.75 2.75 0 0 1 6.75 2h10.5A2.75 2.75 0 0 1 20 4.75v7.917a3.9 3.9 0 0 0-4.091.909l-3.75 3.75a2.25 2.25 0 0 0-.659 1.59v2.334c0 .263.045.515.128.75H6.75A2.75 2.75 0 0 1 4 19.25V4.75Z"/>
<path fill="currentColor" fill-rule="evenodd" d="M19.303 15.697a.9.9 0 0 0-1.273 0l-3.53 3.53V20.5h1.273l3.53-3.53a.9.9 0 0 0 0-1.273Zm-2.333-1.06a2.4 2.4 0 1 1 3.394 3.393l-3.75 3.75a.75.75 0 0 1-.53.22H13.75a.75.75 0 0 1-.75-.75v-2.333a.75.75 0 0 1 .22-.53l3.75-3.75Z" clip-rule="evenodd"/>
</svg>

Before

Width:  |  Height:  |  Size: 622 B

-3
View File
@@ -1,3 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path stroke="currentColor" stroke-linejoin="round" stroke-width="1.5" d="m21.76 11.45-8.146-7.535a.75.75 0 0 0-1.26.55V8a.51.51 0 0 1-.504.504C3.765 8.632 1.604 11.92 1.604 20.25c1.47-2.94 2.22-4.679 10.245-4.748a.501.501 0 0 1 .505.498v3.535a.75.75 0 0 0 1.26.55l8.145-7.535a.75.75 0 0 0 0-1.1Z"/>
</svg>

Before

Width:  |  Height:  |  Size: 405 B

-3
View File
@@ -1,3 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path fill="currentColor" d="M3 11a8 8 0 1 1 14.162 5.102l3.368 3.368a.75.75 0 1 1-1.06 1.06l-3.368-3.368A8 8 0 0 1 3 11Z"/>
</svg>

Before

Width:  |  Height:  |  Size: 230 B

+4
View File
@@ -0,0 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path stroke="currentColor" stroke-linecap="square" stroke-linejoin="round" stroke-width="1.5" d="M21.25 12V6.75a2 2 0 0 0-2-2H4.75a2 2 0 0 0-2 2V12m18.5 0H2.75m18.5 0v5.25a2 2 0 0 1-2 2H4.75a2 2 0 0 1-2-2V12"/>
<path fill="currentColor" stroke="currentColor" stroke-width=".5" d="M6.5 14.875a.75.75 0 1 1 0 1.5.75.75 0 0 1 0-1.5Zm0-7.25a.75.75 0 1 1 0 1.5.75.75 0 0 1 0-1.5Z"/>
</svg>

After

Width:  |  Height:  |  Size: 486 B

+3
View File
@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 17v2m6-6v6m6-10v10m6-14v14"/>
</svg>

After

Width:  |  Height:  |  Size: 233 B

-1
View File
@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" viewBox="0 0 256 256"><path d="M64.12,147.8a4,4,0,0,1-4,4.2H16a8,8,0,0,1-7.8-6.17,8.35,8.35,0,0,1,1.62-6.93A67.79,67.79,0,0,1,37,117.51a40,40,0,1,1,66.46-35.8,3.94,3.94,0,0,1-2.27,4.18A64.08,64.08,0,0,0,64,144C64,145.28,64,146.54,64.12,147.8Zm182-8.91A67.76,67.76,0,0,0,219,117.51a40,40,0,1,0-66.46-35.8,3.94,3.94,0,0,0,2.27,4.18A64.08,64.08,0,0,1,192,144c0,1.28,0,2.54-.12,3.8a4,4,0,0,0,4,4.2H240a8,8,0,0,0,7.8-6.17A8.33,8.33,0,0,0,246.17,138.89Zm-89,43.18a48,48,0,1,0-58.37,0A72.13,72.13,0,0,0,65.07,212,8,8,0,0,0,72,224H184a8,8,0,0,0,6.93-12A72.15,72.15,0,0,0,157.19,182.07Z"></path></svg>

Before

Width:  |  Height:  |  Size: 676 B

+3
View File
@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path stroke="currentColor" stroke-linecap="round" stroke-width="2" d="M12 9.02v2.993M12 15h.01M10.277 3.99 3.275 15.998C2.499 17.328 3.458 19 4.998 19h14.004c1.54 0 2.5-1.671 1.723-3.002L13.723 3.99c-.77-1.32-2.677-1.32-3.447 0ZM12.25 15a.25.25 0 1 1-.5 0 .25.25 0 0 1 .5 0Z"/>
</svg>

After

Width:  |  Height:  |  Size: 384 B

+3 -6
View File
@@ -108,12 +108,9 @@ impl AutoUpdater {
cx.spawn_in(window, async move |this, cx| {
if let Ok(Some(update)) = checking.await {
cx.update(|window, cx| {
this.update(cx, |this, cx| {
this.set_status(AutoUpdateStatus::checked(update), cx);
this.install_update(window, cx);
})
.ok();
this.update_in(cx, |this, window, cx| {
this.set_status(AutoUpdateStatus::checked(update), cx);
this.install_update(window, cx);
})
.ok();
} else {
+5 -2
View File
@@ -1,5 +1,7 @@
use std::sync::atomic::Ordering;
use global::constants::KEYRING_URL;
use global::first_run;
use global::css;
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Window};
use nostr_sdk::prelude::*;
use smallvec::{smallvec, SmallVec};
@@ -59,6 +61,7 @@ impl ClientKeys {
return;
}
let css = css();
let read_client_keys = cx.read_credentials(KEYRING_URL);
cx.spawn_in(window, async move |this, cx| {
@@ -73,7 +76,7 @@ impl ClientKeys {
this.set_keys(Some(keys), false, true, cx);
})
.ok();
} else if *first_run() {
} else if css.is_first_run.load(Ordering::Acquire) {
// If this is the first run, generate new keys and use them for the client keys
this.update(cx, |this, cx| {
this.new_keys(cx);
+3 -1
View File
@@ -14,7 +14,7 @@ product-name = "Coop"
description = "Chat Freely, Stay Private on Nostr"
identifier = "su.reya.coop"
category = "SocialNetworking"
version = "0.2.5"
version = "0.2.8"
out-dir = "../../dist"
before-packaging-command = "cargo build --release"
resources = ["Cargo.toml", "src"]
@@ -59,6 +59,8 @@ smallvec.workspace = true
smol.workspace = true
futures.workspace = true
oneshot.workspace = true
flume.workspace = true
webbrowser.workspace = true
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
indexset = "0.12.3"
+2 -1
View File
@@ -2,7 +2,8 @@ use std::sync::Mutex;
use gpui::{actions, App};
actions!(coop, [DarkMode, Settings, Logout, Quit]);
actions!(coop, [ReloadMetadata, DarkMode, Settings, Logout, Quit]);
actions!(sidebar, [Reload, RelayStatus]);
pub fn load_embedded_fonts(cx: &App) {
let asset_source = cx.asset_source();
File diff suppressed because it is too large Load Diff
+6 -10
View File
@@ -2,7 +2,7 @@ use std::sync::Arc;
use assets::Assets;
use global::constants::{APP_ID, APP_NAME};
use global::{ingester, nostr_client, sent_ids, starting_time};
use global::{css, nostr_client};
use gpui::{
point, px, size, AppContext, Application, Bounds, KeyBinding, Menu, MenuItem, SharedString,
TitlebarOptions, WindowBackgroundAppearance, WindowBounds, WindowDecorations, WindowKind,
@@ -26,14 +26,8 @@ fn main() {
// Initialize the Nostr client
let _client = nostr_client();
// Initialize the ingester
let _ingester = ingester();
// Initialize the starting time
let _starting_time = starting_time();
// Initialize the sent IDs storage
let _sent_ids = sent_ids();
// Initialize the coop simple storage
let _css = css();
// Initialize the Application
let app = Application::new()
@@ -82,6 +76,9 @@ fn main() {
// Open a window with default options
cx.open_window(opts, |window, cx| {
// Bring the app to the foreground
cx.activate(true);
// Automatically sync theme with system appearance
window
.observe_window_appearance(|window, cx| {
@@ -91,7 +88,6 @@ fn main() {
// Root Entity
cx.new(|cx| {
cx.activate(true);
// Initialize the tokio runtime
gpui_tokio::init(cx);
+37 -31
View File
@@ -5,7 +5,7 @@ use client_keys::ClientKeys;
use common::display::ReadableProfile;
use common::handle_auth::CoopAuthUrlHandler;
use global::constants::{ACCOUNT_IDENTIFIER, BUNKER_TIMEOUT};
use global::{ingester, nostr_client, IngesterSignal};
use global::{css, nostr_client, SignalKind};
use gpui::prelude::FluentBuilder;
use gpui::{
div, relative, rems, svg, AnyElement, App, AppContext, Context, Entity, EventEmitter,
@@ -15,6 +15,7 @@ use gpui::{
use i18n::{shared_t, t};
use nostr_connect::prelude::*;
use nostr_sdk::prelude::*;
use smallvec::{smallvec, SmallVec};
use theme::ActiveTheme;
use ui::avatar::Avatar;
use ui::button::{Button, ButtonVariants};
@@ -44,6 +45,7 @@ pub struct Account {
// Panel
name: SharedString,
focus_handle: FocusHandle,
_tasks: SmallVec<[Task<()>; 1]>,
}
impl Account {
@@ -59,6 +61,7 @@ impl Account {
loading: false,
name: "Account".into(),
focus_handle: cx.focus_handle(),
_tasks: smallvec![],
})
}
@@ -89,24 +92,25 @@ impl Account {
// Handle auth url with the default browser
signer.auth_url_handler(CoopAuthUrlHandler);
// Handle connection
cx.spawn_in(window, async move |_this, cx| {
let client = nostr_client();
self._tasks.push(
// Handle connection
cx.spawn_in(window, async move |_this, cx| {
let client = nostr_client();
match signer.bunker_uri().await {
Ok(_) => {
// Set the client's signer with the current nostr connect instance
client.set_signer(signer).await;
match signer.bunker_uri().await {
Ok(_) => {
// Set the client's signer with the current nostr connect instance
client.set_signer(signer).await;
}
Err(e) => {
cx.update(|window, cx| {
window.push_notification(e.to_string(), cx);
})
.ok();
}
}
Err(e) => {
cx.update(|window, cx| {
window.push_notification(e.to_string(), cx);
})
.ok();
}
}
})
.detach();
}),
);
}
fn set_proxy(&mut self, window: &mut Window, cx: &mut Context<Self>) {
@@ -240,24 +244,26 @@ impl Account {
}
fn logout(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
cx.background_spawn(async move {
let client = nostr_client();
let ingester = ingester();
self._tasks.push(
// Reset the nostr client in the background
cx.background_spawn(async move {
let client = nostr_client();
let css = css();
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.identifier(ACCOUNT_IDENTIFIER);
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.identifier(ACCOUNT_IDENTIFIER);
// Delete account
client.database().delete(filter).await.ok();
// Delete account
client.database().delete(filter).await.ok();
// Unset the client's signer
client.unset_signer().await;
// Unset the client's signer
client.unset_signer().await;
// Notify the channel about the signer being unset
ingester.send(IngesterSignal::SignerUnset).await;
})
.detach();
// Notify the channel about the signer being unset
css.signal.send(SignalKind::SignerUnset).await;
}),
);
}
fn set_loading(&mut self, status: bool, cx: &mut Context<Self>) {
+413 -217
View File
@@ -1,19 +1,20 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use anyhow::anyhow;
use common::display::{ReadableProfile, ReadableTimestamp};
use common::nip96::nip96_upload;
use global::{nostr_client, sent_ids};
use global::{css, nostr_client};
use gpui::prelude::FluentBuilder;
use gpui::{
div, img, list, px, red, relative, rems, svg, white, Action, AnyElement, App, AppContext,
ClipboardItem, Context, Element, Entity, EventEmitter, Flatten, FocusHandle, Focusable,
InteractiveElement, IntoElement, ListAlignment, ListState, MouseButton, ObjectFit,
InteractiveElement, IntoElement, ListAlignment, ListOffset, ListState, MouseButton, ObjectFit,
ParentElement, PathPromptOptions, Render, RetainAllImageCache, SharedString,
StatefulInteractiveElement, Styled, StyledImage, Subscription, Task, Window,
};
use gpui_tokio::Tokio;
use i18n::{shared_t, t};
use indexset::{BTreeMap, BTreeSet};
use itertools::Itertools;
use nostr_sdk::prelude::*;
use registry::message::{Message, RenderedMessage};
@@ -30,7 +31,7 @@ use ui::dock_area::panel::{Panel, PanelEvent};
use ui::emoji_picker::EmojiPicker;
use ui::input::{InputEvent, InputState, TextInput};
use ui::modal::ModalButtonProps;
use ui::popup_menu::PopupMenu;
use ui::popup_menu::{PopupMenu, PopupMenuExt};
use ui::text::RenderedText;
use ui::{
h_flex, v_flex, ContextModal, Disableable, Icon, IconName, InteractiveElementExt, Sizable,
@@ -41,7 +42,7 @@ mod subject;
#[derive(Action, Clone, PartialEq, Eq, Deserialize)]
#[action(namespace = chat, no_json)]
pub struct ChangeSubject(pub String);
pub struct SeenOn(pub EventId);
pub fn init(room: Entity<Room>, window: &mut Window, cx: &mut App) -> Entity<Chat> {
cx.new(|cx| Chat::new(room, window, cx))
@@ -50,15 +51,17 @@ pub fn init(room: Entity<Room>, window: &mut Window, cx: &mut App) -> Entity<Cha
pub struct Chat {
// Chat Room
room: Entity<Room>,
relays: Entity<HashMap<PublicKey, Vec<RelayUrl>>>,
// Messages
list_state: ListState,
messages: Vec<Message>,
rendered_texts_by_id: HashMap<EventId, RenderedText>,
reports_by_id: HashMap<EventId, Vec<SendReport>>,
messages: BTreeSet<Message>,
rendered_texts_by_id: BTreeMap<EventId, RenderedText>,
reports_by_id: BTreeMap<EventId, Vec<SendReport>>,
// New Message
input: Entity<InputState>,
replies_to: Entity<Vec<EventId>>,
sending: bool,
// Media Attachment
attachments: Entity<Vec<Url>>,
@@ -69,13 +72,20 @@ pub struct Chat {
focus_handle: FocusHandle,
image_cache: Entity<RetainAllImageCache>,
_subscriptions: SmallVec<[Subscription; 2]>,
_tasks: SmallVec<[Task<()>; 1]>,
_subscriptions: SmallVec<[Subscription; 4]>,
_tasks: SmallVec<[Task<()>; 2]>,
}
impl Chat {
pub fn new(room: Entity<Room>, window: &mut Window, cx: &mut Context<Self>) -> Self {
let list_state = ListState::new(1, ListAlignment::Bottom, px(1024.));
let attachments = cx.new(|_| vec![]);
let replies_to = cx.new(|_| vec![]);
let relays = cx.new(|_| {
let this: HashMap<PublicKey, Vec<RelayUrl>> = HashMap::new();
this
});
let input = cx.new(|cx| {
InputState::new(window, cx)
.placeholder(t!("chat.placeholder"))
@@ -87,13 +97,38 @@ impl Chat {
.clean_on_escape()
});
let attachments = cx.new(|_| vec![]);
let replies_to = cx.new(|_| vec![]);
let messages = BTreeSet::from([Message::system()]);
let list_state = ListState::new(messages.len(), ListAlignment::Bottom, px(1024.));
let connect_relays = room.read(cx).connect_relays(cx);
let load_messages = room.read(cx).load_messages(cx);
let mut subscriptions = smallvec![];
let mut tasks = smallvec![];
tasks.push(
// Load all messages belonging to this room
cx.spawn_in(window, async move |this, cx| {
match connect_relays.await {
Ok(relays) => {
this.update(cx, |this, cx| {
this.relays.update(cx, |this, cx| {
*this = relays;
cx.notify();
});
})
.ok();
}
Err(e) => {
cx.update(|window, cx| {
window.push_notification(e.to_string(), cx);
})
.ok();
}
};
}),
);
tasks.push(
// Load all messages belonging to this room
cx.spawn_in(window, async move |this, cx| {
@@ -139,7 +174,7 @@ impl Chat {
match signal {
RoomSignal::NewMessage((gift_wrap_id, event)) => {
if !this.is_sent_by_coop(gift_wrap_id) {
this.insert_message(event, cx);
this.insert_message(Message::user(event), false, cx);
}
}
RoomSignal::Refresh => {
@@ -149,15 +184,42 @@ impl Chat {
}),
);
subscriptions.push(
// Observe the messaging relays of the room's members
cx.observe_in(&relays, window, |this, entity, _window, cx| {
for (public_key, urls) in entity.read(cx).clone().into_iter() {
if urls.is_empty() {
let profile = Registry::read_global(cx).get_person(&public_key, cx);
let content = t!("chat.nip17_not_found", u = profile.name());
this.insert_warning(content, cx);
}
}
}),
);
subscriptions.push(
// Observe when user close chat panel
cx.on_release_in(window, move |this, window, cx| {
this.disconnect_relays(cx);
this.messages.clear();
this.rendered_texts_by_id.clear();
this.reports_by_id.clear();
this.image_cache.update(cx, |this, cx| {
this.clear(window, cx);
});
}),
);
Self {
id: room.read(cx).id.to_string().into(),
image_cache: RetainAllImageCache::new(cx),
focus_handle: cx.focus_handle(),
uploading: false,
sending: false,
messages: vec![Message::System],
rendered_texts_by_id: HashMap::new(),
reports_by_id: HashMap::new(),
rendered_texts_by_id: BTreeMap::new(),
reports_by_id: BTreeMap::new(),
relays,
messages,
room,
list_state,
input,
@@ -168,29 +230,45 @@ impl Chat {
}
}
/// Load all messages belonging to this room
fn load_messages(&self, window: &mut Window, cx: &mut Context<Self>) {
let load_messages = self.room.read(cx).load_messages(cx);
/// Disconnect all relays when the user closes the chat panel
fn disconnect_relays(&mut self, cx: &mut App) {
let relays = self.relays.read(cx).clone();
cx.spawn_in(window, async move |this, cx| {
match load_messages.await {
Ok(events) => {
this.update(cx, |this, cx| {
this.insert_messages(events, cx);
})
.ok();
}
Err(e) => {
cx.update(|window, cx| {
window.push_notification(e.to_string(), cx);
})
.ok();
}
};
cx.background_spawn(async move {
let client = nostr_client();
for relay in relays.values().flatten() {
client.disconnect_relay(relay).await.ok();
}
})
.detach();
}
/// Load all messages belonging to this room
fn load_messages(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let load_messages = self.room.read(cx).load_messages(cx);
self._tasks.push(
// Run the task in the background
cx.spawn_in(window, async move |this, cx| {
match load_messages.await {
Ok(events) => {
this.update(cx, |this, cx| {
this.insert_messages(events, cx);
})
.ok();
}
Err(e) => {
cx.update(|window, cx| {
window.push_notification(e.to_string(), cx);
})
.ok();
}
};
}),
);
}
#[allow(dead_code)]
fn mention_popup(&mut self, _text: &str, _input: &Entity<InputState>, _cx: &mut Context<Self>) {
// TODO: open mention popup at current cursor position
@@ -220,14 +298,7 @@ impl Chat {
/// Check if the event is sent by Coop
fn is_sent_by_coop(&self, gift_wrap_id: &EventId) -> bool {
let sent_ids = sent_ids();
sent_ids.read_blocking().contains(gift_wrap_id)
}
/// Set the sending state of the chat panel
fn set_sending(&mut self, sending: bool, cx: &mut Context<Self>) {
self.sending = sending;
cx.notify();
css().sent_ids.read_blocking().contains(gift_wrap_id)
}
/// Send a message to all members of the chat
@@ -235,24 +306,21 @@ impl Chat {
// Get the message which includes all attachments
let content = self.input_content(cx);
// Get the backup setting
let backup = AppSettings::get_backup_messages(cx);
// Return if message is empty
if content.trim().is_empty() {
window.push_notification(t!("chat.empty_message_error"), cx);
return;
}
// Mark sending in progress
self.set_sending(true, cx);
// Temporary disable input
self.input.update(cx, |this, cx| {
this.set_loading(true, cx);
this.set_disabled(true, cx);
});
// Get the backup setting
let backup = AppSettings::get_backup_messages(cx);
// Get replies_to if it's present
let replies = self.replies_to.read(cx).clone();
@@ -267,17 +335,19 @@ impl Chat {
// Create a task for sending the message in the background
let send_message = room.send_in_background(&content, replies, backup, cx);
// Optimistically update message list
self.insert_message(temp_message, cx);
cx.defer_in(window, |this, window, cx| {
// Optimistically update message list
this.insert_message(Message::user(temp_message), true, cx);
// Remove all replies
self.remove_all_replies(cx);
// Remove all replies
this.remove_all_replies(cx);
// Reset the input state
self.input.update(cx, |this, cx| {
this.set_loading(false, cx);
this.set_disabled(false, cx);
this.set_value("", window, cx);
// Reset the input state
this.input.update(cx, |this, cx| {
this.set_loading(false, cx);
this.set_disabled(false, cx);
this.set_value("", window, cx);
});
});
// Continue sending the message in the background
@@ -285,15 +355,20 @@ impl Chat {
match send_message.await {
Ok(reports) => {
this.update(cx, |this, cx| {
// Don't change the room kind if send failed
this.room.update(cx, |this, cx| {
if this.kind != RoomKind::Ongoing {
this.kind = RoomKind::Ongoing;
cx.notify();
// Update the room kind to ongoing
// But keep the room kind if send failed
if reports.iter().all(|r| !r.is_sent_success()) {
this.kind = RoomKind::Ongoing;
cx.notify();
}
}
});
// Insert the sent reports
this.reports_by_id.insert(temp_id, reports);
this.sending = false;
cx.notify();
})
.ok();
@@ -309,6 +384,75 @@ impl Chat {
.detach();
}
fn resend_message(&mut self, id: &EventId, window: &mut Window, cx: &mut Context<Self>) {
if let Some(reports) = self.reports_by_id.get(id).cloned() {
if let Some(message) = self.message(id) {
let backup = AppSettings::get_backup_messages(cx);
let id_clone = id.to_owned();
let message = message.content.to_owned();
let task = self.room.read(cx).resend(reports, message, backup, cx);
cx.spawn_in(window, async move |this, cx| {
match task.await {
Ok(reports) => {
if !reports.is_empty() {
this.update(cx, |this, cx| {
this.reports_by_id.entry(id_clone).and_modify(|this| {
*this = reports;
});
cx.notify();
})
.ok();
}
}
Err(e) => {
cx.update(|window, cx| {
window.push_notification(e.to_string(), cx);
})
.ok();
}
};
})
.detach();
}
}
}
/// Insert a message into the chat panel
fn insert_message<E>(&mut self, m: E, scroll: bool, cx: &mut Context<Self>)
where
E: Into<Message>,
{
let old_len = self.messages.len();
// Extend the messages list with the new events
if self.messages.insert(m.into()) {
self.list_state.splice(old_len..old_len, 1);
if scroll {
self.list_state.scroll_to(ListOffset {
item_ix: self.list_state.item_count(),
offset_in_item: px(0.0),
});
cx.notify();
}
}
}
/// Convert and insert a vector of nostr events into the chat panel
fn insert_messages(&mut self, events: Vec<Event>, cx: &mut Context<Self>) {
for event in events.into_iter() {
let m = Message::user(event);
self.insert_message(m, false, cx);
}
cx.notify();
}
fn insert_warning(&mut self, content: impl Into<String>, cx: &mut Context<Self>) {
let m = Message::warning(content.into());
self.insert_message(m, true, cx);
}
/// Check if a message failed to send by its ID
fn is_sent_failed(&self, id: &EventId) -> bool {
self.reports_by_id
@@ -340,66 +484,6 @@ impl Chat {
})
}
/// Convert and insert a nostr event into the chat panel
fn insert_message<E>(&mut self, event: E, cx: &mut Context<Self>)
where
E: Into<RenderedMessage>,
{
let old_len = self.messages.len();
let new_len = 1;
// Extend the messages list with the new events
self.messages.push(Message::user(event));
// Update list state with the new messages
self.list_state.splice(old_len..old_len, new_len);
cx.notify();
}
/// Convert and insert bulk nostr events into the chat panel
fn insert_messages<E>(&mut self, events: E, cx: &mut Context<Self>)
where
E: IntoIterator,
E::Item: Into<RenderedMessage>,
{
let old_events: HashSet<EventId> = self
.messages
.iter()
.filter_map(|msg| {
if let Message::User(rendered) = msg {
Some(rendered.id)
} else {
None
}
})
.collect();
let events: Vec<Message> = events
.into_iter()
.map(|ev| ev.into())
.filter(|msg: &RenderedMessage| !old_events.contains(&msg.id))
.map(Message::User)
.collect();
let old_len = self.messages.len();
let new_len = events.len();
// Extend the messages list with the new events
self.messages.extend(events);
self.messages.sort_by(|a, b| match (a, b) {
(Message::System, Message::System) => std::cmp::Ordering::Equal,
(Message::System, Message::User(_)) => std::cmp::Ordering::Less,
(Message::User(_), Message::System) => std::cmp::Ordering::Greater,
(Message::User(a_msg), Message::User(b_msg)) => a_msg.created_at.cmp(&b_msg.created_at),
});
// Update list state with the new messages
self.list_state.splice(old_len..old_len, new_len);
cx.notify();
}
fn profile(&self, public_key: &PublicKey, cx: &Context<Self>) -> Profile {
let registry = Registry::read_global(cx);
registry.get_person(public_key, cx)
@@ -499,12 +583,9 @@ impl Chat {
.ok();
}
Err(e) => {
cx.update(|window, cx| {
this.update(cx, |this, cx| {
window.push_notification(e.to_string(), cx);
this.uploading(false, cx);
})
.ok();
this.update_in(cx, |this, window, cx| {
window.push_notification(e.to_string(), cx);
this.uploading(false, cx);
})
.ok();
}
@@ -561,15 +642,46 @@ impl Chat {
.into_any_element()
}
fn render_message_not_found(&self, cx: &Context<Self>) -> AnyElement {
fn render_warning(&mut self, ix: usize, content: String, cx: &mut Context<Self>) -> AnyElement {
div()
.id(ix)
.relative()
.w_full()
.py_1()
.px_3()
.bg(cx.theme().warning_background)
.child(
h_flex()
.gap_3()
.text_sm()
.text_color(cx.theme().warning_foreground)
.child(Avatar::new("brand/system.png").size(rems(2.)))
.child(SharedString::from(content)),
)
.child(
div()
.absolute()
.left_0()
.top_0()
.w(px(2.))
.h_full()
.bg(cx.theme().warning_active),
)
.into_any_element()
}
fn render_message_not_found(&self, ix: usize, cx: &Context<Self>) -> AnyElement {
div()
.id(ix)
.w_full()
.py_1()
.px_3()
.child(
div()
h_flex()
.gap_1()
.text_xs()
.text_color(cx.theme().danger_foreground)
.child(SharedString::from(ix.to_string()))
.child(shared_t!("chat.not_found")),
)
.into_any_element()
@@ -618,9 +730,7 @@ impl Chat {
.flex_initial()
.overflow_hidden()
.child(
div()
.flex()
.items_center()
h_flex()
.gap_2()
.text_sm()
.text_color(cx.theme().text_placeholder)
@@ -642,7 +752,23 @@ impl Chat {
})
.child(text)
.when(is_sent_failed, |this| {
this.child(self.render_message_reports(&id, cx))
this.child(
h_flex()
.gap_1()
.child(self.render_message_reports(&id, cx))
.child(
Button::new(SharedString::from(id.to_hex()))
.label(t!("common.resend"))
.danger()
.xsmall()
.rounded()
.on_click(cx.listener(
move |this, _, window, cx| {
this.resend_message(&id, window, cx);
},
)),
),
)
}),
),
)
@@ -710,15 +836,17 @@ impl Chat {
}
fn render_message_sent(&self, id: &EventId, _cx: &Context<Self>) -> impl IntoElement {
div().id("").child(shared_t!("chat.sent")).when_some(
self.sent_reports(id).cloned(),
|this, reports| {
div()
.id(SharedString::from(id.to_hex()))
.child(shared_t!("chat.sent"))
.when_some(self.sent_reports(id).cloned(), |this, reports| {
this.on_click(move |_e, window, cx| {
let reports = reports.clone();
window.open_modal(cx, move |this, _window, cx| {
this.title(shared_t!("chat.reports")).child(
v_flex().pb_4().gap_4().children({
this.show_close(true)
.title(shared_t!("chat.reports"))
.child(v_flex().pb_4().gap_4().children({
let mut items = Vec::with_capacity(reports.len());
for report in reports.iter() {
@@ -726,30 +854,29 @@ impl Chat {
}
items
}),
)
}))
});
})
},
)
})
}
fn render_message_reports(&self, id: &EventId, cx: &Context<Self>) -> impl IntoElement {
h_flex()
.id("")
.gap_1()
.id(SharedString::from(id.to_hex()))
.gap_0p5()
.text_color(cx.theme().danger_foreground)
.text_xs()
.italic()
.child(Icon::new(IconName::Info).small())
.child(Icon::new(IconName::Info).xsmall())
.child(shared_t!("chat.sent_failed"))
.when_some(self.sent_reports(id).cloned(), |this, reports| {
this.on_click(move |_e, window, cx| {
let reports = reports.clone();
window.open_modal(cx, move |this, _window, cx| {
this.title(shared_t!("chat.reports")).child(
v_flex().pb_4().gap_4().children({
this.show_close(true)
.title(shared_t!("chat.reports"))
.child(v_flex().gap_4().pb_4().w_full().children({
let mut items = Vec::with_capacity(reports.len());
for report in reports.iter() {
@@ -757,8 +884,7 @@ impl Chat {
}
items
}),
)
}))
});
})
})
@@ -772,6 +898,7 @@ impl Chat {
v_flex()
.gap_2()
.w_full()
.child(
h_flex()
.gap_2()
@@ -785,7 +912,7 @@ impl Chat {
.child(name.clone()),
),
)
.when(report.nip17_relays_not_found, |this| {
.when(report.relays_not_found, |this| {
this.child(
h_flex()
.flex_wrap()
@@ -806,7 +933,7 @@ impl Chat {
),
)
})
.when_some(report.local_error.clone(), |this, error| {
.when_some(report.error.clone(), |this, error| {
this.child(
h_flex()
.flex_wrap()
@@ -821,38 +948,36 @@ impl Chat {
.child(div().flex_1().w_full().text_center().child(error)),
)
})
.when_some(report.output.clone(), |this, output| {
.when_some(report.status.clone(), |this, output| {
this.child(
v_flex()
.gap_2()
.text_xs()
.w_full()
.children({
let mut items = Vec::with_capacity(output.failed.len());
for (url, msg) in output.failed.into_iter() {
items.push(
h_flex()
.gap_1()
.justify_between()
.text_sm()
v_flex()
.gap_0p5()
.py_1()
.px_2()
.w_full()
.rounded(cx.theme().radius)
.bg(cx.theme().elevated_surface_background)
.child(
div()
.flex_1()
.py_0p5()
.px_2()
.bg(cx.theme().elevated_surface_background)
.rounded_sm()
.child(url.to_string()),
.text_xs()
.font_semibold()
.line_height(relative(1.25))
.child(SharedString::from(url.to_string())),
)
.child(
div()
.flex_1()
.py_0p5()
.px_2()
.bg(cx.theme().danger_background)
.text_sm()
.text_color(cx.theme().danger_foreground)
.rounded_sm()
.child(msg.to_string()),
.line_height(relative(1.25))
.child(SharedString::from(msg.to_string())),
),
)
}
@@ -864,27 +989,25 @@ impl Chat {
for url in output.success.into_iter() {
items.push(
h_flex()
.gap_1()
.justify_between()
.text_sm()
v_flex()
.gap_0p5()
.py_1()
.px_2()
.w_full()
.rounded(cx.theme().radius)
.bg(cx.theme().elevated_surface_background)
.child(
div()
.flex_1()
.py_0p5()
.px_2()
.bg(cx.theme().elevated_surface_background)
.rounded_sm()
.child(url.to_string()),
.text_xs()
.font_semibold()
.line_height(relative(1.25))
.child(SharedString::from(url.to_string())),
)
.child(
div()
.flex_1()
.py_0p5()
.px_2()
.bg(cx.theme().secondary_background)
.text_sm()
.text_color(cx.theme().secondary_foreground)
.rounded_sm()
.line_height(relative(1.25))
.child(shared_t!("chat.sent_success")),
),
)
@@ -908,31 +1031,6 @@ impl Chat {
}
fn render_actions(&self, id: &EventId, cx: &Context<Self>) -> impl IntoElement {
let groups = vec![
Button::new("reply")
.icon(IconName::Reply)
.tooltip(t!("chat.reply_button"))
.small()
.ghost()
.on_click({
let id = id.to_owned();
cx.listener(move |this, _event, _window, cx| {
this.reply_to(&id, cx);
})
}),
Button::new("copy")
.icon(IconName::Copy)
.tooltip(t!("chat.copy_message_button"))
.small()
.ghost()
.on_click({
let id = id.to_owned();
cx.listener(move |this, _event, _window, cx| {
this.copy_message(&id, cx);
})
}),
];
h_flex()
.p_0p5()
.gap_1()
@@ -945,7 +1043,45 @@ impl Chat {
.border_1()
.border_color(cx.theme().border)
.bg(cx.theme().background)
.children(groups)
.child(
Button::new("reply")
.icon(IconName::Reply)
.tooltip(t!("chat.reply_button"))
.small()
.ghost()
.on_click({
let id = id.to_owned();
cx.listener(move |this, _event, _window, cx| {
this.reply_to(&id, cx);
})
}),
)
.child(
Button::new("copy")
.icon(IconName::Copy)
.tooltip(t!("chat.copy_message_button"))
.small()
.ghost()
.on_click({
let id = id.to_owned();
cx.listener(move |this, _event, _window, cx| {
this.copy_message(&id, cx);
})
}),
)
.child(div().flex_shrink_0().h_4().w_px().bg(cx.theme().border))
.child(
Button::new("seen-on")
.icon(IconName::Ellipsis)
.small()
.ghost()
.popup_menu({
let id = id.to_owned();
move |this, _window, _cx| {
this.menu(t!("common.seen_on"), Box::new(SeenOn(id)))
}
}),
)
.group_hover("", |this| this.visible())
}
@@ -954,7 +1090,7 @@ impl Chat {
let path: SharedString = url.to_string().into();
div()
.id("")
.id(SharedString::from(url.to_string()))
.relative()
.w_16()
.child(
@@ -1121,6 +1257,62 @@ impl Chat {
.ok();
})
}
fn on_open_seen_on(&mut self, ev: &SeenOn, window: &mut Window, cx: &mut Context<Self>) {
let id = ev.0;
let task: Task<Result<Vec<RelayUrl>, Error>> = cx.background_spawn(async move {
let client = nostr_client();
let css = css();
let mut relays: Vec<RelayUrl> = vec![];
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.event(id)
.limit(1);
if let Some(event) = client.database().query(filter).await?.first_owned() {
if let Some(Ok(id)) = event.tags.identifier().map(EventId::parse) {
if let Some(urls) = css.seen_on_relays.read().await.get(&id).cloned() {
relays.extend(urls);
}
}
}
Ok(relays)
});
cx.spawn_in(window, async move |_, cx| {
if let Ok(urls) = task.await {
cx.update(|window, cx| {
window.open_modal(cx, move |this, _window, cx| {
this.title(shared_t!("common.seen_on")).child(
v_flex().pb_4().gap_2().children({
let mut items = Vec::with_capacity(urls.len());
for url in urls.clone().into_iter() {
items.push(
h_flex()
.h_8()
.px_2()
.bg(cx.theme().elevated_surface_background)
.rounded(cx.theme().radius)
.font_semibold()
.text_xs()
.child(url.to_string()),
)
}
items
}),
)
});
})
.ok();
}
})
.detach();
}
}
impl Panel for Chat {
@@ -1167,13 +1359,14 @@ impl Focusable for Chat {
impl Render for Chat {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex()
.on_action(cx.listener(Self::on_open_seen_on))
.image_cache(self.image_cache.clone())
.size_full()
.child(
list(
self.list_state.clone(),
cx.processor(move |this, ix: usize, window, cx| {
if let Some(message) = this.messages.get(ix) {
if let Some(message) = this.messages.get_index(ix) {
match message {
Message::User(rendered) => {
let text = this
@@ -1184,10 +1377,13 @@ impl Render for Chat {
this.render_message(ix, rendered, text, cx)
}
Message::System => this.render_announcement(ix, cx),
Message::Warning(content, _) => {
this.render_warning(ix, content.to_owned(), cx)
}
Message::System(_) => this.render_announcement(ix, cx),
}
} else {
this.render_message_not_found(cx)
this.render_message_not_found(ix, cx)
}
}),
)
+2 -2
View File
@@ -22,7 +22,7 @@ use smallvec::{smallvec, SmallVec};
use smol::Timer;
use theme::ActiveTheme;
use ui::avatar::Avatar;
use ui::button::{Button, ButtonRounded, ButtonVariants};
use ui::button::{Button, ButtonVariants};
use ui::input::{InputEvent, InputState, TextInput};
use ui::notification::Notification;
use ui::{h_flex, v_flex, ContextModal, Disableable, Icon, IconName, Sizable, StyledExt};
@@ -34,7 +34,7 @@ pub fn compose_button() -> impl IntoElement {
.ghost_alt()
.cta()
.small()
.rounded(ButtonRounded::Full)
.rounded()
.on_click(move |_, window, cx| {
let compose = cx.new(|cx| Compose::new(window, cx));
let title = SharedString::new(t!("sidebar.direct_messages"));
+32 -28
View File
@@ -69,32 +69,29 @@ impl EditProfile {
cx.spawn_in(window, async move |this, cx| {
if let Ok(Some(metadata)) = task.await {
cx.update(|window, cx| {
this.update(cx, |this: &mut EditProfile, cx| {
this.avatar_input.update(cx, |this, cx| {
if let Some(avatar) = metadata.picture.as_ref() {
this.set_value(avatar, window, cx);
}
});
this.bio_input.update(cx, |this, cx| {
if let Some(bio) = metadata.about.as_ref() {
this.set_value(bio, window, cx);
}
});
this.name_input.update(cx, |this, cx| {
if let Some(display_name) = metadata.display_name.as_ref() {
this.set_value(display_name, window, cx);
}
});
this.website_input.update(cx, |this, cx| {
if let Some(website) = metadata.website.as_ref() {
this.set_value(website, window, cx);
}
});
this.profile = Some(metadata);
cx.notify();
})
.ok();
this.update_in(cx, |this: &mut EditProfile, window, cx| {
this.avatar_input.update(cx, |this, cx| {
if let Some(avatar) = metadata.picture.as_ref() {
this.set_value(avatar, window, cx);
}
});
this.bio_input.update(cx, |this, cx| {
if let Some(bio) = metadata.about.as_ref() {
this.set_value(bio, window, cx);
}
});
this.name_input.update(cx, |this, cx| {
if let Some(display_name) = metadata.display_name.as_ref() {
this.set_value(display_name, window, cx);
}
});
this.website_input.update(cx, |this, cx| {
if let Some(website) = metadata.website.as_ref() {
this.set_value(website, window, cx);
}
});
this.profile = Some(metadata);
cx.notify();
})
.ok();
}
@@ -167,7 +164,7 @@ impl EditProfile {
.detach();
}
pub fn set_metadata(&mut self, cx: &mut Context<Self>) -> Task<Result<Option<Event>, Error>> {
pub fn set_metadata(&mut self, cx: &mut Context<Self>) -> Task<Result<Option<Profile>, Error>> {
let avatar = self.avatar_input.read(cx).value().to_string();
let name = self.name_input.read(cx).value().to_string();
let bio = self.bio_input.read(cx).value().to_string();
@@ -192,7 +189,14 @@ impl EditProfile {
cx.background_spawn(async move {
let client = nostr_client();
let output = client.set_metadata(&new_metadata).await?;
let event = client.database().event_by_id(&output.val).await?;
let event = client
.database()
.event_by_id(&output.val)
.await?
.map(|event| {
let metadata = Metadata::from_json(&event.content).unwrap_or_default();
Profile::new(event.pubkey, metadata)
});
Ok(event)
})
+46 -12
View File
@@ -1,6 +1,6 @@
use anyhow::anyhow;
use common::nip96::nip96_upload;
use global::constants::ACCOUNT_IDENTIFIER;
use global::constants::{ACCOUNT_IDENTIFIER, NIP17_RELAYS, NIP65_RELAYS};
use global::nostr_client;
use gpui::{
div, relative, rems, AnyElement, App, AppContext, AsyncWindowContext, Context, Entity,
@@ -14,7 +14,7 @@ use settings::AppSettings;
use smol::fs;
use theme::ActiveTheme;
use ui::avatar::Avatar;
use ui::button::{Button, ButtonRounded, ButtonVariants};
use ui::button::{Button, ButtonVariants};
use ui::dock_area::panel::{Panel, PanelEvent};
use ui::input::{InputState, TextInput};
use ui::modal::ModalButtonProps;
@@ -70,6 +70,7 @@ impl NewAccount {
window.open_modal(cx, move |modal, _window, _cx| {
let weak_view = weak_view.clone();
let current_view = current_view.clone();
modal
.alert()
.title(shared_t!("new_account.backup_label"))
@@ -124,8 +125,44 @@ impl NewAccount {
// Set the client's signer with the current keys
cx.background_spawn(async move {
let client = nostr_client();
// Set the client's signer with the current keys
client.set_signer(keys).await;
client.set_metadata(&metadata).await.ok();
// Set metadata
if let Err(e) = client.set_metadata(&metadata).await {
log::error!("Failed to set metadata: {e}");
}
// Set NIP-65 relays
let builder = EventBuilder::new(Kind::RelayList, "").tags(
NIP65_RELAYS.into_iter().filter_map(|url| {
if let Ok(url) = RelayUrl::parse(url) {
Some(Tag::relay_metadata(url, None))
} else {
None
}
}),
);
if let Err(e) = client.send_event_builder(builder).await {
log::error!("Failed to send NIP-65 relay list event: {e}");
}
// Set NIP-17 relays
let builder = EventBuilder::new(Kind::InboxRelays, "").tags(
NIP17_RELAYS.into_iter().filter_map(|url| {
if let Ok(url) = RelayUrl::parse(url) {
Some(Tag::relay(url))
} else {
None
}
}),
);
if let Err(e) = client.send_event_builder(builder).await {
log::error!("Failed to send messaging relay list event: {e}");
};
})
.detach();
}
@@ -194,14 +231,11 @@ impl NewAccount {
cx.spawn_in(window, async move |this, cx| {
match Flatten::flatten(task.await.map_err(|e| e.into())) {
Ok(Ok(url)) => {
cx.update(|window, cx| {
this.update(cx, |this, cx| {
this.uploading(false, cx);
this.avatar_input.update(cx, |this, cx| {
this.set_value(url.to_string(), window, cx);
});
})
.ok();
this.update_in(cx, |this, window, cx| {
this.uploading(false, cx);
this.avatar_input.update(cx, |this, cx| {
this.set_value(url.to_string(), window, cx);
});
})
.ok();
}
@@ -318,7 +352,7 @@ impl Render for NewAccount {
.label(t!("common.upload"))
.ghost()
.small()
.rounded(ButtonRounded::Full)
.rounded()
.disabled(self.submitting || self.uploading)
.loading(self.uploading)
.on_click(cx.listener(move |this, _, window, cx| {
+35 -28
View File
@@ -9,7 +9,7 @@ use gpui::prelude::FluentBuilder;
use gpui::{
div, img, px, relative, svg, AnyElement, App, AppContext, ClipboardItem, Context, Entity,
EventEmitter, FocusHandle, Focusable, Image, InteractiveElement, IntoElement, ParentElement,
Render, SharedString, StatefulInteractiveElement, Styled, Subscription, Window,
Render, SharedString, StatefulInteractiveElement, Styled, Subscription, Task, Window,
};
use i18n::{shared_t, t};
use nostr_connect::prelude::*;
@@ -17,6 +17,7 @@ use smallvec::{smallvec, SmallVec};
use theme::ActiveTheme;
use ui::button::{Button, ButtonVariants};
use ui::dock_area::panel::{Panel, PanelEvent};
use ui::notification::Notification;
use ui::popup_menu::PopupMenu;
use ui::{divider, h_flex, v_flex, ContextModal, Icon, IconName, Sizable, StyledExt};
@@ -65,8 +66,8 @@ pub struct Onboarding {
// Panel
name: SharedString,
focus_handle: FocusHandle,
#[allow(dead_code)]
subscriptions: SmallVec<[Subscription; 2]>,
_subscriptions: SmallVec<[Subscription; 2]>,
_tasks: SmallVec<[Task<()>; 1]>,
}
impl Onboarding {
@@ -103,10 +104,11 @@ impl Onboarding {
nostr_connect,
nostr_connect_uri,
qr_code,
subscriptions,
connecting: false,
name: "Onboarding".into(),
focus_handle: cx.focus_handle(),
_subscriptions: subscriptions,
_tasks: smallvec![],
}
}
@@ -130,32 +132,37 @@ impl Onboarding {
cx.notify();
});
cx.spawn_in(window, async move |this, cx| {
let client = nostr_client();
let connect = this.read_with(cx, |this, cx| this.nostr_connect.read(cx).clone());
self._tasks.push(
// Wait for Nostr Connect approval
cx.spawn_in(window, async move |this, cx| {
let client = nostr_client();
let connect = this.read_with(cx, |this, cx| this.nostr_connect.read(cx).clone());
if let Ok(Some(signer)) = connect {
match signer.bunker_uri().await {
Ok(uri) => {
this.update(cx, |this, cx| {
this.set_connecting(cx);
this.write_uri_to_disk(&uri, cx);
})
.ok();
if let Ok(Some(signer)) = connect {
match signer.bunker_uri().await {
Ok(uri) => {
this.update(cx, |this, cx| {
this.set_connecting(cx);
this.write_uri_to_disk(&uri, cx);
})
.ok();
// Set the client's signer with the current nostr connect instance
client.set_signer(signer).await;
}
Err(e) => {
cx.update(|window, cx| {
window.push_notification(e.to_string(), cx);
})
.ok();
}
};
}
})
.detach();
// Set the client's signer with the current nostr connect instance
client.set_signer(signer).await;
}
Err(e) => {
this.update_in(cx, |_, window, cx| {
window.push_notification(
Notification::error(e.to_string()).title("Nostr Connect"),
cx,
);
})
.ok();
}
};
}
}),
)
}
fn set_proxy(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+9 -10
View File
@@ -10,7 +10,7 @@ use registry::Registry;
use settings::AppSettings;
use theme::ActiveTheme;
use ui::avatar::Avatar;
use ui::button::{Button, ButtonRounded, ButtonVariants};
use ui::button::{Button, ButtonVariants};
use ui::input::{InputState, TextInput};
use ui::modal::ModalButtonProps;
use ui::switch::Switch;
@@ -41,28 +41,28 @@ impl Preferences {
fn open_edit_profile(&self, window: &mut Window, cx: &mut Context<Self>) {
let view = edit_profile::init(window, cx);
let weak_view = view.downgrade();
let title = SharedString::new(t!("profile.title"));
window.open_modal(cx, move |modal, _window, _cx| {
let weak_view = weak_view.clone();
modal
.confirm()
.title(title.clone())
.title(shared_t!("profile.title"))
.child(view.clone())
.button_props(ModalButtonProps::default().ok_text(t!("common.update")))
.on_ok(move |_, window, cx| {
weak_view
.update(cx, |this, cx| {
let set_metadata = this.set_metadata(cx);
let registry = Registry::global(cx);
cx.spawn_in(window, async move |_, cx| {
match set_metadata.await {
Ok(event) => {
if let Some(event) = event {
Ok(profile) => {
if let Some(profile) = profile {
cx.update(|_, cx| {
Registry::global(cx).update(cx, |this, cx| {
this.insert_or_update_person(event, cx);
registry.update(cx, |this, cx| {
this.insert_or_update_person(profile, cx);
});
})
.ok();
@@ -86,7 +86,6 @@ impl Preferences {
}
fn open_relays(&self, window: &mut Window, cx: &mut Context<Self>) {
let title = SharedString::new(t!("relays.modal_title"));
let view = setup_relay::init(Kind::InboxRelays, window, cx);
let weak_view = view.downgrade();
@@ -94,7 +93,7 @@ impl Preferences {
let weak_view = weak_view.clone();
this.confirm()
.title(title.clone())
.title(shared_t!("relays.modal"))
.child(view.clone())
.button_props(ModalButtonProps::default().ok_text(t!("common.update")))
.on_ok(move |_, window, cx| {
@@ -170,7 +169,7 @@ impl Render for Preferences {
.label("Messaging Relays")
.xsmall()
.ghost_alt()
.rounded(ButtonRounded::Full)
.rounded()
.on_click(cx.listener(move |this, _e, window, cx| {
this.open_relays(window, cx);
})),
+230 -103
View File
@@ -1,9 +1,13 @@
use common::display::{shorten_pubkey, ReadableProfile};
use std::time::Duration;
use common::display::{shorten_pubkey, ReadableProfile, ReadableTimestamp};
use common::nip05::nip05_verify;
use global::constants::BOOTSTRAP_RELAYS;
use global::nostr_client;
use gpui::prelude::FluentBuilder;
use gpui::{
div, relative, rems, App, AppContext, Context, Div, Entity, IntoElement, ParentElement, Render,
SharedString, Styled, Task, Window,
div, px, relative, rems, uniform_list, App, AppContext, Context, Div, Entity,
InteractiveElement, IntoElement, ParentElement, Render, SharedString, Styled, Task, Window,
};
use gpui_tokio::Tokio;
use i18n::{shared_t, t};
@@ -13,7 +17,8 @@ use settings::AppSettings;
use smallvec::{smallvec, SmallVec};
use theme::ActiveTheme;
use ui::avatar::Avatar;
use ui::button::{Button, ButtonRounded, ButtonVariants};
use ui::button::{Button, ButtonVariants};
use ui::indicator::Indicator;
use ui::{h_flex, v_flex, ContextModal, Icon, IconName, Sizable, StyledExt};
pub fn init(public_key: PublicKey, window: &mut Window, cx: &mut App) -> Entity<Screening> {
@@ -24,9 +29,9 @@ pub struct Screening {
profile: Profile,
verified: bool,
followed: bool,
dm_relays: bool,
mutual_contacts: usize,
_tasks: SmallVec<[Task<()>; 1]>,
last_active: Option<Timestamp>,
mutual_contacts: Vec<Profile>,
_tasks: SmallVec<[Task<()>; 3]>,
}
impl Screening {
@@ -37,33 +42,47 @@ impl Screening {
let mut tasks = smallvec![];
let check_trust_score: Task<(bool, usize, bool)> = cx.background_spawn(async move {
let contact_check: Task<(bool, Vec<Profile>)> = cx.background_spawn(async move {
let client = nostr_client();
let follow = Filter::new()
.kind(Kind::ContactList)
.author(identity)
.pubkey(public_key)
.limit(1);
// 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);
let contacts = Filter::new()
.kind(Kind::ContactList)
.pubkey(public_key)
.limit(1);
// Check mutual contacts
let contact_list = Filter::new().kind(Kind::ContactList).pubkey(public_key);
let mut mutual_contacts = vec![];
let relays = Filter::new()
.kind(Kind::InboxRelays)
.author(public_key)
.limit(1);
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);
}
}
}
let is_follow = client.database().count(follow).await.unwrap_or(0) >= 1;
let mutual_contacts = client.database().count(contacts).await.unwrap_or(0);
let dm_relays = client.database().count(relays).await.unwrap_or(0) >= 1;
(is_follow, mutual_contacts, dm_relays)
(followed, mutual_contacts)
});
let verify_nip05 = if let Some(address) = profile.metadata().nip05 {
let activity_check = cx.background_spawn(async move {
let client = nostr_client();
let filter = Filter::new().author(public_key).limit(1);
let mut activity: Option<Timestamp> = None;
if let Ok(mut stream) = client
.stream_events_from(BOOTSTRAP_RELAYS, filter, Duration::from_secs(2))
.await
{
while let Some(event) = stream.next().await {
activity = Some(event.created_at);
}
}
activity
});
let addr_check = if let Some(address) = profile.metadata().nip05 {
Some(Tokio::spawn(cx, async move {
nip05_verify(public_key, &address).await.unwrap_or(false)
}))
@@ -72,20 +91,36 @@ impl Screening {
};
tasks.push(
// Load all necessary data
// Run the contact check in the background
cx.spawn_in(window, async move |this, cx| {
let (followed, mutual_contacts, dm_relays) = check_trust_score.await;
let (followed, mutual_contacts) = contact_check.await;
this.update(cx, |this, cx| {
this.followed = followed;
this.mutual_contacts = mutual_contacts;
this.dm_relays = dm_relays;
cx.notify();
})
.ok();
}),
);
// Update the NIP05 verification status if user has NIP05 address
if let Some(task) = verify_nip05 {
tasks.push(
// Run the activity check in the background
cx.spawn_in(window, async move |this, cx| {
let active = activity_check.await;
this.update(cx, |this, cx| {
this.last_active = active;
cx.notify();
})
.ok();
}),
);
tasks.push(
// Run the NIP-05 verification in the background
cx.spawn_in(window, async move |this, cx| {
if let Some(task) = addr_check {
if let Ok(verified) = task.await {
this.update(cx, |this, cx| {
this.verified = verified;
@@ -101,8 +136,8 @@ impl Screening {
profile,
verified: false,
followed: false,
dm_relays: false,
mutual_contacts: 0,
last_active: None,
mutual_contacts: vec![],
_tasks: tasks,
}
}
@@ -141,12 +176,55 @@ impl Screening {
})
.detach();
}
fn mutual_contacts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let contacts = self.mutual_contacts.clone();
window.open_modal(cx, move |this, _window, _cx| {
let contacts = contacts.clone();
let total = contacts.len();
this.title(shared_t!("screening.mutual_label")).child(
v_flex().gap_1().pb_4().child(
uniform_list("contacts", total, move |range, _window, cx| {
let mut items = Vec::with_capacity(total);
for ix in range {
if let Some(contact) = contacts.get(ix) {
items.push(
h_flex()
.h_11()
.w_full()
.px_2()
.gap_1p5()
.rounded(cx.theme().radius)
.text_sm()
.hover(|this| {
this.bg(cx.theme().elevated_surface_background)
})
.child(
Avatar::new(contact.avatar_url(true)).size(rems(1.75)),
)
.child(contact.display_name()),
);
}
}
items
})
.h(px(300.)),
),
)
});
}
}
impl Render for Screening {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let shorten_pubkey = shorten_pubkey(self.profile.public_key(), 8);
let total_mutuals = self.mutual_contacts.len();
let last_active = self.last_active.map(|_| true);
v_flex()
.gap_4()
@@ -168,12 +246,10 @@ impl Render for Screening {
h_flex()
.gap_3()
.child(
div()
h_flex()
.p_1()
.flex_1()
.h_7()
.flex()
.items_center()
.justify_center()
.rounded_full()
.bg(cx.theme().surface_background)
@@ -192,7 +268,7 @@ impl Render for Screening {
.label(t!("profile.njump"))
.secondary()
.small()
.rounded(ButtonRounded::Full)
.rounded()
.on_click(cx.listener(move |this, _e, window, cx| {
this.open_njump(window, cx);
})),
@@ -202,7 +278,7 @@ impl Render for Screening {
.tooltip(t!("screening.report"))
.icon(IconName::Report)
.danger()
.rounded(ButtonRounded::Full)
.rounded()
.on_click(cx.listener(move |this, _e, window, cx| {
this.report(window, cx);
})),
@@ -217,25 +293,70 @@ impl Render for Screening {
.items_start()
.gap_2()
.text_sm()
.child(status_badge(self.followed, cx))
.child(status_badge(Some(self.followed), cx))
.child(
v_flex()
.text_sm()
.child(shared_t!("screening.contact_label"))
.child(div().text_color(cx.theme().text_muted).child({
if self.followed {
shared_t!("screening.contact")
} else {
shared_t!("screening.not_contact")
}
})),
.child(
div()
.line_clamp(1)
.text_color(cx.theme().text_muted)
.child({
if self.followed {
shared_t!("screening.contact")
} else {
shared_t!("screening.not_contact")
}
}),
),
),
)
.child(
h_flex()
.items_start()
.gap_2()
.child(status_badge(self.verified, cx))
.text_sm()
.child(status_badge(last_active, cx))
.child(
v_flex()
.text_sm()
.child(
h_flex()
.gap_0p5()
.child(shared_t!("screening.active_label"))
.child(
Button::new("active")
.icon(IconName::Info)
.xsmall()
.ghost()
.rounded()
.tooltip(t!("screening.active_tooltip")),
),
)
.child(
div()
.w_full()
.line_clamp(1)
.text_color(cx.theme().text_muted)
.map(|this| {
if let Some(date) = self.last_active {
this.child(shared_t!(
"screening.active_at",
d = date.to_human_time()
))
} else {
this.child(shared_t!("screening.no_active"))
}
}),
),
),
)
.child(
h_flex()
.items_start()
.gap_2()
.child(status_badge(Some(self.verified), cx))
.child(
v_flex()
.text_sm()
@@ -246,77 +367,83 @@ impl Render for Screening {
shared_t!("screening.nip05_label")
}
})
.child(div().text_color(cx.theme().text_muted).child({
if self.address(cx).is_some() {
if self.verified {
shared_t!("screening.nip05_ok")
} else {
shared_t!("screening.nip05_failed")
}
} else {
shared_t!("screening.nip05_empty")
}
})),
.child(
div()
.line_clamp(1)
.text_color(cx.theme().text_muted)
.child({
if self.address(cx).is_some() {
if self.verified {
shared_t!("screening.nip05_ok")
} else {
shared_t!("screening.nip05_failed")
}
} else {
shared_t!("screening.nip05_empty")
}
}),
),
),
)
.child(
h_flex()
.items_start()
.gap_2()
.child(status_badge(self.mutual_contacts > 0, cx))
.child(status_badge(Some(total_mutuals > 0), cx))
.child(
v_flex()
.text_sm()
.child(shared_t!("screening.mutual_label"))
.child(div().text_color(cx.theme().text_muted).child({
if self.mutual_contacts > 0 {
shared_t!("screening.mutual", u = self.mutual_contacts)
} else {
shared_t!("screening.no_mutual")
}
})),
),
)
.child(
h_flex()
.items_start()
.gap_2()
.child(status_badge(self.dm_relays, cx))
.child(
v_flex()
.w_full()
.text_sm()
.child({
if self.dm_relays {
shared_t!("screening.relay_found")
} else {
shared_t!("screening.relay_empty")
}
})
.child(div().w_full().text_color(cx.theme().text_muted).child(
{
if self.dm_relays {
shared_t!("screening.relay_found_desc")
} else {
shared_t!("screening.relay_empty_desc")
}
},
)),
.child(
h_flex()
.gap_0p5()
.child(shared_t!("screening.mutual_label"))
.child(
Button::new("mutuals")
.icon(IconName::Info)
.xsmall()
.ghost()
.rounded()
.on_click(cx.listener(
move |this, _, window, cx| {
this.mutual_contacts(window, cx);
},
)),
),
)
.child(
div()
.line_clamp(1)
.text_color(cx.theme().text_muted)
.child({
if total_mutuals > 0 {
shared_t!("screening.mutual", u = total_mutuals)
} else {
shared_t!("screening.no_mutual")
}
}),
),
),
),
)
}
}
fn status_badge(status: bool, cx: &App) -> Div {
div()
.pt_1()
fn status_badge(status: Option<bool>, cx: &App) -> Div {
h_flex()
.size_6()
.justify_center()
.flex_shrink_0()
.child(Icon::new(IconName::CheckCircleFill).small().text_color({
if status {
cx.theme().icon_accent
.map(|this| {
if let Some(status) = status {
this.child(Icon::new(IconName::CheckCircleFill).small().text_color({
if status {
cx.theme().icon_accent
} else {
cx.theme().icon_muted
}
}))
} else {
cx.theme().icon_muted
this.child(Indicator::new().small())
}
}))
})
}
+34 -19
View File
@@ -2,7 +2,7 @@ use std::time::Duration;
use anyhow::{anyhow, Error};
use global::constants::NIP17_RELAYS;
use global::nostr_client;
use global::{css, nostr_client};
use gpui::prelude::FluentBuilder;
use gpui::{
div, px, uniform_list, App, AppContext, Context, Entity, InteractiveElement, IntoElement,
@@ -10,12 +10,11 @@ use gpui::{
TextAlign, UniformList, Window,
};
use i18n::{shared_t, t};
use itertools::Itertools;
use nostr_sdk::prelude::*;
use registry::Registry;
use smallvec::{smallvec, SmallVec};
use theme::ActiveTheme;
use ui::button::{Button, ButtonRounded, ButtonVariants};
use ui::button::{Button, ButtonVariants};
use ui::input::{InputEvent, InputState, TextInput};
use ui::modal::ModalButtonProps;
use ui::{h_flex, v_flex, ContextModal, IconName, Sizable, StyledExt};
@@ -34,7 +33,7 @@ where
.label(label)
.warning()
.xsmall()
.rounded(ButtonRounded::Full)
.rounded()
.on_click(move |_, window, cx| {
let view = cx.new(|cx| SetupRelay::new(Kind::InboxRelays, window, cx));
let weak_view = view.downgrade();
@@ -44,7 +43,7 @@ where
modal
.confirm()
.title(shared_t!("relays.modal_title"))
.title(shared_t!("relays.modal"))
.child(view.clone())
.button_props(ModalButtonProps::default().ok_text(t!("common.update")))
.on_ok(move |_, window, cx| {
@@ -82,7 +81,7 @@ impl SetupRelay {
let filter = Filter::new().kind(kind).author(identity).limit(1);
if let Some(event) = client.database().query(filter).await?.first() {
let relays = event
let relays: Vec<RelayUrl> = event
.tags
.iter()
.filter_map(|tag| tag.as_standardized())
@@ -95,7 +94,7 @@ impl SetupRelay {
None
}
})
.collect_vec();
.collect();
Ok(relays)
} else {
@@ -195,22 +194,41 @@ impl SetupRelay {
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let client = nostr_client();
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let tags: Vec<Tag> = relays
.iter()
.map(|relay| Tag::relay(relay.clone()))
.collect();
let builder = EventBuilder::new(Kind::InboxRelays, "").tags(tags);
let event = EventBuilder::new(Kind::InboxRelays, "")
.tags(tags)
.build(public_key)
.sign(&signer)
.await?;
// Set messaging relays
client.send_event_builder(builder).await?;
client.send_event(&event).await?;
// Connect to messaging relays
for relay in relays.into_iter() {
_ = client.add_relay(&relay).await;
_ = client.connect_relay(&relay).await;
for relay in relays.iter() {
_ = client.add_relay(relay).await;
_ = client.connect_relay(relay).await;
}
// Fetch gift wrap events
let sub_id = css().gift_wrap_sub_id.clone();
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
if client
.subscribe_with_id_to(relays.clone(), sub_id, filter, None)
.await
.is_ok()
{
log::info!("Subscribed to messages in: {relays:?}");
};
Ok(())
});
@@ -223,11 +241,8 @@ impl SetupRelay {
.ok();
}
Err(e) => {
cx.update(|window, cx| {
this.update(cx, |this, cx| {
this.set_error(e.to_string(), window, cx);
})
.ok();
this.update_in(cx, |this, window, cx| {
this.set_error(e.to_string(), window, cx);
})
.ok();
}
@@ -291,7 +306,7 @@ impl SetupRelay {
.justify_center()
.text_sm()
.text_align(TextAlign::Center)
.child(shared_t!("relays.add_some_relays"))
.child(shared_t!("relays.help_text"))
}
}
@@ -331,7 +346,7 @@ impl Render for SetupRelay {
.text_xs()
.font_semibold()
.text_color(cx.theme().text_muted)
.child(shared_t!("relays.recommended")),
.child(shared_t!("common.recommended")),
)
.child(h_flex().gap_1().children({
NIP17_RELAYS.iter().map(|&relay| {
+16 -7
View File
@@ -11,10 +11,9 @@ use registry::room::RoomKind;
use registry::Registry;
use settings::AppSettings;
use theme::ActiveTheme;
use ui::actions::OpenProfile;
use ui::avatar::Avatar;
use ui::context_menu::ContextMenuExt;
use ui::modal::ModalButtonProps;
use ui::skeleton::Skeleton;
use ui::{h_flex, ContextModal, StyledExt};
use crate::views::screening;
@@ -108,7 +107,21 @@ impl RenderOnce for RoomListItem {
self.handler,
)
else {
return div().id(self.ix);
return h_flex()
.id(self.ix)
.h_9()
.w_full()
.px_1p5()
.gap_2()
.child(Skeleton::new().flex_shrink_0().size_6().rounded_full())
.child(
div()
.flex_1()
.flex()
.justify_between()
.child(Skeleton::new().w_32().h_2p5().rounded_sm())
.child(Skeleton::new().w_6().h_2p5().rounded_sm()),
);
};
h_flex()
@@ -152,10 +165,6 @@ impl RenderOnce for RoomListItem {
.child(created_at),
),
)
.context_menu(move |this, _window, _cx| {
// TODO: add share chat room
this.menu(t!("profile.view"), Box::new(OpenProfile(public_key)))
})
.hover(|this| this.bg(cx.theme().elevated_surface_background))
.on_click(move |event, window, cx| {
handler(event, window, cx);
+203 -39
View File
@@ -6,15 +6,15 @@ use anyhow::{anyhow, Error};
use common::debounced_delay::DebouncedDelay;
use common::display::{ReadableTimestamp, TextUtils};
use global::constants::{BOOTSTRAP_RELAYS, SEARCH_RELAYS};
use global::nostr_client;
use global::{css, nostr_client, UnwrappingStatus};
use gpui::prelude::FluentBuilder;
use gpui::{
div, uniform_list, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle,
Focusable, IntoElement, ParentElement, Render, RetainAllImageCache, SharedString, Styled,
Subscription, Task, Window,
div, relative, uniform_list, AnyElement, App, AppContext, Context, Entity, EventEmitter,
FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render,
RetainAllImageCache, SharedString, Styled, Subscription, Task, Window,
};
use gpui_tokio::Tokio;
use i18n::t;
use i18n::{shared_t, t};
use itertools::Itertools;
use list_item::RoomListItem;
use nostr_sdk::prelude::*;
@@ -23,11 +23,13 @@ use registry::{Registry, RegistryEvent};
use settings::AppSettings;
use smallvec::{smallvec, SmallVec};
use theme::ActiveTheme;
use ui::button::{Button, ButtonRounded, ButtonVariants};
use ui::button::{Button, ButtonVariants};
use ui::dock_area::panel::{Panel, PanelEvent};
use ui::input::{InputEvent, InputState, TextInput};
use ui::popup_menu::PopupMenu;
use ui::{v_flex, ContextModal, IconName, Selectable, Sizable, StyledExt};
use ui::popup_menu::{PopupMenu, PopupMenuExt};
use ui::{h_flex, v_flex, ContextModal, Icon, IconName, Selectable, Sizable, StyledExt};
use crate::actions::{RelayStatus, Reload};
mod list_item;
@@ -69,17 +71,16 @@ impl Sidebar {
let global_result = cx.new(|_| None);
let cancel_handle = cx.new(|_| None);
let find_input = cx.new(|cx| {
InputState::new(window, cx).placeholder(t!("sidebar.find_or_start_conversation"))
});
let find_input =
cx.new(|cx| InputState::new(window, cx).placeholder(t!("sidebar.search_label")));
let chats = Registry::global(cx);
let registry = Registry::global(cx);
let mut subscriptions = smallvec![];
subscriptions.push(cx.subscribe_in(
&chats,
&registry,
window,
move |this, _chats, event, _window, cx| {
move |this, _, event, _window, cx| {
if let RegistryEvent::NewRequest(kind) = event {
this.indicator.update(cx, |this, cx| {
*this = Some(kind.to_owned());
@@ -192,11 +193,8 @@ impl Sidebar {
fn debounced_search(&self, window: &mut Window, cx: &mut Context<Self>) -> Task<()> {
cx.spawn_in(window, async move |this, cx| {
cx.update(|window, cx| {
this.update(cx, |this, cx| {
this.search(window, cx);
})
.ok();
this.update_in(cx, |this, window, cx| {
this.search(window, cx);
})
.ok();
})
@@ -227,18 +225,15 @@ impl Sidebar {
cx.spawn_in(window, async move |this, cx| {
match task.await {
Ok(Some(results)) => {
cx.update(|window, cx| {
this.update(cx, |this, cx| {
let msg = t!("sidebar.empty", query = query_cloned);
let rooms = results.into_iter().map(|r| cx.new(|_| r)).collect_vec();
this.update_in(cx, |this, window, cx| {
let msg = t!("sidebar.empty", query = query_cloned);
let rooms = results.into_iter().map(|r| cx.new(|_| r)).collect_vec();
if rooms.is_empty() {
window.push_notification(msg, cx);
}
if rooms.is_empty() {
window.push_notification(msg, cx);
}
this.results(rooms, true, window, cx);
})
.ok();
this.results(rooms, true, window, cx);
})
.ok();
}
@@ -254,12 +249,10 @@ impl Sidebar {
}
// Async task failed
Err(e) => {
cx.update(|window, cx| {
this.update(cx, |this, cx| {
window.push_notification(e.to_string(), cx);
this.set_finding(false, window, cx);
this.set_cancel_handle(None, cx);
})
this.update_in(cx, |this, window, cx| {
window.push_notification(e.to_string(), cx);
this.set_finding(false, window, cx);
this.set_cancel_handle(None, cx);
})
.ok();
}
@@ -527,6 +520,88 @@ impl Sidebar {
});
}
fn on_reload(&mut self, _ev: &Reload, window: &mut Window, cx: &mut Context<Self>) {
Registry::global(cx).update(cx, |this, cx| {
this.load_rooms(window, cx);
});
window.push_notification(t!("common.refreshed"), cx);
}
fn on_manage(&mut self, _ev: &RelayStatus, window: &mut Window, cx: &mut Context<Self>) {
let task: Task<Result<Vec<Relay>, Error>> = cx.background_spawn(async move {
let client = nostr_client();
let css = css();
let subscription = client.subscription(&css.gift_wrap_sub_id).await;
let mut relays: Vec<Relay> = vec![];
for (url, _filter) in subscription.into_iter() {
relays.push(client.pool().relay(url).await?);
}
Ok(relays)
});
cx.spawn_in(window, async move |this, cx| {
if let Ok(relays) = task.await {
this.update_in(cx, |this, window, cx| {
this.manage_relays(relays, window, cx);
})
.ok();
}
})
.detach();
}
fn manage_relays(&mut self, relays: Vec<Relay>, window: &mut Window, cx: &mut Context<Self>) {
window.open_modal(cx, move |this, _window, cx| {
this.show_close(true)
.overlay_closable(true)
.keyboard(true)
.title(shared_t!("manage_relays.modal"))
.child(v_flex().pb_4().gap_2().children({
let mut items = Vec::with_capacity(relays.len());
for relay in relays.clone().into_iter() {
let url = relay.url().to_string();
let time = relay.stats().connected_at().to_ago();
let connected = relay.is_connected();
items.push(
h_flex()
.h_8()
.px_2()
.justify_between()
.text_xs()
.bg(cx.theme().elevated_surface_background)
.rounded(cx.theme().radius)
.child(
h_flex()
.gap_1()
.font_semibold()
.child(
Icon::new(IconName::Signal)
.small()
.text_color(cx.theme().danger_active)
.when(connected, |this| {
this.text_color(gpui::green().alpha(0.75))
}),
)
.child(url),
)
.child(
div()
.text_right()
.text_color(cx.theme().text_muted)
.child(shared_t!("manage_relays.time", t = time)),
),
);
}
items
}))
});
}
fn list_items(
&self,
rooms: &[Entity<Room>],
@@ -594,6 +669,7 @@ impl Focusable for Sidebar {
impl Render for Sidebar {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let registry = Registry::read_global(cx);
let loading = registry.unwrapping_status.read(cx) != &UnwrappingStatus::Complete;
// Get rooms from either search results or the chat registry
let rooms = if let Some(results) = self.local_result.read(cx).as_ref() {
@@ -609,7 +685,17 @@ impl Render for Sidebar {
}
};
// Get total rooms count
let mut total_rooms = rooms.len();
// Add 3 dummy rooms to display as skeletons
if loading {
total_rooms += 3
}
v_flex()
.on_action(cx.listener(Self::on_reload))
.on_action(cx.listener(Self::on_manage))
.image_cache(self.image_cache.clone())
.size_full()
.relative()
@@ -632,7 +718,7 @@ impl Render for Sidebar {
.suffix(
Button::new("find")
.icon(IconName::Search)
.tooltip(t!("sidebar.press_enter_to_search"))
.tooltip(t!("sidebar.search_tooltip"))
.transparent()
.small(),
),
@@ -664,9 +750,10 @@ impl Render for Sidebar {
})
})
.small()
.cta()
.bold()
.secondary()
.rounded(ButtonRounded::Full)
.rounded()
.selected(self.filter(&RoomKind::Ongoing, cx))
.on_click(cx.listener(|this, _, _, cx| {
this.set_filter(RoomKind::Ongoing, cx);
@@ -684,19 +771,96 @@ impl Render for Sidebar {
})
})
.small()
.cta()
.bold()
.secondary()
.rounded(ButtonRounded::Full)
.rounded()
.selected(!self.filter(&RoomKind::Ongoing, cx))
.on_click(cx.listener(|this, _, _, cx| {
this.set_filter(RoomKind::default(), cx);
})),
)
.child(
h_flex()
.flex_1()
.w_full()
.justify_end()
.items_center()
.text_xs()
.child(
Button::new("option")
.icon(IconName::Ellipsis)
.xsmall()
.ghost()
.rounded()
.popup_menu(move |this, _window, _cx| {
this.menu(
t!("sidebar.reload_menu"),
Box::new(Reload),
)
.menu(
t!("sidebar.status_menu"),
Box::new(RelayStatus),
)
}),
),
),
)
.when(!loading && total_rooms == 0, |this| {
this.map(|this| {
if self.filter(&RoomKind::Ongoing, cx) {
this.child(
v_flex()
.py_2()
.gap_1p5()
.items_center()
.justify_center()
.text_center()
.child(
div()
.text_sm()
.font_semibold()
.line_height(relative(1.25))
.child(shared_t!("sidebar.no_conversations")),
)
.child(
div()
.text_xs()
.text_color(cx.theme().text_muted)
.line_height(relative(1.25))
.child(shared_t!("sidebar.no_conversations_label")),
),
)
} else {
this.child(
v_flex()
.py_2()
.gap_1p5()
.items_center()
.justify_center()
.text_center()
.child(
div()
.text_sm()
.font_semibold()
.line_height(relative(1.25))
.child(shared_t!("sidebar.no_requests")),
)
.child(
div()
.text_xs()
.text_color(cx.theme().text_muted)
.line_height(relative(1.25))
.child(shared_t!("sidebar.no_requests_label")),
),
)
}
})
})
.child(
uniform_list(
"rooms",
rooms.len(),
total_rooms,
cx.processor(move |this, range, _window, cx| {
this.list_items(&rooms, range, cx)
}),
+39 -25
View File
@@ -1,12 +1,13 @@
use gpui::{
div, svg, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
IntoElement, ParentElement, Render, SharedString, Styled, Window,
InteractiveElement, IntoElement, ParentElement, Render, SharedString,
StatefulInteractiveElement, Styled, Window,
};
use theme::ActiveTheme;
use ui::button::Button;
use ui::dock_area::panel::{Panel, PanelEvent};
use ui::popup_menu::PopupMenu;
use ui::StyledExt;
use ui::{v_flex, StyledExt};
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Welcome> {
Welcome::new(window, cx)
@@ -14,8 +15,7 @@ pub fn init(window: &mut Window, cx: &mut App) -> Entity<Welcome> {
pub struct Welcome {
name: SharedString,
closable: bool,
zoomable: bool,
version: SharedString,
focus_handle: FocusHandle,
}
@@ -25,10 +25,11 @@ impl Welcome {
}
fn view(_window: &mut Window, cx: &mut Context<Self>) -> Self {
let version = SharedString::from(format!("Version: {}", env!("CARGO_PKG_VERSION")));
Self {
version,
name: "Welcome".into(),
closable: true,
zoomable: true,
focus_handle: cx.focus_handle(),
}
}
@@ -39,16 +40,15 @@ impl Panel for Welcome {
self.name.clone()
}
fn title(&self, _cx: &App) -> AnyElement {
"👋".into_any_element()
}
fn closable(&self, _cx: &App) -> bool {
self.closable
}
fn zoomable(&self, _cx: &App) -> bool {
self.zoomable
fn title(&self, cx: &App) -> AnyElement {
div()
.child(
svg()
.path("brand/coop.svg")
.size_4()
.text_color(cx.theme().element_background),
)
.into_any_element()
}
fn popup_menu(&self, menu: PopupMenu, _cx: &App) -> PopupMenu {
@@ -76,11 +76,10 @@ impl Render for Welcome {
.items_center()
.justify_center()
.child(
div()
.flex()
.flex_col()
v_flex()
.gap_2()
.items_center()
.gap_1()
.justify_center()
.child(
svg()
.path("brand/coop.svg")
@@ -88,11 +87,26 @@ impl Render for Welcome {
.text_color(cx.theme().elevated_surface_background),
)
.child(
div()
.child("coop on nostr")
.text_color(cx.theme().text_placeholder)
.font_semibold()
.text_sm(),
v_flex()
.items_center()
.justify_center()
.text_center()
.child(
div()
.font_semibold()
.text_color(cx.theme().text_muted)
.child("coop on nostr"),
)
.child(
div()
.id("version")
.text_color(cx.theme().text_placeholder)
.text_xs()
.child(self.version.clone())
.on_click(|_, _window, cx| {
cx.open_url("https://github.com/lumehq/coop/releases");
}),
),
),
)
}
+1 -2
View File
@@ -5,11 +5,10 @@ edition.workspace = true
publish.workspace = true
[dependencies]
nostr-connect.workspace = true
nostr-sdk.workspace = true
dirs.workspace = true
smol.workspace = true
futures.workspace = true
flume.workspace = true
log.workspace = true
anyhow.workspace = true
+4 -4
View File
@@ -34,7 +34,10 @@ pub const NIP17_RELAYS: [&str; 2] = ["wss://nip17.com", "wss://auth.nostr1.com"]
pub const NOSTR_CONNECT_RELAY: &str = "wss://relay.nsec.app";
/// Default retry count for fetching NIP-17 relays
pub const TOTAL_RETRY: u64 = 2;
pub const RELAY_RETRY: u64 = 2;
/// Default retry count for sending messages
pub const SEND_RETRY: u64 = 10;
/// Default timeout (in seconds) for Nostr Connect
pub const NOSTR_CONNECT_TIMEOUT: u64 = 200;
@@ -48,9 +51,6 @@ pub const METADATA_BATCH_LIMIT: usize = 100;
/// Maximum timeout for grouping metadata requests. (milliseconds)
pub const METADATA_BATCH_TIMEOUT: u64 = 300;
/// Maximum timeout for waiting for finish (seconds)
pub const WAIT_FOR_FINISH: u64 = 60;
/// Default width of the sidebar.
pub const DEFAULT_SIDEBAR_WIDTH: f32 = 240.;
+134 -55
View File
@@ -1,10 +1,11 @@
use std::collections::{HashMap, HashSet};
use std::sync::atomic::AtomicBool;
use std::sync::OnceLock;
use std::time::Duration;
use nostr_connect::prelude::*;
use flume::{Receiver, Sender};
use nostr_sdk::prelude::*;
use paths::nostr_file;
use smol::channel::{Receiver, Sender};
use smol::lock::RwLock;
use crate::paths::support_dir;
@@ -12,16 +13,18 @@ use crate::paths::support_dir;
pub mod constants;
pub mod paths;
#[derive(Debug, Clone)]
pub struct AuthReq {
pub challenge: String,
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct AuthRequest {
pub url: RelayUrl,
pub challenge: String,
pub sending: bool,
}
impl AuthReq {
impl AuthRequest {
pub fn new(challenge: impl Into<String>, url: RelayUrl) -> Self {
Self {
challenge: challenge.into(),
sending: false,
url,
}
}
@@ -44,9 +47,17 @@ impl Notice {
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum UnwrappingStatus {
#[default]
Initialized,
Processing,
Complete,
}
/// Signals sent through the global event channel to notify UI
#[derive(Debug)]
pub enum IngesterSignal {
pub enum SignalKind {
/// A signal to notify UI that the client's signer has been set
SignerSet(PublicKey),
@@ -54,34 +65,60 @@ pub enum IngesterSignal {
SignerUnset,
/// A signal to notify UI that the relay requires authentication
Auth(AuthReq),
Auth(AuthRequest),
/// A signal to notify UI that the browser proxy service is down
ProxyDown,
/// A signal to notify UI that a new metadata event has been received
Metadata(Event),
/// A signal to notify UI that a new profile has been received
NewProfile(Profile),
/// A signal to notify UI that a new gift wrap event has been received
GiftWrap((EventId, Event)),
NewMessage((EventId, Event)),
/// A signal to notify UI that all gift wrap events have been processed
Finish,
/// A signal to notify UI that no DM relays for current user was found
RelaysNotFound,
/// A signal to notify UI that partial processing of gift wrap events has been completed
PartialFinish,
/// A signal to notify UI that no DM relay for current user was found
DmRelayNotFound,
/// A signal to notify UI that gift wrap status has changed
GiftWrapStatus(UnwrappingStatus),
/// A signal to notify UI that there are errors or notices occurred
Notice(Notice),
}
#[derive(Debug)]
pub struct Signal {
rx: Receiver<SignalKind>,
tx: Sender<SignalKind>,
}
impl Default for Signal {
fn default() -> Self {
Self::new()
}
}
impl Signal {
pub fn new() -> Self {
let (tx, rx) = flume::bounded::<SignalKind>(2048);
Self { rx, tx }
}
pub fn receiver(&self) -> &Receiver<SignalKind> {
&self.rx
}
pub async fn send(&self, kind: SignalKind) {
if let Err(e) = self.tx.send_async(kind).await {
log::error!("Failed to send signal: {e}");
}
}
}
#[derive(Debug)]
pub struct Ingester {
rx: Receiver<IngesterSignal>,
tx: Sender<IngesterSignal>,
rx: Receiver<PublicKey>,
tx: Sender<PublicKey>,
}
impl Default for Ingester {
@@ -92,30 +129,83 @@ impl Default for Ingester {
impl Ingester {
pub fn new() -> Self {
let (tx, rx) = smol::channel::bounded::<IngesterSignal>(2048);
let (tx, rx) = flume::bounded::<PublicKey>(1024);
Self { rx, tx }
}
pub fn signals(&self) -> &Receiver<IngesterSignal> {
pub fn receiver(&self) -> &Receiver<PublicKey> {
&self.rx
}
pub async fn send(&self, signal: IngesterSignal) {
if let Err(e) = self.tx.send(signal).await {
log::error!("Failed to send signal: {e}");
pub async fn send(&self, public_key: PublicKey) {
if let Err(e) = self.tx.send_async(public_key).await {
log::error!("Failed to send public key: {e}");
}
}
}
/// A simple storage to store all states that using across the application.
#[derive(Debug)]
pub struct CoopSimpleStorage {
pub init_at: Timestamp,
pub last_used_at: Option<Timestamp>,
pub is_first_run: AtomicBool,
pub gift_wrap_sub_id: SubscriptionId,
pub gift_wrap_processing: AtomicBool,
pub auto_close_opts: Option<SubscribeAutoCloseOptions>,
pub seen_on_relays: RwLock<HashMap<EventId, HashSet<RelayUrl>>>,
pub sent_ids: RwLock<HashSet<EventId>>,
pub resent_ids: RwLock<Vec<Output<EventId>>>,
pub resend_queue: RwLock<HashMap<EventId, RelayUrl>>,
pub signal: Signal,
pub ingester: Ingester,
}
impl Default for CoopSimpleStorage {
fn default() -> Self {
Self::new()
}
}
impl CoopSimpleStorage {
pub fn new() -> Self {
let init_at = Timestamp::now();
let first_run = first_run();
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
let signal = Signal::default();
let ingester = Ingester::default();
Self {
init_at,
signal,
ingester,
last_used_at: None,
is_first_run: AtomicBool::new(first_run),
gift_wrap_sub_id: SubscriptionId::new("inbox"),
gift_wrap_processing: AtomicBool::new(false),
auto_close_opts: Some(opts),
seen_on_relays: RwLock::new(HashMap::new()),
sent_ids: RwLock::new(HashSet::new()),
resent_ids: RwLock::new(Vec::new()),
resend_queue: RwLock::new(HashMap::new()),
}
}
}
static NOSTR_CLIENT: OnceLock<Client> = OnceLock::new();
static INGESTER: OnceLock<Ingester> = OnceLock::new();
static SENT_IDS: OnceLock<RwLock<Vec<EventId>>> = OnceLock::new();
static CURRENT_TIMESTAMP: OnceLock<Timestamp> = OnceLock::new();
static FIRST_RUN: OnceLock<bool> = OnceLock::new();
static COOP_SIMPLE_STORAGE: OnceLock<CoopSimpleStorage> = OnceLock::new();
pub fn nostr_client() -> &'static Client {
NOSTR_CLIENT.get_or_init(|| {
@@ -132,7 +222,6 @@ pub fn nostr_client() -> &'static Client {
.gossip(true)
.automatic_authentication(false)
.verify_subscriptions(false)
// Sleep after idle for 30 seconds
.sleep_when_idle(SleepWhenIdle::Enabled {
timeout: Duration::from_secs(30),
});
@@ -141,29 +230,19 @@ pub fn nostr_client() -> &'static Client {
})
}
pub fn ingester() -> &'static Ingester {
INGESTER.get_or_init(Ingester::new)
pub fn css() -> &'static CoopSimpleStorage {
COOP_SIMPLE_STORAGE.get_or_init(CoopSimpleStorage::new)
}
pub fn starting_time() -> &'static Timestamp {
CURRENT_TIMESTAMP.get_or_init(Timestamp::now)
}
fn first_run() -> bool {
let flag = support_dir().join(format!(".{}-first_run", env!("CARGO_PKG_VERSION")));
pub fn sent_ids() -> &'static RwLock<Vec<EventId>> {
SENT_IDS.get_or_init(|| RwLock::new(Vec::new()))
}
pub fn first_run() -> &'static bool {
FIRST_RUN.get_or_init(|| {
let flag = support_dir().join(format!(".{}-first_run", env!("CARGO_PKG_VERSION")));
if !flag.exists() {
if std::fs::write(&flag, "").is_err() {
return false;
}
true // First run
} else {
false // Not first run
if !flag.exists() {
if std::fs::write(&flag, "").is_err() {
return false;
}
})
true // First run
} else {
false // Not first run
}
}
-1
View File
@@ -19,4 +19,3 @@ smol.workspace = true
log.workspace = true
fuzzy-matcher = "0.3.7"
hashbrown = "0.15"
+80 -57
View File
@@ -1,12 +1,12 @@
use std::cmp::Reverse;
use std::collections::{HashMap, HashSet};
use anyhow::Error;
use common::event::EventUtils;
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
use global::nostr_client;
use global::{nostr_client, UnwrappingStatus};
use gpui::{App, AppContext, Context, Entity, EventEmitter, Global, Task, WeakEntity, Window};
use hashbrown::{HashMap, HashSet};
use itertools::Itertools;
use nostr_sdk::prelude::*;
use room::RoomKind;
@@ -41,10 +41,8 @@ pub struct Registry {
/// Collection of all persons (user profiles)
pub persons: HashMap<PublicKey, Entity<Profile>>,
/// Indicates if rooms are currently being loaded
///
/// Always equal to `true` when the app starts
pub loading: bool,
/// Status of the unwrapping process
pub unwrapping_status: Entity<UnwrappingStatus>,
/// Public Key of the current user
pub identity: Option<PublicKey>,
@@ -73,6 +71,7 @@ impl Registry {
/// Create a new Registry instance
pub(crate) fn new(cx: &mut Context<Self>) -> Self {
let unwrapping_status = cx.new(|_| UnwrappingStatus::default());
let mut tasks = smallvec![];
let load_local_persons: Task<Result<Vec<Profile>, Error>> =
@@ -104,10 +103,10 @@ impl Registry {
);
Self {
unwrapping_status,
rooms: vec![],
persons: HashMap::new(),
identity: None,
loading: true,
_tasks: tasks,
}
}
@@ -156,21 +155,19 @@ impl Registry {
}
/// Insert or update a person
pub fn insert_or_update_person(&mut self, event: Event, cx: &mut App) {
let public_key = event.pubkey;
let Ok(metadata) = Metadata::from_json(event.content) else {
// Invalid metadata, no need to process further.
return;
};
pub fn insert_or_update_person(&mut self, profile: Profile, cx: &mut App) {
let public_key = profile.public_key();
if let Some(person) = self.persons.get(&public_key) {
person.update(cx, |this, cx| {
*this = Profile::new(public_key, metadata);
cx.notify();
});
} else {
self.persons
.insert(public_key, cx.new(|_| Profile::new(public_key, metadata)));
match self.persons.get(&public_key) {
Some(person) => {
person.update(cx, |this, cx| {
*this = profile;
cx.notify();
});
}
None => {
self.persons.insert(public_key, cx.new(|_| profile));
}
}
}
@@ -244,16 +241,24 @@ impl Registry {
}
/// Set the loading status of the registry.
pub fn set_loading(&mut self, status: bool, cx: &mut Context<Self>) {
self.loading = status;
cx.notify();
pub fn set_unwrapping_status(&mut self, status: UnwrappingStatus, cx: &mut Context<Self>) {
self.unwrapping_status.update(cx, |this, cx| {
*this = status;
cx.notify();
});
}
/// Reset the registry.
pub fn reset(&mut self, cx: &mut Context<Self>) {
self.rooms = vec![];
self.loading = true;
// Reset the unwrapping status
self.set_unwrapping_status(UnwrappingStatus::default(), cx);
// Clear the current identity
self.identity = None;
// Clear all current rooms
self.rooms.clear();
cx.notify();
}
@@ -262,12 +267,13 @@ impl Registry {
log::info!("Starting to load chat rooms...");
// Get the contact bypass setting
let contact_bypass = AppSettings::get_contact_bypass(cx);
let bypass_setting = AppSettings::get_contact_bypass(cx);
let task: Task<Result<HashSet<Room>, Error>> = cx.background_spawn(async move {
let client = nostr_client();
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let contacts = client.database().contacts_public_keys(public_key).await?;
// Get messages sent by the user
let send = Filter::new()
@@ -295,17 +301,17 @@ impl Registry {
continue;
}
// Get all public keys from the event
let public_keys = event.all_pubkeys();
// Get all public keys from the event's tags
let mut public_keys = event.all_pubkeys();
public_keys.retain(|pk| pk != &public_key);
// Bypass screening flag
let mut bypass = false;
let mut bypassed = false;
// If user enabled bypass screening for contacts
// Check if room's members are in contact with current user
if contact_bypass {
let contacts = client.database().contacts_public_keys(public_key).await?;
bypass = public_keys.iter().any(|k| contacts.contains(k));
// If the user has enabled bypass screening in settings,
// check if any of the room's members are contacts of the current user
if bypass_setting {
bypassed = public_keys.iter().any(|k| contacts.contains(k));
}
// Check if the current user has sent at least one message to this room
@@ -320,7 +326,7 @@ impl Registry {
// Create a new room
let room = Room::new(&event).rearrange_by(public_key);
if is_ongoing || bypass {
if is_ongoing || bypassed {
rooms.insert(room.kind(RoomKind::Ongoing));
} else {
rooms.insert(room);
@@ -333,9 +339,11 @@ impl Registry {
cx.spawn_in(window, async move |this, cx| {
match task.await {
Ok(rooms) => {
this.update(cx, |this, cx| {
this.extend_rooms(rooms, cx);
this.sort(cx);
this.update_in(cx, move |_, window, cx| {
cx.defer_in(window, move |this, _window, cx| {
this.extend_rooms(rooms, cx);
this.sort(cx);
});
})
.ok();
}
@@ -348,23 +356,28 @@ impl Registry {
}
pub(crate) fn extend_rooms(&mut self, rooms: HashSet<Room>, cx: &mut Context<Self>) {
let mut room_map: HashMap<u64, usize> = HashMap::with_capacity(self.rooms.len());
for (index, room) in self.rooms.iter().enumerate() {
room_map.insert(room.read(cx).id, index);
}
let mut room_map: HashMap<u64, usize> = self
.rooms
.iter()
.enumerate()
.map(|(idx, room)| (room.read(cx).id, idx))
.collect();
for new_room in rooms.into_iter() {
// Check if we already have a room with this ID
if let Some(&index) = room_map.get(&new_room.id) {
self.rooms[index].update(cx, |this, cx| {
*this = new_room;
cx.notify();
if new_room.created_at > this.created_at {
*this = new_room;
cx.notify();
}
});
} else {
let new_index = self.rooms.len();
room_map.insert(new_room.id, new_index);
let new_room_id = new_room.id;
self.rooms.push(cx.new(|_| new_room));
let new_index = self.rooms.len();
room_map.insert(new_room_id, new_index);
}
}
}
@@ -388,12 +401,14 @@ impl Registry {
}
/// Refresh messages for a room in the global registry
pub fn refresh_rooms(&mut self, ids: Vec<u64>, cx: &mut Context<Self>) {
for room in self.rooms.iter() {
if ids.contains(&room.read(cx).id) {
room.update(cx, |this, cx| {
this.emit_refresh(cx);
});
pub fn refresh_rooms(&mut self, ids: Option<Vec<u64>>, cx: &mut Context<Self>) {
if let Some(ids) = ids {
for room in self.rooms.iter() {
if ids.contains(&room.read(cx).id) {
room.update(cx, |this, cx| {
this.emit_refresh(cx);
});
}
}
}
}
@@ -417,9 +432,13 @@ impl Registry {
};
if let Some(room) = self.rooms.iter().find(|room| room.read(cx).id == id) {
let is_new_event = event.created_at > room.read(cx).created_at;
// Update room
room.update(cx, |this, cx| {
this.created_at(event.created_at, cx);
if is_new_event {
this.created_at(event.created_at, cx);
}
// Set this room is ongoing if the new message is from current user
if author == identity {
@@ -432,8 +451,12 @@ impl Registry {
});
});
// Re-sort the rooms registry by their created at
self.sort(cx);
// Resort all rooms in the registry by their created at (after updated)
if is_new_event {
cx.defer_in(window, |this, _window, cx| {
this.sort(cx);
});
}
} else {
let room = Room::new(&event)
.kind(RoomKind::default())
+33 -2
View File
@@ -2,16 +2,47 @@ use std::hash::Hash;
use nostr_sdk::prelude::*;
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum Message {
User(RenderedMessage),
System,
Warning(String, Timestamp),
System(Timestamp),
}
impl Message {
pub fn user(user: impl Into<RenderedMessage>) -> Self {
Self::User(user.into())
}
pub fn warning(content: String) -> Self {
Self::Warning(content, Timestamp::now())
}
pub fn system() -> Self {
Self::System(Timestamp::default())
}
}
impl Ord for Message {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (self, other) {
(Message::User(a), Message::User(b)) => a.cmp(b),
(Message::System(a), Message::System(b)) => a.cmp(b),
(Message::User(a), Message::System(b)) => a.created_at.cmp(b),
(Message::System(a), Message::User(b)) => a.cmp(&b.created_at),
(Message::Warning(_, a), Message::Warning(_, b)) => a.cmp(b),
(Message::Warning(_, a), Message::User(b)) => a.cmp(&b.created_at),
(Message::User(a), Message::Warning(_, b)) => a.created_at.cmp(b),
(Message::Warning(_, a), Message::System(b)) => a.cmp(b),
(Message::System(a), Message::Warning(_, b)) => a.cmp(b),
}
}
}
impl PartialOrd for Message {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone)]
+191 -75
View File
@@ -1,10 +1,13 @@
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::time::Duration;
use anyhow::Error;
use common::display::ReadableProfile;
use common::event::EventUtils;
use global::nostr_client;
use global::constants::SEND_RETRY;
use global::{css, nostr_client};
use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task};
use itertools::Itertools;
use nostr_sdk::prelude::*;
@@ -14,45 +17,51 @@ use crate::Registry;
#[derive(Debug, Clone)]
pub struct SendReport {
pub receiver: PublicKey,
pub output: Option<Output<EventId>>,
pub local_error: Option<SharedString>,
pub nip17_relays_not_found: bool,
pub tags: Option<Vec<Tag>>,
pub status: Option<Output<EventId>>,
pub error: Option<SharedString>,
pub relays_not_found: bool,
}
impl SendReport {
pub fn output(receiver: PublicKey, output: Output<EventId>) -> Self {
pub fn new(receiver: PublicKey) -> Self {
Self {
receiver,
output: Some(output),
local_error: None,
nip17_relays_not_found: false,
status: None,
error: None,
tags: None,
relays_not_found: false,
}
}
pub fn error(receiver: PublicKey, error: impl Into<SharedString>) -> Self {
Self {
receiver,
output: None,
local_error: Some(error.into()),
nip17_relays_not_found: false,
}
pub fn not_found(mut self) -> Self {
self.relays_not_found = true;
self
}
pub fn nip17_relays_not_found(receiver: PublicKey) -> Self {
Self {
receiver,
output: None,
local_error: None,
nip17_relays_not_found: true,
}
pub fn error(mut self, error: impl Into<SharedString>) -> Self {
self.error = Some(error.into());
self.relays_not_found = false;
self
}
pub fn status(mut self, output: Output<EventId>) -> Self {
self.status = Some(output);
self.relays_not_found = false;
self
}
pub fn tags(mut self, tags: &Vec<Tag>) -> Self {
self.tags = Some(tags.to_owned());
self
}
pub fn is_relay_error(&self) -> bool {
self.local_error.is_some() || self.nip17_relays_not_found
self.error.is_some() || self.relays_not_found
}
pub fn is_sent_success(&self) -> bool {
if let Some(output) = self.output.as_ref() {
if let Some(output) = self.status.as_ref() {
!output.success.is_empty()
} else {
false
@@ -322,48 +331,69 @@ impl Room {
}
}
/// Connects to all members' messaging relays
pub fn connect_relays(
&self,
cx: &App,
) -> Task<Result<HashMap<PublicKey, Vec<RelayUrl>>, Error>> {
let members = self.members.clone();
cx.background_spawn(async move {
let client = nostr_client();
let timeout = Duration::from_secs(3);
let mut processed = HashSet::new();
let mut relays: HashMap<PublicKey, Vec<RelayUrl>> = HashMap::new();
if let Some((_, members)) = members.split_last() {
for member in members.iter() {
relays.insert(member.to_owned(), vec![]);
let filter = Filter::new()
.kind(Kind::InboxRelays)
.author(member.to_owned())
.limit(1);
if let Ok(mut stream) = client.stream_events(filter, timeout).await {
if let Some(event) = stream.next().await {
if processed.insert(event.id) {
let urls = nip17::extract_owned_relay_list(event).collect_vec();
relays.entry(member.to_owned()).or_default().extend(urls);
}
}
}
}
};
Ok(relays)
})
}
/// Loads all messages for this room from the database
///
/// # Arguments
///
/// * `cx` - The App context
///
/// # Returns
///
/// A Task that resolves to Result<Vec<Event>, Error> containing all messages for this room
pub fn load_messages(&self, cx: &App) -> Task<Result<Vec<Event>, Error>> {
let members = self.members.clone();
cx.background_spawn(async move {
let client = nostr_client();
let public_key = members[members.len() - 1];
let filter = Filter::new()
let sent = Filter::new()
.kind(Kind::PrivateDirectMessage)
.authors(members.clone())
.author(public_key)
.pubkeys(members.clone());
let events: Vec<Event> = client
.database()
.query(filter)
.await?
.into_iter()
.filter(|ev| ev.compare_pubkeys(&members))
.collect();
let recv = Filter::new()
.kind(Kind::PrivateDirectMessage)
.authors(members)
.pubkey(public_key);
let sent_events = client.database().query(sent).await?;
let recv_events = client.database().query(recv).await?;
let events: Vec<Event> = sent_events.merge(recv_events).into_iter().collect();
Ok(events)
})
}
/// Emits a new message signal to the current room
pub fn emit_message(&self, gift_wrap_id: EventId, event: Event, cx: &mut Context<Self>) {
cx.emit(RoomSignal::NewMessage((gift_wrap_id, Box::new(event))));
}
/// Emits a signal to refresh the current room's messages.
pub fn emit_refresh(&mut self, cx: &mut Context<Self>) {
cx.emit(RoomSignal::Refresh);
}
/// Creates a temporary message for optimistic updates
///
/// The event must not been published to relays.
@@ -396,17 +426,7 @@ impl Room {
event
}
/// Sends a message to all members in the background task
///
/// # Arguments
///
/// * `content` - The content of the message to send
/// * `cx` - The App context
///
/// # Returns
///
/// A Task that resolves to Result<Vec<String>, Error> where the
/// strings contain error messages for any failed sends
/// Create a task to sends a message to all members in the background
pub fn send_in_background(
&self,
content: &str,
@@ -420,20 +440,21 @@ impl Room {
let mut public_keys = self.members.clone();
cx.background_spawn(async move {
let css = css();
let client = nostr_client();
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let mut tags = public_keys
let mut tags: Vec<Tag> = public_keys
.iter()
.filter_map(|pubkey| {
if pubkey != &public_key {
Some(Tag::public_key(*pubkey))
.filter_map(|&this| {
if this != public_key {
Some(Tag::public_key(this))
} else {
None
}
})
.collect_vec();
.collect();
// Add event reference if it's present (replying to another event)
if replies.len() == 1 {
@@ -466,19 +487,50 @@ impl Room {
// Stored all send errors
let mut reports = vec![];
for receiver in public_keys.into_iter() {
for pubkey in public_keys.into_iter() {
match client
.send_private_msg(receiver, &content, tags.clone())
.send_private_msg(pubkey, &content, tags.clone())
.await
{
Ok(output) => {
reports.push(SendReport::output(receiver, output));
let id = output.id().to_owned();
let auth_required = output.failed.iter().any(|m| m.1.starts_with("auth-"));
let report = SendReport::new(pubkey).status(output).tags(&tags);
if auth_required {
// Wait for authenticated and resent event successfully
for attempt in 0..=SEND_RETRY {
// Check if event was successfully resent
if let Some(output) = css
.resent_ids
.read()
.await
.iter()
.find(|e| e.id() == &id)
.cloned()
{
let output = SendReport::new(pubkey).status(output).tags(&tags);
reports.push(output);
break;
}
// Check if retry limit exceeded
if attempt == SEND_RETRY {
reports.push(report);
break;
}
smol::Timer::after(Duration::from_millis(1200)).await;
}
} else {
reports.push(report);
}
}
Err(e) => {
if let nostr_sdk::client::Error::PrivateMsgRelaysNotFound = e {
reports.push(SendReport::nip17_relays_not_found(receiver));
reports.push(SendReport::new(pubkey).not_found().tags(&tags));
} else {
reports.push(SendReport::error(receiver, e.to_string()));
reports.push(SendReport::new(pubkey).error(e.to_string()).tags(&tags));
}
}
}
@@ -491,13 +543,14 @@ impl Room {
.await
{
Ok(output) => {
reports.push(SendReport::output(public_key, output));
reports.push(SendReport::new(public_key).status(output).tags(&tags));
}
Err(e) => {
if let nostr_sdk::client::Error::PrivateMsgRelaysNotFound = e {
reports.push(SendReport::nip17_relays_not_found(public_key));
reports.push(SendReport::new(public_key).not_found());
} else {
reports.push(SendReport::error(public_key, e.to_string()));
reports
.push(SendReport::new(public_key).error(e.to_string()).tags(&tags));
}
}
}
@@ -506,4 +559,67 @@ impl Room {
Ok(reports)
})
}
/// Create a task to resend a failed message
pub fn resend(
&self,
reports: Vec<SendReport>,
message: String,
backup: bool,
cx: &App,
) -> Task<Result<Vec<SendReport>, Error>> {
cx.background_spawn(async move {
let client = nostr_client();
let mut resend_reports = vec![];
let mut resend_tag = vec![];
for report in reports.into_iter() {
if let Some(output) = report.status {
let id = output.id();
let urls: Vec<&RelayUrl> = output.failed.keys().collect();
if let Some(event) = client.database().event_by_id(id).await? {
for url in urls.into_iter() {
let relay = client.pool().relay(url).await?;
let id = relay.send_event(&event).await?;
let resent: Output<EventId> = Output {
val: id,
success: HashSet::from([url.to_owned()]),
failed: HashMap::new(),
};
resend_reports.push(SendReport::new(report.receiver).status(resent));
}
if let Some(tags) = report.tags {
resend_tag.extend(tags);
}
}
}
}
// Only send a backup message to current user if sent successfully to others
if backup && !resend_reports.is_empty() {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let output = client
.send_private_msg(public_key, message, resend_tag)
.await?;
resend_reports.push(SendReport::new(public_key).status(output));
}
Ok(resend_reports)
})
}
/// Emits a new message signal to the current room
pub fn emit_message(&self, gift_wrap_id: EventId, event: Event, cx: &mut Context<Self>) {
cx.emit(RoomSignal::NewMessage((gift_wrap_id, Box::new(event))));
}
/// Emits a signal to refresh the current room's messages.
pub fn emit_refresh(&mut self, cx: &mut Context<Self>) {
cx.emit(RoomSignal::Refresh);
}
}
+46 -42
View File
@@ -94,7 +94,6 @@ impl Global for GlobalAppSettings {}
pub struct AppSettings {
setting_values: Settings,
_subscriptions: SmallVec<[Subscription; 1]>,
_tasks: SmallVec<[Task<()>; 1]>,
}
impl AppSettings {
@@ -113,15 +112,23 @@ impl AppSettings {
cx.set_global(GlobalAppSettings(state));
}
fn new(cx: &mut Context<Self>) -> Self {
let setting_values = Settings::default();
let mut tasks = smallvec![];
fn new(_cx: &mut Context<Self>) -> Self {
Self {
setting_values: Settings::default(),
_subscriptions: smallvec![],
}
}
pub fn load_settings(&self, cx: &mut Context<Self>) {
let task: Task<Result<Settings, anyhow::Error>> = cx.background_spawn(async move {
let client = nostr_client();
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.identifier(SETTINGS_IDENTIFIER)
.author(public_key)
.limit(1);
if let Some(event) = client.database().query(filter).await?.first_owned() {
@@ -131,44 +138,37 @@ impl AppSettings {
}
});
tasks.push(
// Load settings from database
cx.spawn(async move |this, cx| {
if let Ok(settings) = task.await {
this.update(cx, |this, cx| {
this.setting_values = settings;
cx.notify();
})
.ok();
}
}),
);
Self {
setting_values,
_subscriptions: smallvec![],
_tasks: tasks,
}
cx.spawn(async move |this, cx| {
if let Ok(settings) = task.await {
this.update(cx, |this, cx| {
this.setting_values = settings;
cx.notify();
})
.ok();
}
})
.detach();
}
pub(crate) fn set_settings(&self, cx: &mut Context<Self>) {
pub fn set_settings(&self, cx: &mut Context<Self>) {
if let Ok(content) = serde_json::to_string(&self.setting_values) {
cx.background_spawn(async move {
let task: Task<Result<(), anyhow::Error>> = cx.background_spawn(async move {
let client = nostr_client();
let builder = EventBuilder::new(Kind::ApplicationSpecificData, content)
.tags(vec![Tag::identifier(SETTINGS_IDENTIFIER)])
.sign(&Keys::generate())
.await;
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
if let Ok(event) = builder {
if let Err(e) = client.database().save_event(&event).await {
log::error!("Failed to save user settings: {e}");
} else {
log::info!("New settings have been saved successfully");
}
}
})
.detach();
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
.tag(Tag::identifier(SETTINGS_IDENTIFIER))
.build(public_key)
.sign(&Keys::generate())
.await?;
client.database().save_event(&event).await?;
Ok(())
});
task.detach();
}
}
@@ -176,12 +176,16 @@ impl AppSettings {
!self.setting_values.authenticated_relays.is_empty() && self.setting_values.auto_auth
}
pub fn auth_relays(&self) -> Vec<RelayUrl> {
self.setting_values.authenticated_relays.clone()
pub fn is_authenticated(&self, url: &RelayUrl) -> bool {
self.setting_values.authenticated_relays.contains(url)
}
pub fn push_auth_relay(&mut self, relay_url: RelayUrl, cx: &mut Context<Self>) {
self.setting_values.authenticated_relays.push(relay_url);
cx.notify();
pub fn push_relay(&mut self, relay_url: &RelayUrl, cx: &mut Context<Self>) {
if !self.is_authenticated(relay_url) {
self.setting_values
.authenticated_relays
.push(relay_url.to_owned());
cx.notify();
}
}
}
+1 -1
View File
@@ -3,4 +3,4 @@
///
/// Magic number: There is one extra pixel of padding on the left side due to
/// the 1px border around the window on macOS apps.
pub const TRAFFIC_LIGHT_PADDING: f32 = 71.;
pub const TRAFFIC_LIGHT_PADDING: f32 = 80.;
+30 -30
View File
@@ -1,8 +1,8 @@
use gpui::prelude::FluentBuilder as _;
use gpui::{
div, relative, AnyElement, App, ClickEvent, Div, ElementId, Hsla, InteractiveElement,
IntoElement, MouseButton, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, Window,
IntoElement, MouseButton, ParentElement, RenderOnce, SharedString, Stateful,
StatefulInteractiveElement as _, StyleRefinement, Styled, Window,
};
use theme::ActiveTheme;
@@ -10,11 +10,6 @@ use crate::indicator::Indicator;
use crate::tooltip::Tooltip;
use crate::{h_flex, Disableable, Icon, Selectable, Sizable, Size, StyledExt};
pub enum ButtonRounded {
Normal,
Full,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct ButtonCustomVariant {
color: Hsla,
@@ -121,8 +116,8 @@ type OnClick = Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>
/// A Button element.
#[derive(IntoElement)]
pub struct Button {
pub base: Div,
id: ElementId,
base: Stateful<Div>,
style: StyleRefinement,
icon: Option<Icon>,
label: Option<SharedString>,
@@ -130,7 +125,7 @@ pub struct Button {
children: Vec<AnyElement>,
variant: ButtonVariant,
rounded: ButtonRounded,
rounded: bool,
size: Size,
disabled: bool,
@@ -156,14 +151,14 @@ impl From<Button> for AnyElement {
impl Button {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
base: div().flex_shrink_0(),
id: id.into(),
base: div().id(id.into()).flex_shrink_0(),
style: StyleRefinement::default(),
icon: None,
label: None,
disabled: false,
selected: false,
variant: ButtonVariant::default(),
rounded: ButtonRounded::Normal,
rounded: false,
size: Size::Medium,
tooltip: None,
on_click: None,
@@ -177,9 +172,9 @@ impl Button {
}
}
/// Set the border radius of the Button.
pub fn rounded(mut self, rounded: impl Into<ButtonRounded>) -> Self {
self.rounded = rounded.into();
/// Make the button rounded.
pub fn rounded(mut self) -> Self {
self.rounded = true;
self
}
@@ -255,14 +250,14 @@ impl Disableable for Button {
}
impl Selectable for Button {
fn element_id(&self) -> &ElementId {
&self.id
}
fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
fn is_selected(&self) -> bool {
self.selected
}
}
impl Sizable for Button {
@@ -280,8 +275,8 @@ impl ButtonVariants for Button {
}
impl Styled for Button {
fn style(&mut self) -> &mut gpui::StyleRefinement {
self.base.style()
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
@@ -308,15 +303,15 @@ impl RenderOnce for Button {
};
self.base
.id(self.id)
.flex_shrink_0()
.flex()
.items_center()
.justify_center()
.cursor_pointer()
.cursor_default()
.overflow_hidden()
.map(|this| match self.rounded {
ButtonRounded::Normal => this.rounded(cx.theme().radius),
ButtonRounded::Full => this.rounded_full(),
false => this.rounded(cx.theme().radius),
true => this.rounded_full(),
})
.map(|this| {
if self.label.is_none() && self.children.is_empty() {
@@ -359,6 +354,8 @@ impl RenderOnce for Button {
Size::XSmall => {
if self.icon.is_some() {
this.h_6().pl_2().pr_2p5()
} else if self.cta {
this.h_6().px_4()
} else {
this.h_6().px_2()
}
@@ -366,6 +363,8 @@ impl RenderOnce for Button {
Size::Small => {
if self.icon.is_some() {
this.h_7().pl_2().pr_2p5()
} else if self.cta {
this.h_7().px_4()
} else {
this.h_7().px_2()
}
@@ -388,10 +387,6 @@ impl RenderOnce for Button {
}
})
.text_color(normal_style.fg)
.when(self.selected, |this| {
let selected_style = style.selected(window, cx);
this.bg(selected_style.bg).text_color(selected_style.fg)
})
.when(!self.disabled && !self.selected, |this| {
this.bg(normal_style.bg)
.hover(|this| {
@@ -403,6 +398,10 @@ impl RenderOnce for Button {
this.bg(active_style.bg).text_color(active_style.fg)
})
})
.when(self.selected, |this| {
let selected_style = style.selected(window, cx);
this.bg(selected_style.bg).text_color(selected_style.fg)
})
.when(self.disabled, |this| {
let disabled_style = style.disabled(window, cx);
this.cursor_not_allowed()
@@ -410,6 +409,7 @@ impl RenderOnce for Button {
.text_color(disabled_style.fg)
.shadow_none()
})
.refine_style(&self.style)
.child({
h_flex()
.id("label")
+4 -4
View File
@@ -54,13 +54,13 @@ impl Disableable for Checkbox {
}
impl Selectable for Checkbox {
fn element_id(&self) -> &ElementId {
&self.id
}
fn selected(self, selected: bool) -> Self {
self.checked(selected)
}
fn is_selected(&self) -> bool {
self.checked
}
}
impl RenderOnce for Checkbox {
+12 -8
View File
@@ -412,16 +412,15 @@ impl TabPanel {
let is_zoomed = self.is_zoomed && state.zoomable;
let view = cx.entity().clone();
let build_popup_menu = move |this, cx: &App| view.read(cx).popup_menu(this, cx);
let toolbar = self.toolbar_buttons(window, cx);
let has_toolbar = !toolbar.is_empty();
h_flex()
.p_0p5()
.gap_1()
.occlude()
.items_center()
.children(
self.toolbar_buttons(window, cx)
.into_iter()
.map(|btn| btn.small().ghost()),
)
.rounded_full()
.children(toolbar.into_iter().map(|btn| btn.small().ghost().rounded()))
.when(self.is_zoomed, |this| {
this.child(
Button::new("zoom")
@@ -434,11 +433,16 @@ impl TabPanel {
})),
)
})
.when(has_toolbar, |this| {
this.bg(cx.theme().surface_background)
.child(div().flex_shrink_0().h_4().w_px().bg(cx.theme().border))
})
.child(
Button::new("menu")
.icon(IconName::Ellipsis)
.small()
.ghost()
.rounded()
.popup_menu({
let zoomable = state.zoomable;
let closable = state.closable;
@@ -647,7 +651,7 @@ impl TabPanel {
.child(
div()
.size_full()
.rounded_lg()
.rounded_xl()
.shadow_sm()
.when(cx.theme().mode.is_dark(), |this| this.shadow_lg())
.bg(cx.theme().panel_background)
@@ -667,7 +671,7 @@ impl TabPanel {
.p_1()
.child(
div()
.rounded_lg()
.rounded_xl()
.border_1()
.border_color(cx.theme().element_disabled)
.bg(cx.theme().drop_target_background)
+6 -12
View File
@@ -14,8 +14,6 @@ pub enum IconName {
ArrowLeft,
ArrowRight,
ArrowUp,
ArrowUpCircle,
Bell,
CaretUp,
CaretDown,
CaretDownFill,
@@ -28,7 +26,6 @@ pub enum IconName {
CloseCircleFill,
Copy,
Edit,
EditFill,
Ellipsis,
Eye,
EyeOff,
@@ -52,18 +49,18 @@ pub enum IconName {
Reply,
Report,
Refresh,
Forward,
Signal,
Search,
SearchFill,
Settings,
Server,
SortAscending,
SortDescending,
Sun,
ThumbsDown,
ThumbsUp,
Upload,
UsersThreeFill,
OpenUrl,
Warning,
WindowClose,
WindowMaximize,
WindowMinimize,
@@ -78,8 +75,6 @@ impl IconName {
Self::ArrowLeft => "icons/arrow-left.svg",
Self::ArrowRight => "icons/arrow-right.svg",
Self::ArrowUp => "icons/arrow-up.svg",
Self::ArrowUpCircle => "icons/arrow-up-circle.svg",
Self::Bell => "icons/bell.svg",
Self::CaretRight => "icons/caret-right.svg",
Self::CaretUp => "icons/caret-up.svg",
Self::CaretDown => "icons/caret-down.svg",
@@ -92,7 +87,6 @@ impl IconName {
Self::CloseCircleFill => "icons/close-circle-fill.svg",
Self::Copy => "icons/copy.svg",
Self::Edit => "icons/edit.svg",
Self::EditFill => "icons/edit-fill.svg",
Self::Ellipsis => "icons/ellipsis.svg",
Self::Eye => "icons/eye.svg",
Self::EmojiFill => "icons/emoji-fill.svg",
@@ -116,18 +110,18 @@ impl IconName {
Self::Reply => "icons/reply.svg",
Self::Report => "icons/report.svg",
Self::Refresh => "icons/refresh.svg",
Self::Forward => "icons/forward.svg",
Self::Signal => "icons/signal.svg",
Self::Search => "icons/search.svg",
Self::SearchFill => "icons/search-fill.svg",
Self::Settings => "icons/settings.svg",
Self::Server => "icons/server.svg",
Self::SortAscending => "icons/sort-ascending.svg",
Self::SortDescending => "icons/sort-descending.svg",
Self::Sun => "icons/sun.svg",
Self::ThumbsDown => "icons/thumbs-down.svg",
Self::ThumbsUp => "icons/thumbs-up.svg",
Self::Upload => "icons/upload.svg",
Self::UsersThreeFill => "icons/users-three-fill.svg",
Self::OpenUrl => "icons/open-url.svg",
Self::Warning => "icons/warning.svg",
Self::WindowClose => "icons/window-close.svg",
Self::WindowMaximize => "icons/window-maximize.svg",
Self::WindowMinimize => "icons/window-minimize.svg",
+11 -13
View File
@@ -20,7 +20,7 @@ impl Indicator {
pub fn new() -> Self {
Self {
size: Size::Small,
speed: Duration::from_secs_f64(0.8),
speed: Duration::from_secs(1),
icon: Icon::new(IconName::Loader),
color: None,
}
@@ -52,17 +52,15 @@ impl Sizable for Indicator {
impl RenderOnce for Indicator {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.child(
self.icon
.with_size(self.size)
.when_some(self.color, |this, color| this.text_color(color))
.with_animation(
"circle",
Animation::new(self.speed).repeat().with_easing(ease_in_out),
|this, delta| this.transform(Transformation::rotate(percentage(delta))),
),
)
.into_element()
div().child(
self.icon
.with_size(self.size)
.when_some(self.color, |this, color| this.text_color(color))
.with_animation(
"circle",
Animation::new(self.speed).repeat().with_easing(ease_in_out),
|this, delta| this.transform(Transformation::rotate(percentage(delta))),
),
)
}
}
+5 -6
View File
@@ -15,7 +15,6 @@ type Suffix = Option<Box<dyn Fn(&mut Window, &mut App) -> AnyElement + 'static>>
#[derive(IntoElement)]
pub struct ListItem {
id: ElementId,
base: Stateful<Div>,
disabled: bool,
selected: bool,
@@ -30,8 +29,8 @@ pub struct ListItem {
impl ListItem {
pub fn new(id: impl Into<ElementId>) -> Self {
let id: ElementId = id.into();
Self {
id: id.clone(),
base: h_flex().id(id).gap_x_1().py_1().px_2().text_base(),
disabled: false,
selected: false,
@@ -104,14 +103,14 @@ impl Disableable for ListItem {
}
impl Selectable for ListItem {
fn element_id(&self) -> &ElementId {
&self.id
}
fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
fn is_selected(&self) -> bool {
self.selected
}
}
impl Styled for ListItem {
+5 -9
View File
@@ -30,14 +30,10 @@ pub enum NotificationType {
impl NotificationType {
fn icon(&self, cx: &App) -> Icon {
match self {
Self::Info => Icon::new(IconName::Info).text_color(cx.theme().element_active),
Self::Warning => Icon::new(IconName::Report).text_color(cx.theme().warning_foreground),
Self::Success => {
Icon::new(IconName::CheckCircle).text_color(cx.theme().element_foreground)
}
Self::Error => {
Icon::new(IconName::CloseCircle).text_color(cx.theme().danger_foreground)
}
Self::Info => Icon::new(IconName::Info).text_color(cx.theme().element_foreground),
Self::Success => Icon::new(IconName::Info).text_color(cx.theme().secondary_foreground),
Self::Warning => Icon::new(IconName::Warning).text_color(cx.theme().warning_foreground),
Self::Error => Icon::new(IconName::Warning).text_color(cx.theme().danger_foreground),
}
}
}
@@ -299,7 +295,7 @@ impl Render for Notification {
.border_1()
.border_color(cx.theme().border)
.bg(cx.theme().surface_background)
.rounded(cx.theme().radius)
.rounded(cx.theme().radius * 1.6)
.shadow_md()
.p_2()
.gap_3()
+3 -3
View File
@@ -44,7 +44,7 @@ pub fn init(cx: &mut App) {
]);
}
pub trait PopupMenuExt: Styled + Selectable + IntoElement + 'static {
pub trait PopupMenuExt: Styled + Selectable + InteractiveElement + IntoElement + 'static {
/// Create a popup menu with the given items, anchored to the TopLeft corner
fn popup_menu(
self,
@@ -60,9 +60,9 @@ pub trait PopupMenuExt: Styled + Selectable + IntoElement + 'static {
f: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
) -> Popover<PopupMenu> {
let style = self.style().clone();
let element_id = self.element_id();
let id = self.interactivity().element_id.clone();
Popover::new(SharedString::from(format!("popup-menu:{element_id:?}")))
Popover::new(SharedString::from(format!("popup-menu:{id:?}")))
.no_style()
.trigger(self)
.trigger_style(style)
+2 -1
View File
@@ -405,13 +405,14 @@ impl Render for ResizablePanel {
return div();
}
let view = cx.entity().clone();
let total_size = self
.group
.as_ref()
.and_then(|group| group.upgrade())
.map(|group| group.read(cx).total_size());
let view = cx.entity();
div()
.flex()
.flex_grow()
+1 -1
View File
@@ -56,7 +56,7 @@ impl RenderOnce for Skeleton {
.bg(color)
.with_animation(
"skeleton",
Animation::new(Duration::from_secs(2))
Animation::new(Duration::from_secs(3))
.repeat()
.with_easing(bounce(ease_in_out)),
move |this, delta| {
+9 -4
View File
@@ -1,8 +1,6 @@
use std::fmt::{self, Display, Formatter};
use gpui::{
div, px, App, Axis, Div, Element, ElementId, Pixels, Refineable, StyleRefinement, Styled,
};
use gpui::{div, px, App, Axis, Div, Element, Pixels, Refineable, StyleRefinement, Styled};
use serde::{Deserialize, Serialize};
use theme::ActiveTheme;
@@ -105,9 +103,16 @@ impl From<Pixels> for Size {
/// A trait for defining element that can be selected.
pub trait Selectable: Sized {
fn element_id(&self) -> &ElementId;
/// Set the selected state of the element.
fn selected(self, selected: bool) -> Self;
/// Returns true if the element is selected.
fn is_selected(&self) -> bool;
/// Set is the element mouse right clicked, default do nothing.
fn secondary_selected(self, _: bool) -> Self {
self
}
}
/// A trait for defining element that can be disabled.
+4 -6
View File
@@ -11,7 +11,6 @@ pub mod tab_bar;
#[derive(IntoElement)]
pub struct Tab {
id: ElementId,
base: Stateful<Div>,
label: AnyElement,
prefix: Option<AnyElement>,
@@ -25,7 +24,6 @@ impl Tab {
let id: ElementId = id.into();
Self {
id: id.clone(),
base: div().id(id),
label: label.into_any_element(),
disabled: false,
@@ -55,14 +53,14 @@ impl Tab {
}
impl Selectable for Tab {
fn element_id(&self) -> &ElementId {
&self.id
}
fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
fn is_selected(&self) -> bool {
self.selected
}
}
impl InteractiveElement for Tab {
+47 -15
View File
@@ -45,6 +45,14 @@ common:
en: "Ignore"
relay:
en: "Relay"
relay_invalid:
en: "Relay URL is not valid."
recommended:
en: "Recommended:"
resend:
en: "Resend"
seen_on:
en: "Seen on"
auto_update:
updating:
@@ -54,9 +62,11 @@ auto_update:
user:
dark_mode:
en: "Dark Mode"
en: "Dark mode"
settings:
en: "Settings"
reload_metadata:
en: "Reload metadata"
sign_out:
en: "Sign out"
@@ -96,9 +106,9 @@ auth:
label:
en: "Authentication Required"
message:
en: "Approve the authentication request to allow Coop to continue getting your messages."
en: "Approve the authentication request to allow Coop to continue sending or receiving events."
requests:
en: "You have %{u} total pending authentication requests"
en: "You have %{u} pending authentication requests"
startup:
client_keys_warning:
@@ -167,20 +177,22 @@ login:
en: "Logging in..."
relays:
button_label:
button:
en: "Configure the Messaging Relays to receive messages"
modal_title:
modal:
en: "Set Up Messaging Relays"
description:
en: "In order to receive messages from others, you need to set up at least one Messaging Relay."
add_some_relays:
help_text:
en: "Please add some relays."
invalid:
en: "Relay URL is not valid."
empty:
en: "You need to add at least 1 relay to receive messages."
recommended:
en: "Recommended:"
en: "You need to add at least 1 relay to receive messages from others."
manage_relays:
modal:
en: "Messaging Relay Status"
time:
en: "Last activity: %{t}"
subject:
title:
@@ -213,6 +225,14 @@ screening:
en: "This person is one of your contacts."
not_contact:
en: "This person is not one of your contacts."
active_label:
en: "Activity on Public Relays"
active_tooltip:
en: "This may be inaccurate if the user only publishes to their private relays."
no_active:
en: "This person hasn't had any activity."
active_at:
en: "Last active: %{d}."
mutual_label:
en: "Mutual contacts"
mutual:
@@ -353,9 +373,13 @@ chat:
en: "%{u} has not set up Messaging Relays, so they won't receive your message."
sidebar:
find_or_start_conversation:
reload_menu:
en: "Reload"
status_menu:
en: "Relay Status"
search_label:
en: "Find or start a conversation"
press_enter_to_search:
search_tooltip:
en: "Press Enter to search"
empty:
en: "There are no users matching query %{query}"
@@ -377,9 +401,17 @@ sidebar:
en: "Incoming new conversations"
trusted_contacts_tooltip:
en: "Only show rooms from trusted contacts"
no_requests:
en: "No message requests"
no_requests_label:
en: "New message requests from people you don't know will appear here."
no_conversations:
en: "No conversations"
no_conversations_label:
en: "Start a conversation with someone to get started."
loading:
label:
en: "Downloading messages"
en: "Getting messages. This may take a while..."
tooltip:
en: "This may take a while. Please be patient."
en: "The progress runs in the background. It doesn't affect your experience."