@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.d.cts CHANGED
@@ -3740,7 +3740,7 @@ declare class GroupChatModule {
3740
3740
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
3741
3741
  * only sends events we don't already have.
3742
3742
  */
3743
- private getLatestMessageTimestamp;
3743
+ private getLatestKnownTimestamp;
3744
3744
  private fetchRelayAdmins;
3745
3745
  private doFetchRelayAdmins;
3746
3746
  private fetchGroupMetadataInternal;
package/dist/index.d.ts CHANGED
@@ -3740,7 +3740,7 @@ declare class GroupChatModule {
3740
3740
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
3741
3741
  * only sends events we don't already have.
3742
3742
  */
3743
- private getLatestMessageTimestamp;
3743
+ private getLatestKnownTimestamp;
3744
3744
  private fetchRelayAdmins;
3745
3745
  private doFetchRelayAdmins;
3746
3746
  private fetchGroupMetadataInternal;
package/dist/index.js CHANGED
@@ -2461,9 +2461,9 @@ var NostrTransportProvider = class _NostrTransportProvider {
2461
2461
  if (subId) {
2462
2462
  this.nostrClient?.unsubscribe(subId);
2463
2463
  }
2464
- logger.warn("Nostr", `queryEvents timed out after 5s, returning ${events.length} event(s)`, { kinds: filterObj.kinds, limit: filterObj.limit });
2464
+ logger.warn("Nostr", `queryEvents timed out after 15s, returning ${events.length} event(s)`, { kinds: filterObj.kinds, limit: filterObj.limit });
2465
2465
  resolve(events);
2466
- }, 5e3);
2466
+ }, 15e3);
2467
2467
  const subId = this.nostrClient.subscribe(filter, {
2468
2468
  onEvent: (event) => {
2469
2469
  events.push({
@@ -12519,45 +12519,48 @@ var GroupChatModule = class {
12519
12519
  if (!this.client) return;
12520
12520
  const groupIds = Array.from(this.groups.keys());
12521
12521
  if (groupIds.length === 0) return;
12522
- const latestTimestamp = this.getLatestMessageTimestamp(groupIds);
12522
+ const sinceTimestamp = this.getLatestKnownTimestamp(groupIds);
12523
12523
  this.trackSubscription(
12524
12524
  createNip29Filter({
12525
12525
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12526
12526
  "#h": groupIds,
12527
- ...latestTimestamp ? { since: latestTimestamp } : {}
12527
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12528
12528
  }),
12529
12529
  { onEvent: (event) => this.handleGroupEvent(event) }
12530
12530
  );
12531
12531
  this.trackSubscription(
12532
12532
  createNip29Filter({
12533
12533
  kinds: [NIP29_KINDS.GROUP_METADATA, NIP29_KINDS.GROUP_MEMBERS, NIP29_KINDS.GROUP_ADMINS],
12534
- "#d": groupIds
12534
+ "#d": groupIds,
12535
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12535
12536
  }),
12536
12537
  { onEvent: (event) => this.handleMetadataEvent(event) }
12537
12538
  );
12538
12539
  this.trackSubscription(
12539
12540
  createNip29Filter({
12540
12541
  kinds: [NIP29_KINDS.DELETE_EVENT, NIP29_KINDS.REMOVE_USER, NIP29_KINDS.DELETE_GROUP],
12541
- "#h": groupIds
12542
+ "#h": groupIds,
12543
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12542
12544
  }),
12543
12545
  { onEvent: (event) => this.handleModerationEvent(event) }
12544
12546
  );
12545
12547
  }
12546
12548
  subscribeToGroup(groupId) {
12547
12549
  if (!this.client) return;
12548
- const latestTimestamp = this.getLatestMessageTimestamp([groupId]);
12550
+ const sinceTimestamp = this.getLatestKnownTimestamp([groupId]);
12549
12551
  this.trackSubscription(
12550
12552
  createNip29Filter({
12551
12553
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12552
12554
  "#h": [groupId],
12553
- ...latestTimestamp ? { since: latestTimestamp } : {}
12555
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12554
12556
  }),
12555
12557
  { onEvent: (event) => this.handleGroupEvent(event) }
12556
12558
  );
12557
12559
  this.trackSubscription(
12558
12560
  createNip29Filter({
12559
12561
  kinds: [NIP29_KINDS.DELETE_EVENT, NIP29_KINDS.REMOVE_USER, NIP29_KINDS.DELETE_GROUP],
12560
- "#h": [groupId]
12562
+ "#h": [groupId],
12563
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12561
12564
  }),
12562
12565
  { onEvent: (event) => this.handleModerationEvent(event) }
12563
12566
  );
@@ -12624,7 +12627,7 @@ var GroupChatModule = class {
12624
12627
  }
12625
12628
  group.updatedAt = event.created_at * 1e3;
12626
12629
  this.groups.set(groupId, group);
12627
- this.persistGroups();
12630
+ this.schedulePersist();
12628
12631
  } else if (event.kind === NIP29_KINDS.GROUP_MEMBERS) {
12629
12632
  this.updateMembersFromEvent(groupId, event);
12630
12633
  } else if (event.kind === NIP29_KINDS.GROUP_ADMINS) {
@@ -12645,7 +12648,7 @@ var GroupChatModule = class {
12645
12648
  }
12646
12649
  }
12647
12650
  this.deps.emitEvent("groupchat:updated", {});
12648
- this.persistMessages();
12651
+ this.schedulePersist();
12649
12652
  } else if (event.kind === NIP29_KINDS.REMOVE_USER) {
12650
12653
  if (this.processedEventIds.has(event.id)) return;
12651
12654
  const eventTimestampMs = event.created_at * 1e3;
@@ -12706,7 +12709,7 @@ var GroupChatModule = class {
12706
12709
  };
12707
12710
  this.saveMemberToMemory(member);
12708
12711
  }
12709
- this.persistMembers();
12712
+ this.schedulePersist();
12710
12713
  }
12711
12714
  updateAdminsFromEvent(groupId, event) {
12712
12715
  const pTags = event.tags.filter((t) => t[0] === "p");
@@ -12726,7 +12729,7 @@ var GroupChatModule = class {
12726
12729
  });
12727
12730
  }
12728
12731
  }
12729
- this.persistMembers();
12732
+ this.schedulePersist();
12730
12733
  }
