@unicitylabs/sphere-sdk 0.4.4 → 0.4.6
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.
- package/dist/connect/index.cjs +27 -10
- package/dist/connect/index.cjs.map +1 -1
- package/dist/connect/index.js +27 -10
- package/dist/connect/index.js.map +1 -1
- package/dist/core/index.cjs +72 -25
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +12 -0
- package/dist/core/index.d.ts +12 -0
- package/dist/core/index.js +72 -25
- package/dist/core/index.js.map +1 -1
- package/dist/impl/browser/connect/index.cjs +10 -10
- package/dist/impl/browser/connect/index.cjs.map +1 -1
- package/dist/impl/browser/connect/index.js +10 -10
- package/dist/impl/browser/connect/index.js.map +1 -1
- package/dist/impl/browser/index.cjs +10 -10
- package/dist/impl/browser/index.cjs.map +1 -1
- package/dist/impl/browser/index.js +10 -10
- package/dist/impl/browser/index.js.map +1 -1
- package/dist/impl/browser/ipfs.cjs +10 -10
- package/dist/impl/browser/ipfs.cjs.map +1 -1
- package/dist/impl/browser/ipfs.js +10 -10
- package/dist/impl/browser/ipfs.js.map +1 -1
- package/dist/impl/nodejs/connect/index.cjs +10 -10
- package/dist/impl/nodejs/connect/index.cjs.map +1 -1
- package/dist/impl/nodejs/connect/index.js +10 -10
- package/dist/impl/nodejs/connect/index.js.map +1 -1
- package/dist/impl/nodejs/index.cjs +10 -10
- package/dist/impl/nodejs/index.cjs.map +1 -1
- package/dist/impl/nodejs/index.js +10 -10
- package/dist/impl/nodejs/index.js.map +1 -1
- package/dist/index.cjs +72 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -18
- package/dist/index.d.ts +30 -18
- package/dist/index.js +72 -25
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -3266,6 +3266,7 @@ declare class CommunicationsModule {
|
|
|
3266
3266
|
private dmHandlers;
|
|
3267
3267
|
private composingHandlers;
|
|
3268
3268
|
private broadcastHandlers;
|
|
3269
|
+
private initializedAt;
|
|
3269
3270
|
constructor(config?: CommunicationsModuleConfig);
|
|
3270
3271
|
/**
|
|
3271
3272
|
* Initialize module with dependencies
|
|
@@ -3344,6 +3345,12 @@ declare class CommunicationsModule {
|
|
|
3344
3345
|
* Subscribe to incoming broadcasts
|
|
3345
3346
|
*/
|
|
3346
3347
|
onBroadcast(handler: (message: BroadcastMessage) => void): () => void;
|
|
3348
|
+
/**
|
|
3349
|
+
* Resolve a peer's nametag by their transport pubkey.
|
|
3350
|
+
* Uses transport.resolveTransportPubkeyInfo() for live lookup from relay binding events.
|
|
3351
|
+
* Returns undefined if transport doesn't support resolution or peer has no nametag.
|
|
3352
|
+
*/
|
|
3353
|
+
resolvePeerNametag(peerPubkey: string): Promise<string | undefined>;
|
|
3347
3354
|
private handleIncomingMessage;
|
|
3348
3355
|
private handleComposingIndicator;
|
|
3349
3356
|
private handleIncomingBroadcast;
|
|
@@ -3394,6 +3401,11 @@ declare class GroupChatModule {
|
|
|
3394
3401
|
private destroyConnection;
|
|
3395
3402
|
connect(): Promise<void>;
|
|
3396
3403
|
getConnectionStatus(): boolean;
|
|
3404
|
+
/**
|
|
3405
|
+
* Refresh subscriptions after load() switched to a different address.
|
|
3406
|
+
* Clears old subscriptions, restores groups if needed, and re-subscribes.
|
|
3407
|
+
*/
|
|
3408
|
+
private refreshSubscriptions;
|
|
3397
3409
|
private doConnect;
|
|
3398
3410
|
private scheduleReconnect;
|
|
3399
3411
|
private subscribeToJoinedGroups;
|
|
@@ -3661,15 +3673,7 @@ declare const STORAGE_KEYS_GLOBAL: {
|
|
|
3661
3673
|
readonly TRACKED_ADDRESSES: "tracked_addresses";
|
|
3662
3674
|
/** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
|
|
3663
3675
|
readonly LAST_WALLET_EVENT_TS: "last_wallet_event_ts";
|
|
3664
|
-
/** Group chat:
|
|
3665
|
-
readonly GROUP_CHAT_GROUPS: "group_chat_groups";
|
|
3666
|
-
/** Group chat: messages */
|
|
3667
|
-
readonly GROUP_CHAT_MESSAGES: "group_chat_messages";
|
|
3668
|
-
/** Group chat: members */
|
|
3669
|
-
readonly GROUP_CHAT_MEMBERS: "group_chat_members";
|
|
3670
|
-
/** Group chat: processed event IDs for deduplication */
|
|
3671
|
-
readonly GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events";
|
|
3672
|
-
/** Group chat: last used relay URL (stale data detection) */
|
|
3676
|
+
/** Group chat: last used relay URL (stale data detection) — global, same relay for all addresses */
|
|
3673
3677
|
readonly GROUP_CHAT_RELAY_URL: "group_chat_relay_url";
|
|
3674
3678
|
/** Cached token registry JSON (fetched from remote) */
|
|
3675
3679
|
readonly TOKEN_REGISTRY_CACHE: "token_registry_cache";
|
|
@@ -3701,6 +3705,14 @@ declare const STORAGE_KEYS_ADDRESS: {
|
|
|
3701
3705
|
readonly TRANSACTION_HISTORY: "transaction_history";
|
|
3702
3706
|
/** Pending V5 finalization tokens (unconfirmed instant split tokens) */
|
|
3703
3707
|
readonly PENDING_V5_TOKENS: "pending_v5_tokens";
|
|
3708
|
+
/** Group chat: joined groups for this address */
|
|
3709
|
+
readonly GROUP_CHAT_GROUPS: "group_chat_groups";
|
|
3710
|
+
/** Group chat: messages for this address */
|
|
3711
|
+
readonly GROUP_CHAT_MESSAGES: "group_chat_messages";
|
|
3712
|
+
/** Group chat: members for this address */
|
|
3713
|
+
readonly GROUP_CHAT_MEMBERS: "group_chat_members";
|
|
3714
|
+
/** Group chat: processed event IDs for deduplication */
|
|
3715
|
+
readonly GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events";
|
|
3704
3716
|
};
|
|
3705
3717
|
/** @deprecated Use STORAGE_KEYS_GLOBAL and STORAGE_KEYS_ADDRESS instead */
|
|
3706
3718
|
declare const STORAGE_KEYS: {
|
|
@@ -3716,6 +3728,14 @@ declare const STORAGE_KEYS: {
|
|
|
3716
3728
|
readonly TRANSACTION_HISTORY: "transaction_history";
|
|
3717
3729
|
/** Pending V5 finalization tokens (unconfirmed instant split tokens) */
|
|
3718
3730
|
readonly PENDING_V5_TOKENS: "pending_v5_tokens";
|
|
3731
|
+
/** Group chat: joined groups for this address */
|
|
3732
|
+
readonly GROUP_CHAT_GROUPS: "group_chat_groups";
|
|
3733
|
+
/** Group chat: messages for this address */
|
|
3734
|
+
readonly GROUP_CHAT_MESSAGES: "group_chat_messages";
|
|
3735
|
+
/** Group chat: members for this address */
|
|
3736
|
+
readonly GROUP_CHAT_MEMBERS: "group_chat_members";
|
|
3737
|
+
/** Group chat: processed event IDs for deduplication */
|
|
3738
|
+
readonly GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events";
|
|
3719
3739
|
/** Encrypted BIP39 mnemonic */
|
|
3720
3740
|
readonly MNEMONIC: "mnemonic";
|
|
3721
3741
|
/** Encrypted master private key */
|
|
@@ -3740,15 +3760,7 @@ declare const STORAGE_KEYS: {
|
|
|
3740
3760
|
readonly TRACKED_ADDRESSES: "tracked_addresses";
|
|
3741
3761
|
/** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
|
|
3742
3762
|
readonly LAST_WALLET_EVENT_TS: "last_wallet_event_ts";
|
|
3743
|
-
/** Group chat:
|
|
3744
|
-
readonly GROUP_CHAT_GROUPS: "group_chat_groups";
|
|
3745
|
-
/** Group chat: messages */
|
|
3746
|
-
readonly GROUP_CHAT_MESSAGES: "group_chat_messages";
|
|
3747
|
-
/** Group chat: members */
|
|
3748
|
-
readonly GROUP_CHAT_MEMBERS: "group_chat_members";
|
|
3749
|
-
/** Group chat: processed event IDs for deduplication */
|
|
3750
|
-
readonly GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events";
|
|
3751
|
-
/** Group chat: last used relay URL (stale data detection) */
|
|
3763
|
+
/** Group chat: last used relay URL (stale data detection) — global, same relay for all addresses */
|
|
3752
3764
|
readonly GROUP_CHAT_RELAY_URL: "group_chat_relay_url";
|
|
3753
3765
|
/** Cached token registry JSON (fetched from remote) */
|
|
3754
3766
|
readonly TOKEN_REGISTRY_CACHE: "token_registry_cache";
|
package/dist/index.d.ts
CHANGED
|
@@ -3266,6 +3266,7 @@ declare class CommunicationsModule {
|
|
|
3266
3266
|
private dmHandlers;
|
|
3267
3267
|
private composingHandlers;
|
|
3268
3268
|
private broadcastHandlers;
|
|
3269
|
+
private initializedAt;
|
|
3269
3270
|
constructor(config?: CommunicationsModuleConfig);
|
|
3270
3271
|
/**
|
|
3271
3272
|
* Initialize module with dependencies
|
|
@@ -3344,6 +3345,12 @@ declare class CommunicationsModule {
|
|
|
3344
3345
|
* Subscribe to incoming broadcasts
|
|
3345
3346
|
*/
|
|
3346
3347
|
onBroadcast(handler: (message: BroadcastMessage) => void): () => void;
|
|
3348
|
+
/**
|
|
3349
|
+
* Resolve a peer's nametag by their transport pubkey.
|
|
3350
|
+
* Uses transport.resolveTransportPubkeyInfo() for live lookup from relay binding events.
|
|
3351
|
+
* Returns undefined if transport doesn't support resolution or peer has no nametag.
|
|
3352
|
+
*/
|
|
3353
|
+
resolvePeerNametag(peerPubkey: string): Promise<string | undefined>;
|
|
3347
3354
|
private handleIncomingMessage;
|
|
3348
3355
|
private handleComposingIndicator;
|
|
3349
3356
|
private handleIncomingBroadcast;
|
|
@@ -3394,6 +3401,11 @@ declare class GroupChatModule {
|
|
|
3394
3401
|
private destroyConnection;
|
|
3395
3402
|
connect(): Promise<void>;
|
|
3396
3403
|
getConnectionStatus(): boolean;
|
|
3404
|
+
/**
|
|
3405
|
+
* Refresh subscriptions after load() switched to a different address.
|
|
3406
|
+
* Clears old subscriptions, restores groups if needed, and re-subscribes.
|
|
3407
|
+
*/
|
|
3408
|
+
private refreshSubscriptions;
|
|
3397
3409
|
private doConnect;
|
|
3398
3410
|
private scheduleReconnect;
|
|
3399
3411
|
private subscribeToJoinedGroups;
|
|
@@ -3661,15 +3673,7 @@ declare const STORAGE_KEYS_GLOBAL: {
|
|
|
3661
3673
|
readonly TRACKED_ADDRESSES: "tracked_addresses";
|
|
3662
3674
|
/** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
|
|
3663
3675
|
readonly LAST_WALLET_EVENT_TS: "last_wallet_event_ts";
|
|
3664
|
-
/** Group chat:
|
|
3665
|
-
readonly GROUP_CHAT_GROUPS: "group_chat_groups";
|
|
3666
|
-
/** Group chat: messages */
|
|
3667
|
-
readonly GROUP_CHAT_MESSAGES: "group_chat_messages";
|
|
3668
|
-
/** Group chat: members */
|
|
3669
|
-
readonly GROUP_CHAT_MEMBERS: "group_chat_members";
|
|
3670
|
-
/** Group chat: processed event IDs for deduplication */
|
|
3671
|
-
readonly GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events";
|
|
3672
|
-
/** Group chat: last used relay URL (stale data detection) */
|
|
3676
|
+
/** Group chat: last used relay URL (stale data detection) — global, same relay for all addresses */
|
|
3673
3677
|
readonly GROUP_CHAT_RELAY_URL: "group_chat_relay_url";
|
|
3674
3678
|
/** Cached token registry JSON (fetched from remote) */
|
|
3675
3679
|
readonly TOKEN_REGISTRY_CACHE: "token_registry_cache";
|
|
@@ -3701,6 +3705,14 @@ declare const STORAGE_KEYS_ADDRESS: {
|
|
|
3701
3705
|
readonly TRANSACTION_HISTORY: "transaction_history";
|
|
3702
3706
|
/** Pending V5 finalization tokens (unconfirmed instant split tokens) */
|
|
3703
3707
|
readonly PENDING_V5_TOKENS: "pending_v5_tokens";
|
|
3708
|
+
/** Group chat: joined groups for this address */
|
|
3709
|
+
readonly GROUP_CHAT_GROUPS: "group_chat_groups";
|
|
3710
|
+
/** Group chat: messages for this address */
|
|
3711
|
+
readonly GROUP_CHAT_MESSAGES: "group_chat_messages";
|
|
3712
|
+
/** Group chat: members for this address */
|
|
3713
|
+
readonly GROUP_CHAT_MEMBERS: "group_chat_members";
|
|
3714
|
+
/** Group chat: processed event IDs for deduplication */
|
|
3715
|
+
readonly GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events";
|
|
3704
3716
|
};
|
|
3705
3717
|
/** @deprecated Use STORAGE_KEYS_GLOBAL and STORAGE_KEYS_ADDRESS instead */
|
|
3706
3718
|
declare const STORAGE_KEYS: {
|
|
@@ -3716,6 +3728,14 @@ declare const STORAGE_KEYS: {
|
|
|
3716
3728
|
readonly TRANSACTION_HISTORY: "transaction_history";
|
|
3717
3729
|
/** Pending V5 finalization tokens (unconfirmed instant split tokens) */
|
|
3718
3730
|
readonly PENDING_V5_TOKENS: "pending_v5_tokens";
|
|
3731
|
+
/** Group chat: joined groups for this address */
|
|
3732
|
+
readonly GROUP_CHAT_GROUPS: "group_chat_groups";
|
|
3733
|
+
/** Group chat: messages for this address */
|
|
3734
|
+
readonly GROUP_CHAT_MESSAGES: "group_chat_messages";
|
|
3735
|
+
/** Group chat: members for this address */
|
|
3736
|
+
readonly GROUP_CHAT_MEMBERS: "group_chat_members";
|
|
3737
|
+
/** Group chat: processed event IDs for deduplication */
|
|
3738
|
+
readonly GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events";
|
|
3719
3739
|
/** Encrypted BIP39 mnemonic */
|
|
3720
3740
|
readonly MNEMONIC: "mnemonic";
|
|
3721
3741
|
/** Encrypted master private key */
|
|
@@ -3740,15 +3760,7 @@ declare const STORAGE_KEYS: {
|
|
|
3740
3760
|
readonly TRACKED_ADDRESSES: "tracked_addresses";
|
|
3741
3761
|
/** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
|
|
3742
3762
|
readonly LAST_WALLET_EVENT_TS: "last_wallet_event_ts";
|
|
3743
|
-
/** Group chat:
|
|
3744
|
-
readonly GROUP_CHAT_GROUPS: "group_chat_groups";
|
|
3745
|
-
/** Group chat: messages */
|
|
3746
|
-
readonly GROUP_CHAT_MESSAGES: "group_chat_messages";
|
|
3747
|
-
/** Group chat: members */
|
|
3748
|
-
readonly GROUP_CHAT_MEMBERS: "group_chat_members";
|
|
3749
|
-
/** Group chat: processed event IDs for deduplication */
|
|
3750
|
-
readonly GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events";
|
|
3751
|
-
/** Group chat: last used relay URL (stale data detection) */
|
|
3763
|
+
/** Group chat: last used relay URL (stale data detection) — global, same relay for all addresses */
|
|
3752
3764
|
readonly GROUP_CHAT_RELAY_URL: "group_chat_relay_url";
|
|
3753
3765
|
/** Cached token registry JSON (fetched from remote) */
|
|
3754
3766
|
readonly TOKEN_REGISTRY_CACHE: "token_registry_cache";
|
package/dist/index.js
CHANGED
|
@@ -2503,15 +2503,7 @@ var STORAGE_KEYS_GLOBAL = {
|
|
|
2503
2503
|
TRACKED_ADDRESSES: "tracked_addresses",
|
|
2504
2504
|
/** Last processed Nostr wallet event timestamp (unix seconds), keyed per pubkey */
|
|
2505
2505
|
LAST_WALLET_EVENT_TS: "last_wallet_event_ts",
|
|
2506
|
-
/** Group chat:
|
|
2507
|
-
GROUP_CHAT_GROUPS: "group_chat_groups",
|
|
2508
|
-
/** Group chat: messages */
|
|
2509
|
-
GROUP_CHAT_MESSAGES: "group_chat_messages",
|
|
2510
|
-
/** Group chat: members */
|
|
2511
|
-
GROUP_CHAT_MEMBERS: "group_chat_members",
|
|
2512
|
-
/** Group chat: processed event IDs for deduplication */
|
|
2513
|
-
GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events",
|
|
2514
|
-
/** Group chat: last used relay URL (stale data detection) */
|
|
2506
|
+
/** Group chat: last used relay URL (stale data detection) — global, same relay for all addresses */
|
|
2515
2507
|
GROUP_CHAT_RELAY_URL: "group_chat_relay_url",
|
|
2516
2508
|
/** Cached token registry JSON (fetched from remote) */
|
|
2517
2509
|
TOKEN_REGISTRY_CACHE: "token_registry_cache",
|
|
@@ -2534,7 +2526,15 @@ var STORAGE_KEYS_ADDRESS = {
|
|
|
2534
2526
|
/** Transaction history for this address */
|
|
2535
2527
|
TRANSACTION_HISTORY: "transaction_history",
|
|
2536
2528
|
/** Pending V5 finalization tokens (unconfirmed instant split tokens) */
|
|
2537
|
-
PENDING_V5_TOKENS: "pending_v5_tokens"
|
|
2529
|
+
PENDING_V5_TOKENS: "pending_v5_tokens",
|
|
2530
|
+
/** Group chat: joined groups for this address */
|
|
2531
|
+
GROUP_CHAT_GROUPS: "group_chat_groups",
|
|
2532
|
+
/** Group chat: messages for this address */
|
|
2533
|
+
GROUP_CHAT_MESSAGES: "group_chat_messages",
|
|
2534
|
+
/** Group chat: members for this address */
|
|
2535
|
+
GROUP_CHAT_MEMBERS: "group_chat_members",
|
|
2536
|
+
/** Group chat: processed event IDs for deduplication */
|
|
2537
|
+
GROUP_CHAT_PROCESSED_EVENTS: "group_chat_processed_events"
|
|
2538
2538
|
};
|
|
2539
2539
|
var STORAGE_KEYS = {
|
|
2540
2540
|
...STORAGE_KEYS_GLOBAL,
|
|
@@ -7645,6 +7645,8 @@ var CommunicationsModule = class {
|
|
|
7645
7645
|
dmHandlers = /* @__PURE__ */ new Set();
|
|
7646
7646
|
composingHandlers = /* @__PURE__ */ new Set();
|
|
7647
7647
|
broadcastHandlers = /* @__PURE__ */ new Set();
|
|
7648
|
+
// Timestamp of module initialization — messages older than this are historical
|
|
7649
|
+
initializedAt = 0;
|
|
7648
7650
|
constructor(config) {
|
|
7649
7651
|
this.config = {
|
|
7650
7652
|
autoSave: config?.autoSave ?? true,
|
|
@@ -7663,6 +7665,7 @@ var CommunicationsModule = class {
|
|
|
7663
7665
|
this.unsubscribeMessages?.();
|
|
7664
7666
|
this.unsubscribeComposing?.();
|
|
7665
7667
|
this.deps = deps;
|
|
7668
|
+
this.initializedAt = Date.now();
|
|
7666
7669
|
this.unsubscribeMessages = deps.transport.onMessage((msg) => {
|
|
7667
7670
|
this.handleIncomingMessage(msg);
|
|
7668
7671
|
});
|
|
@@ -7951,9 +7954,27 @@ var CommunicationsModule = class {
|
|
|
7951
7954
|
return () => this.broadcastHandlers.delete(handler);
|
|
7952
7955
|
}
|
|
7953
7956
|
// ===========================================================================
|
|
7957
|
+
// Public API - Peer Resolution
|
|
7958
|
+
// ===========================================================================
|
|
7959
|
+
/**
|
|
7960
|
+
* Resolve a peer's nametag by their transport pubkey.
|
|
7961
|
+
* Uses transport.resolveTransportPubkeyInfo() for live lookup from relay binding events.
|
|
7962
|
+
* Returns undefined if transport doesn't support resolution or peer has no nametag.
|
|
7963
|
+
*/
|
|
7964
|
+
async resolvePeerNametag(peerPubkey) {
|
|
7965
|
+
if (!this.deps?.transport.resolveTransportPubkeyInfo) return void 0;
|
|
7966
|
+
try {
|
|
7967
|
+
const info = await this.deps.transport.resolveTransportPubkeyInfo(peerPubkey);
|
|
7968
|
+
return info?.nametag;
|
|
7969
|
+
} catch {
|
|
7970
|
+
return void 0;
|
|
7971
|
+
}
|
|
7972
|
+
}
|
|
7973
|
+
// ===========================================================================
|
|
7954
7974
|
// Private: Message Handling
|
|
7955
7975
|
// ===========================================================================
|
|
7956
7976
|
handleIncomingMessage(msg) {
|
|
7977
|
+
const isHistorical = msg.timestamp < this.initializedAt;
|
|
7957
7978
|
if (msg.isSelfWrap && msg.recipientTransportPubkey) {
|
|
7958
7979
|
if (this.messages.has(msg.id)) return;
|
|
7959
7980
|
const message2 = {
|
|
@@ -7963,7 +7984,7 @@ var CommunicationsModule = class {
|
|
|
7963
7984
|
recipientPubkey: msg.recipientTransportPubkey,
|
|
7964
7985
|
content: msg.content,
|
|
7965
7986
|
timestamp: msg.timestamp,
|
|
7966
|
-
isRead:
|
|
7987
|
+
isRead: isHistorical
|
|
7967
7988
|
};
|
|
7968
7989
|
this.messages.set(message2.id, message2);
|
|
7969
7990
|
this.deps.emitEvent("message:dm", message2);
|
|
@@ -7981,7 +8002,7 @@ var CommunicationsModule = class {
|
|
|
7981
8002
|
recipientPubkey: this.deps.identity.chainPubkey,
|
|
7982
8003
|
content: msg.content,
|
|
7983
8004
|
timestamp: msg.timestamp,
|
|
7984
|
-
isRead:
|
|
8005
|
+
isRead: isHistorical
|
|
7985
8006
|
};
|
|
7986
8007
|
this.messages.set(message.id, message);
|
|
7987
8008
|
this.deps.emitEvent("message:dm", message);
|
|
@@ -8157,22 +8178,24 @@ var GroupChatModule = class {
|
|
|
8157
8178
|
async load() {
|
|
8158
8179
|
this.ensureInitialized();
|
|
8159
8180
|
const storage = this.deps.storage;
|
|
8160
|
-
|
|
8181
|
+
this.groups.clear();
|
|
8182
|
+
this.messages.clear();
|
|
8183
|
+
this.members.clear();
|
|
8184
|
+
this.processedEventIds.clear();
|
|
8185
|
+
const groupsJson = await storage.get(STORAGE_KEYS_ADDRESS.GROUP_CHAT_GROUPS);
|
|
8161
8186
|
if (groupsJson) {
|
|
8162
8187
|
try {
|
|
8163
8188
|
const parsed = JSON.parse(groupsJson);
|
|
8164
|
-
this.groups.clear();
|
|
8165
8189
|
for (const g of parsed) {
|
|
8166
8190
|
this.groups.set(g.id, g);
|
|
8167
8191
|
}
|
|
8168
8192
|
} catch {
|
|
8169
8193
|
}
|
|
8170
8194
|
}
|
|
8171
|
-
const messagesJson = await storage.get(
|
|
8195
|
+
const messagesJson = await storage.get(STORAGE_KEYS_ADDRESS.GROUP_CHAT_MESSAGES);
|
|
8172
8196
|
if (messagesJson) {
|
|
8173
8197
|
try {
|
|
8174
8198
|
const parsed = JSON.parse(messagesJson);
|
|
8175
|
-
this.messages.clear();
|
|
8176
8199
|
for (const m of parsed) {
|
|
8177
8200
|
const groupId = m.groupId;
|
|
8178
8201
|
if (!this.messages.has(groupId)) {
|
|
@@ -8183,11 +8206,10 @@ var GroupChatModule = class {
|
|
|
8183
8206
|
} catch {
|
|
8184
8207
|
}
|
|
8185
8208
|
}
|
|
8186
|
-
const membersJson = await storage.get(
|
|
8209
|
+
const membersJson = await storage.get(STORAGE_KEYS_ADDRESS.GROUP_CHAT_MEMBERS);
|
|
8187
8210
|
if (membersJson) {
|
|
8188
8211
|
try {
|
|
8189
8212
|
const parsed = JSON.parse(membersJson);
|
|
8190
|
-
this.members.clear();
|
|
8191
8213
|
for (const m of parsed) {
|
|
8192
8214
|
const groupId = m.groupId;
|
|
8193
8215
|
if (!this.members.has(groupId)) {
|
|
@@ -8198,7 +8220,7 @@ var GroupChatModule = class {
|
|
|
8198
8220
|
} catch {
|
|
8199
8221
|
}
|
|
8200
8222
|
}
|
|
8201
|
-
const processedJson = await storage.get(
|
|
8223
|
+
const processedJson = await storage.get(STORAGE_KEYS_ADDRESS.GROUP_CHAT_PROCESSED_EVENTS);
|
|
8202
8224
|
if (processedJson) {
|
|
8203
8225
|
try {
|
|
8204
8226
|
const parsed = JSON.parse(processedJson);
|
|
@@ -8252,7 +8274,10 @@ var GroupChatModule = class {
|
|
|
8252
8274
|
// Connection
|
|
8253
8275
|
// ===========================================================================
|
|
8254
8276
|
async connect() {
|
|
8255
|
-
if (this.connected)
|
|
8277
|
+
if (this.connected) {
|
|
8278
|
+
await this.refreshSubscriptions();
|
|
8279
|
+
return;
|
|
8280
|
+
}
|
|
8256
8281
|
if (this.connectPromise) {
|
|
8257
8282
|
return this.connectPromise;
|
|
8258
8283
|
}
|
|
@@ -8266,6 +8291,28 @@ var GroupChatModule = class {
|
|
|
8266
8291
|
getConnectionStatus() {
|
|
8267
8292
|
return this.connected;
|
|
8268
8293
|
}
|
|
8294
|
+
/**
|
|
8295
|
+
* Refresh subscriptions after load() switched to a different address.
|
|
8296
|
+
* Clears old subscriptions, restores groups if needed, and re-subscribes.
|
|
8297
|
+
*/
|
|
8298
|
+
async refreshSubscriptions() {
|
|
8299
|
+
if (!this.client) return;
|
|
8300
|
+
for (const subId of this.subscriptionIds) {
|
|
8301
|
+
try {
|
|
8302
|
+
this.client.unsubscribe(subId);
|
|
8303
|
+
} catch {
|
|
8304
|
+
}
|
|
8305
|
+
}
|
|
8306
|
+
this.subscriptionIds = [];
|
|
8307
|
+
const secretKey = Buffer.from(this.deps.identity.privateKey, "hex");
|
|
8308
|
+
this.keyManager = NostrKeyManager.fromPrivateKey(secretKey);
|
|
8309
|
+
if (this.groups.size === 0) {
|
|
8310
|
+
await this.restoreJoinedGroups();
|
|
8311
|
+
} else {
|
|
8312
|
+
await this.subscribeToJoinedGroups();
|
|
8313
|
+
}
|
|
8314
|
+
this.deps.emitEvent("groupchat:connection", { connected: true });
|
|
8315
|
+
}
|
|
8269
8316
|
async doConnect() {
|
|
8270
8317
|
this.ensureInitialized();
|
|
8271
8318
|
if (!this.keyManager) {
|
|
@@ -8281,12 +8328,12 @@ var GroupChatModule = class {
|
|
|
8281
8328
|
await this.client.connect(...this.config.relays);
|
|
8282
8329
|
this.connected = true;
|
|
8283
8330
|
this.reconnectAttempts = 0;
|
|
8284
|
-
this.deps.emitEvent("groupchat:connection", { connected: true });
|
|
8285
8331
|
if (this.groups.size === 0) {
|
|
8286
8332
|
await this.restoreJoinedGroups();
|
|
8287
8333
|
} else {
|
|
8288
8334
|
await this.subscribeToJoinedGroups();
|
|
8289
8335
|
}
|
|
8336
|
+
this.deps.emitEvent("groupchat:connection", { connected: true });
|
|
8290
8337
|
} catch (error) {
|
|
8291
8338
|
console.error("[GroupChat] Failed to connect to relays", error);
|
|
8292
8339
|
this.deps.emitEvent("groupchat:connection", { connected: false });
|
|
@@ -9269,7 +9316,7 @@ var GroupChatModule = class {
|
|
|
9269
9316
|
async persistGroups() {
|
|
9270
9317
|
if (!this.deps) return;
|
|
9271
9318
|
const data = Array.from(this.groups.values());
|
|
9272
|
-
await this.deps.storage.set(
|
|
9319
|
+
await this.deps.storage.set(STORAGE_KEYS_ADDRESS.GROUP_CHAT_GROUPS, JSON.stringify(data));
|
|
9273
9320
|
}
|
|
9274
9321
|
async persistMessages() {
|
|
9275
9322
|
if (!this.deps) return;
|
|
@@ -9277,7 +9324,7 @@ var GroupChatModule = class {
|
|
|
9277
9324
|
for (const msgs of this.messages.values()) {
|
|
9278
9325
|
allMessages.push(...msgs);
|
|
9279
9326
|
}
|
|
9280
|
-
await this.deps.storage.set(
|
|
9327
|
+
await this.deps.storage.set(STORAGE_KEYS_ADDRESS.GROUP_CHAT_MESSAGES, JSON.stringify(allMessages));
|
|
9281
9328
|
}
|
|
9282
9329
|
async persistMembers() {
|
|
9283
9330
|
if (!this.deps) return;
|
|
@@ -9285,12 +9332,12 @@ var GroupChatModule = class {
|
|
|
9285
9332
|
for (const mems of this.members.values()) {
|
|
9286
9333
|
allMembers.push(...mems);
|
|
9287
9334
|
}
|
|
9288
|
-
await this.deps.storage.set(
|
|
9335
|
+
await this.deps.storage.set(STORAGE_KEYS_ADDRESS.GROUP_CHAT_MEMBERS, JSON.stringify(allMembers));
|
|
9289
9336
|
}
|
|
9290
9337
|
async persistProcessedEvents() {
|
|
9291
9338
|
if (!this.deps) return;
|
|
9292
9339
|
const arr = Array.from(this.processedEventIds);
|
|
9293
|
-
await this.deps.storage.set(
|
|
9340
|
+
await this.deps.storage.set(STORAGE_KEYS_ADDRESS.GROUP_CHAT_PROCESSED_EVENTS, JSON.stringify(arr));
|
|
9294
9341
|
}
|
|
9295
9342
|
// ===========================================================================
|
|
9296
9343
|
// Private — Relay URL Change Detection
|