Refactor market components for improved structure and functionality

- Update imports in DashboardOverview.vue to use relative paths for better module organization.
- Modify OrderHistory.vue to replace 'lightningInvoice' with 'paymentRequest' for consistency in payment handling.
- Enhance order event handling in useMarket.ts by adding subscription and decryption logic for order-related DMs.
- Update nostrmarketService.ts to use relative imports, ensuring consistency across the module.
- Introduce error handling and logging for order updates, improving the robustness of the market module.
This commit is contained in:
padreug 2025-09-05 04:38:51 +02:00
parent 36638d1080
commit f5ea2a8d5e
4 changed files with 110 additions and 12 deletions

View file

@ -3,6 +3,8 @@ import { useNostrStore } from '@/stores/nostr'
import { useMarketStore } from '../stores/market'
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
import { config } from '@/lib/config'
import { nostrmarketService } from '../services/nostrmarketService'
import { nip04 } from 'nostr-tools'
// Nostr event kinds for market functionality
const MARKET_EVENT_KINDS = {
@ -333,6 +335,89 @@ export function useMarket() {
}
}
// Subscribe to order-related DMs (payment requests, status updates)
const subscribeToOrderUpdates = (): (() => void) | null => {
try {
// TODO: Confirm if this should use nostrStore.account?.pubkey or authService.user.value?.pubkey
const userPubkey = nostrStore.account?.pubkey || authService.user.value?.pubkey
if (!userPubkey) {
console.warn('Cannot subscribe to order updates: no user pubkey available', {
nostrStorePubkey: nostrStore.account?.pubkey,
authServicePubkey: authService.user.value?.pubkey,
isAuthenticated: authService.isAuthenticated.value
})
return null
}
console.log('🔔 Setting up order updates subscription for user:', userPubkey.slice(0, 8))
// Subscribe to encrypted DMs directed to this user (payment requests, status updates)
const unsubscribe = relayHub.subscribe({
id: `order-updates-${userPubkey}`,
filters: [
{
kinds: [4], // Encrypted DMs
'#p': [userPubkey], // Messages directed to this user
since: Math.floor(Date.now() / 1000) - 3600 // Last hour to avoid old messages
}
],
onEvent: async (event: any) => {
await handleOrderDM(event)
}
})
return unsubscribe
} catch (error) {
console.error('Failed to subscribe to order updates:', error)
return null
}
}
// Handle incoming order DMs (payment requests, status updates)
const handleOrderDM = async (event: any) => {
try {
console.log('🔔 Received order-related DM:', event.id, 'from:', event.pubkey.slice(0, 8))
// TODO: Confirm if this should use nostrStore.account?.pubkey or authService.user.value?.pubkey
const userPubkey = nostrStore.account?.pubkey || authService.user.value?.pubkey
const userPrivkey = nostrStore.account?.privkey || authService.user.value?.prvkey
if (!userPrivkey) {
console.warn('Cannot decrypt DM: no user private key available', {
nostrStorePrivkey: !!nostrStore.account?.privkey,
authServicePrivkey: !!authService.user.value?.prvkey
})
return
}
console.log('🔓 Attempting to decrypt DM with private key available')
// Decrypt the DM content
const decryptedContent = await nip04.decrypt(userPrivkey, event.pubkey, event.content)
console.log('🔓 Decrypted DM content:', decryptedContent)
// Parse the decrypted content as JSON
const messageData = JSON.parse(decryptedContent)
console.log('📨 Parsed message data:', messageData)
// Handle different types of messages
switch (messageData.type) {
case 1: // Payment request
console.log('💰 Processing payment request')
await nostrmarketService.handlePaymentRequest(messageData)
break
case 2: // Order status update
console.log('📦 Processing order status update')
await nostrmarketService.handleOrderStatusUpdate(messageData)
break
default:
console.log('❓ Unknown message type:', messageData.type)
}
} catch (error) {
console.error('Failed to handle order DM:', error)
}
}
// Handle incoming market events
const handleMarketEvent = (event: any) => {
// Process market event
@ -494,6 +579,10 @@ export function useMarket() {
// Subscribe to updates
console.log('🛒 Subscribing to market updates...')
subscribeToMarketUpdates()
// Subscribe to order-related DMs
console.log('🛒 Subscribing to order updates...')
subscribeToOrderUpdates()
} catch (err) {
console.error('🛒 Failed to connect to market:', err)
@ -540,6 +629,7 @@ export function useMarket() {
processPendingProducts,
publishProduct,
publishStall,
subscribeToMarketUpdates
subscribeToMarketUpdates,
subscribeToOrderUpdates
}
}