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(
|
|
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(
|
|
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
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
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
|
-
|
|
447
|
-
|
|
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
|
// ============================================================================
|