@unicitylabs/sphere-sdk 0.6.9 → 0.6.10-dev.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.
package/dist/index.cjs CHANGED
@@ -2612,9 +2612,9 @@ var NostrTransportProvider = class _NostrTransportProvider {
2612
2612
  if (subId) {
2613
2613
  this.nostrClient?.unsubscribe(subId);
2614
2614
  }
2615
- logger.warn("Nostr", `queryEvents timed out after 5s, returning ${events.length} event(s)`, { kinds: filterObj.kinds, limit: filterObj.limit });
2615
+ logger.warn("Nostr", `queryEvents timed out after 15s, returning ${events.length} event(s)`, { kinds: filterObj.kinds, limit: filterObj.limit });
2616
2616
  resolve(events);
2617
- }, 5e3);
2617
+ }, 15e3);
2618
2618
  const subId = this.nostrClient.subscribe(filter, {
2619
2619
  onEvent: (event) => {
2620
2620
  events.push({
@@ -12666,45 +12666,48 @@ var GroupChatModule = class {
12666
12666
  if (!this.client) return;
12667
12667
  const groupIds = Array.from(this.groups.keys());
12668
12668
  if (groupIds.length === 0) return;
12669
- const latestTimestamp = this.getLatestMessageTimestamp(groupIds);
12669
+ const sinceTimestamp = this.getLatestKnownTimestamp(groupIds);
12670
12670
  this.trackSubscription(
12671
12671
  createNip29Filter({
12672
12672
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12673
12673
  "#h": groupIds,
12674
- ...latestTimestamp ? { since: latestTimestamp } : {}
12674
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12675
12675
  }),
12676
12676
  { onEvent: (event) => this.handleGroupEvent(event) }
12677
12677
  );
12678
12678
  this.trackSubscription(
12679
12679
  createNip29Filter({
12680
12680
  kinds: [NIP29_KINDS.GROUP_METADATA, NIP29_KINDS.GROUP_MEMBERS, NIP29_KINDS.GROUP_ADMINS],
12681
- "#d": groupIds
12681
+ "#d": groupIds,
12682
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12682
12683
  }),
12683
12684
  { onEvent: (event) => this.handleMetadataEvent(event) }
12684
12685
  );
12685
12686
  this.trackSubscription(
12686
12687
  createNip29Filter({
12687
12688
  kinds: [NIP29_KINDS.DELETE_EVENT, NIP29_KINDS.REMOVE_USER, NIP29_KINDS.DELETE_GROUP],
12688
- "#h": groupIds
12689
+ "#h": groupIds,
12690
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12689
12691
  }),
12690
12692
  { onEvent: (event) => this.handleModerationEvent(event) }
12691
12693
  );
12692
12694
  }
12693
12695
  subscribeToGroup(groupId) {
12694
12696
  if (!this.client) return;
12695
- const latestTimestamp = this.getLatestMessageTimestamp([groupId]);
12697
+ const sinceTimestamp = this.getLatestKnownTimestamp([groupId]);
12696
12698
  this.trackSubscription(
12697
12699
  createNip29Filter({
12698
12700
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12699
12701
  "#h": [groupId],
12700
- ...latestTimestamp ? { since: latestTimestamp } : {}
12702
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12701
12703
  }),
12702
12704
  { onEvent: (event) => this.handleGroupEvent(event) }
12703
12705
  );
12704
12706
  this.trackSubscription(
12705
12707
  createNip29Filter({
12706
12708
  kinds: [NIP29_KINDS.DELETE_EVENT, NIP29_KINDS.REMOVE_USER, NIP29_KINDS.DELETE_GROUP],
12707
- "#h": [groupId]
12709
+ "#h": [groupId],
12710
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12708
12711
  }),
12709
12712
  { onEvent: (event) => this.handleModerationEvent(event) }
12710
12713
  );
@@ -12771,7 +12774,7 @@ var GroupChatModule = class {
12771
12774
  }
12772
12775
  group.updatedAt = event.created_at * 1e3;
12773
12776
  this.groups.set(groupId, group);
12774
- this.persistGroups();
12777
+ this.schedulePersist();
12775
12778
  } else if (event.kind === NIP29_KINDS.GROUP_MEMBERS) {
12776
12779
  this.updateMembersFromEvent(groupId, event);
12777
12780
  } else if (event.kind === NIP29_KINDS.GROUP_ADMINS) {
@@ -12792,7 +12795,7 @@ var GroupChatModule = class {
12792
12795
  }
12793
12796
  }
12794
12797
  this.deps.emitEvent("groupchat:updated", {});
12795
- this.persistMessages();
12798
+ this.schedulePersist();
12796
12799
  } else if (event.kind === NIP29_KINDS.REMOVE_USER) {
12797
12800
  if (this.processedEventIds.has(event.id)) return;
12798
12801
  const eventTimestampMs = event.created_at * 1e3;
@@ -12853,7 +12856,7 @@ var GroupChatModule = class {
12853
12856
  };
12854
12857
  this.saveMemberToMemory(member);
12855
12858
  }
12856
- this.persistMembers();
12859
+ this.schedulePersist();
12857
12860
  }
12858
12861
  updateAdminsFromEvent(groupId, event) {
12859
12862
  const pTags = event.tags.filter((t) => t[0] === "p");
@@ -12873,7 +12876,7 @@ var GroupChatModule = class {
12873
12876
  });
12874
12877
  }
12875
12878
  }
12876
- this.persistMembers();
12879
+ this.schedulePersist();
12877
12880
  }
