Browse Source

Made localhost relays off by default, with localstorage checkbox to turn it on.

master
silberengel 7 months ago
parent
commit
41a7c91893
  1. 29
      src/lib/components/RelayStatus.svelte
  2. 11
      src/lib/ndk.ts
  3. 30
      src/lib/stores/userStore.ts
  4. 28
      src/lib/utils/relay_management.ts

29
src/lib/components/RelayStatus.svelte

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
<script lang="ts">
import { Button, Alert } from "flowbite-svelte";
import { Button, Alert, Checkbox } from "flowbite-svelte";
import {
ndkSignedIn,
testRelayConnection,
@ -7,7 +7,8 @@ @@ -7,7 +7,8 @@
checkEnvironmentForWebSocketDowngrade,
} from "$lib/ndk";
import { onMount } from "svelte";
import { activeInboxRelays, activeOutboxRelays, getNdkContext } from "$lib/ndk";
import { activeInboxRelays, activeOutboxRelays, getNdkContext, updateActiveRelayStores } from "$lib/ndk";
import { includeLocalhostRelays } from "$lib/stores/userStore";
const ndk = getNdkContext();
@ -114,6 +115,19 @@ @@ -114,6 +115,19 @@
if (status.error) return `Error: ${status.error}`;
return "Failed to Connect";
}
// AI-NOTE: 2025-01-24 - Handle localhost relay preference change
async function handleLocalhostToggle() {
// Update relay stores to reflect the new preference
try {
await updateActiveRelayStores(ndk, true); // Force update
} catch (error) {
console.warn("Failed to update relay stores after localhost preference change:", error);
}
// Re-run relay tests when the preference changes
void runRelayTests();
}
</script>
<div class="space-y-4">
@ -124,6 +138,17 @@ @@ -124,6 +138,17 @@
</Button>
</div>
<div class="flex items-center gap-2 p-3 bg-gray-50 dark:bg-gray-800 rounded">
<Checkbox
id="localhost-relays-checkbox"
bind:checked={$includeLocalhostRelays}
onchange={handleLocalhostToggle}
/>
<label for="localhost-relays-checkbox" class="text-sm text-gray-700 dark:text-gray-300 cursor-pointer">
Include localhost relays (for development/testing)
</label>
</div>
{#if !$ndkSignedIn}
<Alert color="yellow">
<span class="font-medium">Anonymous Mode</span>

11
src/lib/ndk.ts

@ -12,6 +12,7 @@ import { @@ -12,6 +12,7 @@ import {
buildCompleteRelaySet,
deduplicateRelayUrls,
testRelayConnection,
setLocalhostPreferenceChangeCallback,
} from "./utils/relay_management.ts";
import { userStore } from "./stores/userStore.ts";
@ -52,6 +53,16 @@ let relaySetLastUpdated: number = 0; @@ -52,6 +53,16 @@ let relaySetLastUpdated: number = 0;
const RELAY_SET_CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
const RELAY_SET_STORAGE_KEY = "alexandria/relay_set_cache";
// AI-NOTE: 2025-01-24 - Function to invalidate relay cache when localhost preference changes
function invalidateRelayCache() {
persistentRelaySet = null;
relaySetLastUpdated = 0;
console.debug("[NDK.ts] Relay cache invalidated due to localhost preference change");
}
// Set up callback for localhost preference changes
setLocalhostPreferenceChangeCallback(invalidateRelayCache);
/**
* Load persistent relay set from localStorage
*/

30
src/lib/stores/userStore.ts

@ -39,6 +39,36 @@ export const userStore = writable<UserState>({ @@ -39,6 +39,36 @@ export const userStore = writable<UserState>({
signedIn: false,
});
// AI-NOTE: 2025-01-24 - User preference for localhost relay inclusion
// This allows users to disable localhost relays for security reasons
const LOCALHOST_RELAYS_STORAGE_KEY = "alexandria/preferences/include_localhost_relays";
// Initialize from localStorage if available
let initialLocalhostPreference = false;
if (typeof window !== "undefined") {
try {
const stored = localStorage.getItem(LOCALHOST_RELAYS_STORAGE_KEY);
if (stored !== null) {
initialLocalhostPreference = JSON.parse(stored);
}
} catch (error) {
console.warn("Failed to load localhost relay preference from localStorage:", error);
}
}
export const includeLocalhostRelays = writable<boolean>(initialLocalhostPreference);
// Subscribe to changes and persist to localStorage
if (typeof window !== "undefined") {
includeLocalhostRelays.subscribe((value) => {
try {
localStorage.setItem(LOCALHOST_RELAYS_STORAGE_KEY, JSON.stringify(value));
} catch (error) {
console.warn("Failed to save localhost relay preference to localStorage:", error);
}
});
}
// Helper functions for relay management
function getRelayStorageKey(user: NDKUser, type: "inbox" | "outbox"): string {
return `${loginStorageKey}/${user.pubkey}/${type}`;

28
src/lib/utils/relay_management.ts

@ -8,6 +8,7 @@ import { @@ -8,6 +8,7 @@ import {
} from "../consts.ts";
import { getRelaySetForNetworkCondition } from "./network_detection.ts";
import { networkCondition } from "../stores/networkStore.ts";
import { includeLocalhostRelays } from "../stores/userStore.ts";
import { get } from "svelte/store";
/**
@ -349,6 +350,23 @@ async function testLocalRelays( @@ -349,6 +350,23 @@ async function testLocalRelays(
}
}
// AI-NOTE: 2025-01-24 - Cache invalidation for localhost preference changes
// This allows the relay management system to react to user preference changes
let localhostPreferenceChangeCallback: (() => void) | null = null;
export function setLocalhostPreferenceChangeCallback(callback: () => void) {
localhostPreferenceChangeCallback = callback;
}
// Subscribe to localhost preference changes and trigger relay updates
if (typeof window !== "undefined") {
includeLocalhostRelays.subscribe(() => {
if (localhostPreferenceChangeCallback) {
localhostPreferenceChangeCallback();
}
});
}
/**
* Discovers local relays by testing common localhost URLs
* @param ndk NDK instance
@ -356,6 +374,13 @@ async function testLocalRelays( @@ -356,6 +374,13 @@ async function testLocalRelays(
*/
export async function discoverLocalRelays(ndk: NDK): Promise<string[]> {
try {
// Check if user has disabled localhost relays
const shouldIncludeLocalhost = get(includeLocalhostRelays);
if (!shouldIncludeLocalhost) {
console.debug("[relay_management.ts] Localhost relays disabled by user preference");
return [];
}
// If no local relays are configured, return empty array
if (localRelays.length === 0) {
console.debug("[relay_management.ts] No local relays configured");
@ -686,7 +711,8 @@ export async function buildCompleteRelaySet( @@ -686,7 +711,8 @@ export async function buildCompleteRelaySet(
user?.pubkey || "null",
);
// Discover local relays first
// AI-NOTE: 2025-01-24 - Local relay discovery respects user preference
// The discoverLocalRelays function will return empty array if user has disabled localhost relays
const discoveredLocalRelays = await discoverLocalRelays(ndk);
console.debug(
"[relay_management.ts] buildCompleteRelaySet: Discovered local relays:",

Loading…
Cancel
Save