mirror of
https://github.com/dergigi/boris.git
synced 2025-12-24 18:14:21 +01:00
- Add explicit signing permissions for event kinds: 5, 7, 17, 9802, 30078, 39701, 0 - Add encryption/decryption permissions: nip04_encrypt/decrypt, nip44_encrypt/decrypt - Improve error messages when bunker permissions are missing or denied - Add debug logging hint for bunker permission issues in write service - This ensures highlights, reactions, settings, reading positions, and web bookmarks all work with bunker
63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
import { RelayPool } from 'applesauce-relay'
|
|
import { NostrEvent } from 'nostr-tools'
|
|
import { IEventStore } from 'applesauce-core'
|
|
import { RELAYS } from '../config/relays'
|
|
import { isLocalRelay, areAllRelaysLocal } from '../utils/helpers'
|
|
import { markEventAsOfflineCreated } from './offlineSyncService'
|
|
|
|
/**
|
|
* Unified write helper: add event to EventStore, detect connectivity,
|
|
* mark for offline sync if needed, and publish in background.
|
|
*/
|
|
export async function publishEvent(
|
|
relayPool: RelayPool,
|
|
eventStore: IEventStore,
|
|
event: NostrEvent
|
|
): Promise<void> {
|
|
// Store the event in the local EventStore FIRST for immediate UI display
|
|
eventStore.add(event)
|
|
console.log('💾 Stored event in EventStore:', event.id.slice(0, 8), `(kind ${event.kind})`)
|
|
|
|
// Check current connection status - are we online or in flight mode?
|
|
const connectedRelays = Array.from(relayPool.relays.values())
|
|
.filter(relay => relay.connected)
|
|
.map(relay => relay.url)
|
|
|
|
const hasRemoteConnection = connectedRelays.some(url => !isLocalRelay(url))
|
|
|
|
// Determine which relays we expect to succeed
|
|
const expectedSuccessRelays = hasRemoteConnection
|
|
? RELAYS
|
|
: RELAYS.filter(isLocalRelay)
|
|
|
|
const isLocalOnly = areAllRelaysLocal(expectedSuccessRelays)
|
|
|
|
console.log('📍 Event relay status:', {
|
|
targetRelays: RELAYS.length,
|
|
expectedSuccessRelays: expectedSuccessRelays.length,
|
|
isLocalOnly,
|
|
hasRemoteConnection,
|
|
eventId: event.id.slice(0, 8)
|
|
})
|
|
|
|
// If we're in local-only mode, mark this event for later sync
|
|
if (isLocalOnly) {
|
|
markEventAsOfflineCreated(event.id)
|
|
}
|
|
|
|
// Publish to all configured relays in the background (non-blocking)
|
|
relayPool.publish(RELAYS, event)
|
|
.then(() => {
|
|
console.log('✅ Event published to', RELAYS.length, 'relay(s):', event.id.slice(0, 8))
|
|
})
|
|
.catch((error) => {
|
|
console.warn('⚠️ Failed to publish event to relays (event still saved locally):', error)
|
|
|
|
// Surface common bunker signing errors for debugging
|
|
if (error instanceof Error && error.message.includes('permission')) {
|
|
console.warn('💡 Hint: This may be a bunker permission issue. Ensure your bunker connection has signing permissions.')
|
|
}
|
|
})
|
|
}
|
|
|