Enhance ChatService to process market messages in chat

- Updated ChatService to forward market messages to the chat interface, ensuring they are displayed alongside regular chat messages.
- Improved message formatting for payment requests and order status updates, providing clearer user notifications.
- Deprecated the previous order updates subscription method in useMarket, redirecting functionality to the chat service for better integration.

These changes improve the user experience by consolidating message handling and enhancing clarity in communication.
This commit is contained in:
padreug 2025-09-07 00:08:38 +02:00
parent c7e11a7c01
commit 5a899d1501
2 changed files with 32 additions and 36 deletions

View file

@ -722,6 +722,7 @@ export class ChatService extends BaseService {
// Forward to market handler // Forward to market handler
if (this.marketMessageHandler) { if (this.marketMessageHandler) {
await this.marketMessageHandler(event) await this.marketMessageHandler(event)
console.log('💬 Market message forwarded to market handler and will also be added to chat')
} else { } else {
console.warn('Market message handler not available, message will be treated as chat') console.warn('Market message handler not available, message will be treated as chat')
} }
@ -730,12 +731,36 @@ export class ChatService extends BaseService {
// Not JSON or not a market message, treat as regular chat // Not JSON or not a market message, treat as regular chat
} }
// Only process as chat message if it's not a market message // Process as chat message regardless (market messages should also appear in chat)
if (!isMarketMessage) { {
// Format the content for display based on whether it's a market message
let displayContent = decryptedContent
if (isMarketMessage) {
try {
const parsedContent = JSON.parse(decryptedContent)
if (parsedContent.type === 1) {
// Payment request
displayContent = `💰 Payment Request for Order ${parsedContent.id}\n${parsedContent.message || 'Please pay to proceed with your order'}`
} else if (parsedContent.type === 2) {
// Order status update
const status = []
if (parsedContent.paid === true) status.push('✅ Paid')
else if (parsedContent.paid === false) status.push('⏳ Payment Pending')
if (parsedContent.shipped === true) status.push('📦 Shipped')
else if (parsedContent.shipped === false) status.push('🔄 Processing')
displayContent = `📋 Order Update: ${parsedContent.id}\n${status.join(' | ')}\n${parsedContent.message || ''}`
}
} catch (e) {
// Fallback to raw content if parsing fails
}
}
// Create a chat message // Create a chat message
const message: ChatMessage = { const message: ChatMessage = {
id: event.id, id: event.id,
content: decryptedContent, content: displayContent,
created_at: event.created_at, created_at: event.created_at,
sent: false, sent: false,
pubkey: senderPubkey pubkey: senderPubkey

View file

@ -344,40 +344,11 @@ export function useMarket() {
} }
} }
// Subscribe to order-related DMs (payment requests, status updates) // Note: Order DMs are now handled by the chat service forwarding mechanism
// This method is deprecated and should not be called
const subscribeToOrderUpdates = (): (() => void) | null => { const subscribeToOrderUpdates = (): (() => void) | null => {
try { console.log('🔔 Order updates are now handled by chat service forwarding')
const userPubkey = authService.user.value?.pubkey return null
if (!userPubkey) {
console.warn('Cannot subscribe to order updates: no user pubkey available', {
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) // Handle incoming order DMs (payment requests, status updates)