12731
12734
  // ===========================================================================
12732
12735
  // Group Membership Restoration
@@ -12737,13 +12740,11 @@ var GroupChatModule = class {
12737
12740
  if (!myPubkey) return [];
12738
12741
  const groupIdsWithMembership = /* @__PURE__ */ new Set();
12739
12742
  await this.oneshotSubscription(
12740
- new Filter3({ kinds: [NIP29_KINDS.GROUP_MEMBERS] }),
12743
+ createNip29Filter({ kinds: [NIP29_KINDS.GROUP_MEMBERS], "#p": [myPubkey] }),
12741
12744
  {
12742
12745
  onEvent: (event) => {
12743
12746
  const groupId = this.getGroupIdFromMetadataEvent(event);
12744
- if (!groupId) return;
12745
- const pTags = event.tags.filter((t) => t[0] === "p");
12746
- if (pTags.some((tag) => tag[1] === myPubkey)) {
12747
+ if (groupId) {
12747
12748
  groupIdsWithMembership.add(groupId);
12748
12749
  }
12749
12750
  },
@@ -12755,22 +12756,24 @@ var GroupChatModule = class {
12755
12756
  );
12756
12757
  if (groupIdsWithMembership.size === 0) return [];
12757
12758
  const restoredGroups = [];
12758
- for (const groupId of groupIdsWithMembership) {
12759
- if (this.groups.has(groupId)) continue;
12760
- try {
12761
- const group = await this.fetchGroupMetadataInternal(groupId);
12762
- if (group) {
12763
- this.groups.set(groupId, group);
12764
- restoredGroups.push(group);
12765
- await Promise.all([
12766
- this.fetchAndSaveMembers(groupId),
12767
- this.fetchMessages(groupId)
12768
- ]);
12759
+ await Promise.all(
12760
+ Array.from(groupIdsWithMembership).map(async (groupId) => {
12761
+ if (this.groups.has(groupId)) return;
12762
+ try {
12763
+ const group = await this.fetchGroupMetadataInternal(groupId);
12764
+ if (group) {
12765
+ this.groups.set(groupId, group);
12766
+ restoredGroups.push(group);
12767
+ await Promise.all([
12768
+ this.fetchAndSaveMembers(groupId),
12769
+ this.fetchMessages(groupId)
12770
+ ]);
12771
+ }
12772
+ } catch (error) {
12773
+ logger.warn("GroupChat", "Failed to restore group", groupId, error);
12769
12774
  }
12770
- } catch (error) {
12771
- logger.warn("GroupChat", "Failed to restore group", groupId, error);
12772
- }
12773
- }
12775
+ })
12776
+ );
12774
12777
  if (restoredGroups.length > 0) {
12775
12778
  await this.subscribeToJoinedGroups();
12776
12779
  this.deps.emitEvent("groupchat:updated", {});
@@ -13156,7 +13159,7 @@ var GroupChatModule = class {
13156
13159
  if (group && (group.unreadCount || 0) > 0) {
13157
13160
  group.unreadCount = 0;
13158
13161
  this.groups.set(groupId, group);
13159
- this.persistGroups();
13162
+ this.schedulePersist();
13160
13163
  }
13161
13164
  }
13162
13165
  // ===========================================================================
@@ -13178,7 +13181,7 @@ var GroupChatModule = class {
13178
13181
  if (eventId) {
13179
13182
  this.removeMemberFromMemory(groupId, userPubkey);
13180
13183
  this.deps.emitEvent("groupchat:updated", {});
13181
- this.persistMembers();
13184
+ this.schedulePersist();
13182
13185
  return true;
13183
13186
  }
13184
13187
  return false;
@@ -13201,7 +13204,7 @@ var GroupChatModule = class {
13201
13204
  if (eventId) {
13202
13205
  this.deleteMessageFromMemory(groupId, messageId);
13203
13206
  this.deps.emitEvent("groupchat:updated", {});
13204
- this.persistMessages();
13207
+ this.schedulePersist();
13205
13208
  return true;
13206
13209
  }
13207
13210
  return false;
@@ -13281,13 +13284,19 @@ var GroupChatModule = class {
13281
13284
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
13282
13285
  * only sends events we don't already have.
13283
13286
  */
13284
- getLatestMessageTimestamp(groupIds) {
13287
+ getLatestKnownTimestamp(groupIds) {
13285
13288
  let latest = 0;
13286
13289
  for (const gid of groupIds) {
13287
13290
  const msgs = this.messages.get(gid);
13288
- if (!msgs) continue;
13289
- for (const m of msgs) {
13290
- const ts = Math.floor(m.timestamp / 1e3);
13291
+ if (msgs) {
13292
+ for (const m of msgs) {
13293
+ const ts = Math.floor(m.timestamp / 1e3);
13294
+ if (ts > latest) latest = ts;
13295
+ }
13296
+ }
13297
+ const group = this.groups.get(gid);
13298
+ if (group) {
13299
+ const ts = Math.floor((group.updatedAt || group.createdAt) / 1e3);
13291
13300
  if (ts > latest) latest = ts;
13292
13301
  }
13293
13302
  }
@@ -13362,7 +13371,7 @@ var GroupChatModule = class {
13362
13371
  });
13363
13372
  }
13364
13373
  }
13365
- this.persistMembers();
13374
+ this.schedulePersist();
13366
13375
  }
13367
13376
  async fetchGroupMembersInternal(groupId) {
13368
13377
  if (!this.client) return [];
@@ -13489,8 +13498,11 @@ var GroupChatModule = class {
13489
13498
  addProcessedEventId(eventId) {
13490
13499
  this.processedEventIds.add(eventId);
13491
13500
  if (this.processedEventIds.size > 1e4) {
13492
- const arr = Array.from(this.processedEventIds);
13493
- this.processedEventIds = new Set(arr.slice(arr.length - 1e4));
13501
+ let toDelete = 5e3;
13502
+ for (const id of this.processedEventIds) {
13503
+ if (toDelete-- <= 0) break;
13504
+ this.processedEventIds.delete(id);
13505
+ }
13494
13506
  }
13495
13507
  }
13496
13508
  // ===========================================================================