diff --git a/src/App.tsx b/src/App.tsx index f41891f..2dcab48 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -13,7 +13,6 @@ import { NostrLoginProvider } from '@nostrify/react/login'; import { AppProvider } from '@/components/AppProvider'; import { AppConfig } from '@/contexts/AppContext'; import AppRouter from './AppRouter'; - const head = createHead({ plugins: [ InferSeoMetaPlugin(), @@ -40,6 +39,15 @@ const defaultConfig: AppConfig = { ], updatedAt: 0, }, + blossomServerMetadata: { + servers: [ + 'https://blossom.ditto.pub/', + 'https://blossom.dreamith.to/', + 'https://blossom.primal.net/', + ], + updatedAt: 0, + }, + useAppBlossomServers: true, }; export function App() { diff --git a/src/components/AppProvider.tsx b/src/components/AppProvider.tsx index 8bacdac..2da8a41 100644 --- a/src/components/AppProvider.tsx +++ b/src/components/AppProvider.tsx @@ -1,7 +1,7 @@ import { ReactNode, useEffect } from 'react'; import { z } from 'zod'; import { useLocalStorage } from '@/hooks/useLocalStorage'; -import { AppContext, type AppConfig, type AppContextType, type Theme, type RelayMetadata } from '@/contexts/AppContext'; +import { AppContext, type AppConfig, type AppContextType, type Theme, type RelayMetadata, type BlossomServerMetadata } from '@/contexts/AppContext'; interface AppProviderProps { children: ReactNode; @@ -21,10 +21,18 @@ const RelayMetadataSchema = z.object({ updatedAt: z.number(), }) satisfies z.ZodType; +// Zod schema for BlossomServerMetadata validation +const BlossomServerMetadataSchema = z.object({ + servers: z.array(z.url()), + updatedAt: z.number(), +}) satisfies z.ZodType; + // Zod schema for AppConfig validation const AppConfigSchema = z.object({ theme: z.enum(['dark', 'light', 'system']), relayMetadata: RelayMetadataSchema, + blossomServerMetadata: BlossomServerMetadataSchema, + useAppBlossomServers: z.boolean(), }) satisfies z.ZodType; export function AppProvider(props: AppProviderProps) { diff --git a/src/components/NostrSync.tsx b/src/components/NostrSync.tsx index 2ebf856..d9bbefd 100644 --- a/src/components/NostrSync.tsx +++ b/src/components/NostrSync.tsx @@ -2,6 +2,7 @@ import { useEffect } from 'react'; import { useNostr } from '@nostrify/react'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAppContext } from '@/hooks/useAppContext'; +import { parseBlossomServerList } from '@/lib/appBlossom'; /** * NostrSync - Syncs user's Nostr data @@ -9,6 +10,7 @@ import { useAppContext } from '@/hooks/useAppContext'; * This component runs globally to sync various Nostr data when the user logs in. * Currently syncs: * - NIP-65 relay list (kind 10002) + * - BUD-03 Blossom server list (kind 10063) */ export function NostrSync() { const { nostr } = useNostr(); @@ -58,5 +60,42 @@ export function NostrSync() { syncRelaysFromNostr(); }, [user, config.relayMetadata.updatedAt, nostr, updateConfig]); + useEffect(() => { + if (!user) return; + + const syncBlossomServersFromNostr = async () => { + try { + const events = await nostr.query( + [{ kinds: [10063], authors: [user.pubkey], limit: 1 }], + { signal: AbortSignal.timeout(5000) } + ); + + if (events.length > 0) { + const event = events[0]; + + // Only update if the event is newer than our stored data + if (event.created_at > config.blossomServerMetadata.updatedAt) { + const fetchedServers = parseBlossomServerList(event); + + if (fetchedServers.length > 0) { + console.log('Syncing Blossom server list from Nostr:', fetchedServers); + updateConfig((current) => ({ + ...current, + blossomServerMetadata: { + servers: fetchedServers, + updatedAt: event.created_at, + }, + })); + } + } + } + } catch (error) { + console.error('Failed to sync Blossom servers from Nostr:', error); + } + }; + + syncBlossomServersFromNostr(); + }, [user, config.blossomServerMetadata.updatedAt, nostr, updateConfig]); + return null; -} \ No newline at end of file +} diff --git a/src/contexts/AppContext.ts b/src/contexts/AppContext.ts index 6af4057..d35cab6 100644 --- a/src/contexts/AppContext.ts +++ b/src/contexts/AppContext.ts @@ -9,11 +9,27 @@ export interface RelayMetadata { updatedAt: number; } +/** Blossom server list metadata (BUD-03 / kind 10063), mirroring RelayMetadata for parity. */ +export interface BlossomServerMetadata { + /** Ordered list of Blossom server URLs (most trusted/reliable first per BUD-03). */ + servers: string[]; + /** Unix timestamp of when the server list was last updated (from kind 10063 created_at). */ + updatedAt: number; +} + export interface AppConfig { /** Current theme */ theme: Theme; /** NIP-65 relay list metadata */ relayMetadata: RelayMetadata; + /** User's kind 10063 Blossom server list. */ + blossomServerMetadata: BlossomServerMetadata; + /** + * When true, the app's default Blossom servers are merged with the user's + * servers (app servers first, deduped). When false, only the user's servers + * are used. + */ + useAppBlossomServers: boolean; } export interface AppContextType { diff --git a/src/hooks/useUploadFile.ts b/src/hooks/useUploadFile.ts index a3c9793..f287378 100644 --- a/src/hooks/useUploadFile.ts +++ b/src/hooks/useUploadFile.ts @@ -2,9 +2,12 @@ import { useMutation } from "@tanstack/react-query"; import { BlossomUploader } from '@nostrify/nostrify/uploaders'; import { useCurrentUser } from "./useCurrentUser"; +import { useAppContext } from "./useAppContext"; +import { getEffectiveBlossomServers } from "@/lib/appBlossom"; export function useUploadFile() { const { user } = useCurrentUser(); + const { config } = useAppContext(); return useMutation({ mutationFn: async (file: File) => { @@ -12,10 +15,17 @@ export function useUploadFile() { throw new Error('Must be logged in to upload files'); } + const servers = getEffectiveBlossomServers( + config.blossomServerMetadata, + config.useAppBlossomServers, + ); + + if (servers.length === 0) { + throw new Error('No Blossom servers configured'); + } + const uploader = new BlossomUploader({ - servers: [ - 'https://blossom.primal.net/', - ], + servers, signer: user.signer, }); @@ -23,4 +33,4 @@ export function useUploadFile() { return tags; }, }); -} \ No newline at end of file +} diff --git a/src/lib/appBlossom.ts b/src/lib/appBlossom.ts new file mode 100644 index 0000000..b233e9d --- /dev/null +++ b/src/lib/appBlossom.ts @@ -0,0 +1,78 @@ +import type { NostrEvent } from "@nostrify/nostrify"; +import type { BlossomServerMetadata } from "@/contexts/AppContext"; + +/** Normalize a Blossom server URL for deduplication (lowercase, strip trailing slashes). */ +function normalizeUrl(url: string): string { + return url.toLowerCase().replace(/\/+$/, ""); +} + +/** Parse a kind 10063 Blossom server list event into validated server URLs. */ +export function parseBlossomServerList(event: NostrEvent): string[] { + return event.tags + .filter(([name]) => name === "server") + .map(([, url]) => url) + .filter((url) => { + try { + new URL(url); + return true; + } catch { + return false; + } + }); +} + +/** + * App default Blossom servers used as a fallback when the user has no kind 10063 + * server list, and optionally merged with user servers via `useAppBlossomServers`. + */ +export const APP_BLOSSOM_SERVERS: BlossomServerMetadata = { + servers: [ + "https://blossom.ditto.pub/", + "https://blossom.dreamith.to/", + "https://blossom.primal.net/", + ], + updatedAt: 0, +}; + +/** + * Get the effective Blossom server list based on user settings. + * + * - If `useAppBlossomServers` is true, merges app servers with user servers (deduped, + * app servers first per BUD-03's "most trusted first" ordering). + * - If `useAppBlossomServers` is false, returns only the user's servers (deduped). + */ +export function getEffectiveBlossomServers( + userMeta: BlossomServerMetadata, + useAppBlossomServers: boolean, +): string[] { + if (!useAppBlossomServers) { + return deduplicateServers(userMeta.servers); + } + + const seen = new Set(); + const merged: string[] = []; + + for (const url of [...APP_BLOSSOM_SERVERS.servers, ...userMeta.servers]) { + const normalized = normalizeUrl(url); + if (!seen.has(normalized)) { + seen.add(normalized); + merged.push(url); + } + } + + return merged; +} + +/** Deduplicate servers by normalized URL, preserving order. */ +function deduplicateServers(servers: string[]): string[] { + const seen = new Set(); + const result: string[] = []; + for (const url of servers) { + const normalized = normalizeUrl(url); + if (!seen.has(normalized)) { + seen.add(normalized); + result.push(url); + } + } + return result; +} diff --git a/src/test/TestApp.tsx b/src/test/TestApp.tsx index 5d87264..eb47482 100644 --- a/src/test/TestApp.tsx +++ b/src/test/TestApp.tsx @@ -28,6 +28,11 @@ export function TestApp({ children }: TestAppProps) { ], updatedAt: 0, }, + blossomServerMetadata: { + servers: ['https://blossom.primal.net/'], + updatedAt: 0, + }, + useAppBlossomServers: true, }; return (