mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-03 08:07:21 +00:00
Add Blossom server list to app config (kind 10063)
Blossom servers now live in AppConfig alongside the NIP-65 relay list: - blossomServerMetadata (BUD-03 server list) and useAppBlossomServers flag - src/lib/appBlossom.ts provides APP_BLOSSOM_SERVERS defaults (blossom.ditto.pub, blossom.dreamith.to, blossom.primal.net), parseBlossomServerList, and getEffectiveBlossomServers - NostrSync hydrates the user's kind 10063 event on login - useUploadFile reads effective servers from config
This commit is contained in:
7 files changed
+171
-7
No files matched your search
+9
-1
@@ -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() {
|
||||
|
||||
@@ -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<RelayMetadata>;
|
||||
|
||||
// Zod schema for BlossomServerMetadata validation
|
||||
const BlossomServerMetadataSchema = z.object({
|
||||
servers: z.array(z.url()),
|
||||
updatedAt: z.number(),
|
||||
}) satisfies z.ZodType<BlossomServerMetadata>;
|
||||
|
||||
// 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<AppConfig>;
|
||||
|
||||
export function AppProvider(props: AppProviderProps) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<string>();
|
||||
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<string>();
|
||||
const result: string[] = [];
|
||||
for (const url of servers) {
|
||||
const normalized = normalizeUrl(url);
|
||||
if (!seen.has(normalized)) {
|
||||
seen.add(normalized);
|
||||
result.push(url);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -28,6 +28,11 @@ export function TestApp({ children }: TestAppProps) {
|
||||
],
|
||||
updatedAt: 0,
|
||||
},
|
||||
blossomServerMetadata: {
|
||||
servers: ['https://blossom.primal.net/'],
|
||||
updatedAt: 0,
|
||||
},
|
||||
useAppBlossomServers: true,
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user