update relay list panel
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 2m0s
Rust / build (ubuntu-latest, stable) (pull_request) Failing after 2m23s
Rust / build (macos-latest, stable) (push) Has been cancelled
Rust / build (windows-latest, stable) (push) Has been cancelled
Rust / build (macos-latest, stable) (pull_request) Has been cancelled
Rust / build (windows-latest, stable) (pull_request) Has been cancelled
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 2m0s
Rust / build (ubuntu-latest, stable) (pull_request) Failing after 2m23s
Rust / build (macos-latest, stable) (push) Has been cancelled
Rust / build (windows-latest, stable) (push) Has been cancelled
Rust / build (macos-latest, stable) (pull_request) Has been cancelled
Rust / build (windows-latest, stable) (pull_request) Has been cancelled
This commit is contained in:
@@ -4,23 +4,36 @@ use std::time::Duration;
|
|||||||
use anyhow::{anyhow, Context as AnyhowContext, Error};
|
use anyhow::{anyhow, Context as AnyhowContext, Error};
|
||||||
use gpui::prelude::FluentBuilder;
|
use gpui::prelude::FluentBuilder;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
div, relative, uniform_list, AnyElement, App, AppContext, Context, Entity, EventEmitter,
|
div, px, rems, Action, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle,
|
||||||
FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render, SharedString,
|
Focusable, InteractiveElement, IntoElement, ParentElement, Render, SharedString, Styled,
|
||||||
Styled, Subscription, Task, TextAlign, UniformList, Window,
|
Subscription, Task, TextAlign, Window,
|
||||||
};
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
|
use serde::Deserialize;
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
use state::{NostrRegistry, BOOTSTRAP_RELAYS};
|
use state::NostrRegistry;
|
||||||
use theme::ActiveTheme;
|
use theme::ActiveTheme;
|
||||||
use ui::button::{Button, ButtonVariants};
|
use ui::button::{Button, ButtonVariants};
|
||||||
use ui::dock_area::panel::{Panel, PanelEvent};
|
use ui::dock_area::panel::{Panel, PanelEvent};
|
||||||
use ui::input::{InputEvent, InputState, TextInput};
|
use ui::input::{InputEvent, InputState, TextInput};
|
||||||
use ui::{divider, h_flex, v_flex, IconName, Sizable, StyledExt};
|
use ui::menu::DropdownMenu;
|
||||||
|
use ui::{divider, h_flex, v_flex, Disableable, IconName, Sizable, StyledExt, WindowExtension};
|
||||||
|
|
||||||
|
const MSG: &str = "Relay List (or Gossip Relays) are a set of relays \
|
||||||
|
where you will publish all your events. Others also publish events \
|
||||||
|
related to you here.";
|
||||||
|
|
||||||
pub fn init(window: &mut Window, cx: &mut App) -> Entity<RelayListPanel> {
|
pub fn init(window: &mut Window, cx: &mut App) -> Entity<RelayListPanel> {
|
||||||
cx.new(|cx| RelayListPanel::new(window, cx))
|
cx.new(|cx| RelayListPanel::new(window, cx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Action, Clone, PartialEq, Eq, Deserialize)]
|
||||||
|
#[action(namespace = relay, no_json)]
|
||||||
|
enum SetMetadata {
|
||||||
|
Read,
|
||||||
|
Write,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RelayListPanel {
|
pub struct RelayListPanel {
|
||||||
name: SharedString,
|
name: SharedString,
|
||||||
@@ -29,6 +42,9 @@ pub struct RelayListPanel {
|
|||||||
/// Relay URL input
|
/// Relay URL input
|
||||||
input: Entity<InputState>,
|
input: Entity<InputState>,
|
||||||
|
|
||||||
|
/// Whether the panel is updating
|
||||||
|
updating: bool,
|
||||||
|
|
||||||
/// Relay metadata input
|
/// Relay metadata input
|
||||||
metadata: Entity<Option<RelayMetadata>>,
|
metadata: Entity<Option<RelayMetadata>>,
|
||||||
|
|
||||||
@@ -42,7 +58,7 @@ pub struct RelayListPanel {
|
|||||||
_subscriptions: SmallVec<[Subscription; 1]>,
|
_subscriptions: SmallVec<[Subscription; 1]>,
|
||||||
|
|
||||||
// Background tasks
|
// Background tasks
|
||||||
_tasks: SmallVec<[Task<()>; 1]>,
|
tasks: Vec<Task<Result<(), Error>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RelayListPanel {
|
impl RelayListPanel {
|
||||||
@@ -50,28 +66,7 @@ impl RelayListPanel {
|
|||||||
let input = cx.new(|cx| InputState::new(window, cx).placeholder("wss://example.com"));
|
let input = cx.new(|cx| InputState::new(window, cx).placeholder("wss://example.com"));
|
||||||
let metadata = cx.new(|_| None);
|
let metadata = cx.new(|_| None);
|
||||||
|
|
||||||
let nostr = NostrRegistry::global(cx);
|
|
||||||
let client = nostr.read(cx).client();
|
|
||||||
|
|
||||||
let mut subscriptions = smallvec![];
|
let mut subscriptions = smallvec![];
|
||||||
let mut tasks = smallvec![];
|
|
||||||
|
|
||||||
tasks.push(
|
|
||||||
// Load user's relays in the local database
|
|
||||||
cx.spawn_in(window, async move |this, cx| {
|
|
||||||
let result = cx
|
|
||||||
.background_spawn(async move { Self::load(&client).await })
|
|
||||||
.await;
|
|
||||||
|
|
||||||
if let Ok(relays) = result {
|
|
||||||
this.update(cx, |this, cx| {
|
|
||||||
this.relays.extend(relays);
|
|
||||||
cx.notify();
|
|
||||||
})
|
|
||||||
.ok();
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
subscriptions.push(
|
subscriptions.push(
|
||||||
// Subscribe to user's input events
|
// Subscribe to user's input events
|
||||||
@@ -82,32 +77,57 @@ impl RelayListPanel {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Run at the end of current cycle
|
||||||
|
cx.defer_in(window, |this, window, cx| {
|
||||||
|
this.load(window, cx);
|
||||||
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
name: "Update Relay List".into(),
|
name: "Update Relay List".into(),
|
||||||
focus_handle: cx.focus_handle(),
|
focus_handle: cx.focus_handle(),
|
||||||
input,
|
input,
|
||||||
|
updating: false,
|
||||||
metadata,
|
metadata,
|
||||||
relays: HashSet::new(),
|
relays: HashSet::new(),
|
||||||
error: None,
|
error: None,
|
||||||
_subscriptions: subscriptions,
|
_subscriptions: subscriptions,
|
||||||
_tasks: tasks,
|
tasks: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn load(client: &Client) -> Result<Vec<(RelayUrl, Option<RelayMetadata>)>, Error> {
|
#[allow(clippy::type_complexity)]
|
||||||
let signer = client.signer().context("Signer not found")?;
|
fn load(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
let public_key = signer.get_public_key().await?;
|
let nostr = NostrRegistry::global(cx);
|
||||||
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
let filter = Filter::new()
|
let task: Task<Result<Vec<(RelayUrl, Option<RelayMetadata>)>, Error>> = cx
|
||||||
.kind(Kind::RelayList)
|
.background_spawn(async move {
|
||||||
.author(public_key)
|
let signer = client.signer().context("Signer not found")?;
|
||||||
.limit(1);
|
let public_key = signer.get_public_key().await?;
|
||||||
|
|
||||||
if let Some(event) = client.database().query(filter).await?.first_owned() {
|
let filter = Filter::new()
|
||||||
Ok(nip65::extract_owned_relay_list(event).collect())
|
.kind(Kind::RelayList)
|
||||||
} else {
|
.author(public_key)
|
||||||
Err(anyhow!("Not found."))
|
.limit(1);
|
||||||
}
|
|
||||||
|
if let Some(event) = client.database().query(filter).await?.first_owned() {
|
||||||
|
Ok(nip65::extract_owned_relay_list(event).collect())
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("Not found."))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
|
||||||
|
let relays = task.await?;
|
||||||
|
|
||||||
|
// Update state
|
||||||
|
this.update(cx, |this, cx| {
|
||||||
|
this.relays.extend(relays);
|
||||||
|
cx.notify();
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
fn add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
@@ -120,7 +140,7 @@ impl RelayListPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(url) = RelayUrl::parse(&value) {
|
if let Ok(url) = RelayUrl::parse(&value) {
|
||||||
if !self.relays.insert((url, metadata.to_owned())) {
|
if self.relays.insert((url, metadata.to_owned())) {
|
||||||
self.input.update(cx, |this, cx| {
|
self.input.update(cx, |this, cx| {
|
||||||
this.set_value("", window, cx);
|
this.set_value("", window, cx);
|
||||||
});
|
});
|
||||||
@@ -155,7 +175,29 @@ impl RelayListPanel {
|
|||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_relays(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
fn set_updating(&mut self, updating: bool, cx: &mut Context<Self>) {
|
||||||
|
self.updating = updating;
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_metadata(&mut self, ev: &SetMetadata, _window: &mut Window, cx: &mut Context<Self>) {
|
||||||
|
match ev {
|
||||||
|
SetMetadata::Read => {
|
||||||
|
self.metadata.update(cx, |this, cx| {
|
||||||
|
*this = Some(RelayMetadata::Read);
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
SetMetadata::Write => {
|
||||||
|
self.metadata.update(cx, |this, cx| {
|
||||||
|
*this = Some(RelayMetadata::Write);
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_relays(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
if self.relays.is_empty() {
|
if self.relays.is_empty() {
|
||||||
self.set_error("You need to add at least 1 relay", window, cx);
|
self.set_error("You need to add at least 1 relay", window, cx);
|
||||||
return;
|
return;
|
||||||
@@ -163,109 +205,103 @@ impl RelayListPanel {
|
|||||||
|
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
|
// Get all relays
|
||||||
let relays = self.relays.clone();
|
let relays = self.relays.clone();
|
||||||
|
|
||||||
|
// Set updating state
|
||||||
|
self.set_updating(true, cx);
|
||||||
|
|
||||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||||
let builder = EventBuilder::relay_list(relays);
|
let builder = EventBuilder::relay_list(relays);
|
||||||
let event = client.sign_event_builder(builder).await?;
|
let event = client.sign_event_builder(builder).await?;
|
||||||
|
|
||||||
// Set relay list for current user
|
// Set relay list for current user
|
||||||
client.send_event(&event).to(BOOTSTRAP_RELAYS).await?;
|
client.send_event(&event).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|
||||||
cx.spawn_in(window, async move |this, cx| {
|
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
|
||||||
match task.await {
|
match task.await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// TODO
|
this.update_in(cx, |this, window, cx| {
|
||||||
|
this.set_updating(false, cx);
|
||||||
|
this.load(window, cx);
|
||||||
|
|
||||||
|
window.push_notification("Update successful", cx);
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
this.update_in(cx, |this, window, cx| {
|
this.update_in(cx, |this, window, cx| {
|
||||||
this.set_error(e.to_string(), window, cx);
|
this.set_error(e.to_string(), window, cx);
|
||||||
})
|
})?;
|
||||||
.ok();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})
|
|
||||||
.detach();
|
Ok(())
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_list(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> UniformList {
|
fn render_list_items(&mut self, cx: &mut Context<Self>) -> Vec<impl IntoElement> {
|
||||||
let relays = self.relays.clone();
|
let mut items = Vec::new();
|
||||||
let total = relays.len();
|
|
||||||
|
|
||||||
uniform_list(
|
for (url, metadata) in self.relays.iter() {
|
||||||
"relays",
|
items.push(
|
||||||
total,
|
h_flex()
|
||||||
cx.processor(move |_v, range, _window, cx| {
|
.id(SharedString::from(url.to_string()))
|
||||||
let mut items = Vec::new();
|
.group("")
|
||||||
|
.flex_1()
|
||||||
for ix in range {
|
.w_full()
|
||||||
let Some((url, metadata)) = relays.iter().nth(ix) else {
|
.h_8()
|
||||||
continue;
|
.px_2()
|
||||||
};
|
.justify_between()
|
||||||
|
.rounded(cx.theme().radius)
|
||||||
items.push(
|
.bg(cx.theme().secondary_background)
|
||||||
div()
|
.text_color(cx.theme().secondary_foreground)
|
||||||
.id(SharedString::from(url.to_string()))
|
.child(
|
||||||
.group("")
|
h_flex()
|
||||||
.w_full()
|
.gap_1()
|
||||||
.h_9()
|
.text_sm()
|
||||||
.py_0p5()
|
.child(SharedString::from(url.to_string()))
|
||||||
.child(
|
.child(
|
||||||
h_flex()
|
div()
|
||||||
.px_2()
|
.p_0p5()
|
||||||
.flex()
|
.rounded_xs()
|
||||||
.justify_between()
|
.font_semibold()
|
||||||
.rounded(cx.theme().radius)
|
.text_size(px(8.))
|
||||||
.bg(cx.theme().elevated_surface_background)
|
.text_color(cx.theme().secondary_foreground)
|
||||||
.child(
|
.map(|this| {
|
||||||
div().text_sm().child(SharedString::from(url.to_string())),
|
if let Some(metadata) = metadata {
|
||||||
)
|
this.child(SharedString::from(metadata.to_string()))
|
||||||
.child(
|
} else {
|
||||||
h_flex()
|
this.child("Read and Write")
|
||||||
.gap_1()
|
}
|
||||||
.text_xs()
|
}),
|
||||||
.map(|this| {
|
|
||||||
if let Some(metadata) = metadata {
|
|
||||||
this.child(SharedString::from(
|
|
||||||
metadata.to_string(),
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
this.child(SharedString::from("Read+Write"))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.child(
|
|
||||||
Button::new("remove_{ix}")
|
|
||||||
.icon(IconName::Close)
|
|
||||||
.xsmall()
|
|
||||||
.ghost()
|
|
||||||
.invisible()
|
|
||||||
.group_hover("", |this| this.visible())
|
|
||||||
.on_click({
|
|
||||||
let url = url.to_owned();
|
|
||||||
cx.listener(
|
|
||||||
move |this, _ev, _window, cx| {
|
|
||||||
this.remove(&url, cx);
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
.child(
|
||||||
|
Button::new("remove_{ix}")
|
||||||
|
.icon(IconName::Close)
|
||||||
|
.xsmall()
|
||||||
|
.ghost()
|
||||||
|
.invisible()
|
||||||
|
.group_hover("", |this| this.visible())
|
||||||
|
.on_click({
|
||||||
|
let url = url.to_owned();
|
||||||
|
cx.listener(move |this, _ev, _window, cx| {
|
||||||
|
this.remove(&url, cx);
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
items
|
items
|
||||||
}),
|
|
||||||
)
|
|
||||||
.h_full()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_empty(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
fn render_empty(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||||
h_flex()
|
h_flex()
|
||||||
.mt_2()
|
|
||||||
.h_20()
|
.h_20()
|
||||||
.justify_center()
|
.justify_center()
|
||||||
.border_2()
|
.border_2()
|
||||||
@@ -299,36 +335,67 @@ impl Focusable for RelayListPanel {
|
|||||||
impl Render for RelayListPanel {
|
impl Render for RelayListPanel {
|
||||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||||
v_flex()
|
v_flex()
|
||||||
.size_full()
|
.on_action(cx.listener(Self::set_metadata))
|
||||||
.items_center()
|
.p_3()
|
||||||
.justify_center()
|
.gap_2()
|
||||||
.p_2()
|
.w_full()
|
||||||
.gap_10()
|
|
||||||
.child(
|
.child(
|
||||||
div()
|
div()
|
||||||
.text_center()
|
.text_xs()
|
||||||
.font_semibold()
|
.text_color(cx.theme().text_muted)
|
||||||
.line_height(relative(1.25))
|
.child(SharedString::from(MSG)),
|
||||||
.child(SharedString::from("Update Relay List")),
|
|
||||||
)
|
)
|
||||||
|
.child(divider(cx))
|
||||||
.child(
|
.child(
|
||||||
v_flex()
|
v_flex()
|
||||||
.w_112()
|
|
||||||
.gap_2()
|
.gap_2()
|
||||||
|
.flex_1()
|
||||||
|
.w_full()
|
||||||
.text_sm()
|
.text_sm()
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.text_xs()
|
||||||
|
.font_semibold()
|
||||||
|
.text_color(cx.theme().text_muted)
|
||||||
|
.child(SharedString::from("Relays:")),
|
||||||
|
)
|
||||||
.child(
|
.child(
|
||||||
v_flex()
|
v_flex()
|
||||||
.gap_1p5()
|
.gap_1()
|
||||||
.child(
|
.child(
|
||||||
h_flex()
|
h_flex()
|
||||||
.gap_1()
|
.gap_1()
|
||||||
.w_full()
|
.w_full()
|
||||||
.child(TextInput::new(&self.input).small())
|
.child(
|
||||||
|
TextInput::new(&self.input)
|
||||||
|
.small()
|
||||||
|
.bordered(false)
|
||||||
|
.cleanable(),
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
Button::new("metadata")
|
||||||
|
.map(|this| {
|
||||||
|
if let Some(metadata) = self.metadata.read(cx) {
|
||||||
|
this.label(metadata.to_string())
|
||||||
|
} else {
|
||||||
|
this.label("R & W")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.tooltip("Relay metadata")
|
||||||
|
.ghost()
|
||||||
|
.h(rems(2.))
|
||||||
|
.text_xs()
|
||||||
|
.dropdown_menu(|this, _window, _cx| {
|
||||||
|
this.menu("Read", Box::new(SetMetadata::Read))
|
||||||
|
.menu("Write", Box::new(SetMetadata::Write))
|
||||||
|
}),
|
||||||
|
)
|
||||||
.child(
|
.child(
|
||||||
Button::new("add")
|
Button::new("add")
|
||||||
.icon(IconName::Plus)
|
.icon(IconName::Plus)
|
||||||
.label("Add")
|
.tooltip("Add relay")
|
||||||
.ghost()
|
.ghost()
|
||||||
|
.size(rems(2.))
|
||||||
.on_click(cx.listener(move |this, _, window, cx| {
|
.on_click(cx.listener(move |this, _, window, cx| {
|
||||||
this.add(window, cx);
|
this.add(window, cx);
|
||||||
})),
|
})),
|
||||||
@@ -345,17 +412,25 @@ impl Render for RelayListPanel {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.map(|this| {
|
.map(|this| {
|
||||||
if !self.relays.is_empty() {
|
if self.relays.is_empty() {
|
||||||
this.child(self.render_list(window, cx))
|
|
||||||
} else {
|
|
||||||
this.child(self.render_empty(window, cx))
|
this.child(self.render_empty(window, cx))
|
||||||
|
} else {
|
||||||
|
this.child(
|
||||||
|
v_flex()
|
||||||
|
.gap_1()
|
||||||
|
.flex_1()
|
||||||
|
.w_full()
|
||||||
|
.children(self.render_list_items(cx)),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.child(divider(cx))
|
|
||||||
.child(
|
.child(
|
||||||
Button::new("submit")
|
Button::new("submit")
|
||||||
.label("Update")
|
.label("Update")
|
||||||
.primary()
|
.primary()
|
||||||
|
.small()
|
||||||
|
.loading(self.updating)
|
||||||
|
.disabled(self.updating)
|
||||||
.on_click(cx.listener(move |this, _ev, window, cx| {
|
.on_click(cx.listener(move |this, _ev, window, cx| {
|
||||||
this.set_relays(window, cx);
|
this.set_relays(window, cx);
|
||||||
})),
|
})),
|
||||||
|
|||||||
Reference in New Issue
Block a user