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.
@@ -259,7 +259,7 @@ class SHIP_01 {
259
259
  messageId: messageId
260
260
  };
261
261
  // Salva sul nodo globale 'messages' per permettere il listener
262
- await this.shogun.db.gun.get('messages').get(messageId).put(messageData).then();
262
+ await this.shogun.db.gun.get(SHIP_01.NODES.MESSAGES).get(messageId).put(messageData).then();
263
263
  console.log(`✅ Messaggio salvato: ${messageId}`);
264
264
  return {
265
265
  success: true,
@@ -323,7 +323,7 @@ class SHIP_01 {
323
323
  const receivedMessages = new Set();
324
324
  // Ascolta messaggi in tempo reale su GunDB
325
325
  this.shogun.db.gun
326
- .get(`messages`)
326
+ .get(SHIP_01.NODES.MESSAGES)
327
327
  .map()
328
328
  .on(async (data, key) => {
329
329
  // Filtra solo i messaggi destinati a questo utente (per username)
@@ -408,43 +408,62 @@ class SHIP_01 {
408
408
  const userPub = currentUser.is.pub;
409
409
  const username = currentUser.is.alias;
410
410
  const allMessages = [];
411
+ console.log(`[SHIP-01] Getting history between ${username} (${userPub.substring(0, 20)}...) and ${withUsername}`);
411
412
  // Ottieni il userPub dell'altro utente
412
413
  const otherUserData = await this.shogun.db.getUserByAlias(withUsername);
413
414
  const otherUserPub = otherUserData?.userPub;
414
- // Recupera tutti i messaggi dal nodo 'messages' e filtra
415
- const allMessagesNode = await this.shogun.db.gun.get('messages').then();
416
- if (allMessagesNode) {
417
- for (const [messageId, data] of Object.entries(allMessagesNode)) {
418
- if (typeof data === 'object' && data !== null) {
419
- const msgData = data;
420
- // Filtra messaggi tra questo utente e withUsername
421
- // Messaggi inviati da me a loro
422
- const isSentToTarget = msgData.from === userPub && msgData.to === withUsername;
423
- // Messaggi ricevuti da loro
424
- const isReceivedFromTarget = msgData.to === username &&
425
- (msgData.from === otherUserPub ||
426
- msgData.from);
427
- if ((isSentToTarget || isReceivedFromTarget) && msgData.content && msgData.messageId) {
428
- try {
429
- // Decripta ogni messaggio
430
- const decryptedContent = await this.decryptMessage(msgData.content, msgData.from);
431
- allMessages.push({
432
- from: msgData.from,
433
- to: msgData.to,
434
- content: decryptedContent,
435
- timestamp: parseInt(msgData.timestamp)
436
- });
437
- }
438
- catch (error) {
439
- console.error(`❌ Errore decrittazione messaggio ${messageId}:`, error);
440
- // Salta messaggi che non possono essere decriptati
441
- }
415
+ console.log(`[SHIP-01] Other user pub: ${otherUserPub?.substring(0, 20) || 'not found'}...`);
416
+ // Use .map() instead of .then() to iterate all messages
417
+ return new Promise((resolve) => {
418
+ const messages = [];
419
+ let messageCount = 0;
420
+ this.shogun.db.gun.get(SHIP_01.NODES.MESSAGES).map().once(async (msgData, messageId) => {
421
+ // Skip metadata fields
422
+ if (!msgData || typeof msgData !== 'object' || messageId === '_') {
423
+ return;
424
+ }
425
+ messageCount++;
426
+ console.log(`[SHIP-01] Checking message ${messageId}:`, {
427
+ from: msgData.from?.substring(0, 20),
428
+ to: msgData.to,
429
+ hasContent: !!msgData.content
430
+ });
431
+ try {
432
+ // Check if message is part of this conversation
433
+ // Fixed: Handle both username and userPub in matching
434
+ const isSentToTarget = msgData.from === userPub && (msgData.to === withUsername || msgData.to === otherUserPub);
435
+ const isReceivedFromTarget = (msgData.to === username || msgData.to === userPub) && msgData.from === otherUserPub;
436
+ console.log(`[SHIP-01] Message ${messageId} match:`, {
437
+ isSentToTarget,
438
+ isReceivedFromTarget,
439
+ fromMatch: msgData.from === userPub,
440
+ toMatch: msgData.to === withUsername,
441
+ receivedToMatch: msgData.to === username,
442
+ receivedFromMatch: msgData.from === otherUserPub
443
+ });
444
+ if (isSentToTarget || isReceivedFromTarget) {
445
+ // Decrypt message
446
+ const decryptedContent = await this.decryptMessage(msgData.content, msgData.from);
447
+ messages.push({
448
+ from: msgData.from,
449
+ to: msgData.to,
450
+ content: decryptedContent,
451
+ timestamp: parseInt(msgData.timestamp)
452
+ });
453
+ console.log(`[SHIP-01] ✅ Decrypted message from ${msgData.from.substring(0, 20)}...`);
442
454
  }
443
455
  }
444
- }
445
- }
446
- // Ordina per timestamp
447
- return allMessages.sort((a, b) => a.timestamp - b.timestamp);
456
+ catch (error) {
457
+ console.error(`[SHIP-01] ❌ Error processing message ${messageId}:`, error);
458
+ }
459
+ });
460
+ // Wait a bit for GunDB to return all messages, then resolve
461
+ setTimeout(() => {
462
+ console.log(`[SHIP-01] Found ${messages.length} messages out of ${messageCount} total`);
463
+ const sorted = messages.sort((a, b) => a.timestamp - b.timestamp);
464
+ resolve(sorted);
465
+ }, 2000);
466
+ });
448
467
  }
449
468
  // ========================================================================
450
469
  // 7. UTILITY FUNCTIONS
@@ -490,6 +509,12 @@ class SHIP_01 {
490
509
  }
491
510
  }
492
511
  exports.SHIP_01 = SHIP_01;
512
+ // GunDB Node Names - Constants for clarity
513
+ SHIP_01.NODES = {
514
+ MESSAGES: 'messages',
515
+ USERS: 'users',
516
+ PUBLIC_KEYS: 'publicKeys'
517
+ };
493
518
  // ============================================================================
494
519
  // 8. ESEMPIO D'USO COMPLETO
495
520
  // ============================================================================
@@ -29,6 +29,7 @@ import { ISEAPair } from "../../src/types/shogun";
29
29
  */
30
30
  declare class SHIP_01 implements ISHIP_01 {
31
31
  private shogun;
32
+ private static readonly NODES;
32
33
  constructor(shogunConfig: ShogunCoreConfig);
33
34
  /**
34
35
  * Registra un nuovo utente
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shogun-core",
3
- "version": "3.2.0",
3
+ "version": "3.2.2",
4
4
  "description": "SHOGUN CORE - Core library for Shogun Ecosystem",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",