Enhance chat and market integration with message forwarding

- Make ChatService globally available for other modules, enabling market-related message handling.
- Introduce a market message handler in ChatService to process market-related direct messages (DMs).
- Update useMarket to register the market message handler with ChatService, streamlining order-related DM processing.
- Refactor message handling logic to differentiate between market messages and regular chat messages, improving message management.
- Enhance order update logging in nostrmarketService for better debugging and verification of order status.
This commit is contained in:
padreug 2025-09-05 05:00:46 +02:00
parent db9b50240d
commit 4258ea87c4
5 changed files with 98 additions and 19 deletions

View file

@ -15,6 +15,7 @@ export class ChatService {
private config: ChatConfig
private subscriptionUnsubscriber?: () => void
private isInitialized = ref(false)
private marketMessageHandler?: (event: any) => Promise<void>
constructor(config: ChatConfig) {
this.config = config
@ -24,6 +25,11 @@ export class ChatService {
this.deferredInitialization()
}
// Register market message handler for forwarding market-related DMs
setMarketMessageHandler(handler: (event: any) => Promise<void>) {
this.marketMessageHandler = handler
}
// Defer initialization until services are ready
private deferredInitialization(): void {
// Try initialization immediately
@ -511,22 +517,45 @@ export class ChatService {
// Decrypt the message
const decryptedContent = await nip04.decrypt(userPrivkey, senderPubkey, event.content)
// Create a chat message
const message: ChatMessage = {
id: event.id,
content: decryptedContent,
created_at: event.created_at,
sent: false,
pubkey: senderPubkey
// Check if this is a market-related message (JSON with type field)
let isMarketMessage = false
try {
const parsedContent = JSON.parse(decryptedContent)
if (parsedContent && typeof parsedContent.type === 'number' && (parsedContent.type === 1 || parsedContent.type === 2)) {
// This is a market message (payment request type 1 or status update type 2)
isMarketMessage = true
console.log('🛒 Forwarding market message to market handler:', parsedContent.type)
// Forward to market handler
if (this.marketMessageHandler) {
await this.marketMessageHandler(event)
} else {
console.warn('Market message handler not available, message will be treated as chat')
}
}
} catch (e) {
// Not JSON or not a market message, treat as regular chat
}
// Ensure we have a peer record for the sender
this.addPeer(senderPubkey)
// Only process as chat message if it's not a market message
if (!isMarketMessage) {
// Create a chat message
const message: ChatMessage = {
id: event.id,
content: decryptedContent,
created_at: event.created_at,
sent: false,
pubkey: senderPubkey
}
// Add the message
this.addMessage(senderPubkey, message)
// Ensure we have a peer record for the sender
this.addPeer(senderPubkey)
console.log('Received encrypted message from:', senderPubkey.slice(0, 8))
// Add the message
this.addMessage(senderPubkey, message)
console.log('Received encrypted chat message from:', senderPubkey.slice(0, 8))
}
} catch (error) {
console.error('Failed to decrypt incoming message:', error)