12878
12881
  // ===========================================================================
12879
12882
  // Group Membership Restoration
@@ -12884,13 +12887,11 @@ var GroupChatModule = class {
12884
12887
  if (!myPubkey) return [];
12885
12888
  const groupIdsWithMembership = /* @__PURE__ */ new Set();
12886
12889
  await this.oneshotSubscription(
12887
- new import_nostr_js_sdk4.Filter({ kinds: [NIP29_KINDS.GROUP_MEMBERS] }),
12890
+ createNip29Filter({ kinds: [NIP29_KINDS.GROUP_MEMBERS], "#p": [myPubkey] }),
12888
12891
  {
12889
12892
  onEvent: (event) => {
12890
12893
  const groupId = this.getGroupIdFromMetadataEvent(event);
12891
- if (!groupId) return;
12892
- const pTags = event.tags.filter((t) => t[0] === "p");
12893
- if (pTags.some((tag) => tag[1] === myPubkey)) {
12894
+ if (groupId) {
12894
12895
  groupIdsWithMembership.add(groupId);
12895
12896
  }
12896
12897
  },
@@ -12902,22 +12903,24 @@ var GroupChatModule = class {
12902
12903
  );
12903
12904
  if (groupIdsWithMembership.size === 0) return [];
12904
12905
  const restoredGroups = [];
12905
- for (const groupId of groupIdsWithMembership) {
12906
- if (this.groups.has(groupId)) continue;
12907
- try {
12908
- const group = await this.fetchGroupMetadataInternal(groupId);
12909
- if (group) {
12910
- this.groups.set(groupId, group);
12911
- restoredGroups.push(group);
12912
- await Promise.all([
12913
- this.fetchAndSaveMembers(groupId),
12914
- this.fetchMessages(groupId)
12915
- ]);
12906
+ await Promise.all(
12907
+ Array.from(groupIdsWithMembership).map(async (groupId) => {
12908
+ if (this.groups.has(groupId)) return;
12909
+ try {
12910
+ const group = await this.fetchGroupMetadataInternal(groupId);
12911
+ if (group) {
12912
+ this.groups.set(groupId, group);
12913
+ restoredGroups.push(group);
12914
+ await Promise.all([
12915
+ this.fetchAndSaveMembers(groupId),
12916
+ this.fetchMessages(groupId)
12917
+ ]);
12918
+ }
12919
+ } catch (error) {
12920
+ logger.warn("GroupChat", "Failed to restore group", groupId, error);
12916
12921
  }
12917
- } catch (error) {
12918
- logger.warn("GroupChat", "Failed to restore group", groupId, error);
12919
- }
12920
- }
12922
+ })
12923
+ );
12921
12924
  if (restoredGroups.length > 0) {
12922
12925
  await this.subscribeToJoinedGroups();
12923
12926
  this.deps.emitEvent("groupchat:updated", {});
@@ -13303,7 +13306,7 @@ var GroupChatModule = class {
13303
13306
  if (group && (group.unreadCount || 0) > 0) {
13304
13307
  group.unreadCount = 0;
13305
13308
  this.groups.set(groupId, group);
13306
- this.persistGroups();
13309
+ this.schedulePersist();
13307
13310
  }
13308
13311
  }
13309
13312
  // ===========================================================================
@@ -13325,7 +13328,7 @@ var GroupChatModule = class {
13325
13328
  if (eventId) {
13326
13329
  this.removeMemberFromMemory(groupId, userPubkey);
13327
13330
  this.deps.emitEvent("groupchat:updated", {});
13328
- this.persistMembers();
13331
+ this.schedulePersist();
13329
13332
  return true;
13330
13333
  }
13331
13334
  return false;
@@ -13348,7 +13351,7 @@ var GroupChatModule = class {
13348
13351
  if (eventId) {
13349
13352
  this.deleteMessageFromMemory(groupId, messageId);
13350
13353
  this.deps.emitEvent("groupchat:updated", {});
13351
- this.persistMessages();
13354
+ this.schedulePersist();
13352
13355
  return true;
13353
13356
  }
13354
13357
  return false;
@@ -13428,13 +13431,19 @@ var GroupChatModule = class {
13428
13431
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
13429
13432
  * only sends events we don't already have.
13430
13433
  */
13431
- getLatestMessageTimestamp(groupIds) {
13434
+ getLatestKnownTimestamp(groupIds) {
13432
13435
  let latest = 0;
13433
13436
  for (const gid of groupIds) {
13434
13437
  const msgs = this.messages.get(gid);
13435
- if (!msgs) continue;
13436
- for (const m of msgs) {
13437
- const ts = Math.floor(m.timestamp / 1e3);
13438
+ if (msgs) {
13439
+ for (const m of msgs) {
13440
+ const ts = Math.floor(m.timestamp / 1e3);
13441
+ if (ts > latest) latest = ts;
13442
+ }
13443
+ }
13444
+ const group = this.groups.get(gid);
13445
+ if (group) {
13446
+ const ts = Math.floor((group.updatedAt || group.createdAt) / 1e3);
13438
13447
  if (ts > latest) latest = ts;
13439
13448
  }
13440
13449
  }
@@ -13509,7 +13518,7 @@ var GroupChatModule = class {
13509
13518
  });
13510
13519
  }
13511
13520
  }
13512
- this.persistMembers();
13521
+ this.schedulePersist();
13513
13522
  }
13514
13523
  async fetchGroupMembersInternal(groupId) {
13515
13524
  if (!this.client) return [];
@@ -13636,8 +13645,11 @@ var GroupChatModule = class {
13636
13645
  addProcessedEventId(eventId) {
13637
13646
  this.processedEventIds.add(eventId);
13638
13647
  if (this.processedEventIds.size > 1e4) {
13639
- const arr = Array.from(this.processedEventIds);
13640
- this.processedEventIds = new Set(arr.slice(arr.length - 1e4));
13648
+ let toDelete = 5e3;
13649
+ for (const id of this.processedEventIds) {
13650
+ if (toDelete-- <= 0) break;
13651
+ this.processedEventIds.delete(id);
13652
+ }
13641
13653
  }
13642
13654
  }
13643
13655
  // ===========================================================================