feat: improve relay management

This commit is contained in:
reya
2024-08-06 16:00:21 +07:00
parent 7c04f9cf2b
commit a3703bc348
10 changed files with 87 additions and 48 deletions

View File

@@ -1,4 +1,4 @@
wss://purplepag.es/, wss://purplepag.es/,
wss://directory.yabu.me/, wss://directory.yabu.me/,
wss://user.kindpag.es/, wss://user.kindpag.es/,
wss://relay.nos.social/, wss://bostr.online/,

View File

@@ -29,10 +29,13 @@ pub fn get_accounts() -> Vec<String> {
#[specta::specta] #[specta::specta]
pub async fn get_metadata(user_id: String, state: State<'_, Nostr>) -> Result<String, String> { pub async fn get_metadata(user_id: String, state: State<'_, Nostr>) -> Result<String, String> {
let client = &state.client; let client = &state.client;
let bootstrap_relays = state.bootstrap_relays.lock().await.clone();
let public_key = PublicKey::parse(&user_id).map_err(|e| e.to_string())?; let public_key = PublicKey::parse(&user_id).map_err(|e| e.to_string())?;
let filter = Filter::new().author(public_key).kind(Kind::Metadata).limit(1); let filter = Filter::new().author(public_key).kind(Kind::Metadata).limit(1);
match client.get_events_of(vec![filter], Some(Duration::from_secs(2))).await { match client.get_events_from(bootstrap_relays, vec![filter], Some(Duration::from_secs(2))).await
{
Ok(events) => { Ok(events) => {
if let Some(event) = events.first() { if let Some(event) = events.first() {
Ok(Metadata::from_json(&event.content).unwrap_or(Metadata::new()).as_json()) Ok(Metadata::from_json(&event.content).unwrap_or(Metadata::new()).as_json())
@@ -197,7 +200,7 @@ pub async fn login(
let inbox = Filter::new().kind(Kind::Custom(10050)).author(public_key).limit(1); let inbox = Filter::new().kind(Kind::Custom(10050)).author(public_key).limit(1);
if let Ok(events) = client.get_events_of(vec![inbox], Some(Duration::from_secs(5))).await { if let Ok(events) = client.get_events_of(vec![inbox], Some(Duration::from_secs(3))).await {
if let Some(event) = events.into_iter().next() { if let Some(event) = events.into_iter().next() {
let urls = event let urls = event
.tags() .tags()
@@ -212,8 +215,9 @@ pub async fn login(
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for url in urls.iter() { for url in urls.iter() {
let _ = client.add_relay(url).await; if let Err(e) = client.add_relay(url).await {
let _ = client.connect_relay(url).await; println!("Connect relay failed: {}", e)
}
} }
// Workaround for https://github.com/rust-nostr/nostr/issues/509 // Workaround for https://github.com/rust-nostr/nostr/issues/509

View File

@@ -8,13 +8,14 @@ use tauri::{Manager, State};
use crate::Nostr; use crate::Nostr;
async fn get_nip65_list(public_key: PublicKey, client: &Client) -> Vec<String> { async fn connect_nip65_relays(public_key: PublicKey, client: &Client) -> Vec<String> {
let filter = Filter::new().author(public_key).kind(Kind::RelayList).limit(1); let filter = Filter::new().author(public_key).kind(Kind::RelayList).limit(1);
let mut relay_list: Vec<String> = Vec::new(); let mut relay_list: Vec<String> = Vec::new();
if let Ok(events) = client.get_events_of(vec![filter], Some(Duration::from_secs(10))).await { if let Ok(events) = client.get_events_of(vec![filter], Some(Duration::from_secs(2))).await {
if let Some(event) = events.first() { if let Some(event) = events.first() {
for (url, ..) in nip65::extract_relay_list(event) { for (url, ..) in nip65::extract_relay_list(event) {
let _ = client.add_relay(url).await;
relay_list.push(url.to_string()) relay_list.push(url.to_string())
} }
} }
@@ -23,6 +24,14 @@ async fn get_nip65_list(public_key: PublicKey, client: &Client) -> Vec<String> {
relay_list relay_list
} }
async fn disconnect_nip65_relays(relays: Vec<String>, client: &Client) {
for relay in relays.iter() {
if let Err(e) = client.disconnect_relay(relay).await {
println!("Disconnect failed: {}", e)
}
}
}
#[tauri::command] #[tauri::command]
#[specta::specta] #[specta::specta]
pub fn get_bootstrap_relays(app: tauri::AppHandle) -> Result<Vec<String>, String> { pub fn get_bootstrap_relays(app: tauri::AppHandle) -> Result<Vec<String>, String> {
@@ -51,7 +60,7 @@ pub fn set_bootstrap_relays(relays: String, app: tauri::AppHandle) -> Result<(),
#[tauri::command] #[tauri::command]
#[specta::specta] #[specta::specta]
pub async fn get_inbox_relays( pub async fn collect_inbox_relays(
user_id: String, user_id: String,
state: State<'_, Nostr>, state: State<'_, Nostr>,
) -> Result<Vec<String>, String> { ) -> Result<Vec<String>, String> {
@@ -106,12 +115,16 @@ pub async fn connect_inbox_relays(
) -> Result<Vec<String>, String> { ) -> Result<Vec<String>, String> {
let client = &state.client; let client = &state.client;
let public_key = PublicKey::parse(&user_id).map_err(|e| e.to_string())?; let public_key = PublicKey::parse(&user_id).map_err(|e| e.to_string())?;
// let nip65_relays = connect_nip65_relays(public_key, client).await;
let mut inbox_relays = state.inbox_relays.lock().await; let mut inbox_relays = state.inbox_relays.lock().await;
if !ignore_cache { if !ignore_cache {
if let Some(relays) = inbox_relays.get(&public_key) { if let Some(relays) = inbox_relays.get(&public_key) {
for relay in relays { for relay in relays {
let _ = client.connect_relay(relay).await; if let Err(e) = client.connect_relay(relay).await {
println!("Connect relay failed: {}", e)
}
} }
return Ok(relays.to_owned()); return Ok(relays.to_owned());
}; };
@@ -127,33 +140,22 @@ pub async fn connect_inbox_relays(
for tag in &event.tags { for tag in &event.tags {
if let Some(TagStandard::Relay(relay)) = tag.as_standardized() { if let Some(TagStandard::Relay(relay)) = tag.as_standardized() {
let url = relay.to_string(); let url = relay.to_string();
let _ = client.add_relay(&url).await;
let _ = client.connect_relay(&url).await; if let Err(e) = client.add_relay(&url).await {
println!("Connect relay failed: {}", e)
};
relays.push(url) relays.push(url)
} }
} }
// Update state
inbox_relays.insert(public_key, relays.clone()); inbox_relays.insert(public_key, relays.clone());
// Disconnect user's nip65 relays to save bandwidth
// disconnect_nip65_relays(nip65_relays, client).await;
} }
// Workaround for https://github.com/rust-nostr/nostr/issues/509
// TODO: remove this
// let relays_clone = relays.clone();
/*tauri::async_runtime::spawn(async move {
let state = handle.state::<Nostr>();
let client = &state.client;
client
.get_events_from(
relays_clone,
vec![Filter::new().kind(Kind::TextNote).limit(0)],
Some(Duration::from_secs(5)),
)
.await
});
*/
Ok(relays) Ok(relays)
} }
Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()),

View File

@@ -24,6 +24,7 @@ mod commands;
pub struct Nostr { pub struct Nostr {
client: Client, client: Client,
bootstrap_relays: Mutex<Vec<String>>,
inbox_relays: Mutex<HashMap<PublicKey, Vec<String>>>, inbox_relays: Mutex<HashMap<PublicKey, Vec<String>>>,
} }
@@ -34,7 +35,7 @@ fn main() {
let builder = Builder::<tauri::Wry>::new().commands(collect_commands![ let builder = Builder::<tauri::Wry>::new().commands(collect_commands![
get_bootstrap_relays, get_bootstrap_relays,
set_bootstrap_relays, set_bootstrap_relays,
get_inbox_relays, collect_inbox_relays,
set_inbox_relays, set_inbox_relays,
connect_inbox_relays, connect_inbox_relays,
disconnect_inbox_relays, disconnect_inbox_relays,
@@ -97,7 +98,7 @@ fn main() {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
main_window.add_border(None); main_window.add_border(None);
let client = tauri::async_runtime::block_on(async move { let (client, bootstrap_relays) = tauri::async_runtime::block_on(async move {
// Create data folder if not exist // Create data folder if not exist
let dir = handle.path().app_config_dir().expect("App config directory not found."); let dir = handle.path().app_config_dir().expect("App config directory not found.");
let _ = fs::create_dir_all(dir.clone()); let _ = fs::create_dir_all(dir.clone());
@@ -107,6 +108,7 @@ fn main() {
// Setup nostr client // Setup nostr client
let opts = Options::new() let opts = Options::new()
.autoconnect(true)
.timeout(Duration::from_secs(40)) .timeout(Duration::from_secs(40))
.send_timeout(Some(Duration::from_secs(10))) .send_timeout(Some(Duration::from_secs(10)))
.connection_timeout(Some(Duration::from_secs(10))); .connection_timeout(Some(Duration::from_secs(10)));
@@ -143,14 +145,22 @@ fn main() {
} }
} }
// Connect let bootstrap_relays = client
client.connect().await; .relays()
.await
.keys()
.map(|item| item.to_string())
.collect::<Vec<String>>();
client (client, bootstrap_relays)
}); });
// Create global state // Create global state
app.manage(Nostr { client, inbox_relays: Mutex::new(HashMap::new()) }); app.manage(Nostr {
client,
bootstrap_relays: Mutex::new(bootstrap_relays),
inbox_relays: Mutex::new(HashMap::new()),
});
Ok(()) Ok(())
}) })

View File

@@ -21,9 +21,9 @@ async setBootstrapRelays(relays: string) : Promise<Result<null, string>> {
else return { status: "error", error: e as any }; else return { status: "error", error: e as any };
} }
}, },
async getInboxRelays(userId: string) : Promise<Result<string[], string>> { async collectInboxRelays(userId: string) : Promise<Result<string[], string>> {
try { try {
return { status: "ok", data: await TAURI_INVOKE("get_inbox_relays", { userId }) }; return { status: "ok", data: await TAURI_INVOKE("collect_inbox_relays", { userId }) };
} catch (e) { } catch (e) {
if(e instanceof Error) throw e; if(e instanceof Error) throw e;
else return { status: "error", error: e as any }; else return { status: "error", error: e as any };

View File

@@ -1,19 +1,29 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { RouterProvider, createRouter } from "@tanstack/react-router"; import { RouterProvider, createRouter } from "@tanstack/react-router";
import { type } from "@tauri-apps/plugin-os"; import { type } from "@tauri-apps/plugin-os";
import { LRUCache } from "lru-cache";
import { StrictMode } from "react"; import { StrictMode } from "react";
import ReactDOM from "react-dom/client"; import ReactDOM from "react-dom/client";
import "./global.css"; import "./global.css";
import { commands } from "./commands";
// Import the generated route tree // Import the generated route tree
import { routeTree } from "./routes.gen"; import { routeTree } from "./routes.gen";
const queryClient = new QueryClient();
const platform = type(); const platform = type();
const queryClient = new QueryClient();
const chatManager = new LRUCache<string, string>({
max: 3,
dispose: async (v, _) => {
console.log("disconnect: ", v);
await commands.disconnectInboxRelays(v);
},
});
const router = createRouter({ const router = createRouter({
routeTree, routeTree,
context: { context: {
queryClient, queryClient,
chatManager,
platform, platform,
}, },
}); });

View File

@@ -33,17 +33,8 @@ type EventPayload = {
export const Route = createLazyFileRoute("/$account/chats/$id")({ export const Route = createLazyFileRoute("/$account/chats/$id")({
component: Screen, component: Screen,
pendingComponent: Pending,
}); });
function Pending() {
return (
<div className="size-full flex items-center justify-center">
<Spinner />
</div>
);
}
function Screen() { function Screen() {
return ( return (
<div className="size-full flex flex-col"> <div className="size-full flex flex-col">

View File

@@ -1,14 +1,34 @@
import { commands } from "@/commands"; import { commands } from "@/commands";
import { Spinner } from "@/components/spinner";
import { createFileRoute } from "@tanstack/react-router"; import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/$account/chats/$id")({ export const Route = createFileRoute("/$account/chats/$id")({
loader: async ({ params }) => { loader: async ({ params, context }) => {
const res = await commands.connectInboxRelays(params.id, false); const res = await commands.connectInboxRelays(params.id, false);
if (res.status === "ok") { if (res.status === "ok") {
// Add id to chat manager to unsubscribe later.
context.chatManager.set(params.id, params.id);
return res.data; return res.data;
} else { } else {
return []; return [];
} }
}, },
pendingComponent: Pending,
pendingMs: 200,
pendingMinMs: 100,
}); });
function Pending() {
return (
<div className="size-full flex items-center justify-center">
<div className="flex flex-col gap-2 items-center justify-center">
<Spinner />
<span className="text-xs text-center text-neutral-600 dark:text-neutral-400">
Connection in progress. Please wait ...
</span>
</div>
</div>
);
}

View File

@@ -3,7 +3,7 @@ import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/$account/relays")({ export const Route = createFileRoute("/$account/relays")({
loader: async ({ params }) => { loader: async ({ params }) => {
const res = await commands.getInboxRelays(params.account); const res = await commands.collectInboxRelays(params.account);
if (res.status === "ok") { if (res.status === "ok") {
return res.data; return res.data;

View File

@@ -2,9 +2,11 @@ import { cn } from "@/commons";
import type { QueryClient } from "@tanstack/react-query"; import type { QueryClient } from "@tanstack/react-query";
import { Outlet, createRootRouteWithContext } from "@tanstack/react-router"; import { Outlet, createRootRouteWithContext } from "@tanstack/react-router";
import type { OsType } from "@tauri-apps/plugin-os"; import type { OsType } from "@tauri-apps/plugin-os";
import type { LRUCache } from "lru-cache";
interface RouterContext { interface RouterContext {
queryClient: QueryClient; queryClient: QueryClient;
chatManager: LRUCache<string, string, unknown>;
platform: OsType; platform: OsType;
} }