shogun-core 3.2.0 → 3.2.2

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.
@@ -99398,7 +99398,7 @@ class SHIP_01 {
99398
99398
  messageId: messageId
99399
99399
  };
99400
99400
  // Salva sul nodo globale 'messages' per permettere il listener
99401
- await this.shogun.db.gun.get('messages').get(messageId).put(messageData).then();
99401
+ await this.shogun.db.gun.get(SHIP_01.NODES.MESSAGES).get(messageId).put(messageData).then();
99402
99402
  console.log(`✅ Messaggio salvato: ${messageId}`);
99403
99403
  return {
99404
99404
  success: true,
@@ -99462,7 +99462,7 @@ class SHIP_01 {
99462
99462
  const receivedMessages = new Set();
99463
99463
  // Ascolta messaggi in tempo reale su GunDB
99464
99464
  this.shogun.db.gun
99465
- .get(`messages`)
99465
+ .get(SHIP_01.NODES.MESSAGES)
99466
99466
  .map()
99467
99467
  .on(async (data, key) => {
99468
99468
  // Filtra solo i messaggi destinati a questo utente (per username)
@@ -99547,43 +99547,62 @@ 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
99561
- 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
- }
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(SHIP_01.NODES.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
99572
+ // Fixed: Handle both username and userPub in matching
99573
+ const isSentToTarget = msgData.from === userPub && (msgData.to === withUsername || msgData.to === otherUserPub);
99574
+ const isReceivedFromTarget = (msgData.to === username || msgData.to === userPub) && msgData.from === otherUserPub;
99575
+ console.log(`[SHIP-01] Message ${messageId} match:`, {
99576
+ isSentToTarget,
99577
+ isReceivedFromTarget,
99578
+ fromMatch: msgData.from === userPub,
99579
+ toMatch: msgData.to === withUsername,
99580
+ receivedToMatch: msgData.to === username,
99581
+ receivedFromMatch: msgData.from === otherUserPub
99582
+ });
99583
+ if (isSentToTarget || isReceivedFromTarget) {
99584
+ // Decrypt message
99585
+ const decryptedContent = await this.decryptMessage(msgData.content, msgData.from);
99586
+ messages.push({
99587
+ from: msgData.from,
99588
+ to: msgData.to,
99589
+ content: decryptedContent,
99590
+ timestamp: parseInt(msgData.timestamp)
99591
+ });
99592
+ console.log(`[SHIP-01] ✅ Decrypted message from ${msgData.from.substring(0, 20)}...`);
99581
99593
  }
99582
99594
  }
99583
- }
99584
- }
99585
- // Ordina per timestamp
99586
- return allMessages.sort((a, b) => a.timestamp - b.timestamp);
99595
+ catch (error) {
99596
+ console.error(`[SHIP-01] ❌ Error processing message ${messageId}:`, error);
99597
+ }
99598
+ });
99599
+ // Wait a bit for GunDB to return all messages, then resolve
99600
+ setTimeout(() => {
99601
+ console.log(`[SHIP-01] Found ${messages.length} messages out of ${messageCount} total`);
99602
+ const sorted = messages.sort((a, b) => a.timestamp - b.timestamp);
99603
+ resolve(sorted);
99604
+ }, 2000);
99605
+ });
99587
99606
  }
99588
99607
  // ========================================================================
99589
99608
  // 7. UTILITY FUNCTIONS
@@ -99629,6 +99648,12 @@ class SHIP_01 {
99629
99648
  }
99630
99649
  }
99631
99650
  exports.SHIP_01 = SHIP_01;
99651
+ // GunDB Node Names - Constants for clarity
99652
+ SHIP_01.NODES = {
99653
+ MESSAGES: 'messages',
99654
+ USERS: 'users',
99655
+ PUBLIC_KEYS: 'publicKeys'
99656
+ };
99632
99657
  // ============================================================================
99633
99658
  // 8. ESEMPIO D'USO COMPLETO
99634
99659
  // ============================================================================