shogun-core 3.2.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -99547,43 +99547,61 @@ class SHIP_01 {
99547
99547
  const userPub = currentUser.is.pub;
99548
99548
  const username = currentUser.is.alias;
99549
99549
  const allMessages = [];
99550
+ console.log(`[SHIP-01] Getting history between ${username} (${userPub.substring(0, 20)}...) and ${withUsername}`);
99550
99551
  // Ottieni il userPub dell'altro utente
99551
99552
  const otherUserData = await this.shogun.db.getUserByAlias(withUsername);
99552
99553
  const otherUserPub = otherUserData?.userPub;
99553
- // Recupera tutti i messaggi dal nodo 'messages' e filtra
99554
- const allMessagesNode = await this.shogun.db.gun.get('messages').then();
99555
- if (allMessagesNode) {
99556
- for (const [messageId, data] of Object.entries(allMessagesNode)) {
99557
- if (typeof data === 'object' && data !== null) {
99558
- const msgData = data;
99559
- // Filtra messaggi tra questo utente e withUsername
99560
- // Messaggi inviati da me a loro
99554
+ console.log(`[SHIP-01] Other user pub: ${otherUserPub?.substring(0, 20) || 'not found'}...`);
99555
+ // Use .map() instead of .then() to iterate all messages
99556
+ return new Promise((resolve) => {
99557
+ const messages = [];
99558
+ let messageCount = 0;
99559
+ this.shogun.db.gun.get('messages').map().once(async (msgData, messageId) => {
99560
+ // Skip metadata fields
99561
+ if (!msgData || typeof msgData !== 'object' || messageId === '_') {
99562
+ return;
99563
+ }
99564
+ messageCount++;
99565
+ console.log(`[SHIP-01] Checking message ${messageId}:`, {
99566
+ from: msgData.from?.substring(0, 20),
99567
+ to: msgData.to,
99568
+ hasContent: !!msgData.content
99569
+ });
99570
+ try {
99571
+ // Check if message is part of this conversation
99561
99572
  const isSentToTarget = msgData.from === userPub && msgData.to === withUsername;
99562
- // Messaggi ricevuti da loro
99563
- const isReceivedFromTarget = msgData.to === username &&
99564
- (msgData.from === otherUserPub ||
99565
- msgData.from);
99566
- if ((isSentToTarget || isReceivedFromTarget) && msgData.content && msgData.messageId) {
99567
- try {
99568
- // Decripta ogni messaggio
99569
- const decryptedContent = await this.decryptMessage(msgData.content, msgData.from);
99570
- allMessages.push({
99571
- from: msgData.from,
99572
- to: msgData.to,
99573
- content: decryptedContent,
99574
- timestamp: parseInt(msgData.timestamp)
99575
- });
99576
- }
99577
- catch (error) {
99578
- console.error(`❌ Errore decrittazione messaggio ${messageId}:`, error);
99579
- // Salta messaggi che non possono essere decriptati
99580
- }
99573
+ const isReceivedFromTarget = msgData.to === username && msgData.from === otherUserPub;
99574
+ console.log(`[SHIP-01] Message ${messageId} match:`, {
99575
+ isSentToTarget,
99576
+ isReceivedFromTarget,
99577
+ fromMatch: msgData.from === userPub,
99578
+ toMatch: msgData.to === withUsername,
99579
+ receivedToMatch: msgData.to === username,
99580
+ receivedFromMatch: msgData.from === otherUserPub
99581
+ });
99582
+ if (isSentToTarget || isReceivedFromTarget) {
99583
+ // Decrypt message
99584
+ const decryptedContent = await this.decryptMessage(msgData.content, msgData.from);
99585
+ messages.push({
99586
+ from: msgData.from,
99587
+ to: msgData.to,
99588
+ content: decryptedContent,
99589
+ timestamp: parseInt(msgData.timestamp)
99590
+ });
99591
+ console.log(`[SHIP-01] ✅ Decrypted message from ${msgData.from.substring(0, 20)}...`);
99581
99592
  }
99582
99593
  }
99583
- }
99584
- }
99585
- // Ordina per timestamp
99586
- return allMessages.sort((a, b) => a.timestamp - b.timestamp);
99594
+ catch (error) {
99595
+ console.error(`[SHIP-01] ❌ Error processing message ${messageId}:`, error);
99596
+ }
99597
+ });
99598
+ // Wait a bit for GunDB to return all messages, then resolve
99599
+ setTimeout(() => {
99600
+ console.log(`[SHIP-01] Found ${messages.length} messages out of ${messageCount} total`);
99601
+ const sorted = messages.sort((a, b) => a.timestamp - b.timestamp);
99602
+ resolve(sorted);
99603
+ }, 2000);
99604
+ });
99587
99605
  }
99588
99606
  // ========================================================================
99589
99607
  // 7. UTILITY FUNCTIONS