refactor: simplify to single RELAYS constant (DRY)

This commit is contained in:
Gigi
2025-10-05 23:41:27 +01:00
parent 6e2f1102f7
commit 8b023af6a0
6 changed files with 22 additions and 73 deletions

View File

@@ -11,7 +11,7 @@ import { createAddressLoader } from 'applesauce-loaders/loaders'
import Bookmarks from './components/Bookmarks' import Bookmarks from './components/Bookmarks'
import Toast from './components/Toast' import Toast from './components/Toast'
import { useToast } from './hooks/useToast' import { useToast } from './hooks/useToast'
import { ALL_RELAYS, PROFILE_RELAYS } from './config/relays' import { RELAYS } from './config/relays'
const DEFAULT_ARTICLE = import.meta.env.VITE_DEFAULT_ARTICLE_NADDR || const DEFAULT_ARTICLE = import.meta.env.VITE_DEFAULT_ARTICLE_NADDR ||
'naddr1qvzqqqr4gupzqmjxss3dld622uu8q25gywum9qtg4w4cv4064jmg20xsac2aam5nqqxnzd3cxqmrzv3exgmr2wfesgsmew' 'naddr1qvzqqqr4gupzqmjxss3dld622uu8q25gywum9qtg4w4cv4064jmg20xsac2aam5nqqxnzd3cxqmrzv3exgmr2wfesgsmew'
@@ -96,15 +96,14 @@ function App() {
const pool = new RelayPool() const pool = new RelayPool()
// Create a relay group for better event deduplication and management // Create a relay group for better event deduplication and management
// This follows the applesauce-relay documentation pattern pool.group(RELAYS)
pool.group(ALL_RELAYS) console.log('Created relay group with', RELAYS.length, 'relays (including local)')
console.log('Created relay group with', ALL_RELAYS.length, 'relays (including local)') console.log('Relay URLs:', RELAYS)
console.log('Relay URLs:', ALL_RELAYS)
// Attach address/replaceable loaders so ProfileModel can fetch profiles // Attach address/replaceable loaders so ProfileModel can fetch profiles
const addressLoader = createAddressLoader(pool, { const addressLoader = createAddressLoader(pool, {
eventStore: store, eventStore: store,
lookupRelays: PROFILE_RELAYS lookupRelays: RELAYS
}) })
store.addressableLoader = addressLoader store.addressableLoader = addressLoader
store.replaceableLoader = addressLoader store.replaceableLoader = addressLoader

View File

@@ -1,13 +1,11 @@
/** /**
* Centralized relay configuration * Centralized relay configuration
* All relay URLs used throughout the application * Single set of relays used throughout the application
*/ */
// Local relay URL (hardcoded for development) // All relays including local relay
export const LOCAL_RELAY = 'ws://localhost:7777' export const RELAYS = [
'ws://localhost:7777',
// Public relays for general use
export const PUBLIC_RELAYS = [
'wss://relay.damus.io', 'wss://relay.damus.io',
'wss://nos.lol', 'wss://nos.lol',
'wss://relay.nostr.band', 'wss://relay.nostr.band',
@@ -15,56 +13,8 @@ export const PUBLIC_RELAYS = [
'wss://wot.dergigi.com', 'wss://wot.dergigi.com',
'wss://relay.snort.social', 'wss://relay.snort.social',
'wss://relay.current.fyi', 'wss://relay.current.fyi',
'wss://nostr-pub.wellorder.net' 'wss://nostr-pub.wellorder.net',
]
// Relays for profile lookups
export const PROFILE_RELAYS = [
'wss://purplepag.es', 'wss://purplepag.es',
'wss://relay.primal.net',
'wss://relay.nostr.band'
]
// Relays for highlights (read and write)
export const HIGHLIGHT_RELAYS = [
LOCAL_RELAY,
'wss://relay.damus.io',
'wss://nos.lol',
'wss://relay.nostr.band',
'wss://relay.snort.social',
'wss://purplepag.es'
]
// Relays for articles
export const ARTICLE_RELAYS = [
LOCAL_RELAY,
'wss://relay.damus.io',
'wss://nos.lol',
'wss://relay.nostr.band',
'wss://relay.primal.net' 'wss://relay.primal.net'
] ]
// Relays for settings (read and write)
export const SETTINGS_RELAYS = [
LOCAL_RELAY,
'wss://relay.damus.io',
'wss://nos.lol',
'wss://relay.nostr.band',
'wss://relay.dergigi.com',
'wss://wot.dergigi.com'
]
// All relays including local (for general operations and relay pool initialization)
export const ALL_RELAYS = [LOCAL_RELAY, ...PUBLIC_RELAYS]
// All write relays (where we publish events)
export const WRITE_RELAYS = [
LOCAL_RELAY,
'wss://relay.damus.io',
'wss://nos.lol',
'wss://relay.nostr.band',
'wss://relay.snort.social',
'wss://purplepag.es',
'wss://relay.dergigi.com'
]

View File

@@ -5,7 +5,7 @@ import { EventFactory } from 'applesauce-factory'
import { AccountManager } from 'applesauce-accounts' import { AccountManager } from 'applesauce-accounts'
import { UserSettings, loadSettings, saveSettings, watchSettings } from '../services/settingsService' import { UserSettings, loadSettings, saveSettings, watchSettings } from '../services/settingsService'
import { loadFont, getFontFamily } from '../utils/fontLoader' import { loadFont, getFontFamily } from '../utils/fontLoader'
import { SETTINGS_RELAYS } from '../config/relays' import { RELAYS } from '../config/relays'
interface UseSettingsParams { interface UseSettingsParams {
relayPool: RelayPool | null relayPool: RelayPool | null
@@ -25,7 +25,7 @@ export function useSettings({ relayPool, eventStore, pubkey, accountManager }: U
const loadAndWatch = async () => { const loadAndWatch = async () => {
try { try {
const loadedSettings = await loadSettings(relayPool, eventStore, pubkey, SETTINGS_RELAYS) const loadedSettings = await loadSettings(relayPool, eventStore, pubkey, RELAYS)
if (loadedSettings) setSettings(loadedSettings) if (loadedSettings) setSettings(loadedSettings)
} catch (err) { } catch (err) {
console.error('Failed to load settings:', err) console.error('Failed to load settings:', err)
@@ -61,7 +61,7 @@ export function useSettings({ relayPool, eventStore, pubkey, accountManager }: U
const fullAccount = accountManager.getActive() const fullAccount = accountManager.getActive()
if (!fullAccount) throw new Error('No active account') if (!fullAccount) throw new Error('No active account')
const factory = new EventFactory({ signer: fullAccount }) const factory = new EventFactory({ signer: fullAccount })
await saveSettings(relayPool, eventStore, factory, newSettings, SETTINGS_RELAYS) await saveSettings(relayPool, eventStore, factory, newSettings, RELAYS)
setSettings(newSettings) setSettings(newSettings)
setToastType('success') setToastType('success')
setToastMessage('Settings saved') setToastMessage('Settings saved')

View File

@@ -9,7 +9,7 @@ import {
getArticlePublished, getArticlePublished,
getArticleSummary getArticleSummary
} from 'applesauce-core/helpers' } from 'applesauce-core/helpers'
import { ARTICLE_RELAYS } from '../config/relays' import { RELAYS } from '../config/relays'
export interface ArticleContent { export interface ArticleContent {
title: string title: string
@@ -99,7 +99,7 @@ export async function fetchArticleByNaddr(
// Define relays to query - prefer relays from naddr, fallback to configured relays (including local) // Define relays to query - prefer relays from naddr, fallback to configured relays (including local)
const relays = pointer.relays && pointer.relays.length > 0 const relays = pointer.relays && pointer.relays.length > 0
? pointer.relays ? pointer.relays
: ARTICLE_RELAYS : RELAYS
// Fetch the article event // Fetch the article event
const filter = { const filter = {

View File

@@ -4,7 +4,7 @@ import { RelayPool } from 'applesauce-relay'
import { IAccount } from 'applesauce-accounts' import { IAccount } from 'applesauce-accounts'
import { AddressPointer } from 'nostr-tools/nip19' import { AddressPointer } from 'nostr-tools/nip19'
import { NostrEvent } from 'nostr-tools' import { NostrEvent } from 'nostr-tools'
import { WRITE_RELAYS } from '../config/relays' import { RELAYS } from '../config/relays'
/** /**
* Creates and publishes a highlight event (NIP-84) * Creates and publishes a highlight event (NIP-84)
@@ -38,9 +38,9 @@ export async function createHighlight(
const signedEvent = await factory.sign(highlightEvent) const signedEvent = await factory.sign(highlightEvent)
// Publish to relays (including local relay) // Publish to relays (including local relay)
await relayPool.publish(WRITE_RELAYS, signedEvent) await relayPool.publish(RELAYS, signedEvent)
console.log('✅ Highlight published to', WRITE_RELAYS.length, 'relays (including local):', signedEvent) console.log('✅ Highlight published to', RELAYS.length, 'relays (including local):', signedEvent)
} }
/** /**

View File

@@ -11,7 +11,7 @@ import {
getHighlightAttributions getHighlightAttributions
} from 'applesauce-core/helpers' } from 'applesauce-core/helpers'
import { Highlight } from '../types/highlights' import { Highlight } from '../types/highlights'
import { HIGHLIGHT_RELAYS } from '../config/relays' import { RELAYS } from '../config/relays'
/** /**
* Deduplicate highlight events by ID * Deduplicate highlight events by ID
@@ -45,7 +45,7 @@ export const fetchHighlightsForArticle = async (
try { try {
console.log('🔍 Fetching highlights (kind 9802) for article:', articleCoordinate) console.log('🔍 Fetching highlights (kind 9802) for article:', articleCoordinate)
console.log('🔍 Event ID:', eventId || 'none') console.log('🔍 Event ID:', eventId || 'none')
console.log('🔍 From relays (including local):', HIGHLIGHT_RELAYS) console.log('🔍 From relays (including local):', RELAYS)
const seenIds = new Set<string>() const seenIds = new Set<string>()
const processEvent = (event: NostrEvent): Highlight | null => { const processEvent = (event: NostrEvent): Highlight | null => {
@@ -81,7 +81,7 @@ export const fetchHighlightsForArticle = async (
// Query for highlights that reference this article via the 'a' tag // Query for highlights that reference this article via the 'a' tag
const aTagEvents = await lastValueFrom( const aTagEvents = await lastValueFrom(
relayPool relayPool
.req(HIGHLIGHT_RELAYS, { kinds: [9802], '#a': [articleCoordinate] }) .req(RELAYS, { kinds: [9802], '#a': [articleCoordinate] })
.pipe( .pipe(
onlyEvents(), onlyEvents(),
tap((event: NostrEvent) => { tap((event: NostrEvent) => {
@@ -103,7 +103,7 @@ export const fetchHighlightsForArticle = async (
if (eventId) { if (eventId) {
eTagEvents = await lastValueFrom( eTagEvents = await lastValueFrom(
relayPool relayPool
.req(HIGHLIGHT_RELAYS, { kinds: [9802], '#e': [eventId] }) .req(RELAYS, { kinds: [9802], '#e': [eventId] })
.pipe( .pipe(
onlyEvents(), onlyEvents(),
tap((event: NostrEvent) => { tap((event: NostrEvent) => {