@unicitylabs/sphere-sdk 0.6.10-dev.3 → 0.6.10-dev.4

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.
@@ -3269,7 +3269,7 @@ declare class GroupChatModule {
3269
3269
  constructor(config?: GroupChatModuleConfig);
3270
3270
  initialize(deps: GroupChatModuleDependencies): void;
3271
3271
  load(): Promise<void>;
3272
- destroy(): Promise<void>;
3272
+ destroy(): void;
3273
3273
  private destroyConnection;
3274
3274
  connect(): Promise<void>;
3275
3275
  getConnectionStatus(): boolean;
@@ -3330,7 +3330,7 @@ declare class GroupChatModule {
3330
3330
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
3331
3331
  * only sends events we don't already have.
3332
3332
  */
3333
- private getLatestKnownTimestamp;
3333
+ private getLatestMessageTimestamp;
3334
3334
  private fetchRelayAdmins;
3335
3335
  private doFetchRelayAdmins;
3336
3336
  private fetchGroupMetadataInternal;
@@ -3269,7 +3269,7 @@ declare class GroupChatModule {
3269
3269
  constructor(config?: GroupChatModuleConfig);
3270
3270
  initialize(deps: GroupChatModuleDependencies): void;
3271
3271
  load(): Promise<void>;
3272
- destroy(): Promise<void>;
3272
+ destroy(): void;
3273
3273
  private destroyConnection;
3274
3274
  connect(): Promise<void>;
3275
3275
  getConnectionStatus(): boolean;
@@ -3330,7 +3330,7 @@ declare class GroupChatModule {
3330
3330
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
3331
3331
  * only sends events we don't already have.
3332
3332
  */
3333
- private getLatestKnownTimestamp;
3333
+ private getLatestMessageTimestamp;
3334
3334
  private fetchRelayAdmins;
3335
3335
  private doFetchRelayAdmins;
3336
3336
  private fetchGroupMetadataInternal;
@@ -2434,9 +2434,9 @@ var NostrTransportProvider = class _NostrTransportProvider {
2434
2434
  if (subId) {
2435
2435
  this.nostrClient?.unsubscribe(subId);
2436
2436
  }
2437
- logger.warn("Nostr", `queryEvents timed out after 15s, returning ${events.length} event(s)`, { kinds: filterObj.kinds, limit: filterObj.limit });
2437
+ logger.warn("Nostr", `queryEvents timed out after 5s, returning ${events.length} event(s)`, { kinds: filterObj.kinds, limit: filterObj.limit });
2438
2438
  resolve(events);
2439
- }, 15e3);
2439
+ }, 5e3);
2440
2440
  const subId = this.nostrClient.subscribe(filter, {
2441
2441
  onEvent: (event) => {
2442
2442
  events.push({
@@ -12112,20 +12112,8 @@ var GroupChatModule = class {
12112
12112
  }
12113
12113
  }
12114
12114
  }
12115
- async destroy() {
12115
+ destroy() {
12116
12116
  this.destroyConnection();
12117
- if (this.persistTimer) {
12118
- clearTimeout(this.persistTimer);
12119
- this.persistTimer = null;
12120
- }
12121
- if (this.deps) {
12122
- try {
12123
- if (this.persistPromise) await this.persistPromise;
12124
- await this.doPersistAll();
12125
- } catch (err) {
12126
- logger.debug("GroupChat", "Persist on destroy failed", err);
12127
- }
12128
- }
12129
12117
  this.groups.clear();
12130
12118
  this.messages.clear();
12131
12119
  this.members.clear();
@@ -12134,7 +12122,10 @@ var GroupChatModule = class {
12134
12122
  this.messageHandlers.clear();
12135
12123
  this.relayAdminPubkeys = null;
12136
12124
  this.relayAdminFetchPromise = null;
12137
- this.persistPromise = null;
12125
+ if (this.persistTimer) {
12126
+ clearTimeout(this.persistTimer);
12127
+ this.persistTimer = null;
12128
+ }
12138
12129
  this.deps = null;
12139
12130
  }
12140
12131
  destroyConnection() {
@@ -12259,12 +12250,12 @@ var GroupChatModule = class {
12259
12250
  if (!this.client) return;
12260
12251
  const groupIds = Array.from(this.groups.keys());
12261
12252
  if (groupIds.length === 0) return;
12262
- const sinceTimestamp = this.getLatestKnownTimestamp(groupIds);
12253
+ const latestTimestamp = this.getLatestMessageTimestamp(groupIds);
12263
12254
  this.trackSubscription(
12264
12255
  createNip29Filter({
12265
12256
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12266
12257
  "#h": groupIds,
12267
- ...sinceTimestamp ? { since: sinceTimestamp } : {}
12258
+ ...latestTimestamp ? { since: latestTimestamp } : {}
12268
12259
  }),
12269
12260
  { onEvent: (event) => this.handleGroupEvent(event) }
12270
12261
  );
@@ -12285,12 +12276,12 @@ var GroupChatModule = class {
12285
12276
  }
12286
12277
  subscribeToGroup(groupId) {
12287
12278
  if (!this.client) return;
12288
- const sinceTimestamp = this.getLatestKnownTimestamp([groupId]);
12279
+ const latestTimestamp = this.getLatestMessageTimestamp([groupId]);
12289
12280
  this.trackSubscription(
12290
12281
  createNip29Filter({
12291
12282
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12292
12283
  "#h": [groupId],
12293
- ...sinceTimestamp ? { since: sinceTimestamp } : {}
12284
+ ...latestTimestamp ? { since: latestTimestamp } : {}
12294
12285
  }),
12295
12286
  { onEvent: (event) => this.handleGroupEvent(event) }
12296
12287
  );
@@ -12364,7 +12355,7 @@ var GroupChatModule = class {
12364
12355
  }
12365
12356
  group.updatedAt = event.created_at * 1e3;
12366
12357
  this.groups.set(groupId, group);
12367
- this.schedulePersist();
12358
+ this.persistGroups();
12368
12359
  } else if (event.kind === NIP29_KINDS.GROUP_MEMBERS) {
12369
12360
  this.updateMembersFromEvent(groupId, event);
12370
12361
  } else if (event.kind === NIP29_KINDS.GROUP_ADMINS) {
@@ -12385,7 +12376,7 @@ var GroupChatModule = class {
12385
12376
  }
12386
12377
  }
12387
12378
  this.deps.emitEvent("groupchat:updated", {});
12388
- this.schedulePersist();
12379
+ this.persistMessages();
12389
12380
  } else if (event.kind === NIP29_KINDS.REMOVE_USER) {
12390
12381
  if (this.processedEventIds.has(event.id)) return;
12391
12382
  const eventTimestampMs = event.created_at * 1e3;
@@ -12446,7 +12437,7 @@ var GroupChatModule = class {
12446
12437
  };
12447
12438
  this.saveMemberToMemory(member);
12448
12439
  }
12449
- this.schedulePersist();
12440
+ this.persistMembers();
12450
12441
  }
12451
12442
  updateAdminsFromEvent(groupId, event) {
12452
12443
  const pTags = event.tags.filter((t) => t[0] === "p");
@@ -12466,7 +12457,7 @@ var GroupChatModule = class {
12466
12457
  });
12467
12458
  }
12468
12459
  }
12469
- this.schedulePersist();
12460
+ this.persistMembers();
12470
12461
  }
12471
12462
  // ===========================================================================
12472
12463
  // Group Membership Restoration
@@ -12477,11 +12468,13 @@ var GroupChatModule = class {
12477
12468
  if (!myPubkey) return [];
12478
12469
  const groupIdsWithMembership = /* @__PURE__ */ new Set();
12479
12470
  await this.oneshotSubscription(
12480
- createNip29Filter({ kinds: [NIP29_KINDS.GROUP_MEMBERS], "#p": [myPubkey] }),
12471
+ new Filter3({ kinds: [NIP29_KINDS.GROUP_MEMBERS] }),
12481
12472
  {
12482
12473
  onEvent: (event) => {
12483
12474
  const groupId = this.getGroupIdFromMetadataEvent(event);
12484
- if (groupId) {
12475
+ if (!groupId) return;
12476
+ const pTags = event.tags.filter((t) => t[0] === "p");
12477
+ if (pTags.some((tag) => tag[1] === myPubkey)) {
12485
12478
  groupIdsWithMembership.add(groupId);
12486
12479
  }
12487
12480
  },
@@ -12493,24 +12486,22 @@ var GroupChatModule = class {
12493
12486
  );
12494
12487
  if (groupIdsWithMembership.size === 0) return [];
12495
12488
  const restoredGroups = [];
12496
- await Promise.all(
12497
- Array.from(groupIdsWithMembership).map(async (groupId) => {
12498
- if (this.groups.has(groupId)) return;
12499
- try {
12500
- const group = await this.fetchGroupMetadataInternal(groupId);
12501
- if (group) {
12502
- this.groups.set(groupId, group);
12503
- restoredGroups.push(group);
12504
- await Promise.all([
12505
- this.fetchAndSaveMembers(groupId),
12506
- this.fetchMessages(groupId)
12507
- ]);
12508
- }
12509
- } catch (error) {
12510
- logger.warn("GroupChat", "Failed to restore group", groupId, error);
12489
+ for (const groupId of groupIdsWithMembership) {
12490
+ if (this.groups.has(groupId)) continue;
12491
+ try {
12492
+ const group = await this.fetchGroupMetadataInternal(groupId);
12493
+ if (group) {
12494
+ this.groups.set(groupId, group);
12495
+ restoredGroups.push(group);
12496
+ await Promise.all([
12497
+ this.fetchAndSaveMembers(groupId),
12498
+ this.fetchMessages(groupId)
12499
+ ]);
12511
12500
  }
12512
- })
12513
- );
12501
+ } catch (error) {
12502
+ logger.warn("GroupChat", "Failed to restore group", groupId, error);
12503
+ }
12504
+ }
12514
12505
  if (restoredGroups.length > 0) {
12515
12506
  await this.subscribeToJoinedGroups();
12516
12507
  this.deps.emitEvent("groupchat:updated", {});
@@ -12896,7 +12887,7 @@ var GroupChatModule = class {
12896
12887
  if (group && (group.unreadCount || 0) > 0) {
12897
12888
  group.unreadCount = 0;
12898
12889
  this.groups.set(groupId, group);
12899
- this.schedulePersist();
12890
+ this.persistGroups();
12900
12891
  }
12901
12892
  }
12902
12893
  // ===========================================================================
@@ -12918,7 +12909,7 @@ var GroupChatModule = class {
12918
12909
  if (eventId) {
12919
12910
  this.removeMemberFromMemory(groupId, userPubkey);
12920
12911
  this.deps.emitEvent("groupchat:updated", {});
12921
- this.schedulePersist();
12912
+ this.persistMembers();
12922
12913
  return true;
12923
12914
  }
12924
12915
  return false;
@@ -12941,7 +12932,7 @@ var GroupChatModule = class {
12941
12932
  if (eventId) {
12942
12933
  this.deleteMessageFromMemory(groupId, messageId);
12943
12934
  this.deps.emitEvent("groupchat:updated", {});
12944
- this.schedulePersist();
12935
+ this.persistMessages();
12945
12936
  return true;
12946
12937
  }
12947
12938
  return false;
@@ -13021,19 +13012,13 @@ var GroupChatModule = class {
13021
13012
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
13022
13013
  * only sends events we don't already have.
13023
13014
  */
13024
- getLatestKnownTimestamp(groupIds) {
13015
+ getLatestMessageTimestamp(groupIds) {
13025
13016
  let latest = 0;
13026
13017
  for (const gid of groupIds) {
13027
13018
  const msgs = this.messages.get(gid);
13028
- if (msgs) {
13029
- for (const m of msgs) {
13030
- const ts = Math.floor(m.timestamp / 1e3);
13031
- if (ts > latest) latest = ts;
13032
- }
13033
- }
13034
- const group = this.groups.get(gid);
13035
- if (group) {
13036
- const ts = Math.floor((group.updatedAt || group.createdAt) / 1e3);
13019
+ if (!msgs) continue;
13020
+ for (const m of msgs) {
13021
+ const ts = Math.floor(m.timestamp / 1e3);
13037
13022
  if (ts > latest) latest = ts;
13038
13023
  }
13039
13024
  }
@@ -13108,7 +13093,7 @@ var GroupChatModule = class {
13108
13093
  });
13109
13094
  }
13110
13095
  }
13111
- this.schedulePersist();
13096
+ this.persistMembers();
13112
13097
  }
13113
13098
  async fetchGroupMembersInternal(groupId) {
13114
13099
  if (!this.client) return [];
@@ -13235,11 +13220,8 @@ var GroupChatModule = class {
13235
13220
  addProcessedEventId(eventId) {
13236
13221
  this.processedEventIds.add(eventId);
13237
13222
  if (this.processedEventIds.size > 1e4) {
13238
- let toDelete = 5e3;
13239
- for (const id of this.processedEventIds) {
13240
- if (toDelete-- <= 0) break;
13241
- this.processedEventIds.delete(id);
13242
- }
13223
+ const arr = Array.from(this.processedEventIds);
13224
+ this.processedEventIds = new Set(arr.slice(arr.length - 1e4));
13243
13225
  }
13244
13226
  }
13245
13227
  // ===========================================================================