feat: improve relay management
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
wss://purplepag.es/,
|
||||
wss://directory.yabu.me/,
|
||||
wss://user.kindpag.es/,
|
||||
wss://relay.nos.social/,
|
||||
wss://bostr.online/,
|
||||
|
||||
@@ -29,10 +29,13 @@ pub fn get_accounts() -> Vec<String> {
|
||||
#[specta::specta]
|
||||
pub async fn get_metadata(user_id: String, state: State<'_, Nostr>) -> Result<String, String> {
|
||||
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 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) => {
|
||||
if let Some(event) = events.first() {
|
||||
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);
|
||||
|
||||
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() {
|
||||
let urls = event
|
||||
.tags()
|
||||
@@ -212,8 +215,9 @@ pub async fn login(
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for url in urls.iter() {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/rust-nostr/nostr/issues/509
|
||||
|
||||
@@ -8,13 +8,14 @@ use tauri::{Manager, State};
|
||||
|
||||
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 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() {
|
||||
for (url, ..) in nip65::extract_relay_list(event) {
|
||||
let _ = client.add_relay(url).await;
|
||||
relay_list.push(url.to_string())
|
||||
}
|
||||
}
|
||||
@@ -23,6 +24,14 @@ async fn get_nip65_list(public_key: PublicKey, client: &Client) -> Vec<String> {
|
||||
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]
|
||||
#[specta::specta]
|
||||
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]
|
||||
#[specta::specta]
|
||||
pub async fn get_inbox_relays(
|
||||
pub async fn collect_inbox_relays(
|
||||
user_id: String,
|
||||
state: State<'_, Nostr>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
@@ -106,12 +115,16 @@ pub async fn connect_inbox_relays(
|
||||
) -> Result<Vec<String>, String> {
|
||||
let client = &state.client;
|
||||
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;
|
||||
|
||||
if !ignore_cache {
|
||||
if let Some(relays) = inbox_relays.get(&public_key) {
|
||||
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());
|
||||
};
|
||||
@@ -127,33 +140,22 @@ pub async fn connect_inbox_relays(
|
||||
for tag in &event.tags {
|
||||
if let Some(TagStandard::Relay(relay)) = tag.as_standardized() {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// Update state
|
||||
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)
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
|
||||
@@ -24,6 +24,7 @@ mod commands;
|
||||
|
||||
pub struct Nostr {
|
||||
client: Client,
|
||||
bootstrap_relays: Mutex<Vec<String>>,
|
||||
inbox_relays: Mutex<HashMap<PublicKey, Vec<String>>>,
|
||||
}
|
||||
|
||||
@@ -34,7 +35,7 @@ fn main() {
|
||||
let builder = Builder::<tauri::Wry>::new().commands(collect_commands![
|
||||
get_bootstrap_relays,
|
||||
set_bootstrap_relays,
|
||||
get_inbox_relays,
|
||||
collect_inbox_relays,
|
||||
set_inbox_relays,
|
||||
connect_inbox_relays,
|
||||
disconnect_inbox_relays,
|
||||
@@ -97,7 +98,7 @@ fn main() {
|
||||
#[cfg(target_os = "macos")]
|
||||
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
|
||||
let dir = handle.path().app_config_dir().expect("App config directory not found.");
|
||||
let _ = fs::create_dir_all(dir.clone());
|
||||
@@ -107,6 +108,7 @@ fn main() {
|
||||
|
||||
// Setup nostr client
|
||||
let opts = Options::new()
|
||||
.autoconnect(true)
|
||||
.timeout(Duration::from_secs(40))
|
||||
.send_timeout(Some(Duration::from_secs(10)))
|
||||
.connection_timeout(Some(Duration::from_secs(10)));
|
||||
@@ -143,14 +145,22 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
// Connect
|
||||
client.connect().await;
|
||||
let bootstrap_relays = client
|
||||
.relays()
|
||||
.await
|
||||
.keys()
|
||||
.map(|item| item.to_string())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
client
|
||||
(client, bootstrap_relays)
|
||||
});
|
||||
|
||||
// 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(())
|
||||
})
|
||||
|
||||
@@ -21,9 +21,9 @@ async setBootstrapRelays(relays: string) : Promise<Result<null, string>> {
|
||||
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 {
|
||||
return { status: "ok", data: await TAURI_INVOKE("get_inbox_relays", { userId }) };
|
||||
return { status: "ok", data: await TAURI_INVOKE("collect_inbox_relays", { userId }) };
|
||||
} catch (e) {
|
||||
if(e instanceof Error) throw e;
|
||||
else return { status: "error", error: e as any };
|
||||
|
||||
12
src/main.tsx
12
src/main.tsx
@@ -1,19 +1,29 @@
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { RouterProvider, createRouter } from "@tanstack/react-router";
|
||||
import { type } from "@tauri-apps/plugin-os";
|
||||
import { LRUCache } from "lru-cache";
|
||||
import { StrictMode } from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import "./global.css";
|
||||
import { commands } from "./commands";
|
||||
// Import the generated route tree
|
||||
import { routeTree } from "./routes.gen";
|
||||
|
||||
const queryClient = new QueryClient();
|
||||
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({
|
||||
routeTree,
|
||||
context: {
|
||||
queryClient,
|
||||
chatManager,
|
||||
platform,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -33,17 +33,8 @@ type EventPayload = {
|
||||
|
||||
export const Route = createLazyFileRoute("/$account/chats/$id")({
|
||||
component: Screen,
|
||||
pendingComponent: Pending,
|
||||
});
|
||||
|
||||
function Pending() {
|
||||
return (
|
||||
<div className="size-full flex items-center justify-center">
|
||||
<Spinner />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Screen() {
|
||||
return (
|
||||
<div className="size-full flex flex-col">
|
||||
|
||||
@@ -1,14 +1,34 @@
|
||||
import { commands } from "@/commands";
|
||||
import { Spinner } from "@/components/spinner";
|
||||
import { createFileRoute } from "@tanstack/react-router";
|
||||
|
||||
export const Route = createFileRoute("/$account/chats/$id")({
|
||||
loader: async ({ params }) => {
|
||||
loader: async ({ params, context }) => {
|
||||
const res = await commands.connectInboxRelays(params.id, false);
|
||||
|
||||
if (res.status === "ok") {
|
||||
// Add id to chat manager to unsubscribe later.
|
||||
context.chatManager.set(params.id, params.id);
|
||||
|
||||
return res.data;
|
||||
} else {
|
||||
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>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createFileRoute } from "@tanstack/react-router";
|
||||
|
||||
export const Route = createFileRoute("/$account/relays")({
|
||||
loader: async ({ params }) => {
|
||||
const res = await commands.getInboxRelays(params.account);
|
||||
const res = await commands.collectInboxRelays(params.account);
|
||||
|
||||
if (res.status === "ok") {
|
||||
return res.data;
|
||||
|
||||
@@ -2,9 +2,11 @@ import { cn } from "@/commons";
|
||||
import type { QueryClient } from "@tanstack/react-query";
|
||||
import { Outlet, createRootRouteWithContext } from "@tanstack/react-router";
|
||||
import type { OsType } from "@tauri-apps/plugin-os";
|
||||
import type { LRUCache } from "lru-cache";
|
||||
|
||||
interface RouterContext {
|
||||
queryClient: QueryClient;
|
||||
chatManager: LRUCache<string, string, unknown>;
|
||||
platform: OsType;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user