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