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