@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.
@@ -2514,9 +2514,9 @@ var NostrTransportProvider = class _NostrTransportProvider {
2514
2514
  if (subId) {
2515
2515
  this.nostrClient?.unsubscribe(subId);
2516
2516
  }
2517
- logger.warn("Nostr", `queryEvents timed out after 15s, returning ${events.length} event(s)`, { kinds: filterObj.kinds, limit: filterObj.limit });
2517
+ logger.warn("Nostr", `queryEvents timed out after 5s, returning ${events.length} event(s)`, { kinds: filterObj.kinds, limit: filterObj.limit });
2518
2518
  resolve(events);
2519
- }, 15e3);
2519
+ }, 5e3);
2520
2520
  const subId = this.nostrClient.subscribe(filter, {
2521
2521
  onEvent: (event) => {
2522
2522
  events.push({
@@ -12326,48 +12326,45 @@ var GroupChatModule = class {
12326
12326
  if (!this.client) return;
12327
12327
  const groupIds = Array.from(this.groups.keys());
12328
12328
  if (groupIds.length === 0) return;
12329
- const sinceTimestamp = this.getLatestKnownTimestamp(groupIds);
12329
+ const latestTimestamp = this.getLatestMessageTimestamp(groupIds);
12330
12330
  this.trackSubscription(
12331
12331
  createNip29Filter({
12332
12332
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12333
12333
  "#h": groupIds,
12334
- ...sinceTimestamp ? { since: sinceTimestamp } : {}
12334
+ ...latestTimestamp ? { since: latestTimestamp } : {}
12335
12335
  }),
12336
12336
  { onEvent: (event) => this.handleGroupEvent(event) }
12337
12337
  );
12338
12338
  this.trackSubscription(
12339
12339
  createNip29Filter({
12340
12340
  kinds: [NIP29_KINDS.GROUP_METADATA, NIP29_KINDS.GROUP_MEMBERS, NIP29_KINDS.GROUP_ADMINS],
12341
- "#d": groupIds,
12342
- ...sinceTimestamp ? { since: sinceTimestamp } : {}
12341
+ "#d": groupIds
12343
12342
  }),
12344
12343
  { onEvent: (event) => this.handleMetadataEvent(event) }
12345
12344
  );
12346
12345
  this.trackSubscription(
12347
12346
  createNip29Filter({
12348
12347
  kinds: [NIP29_KINDS.DELETE_EVENT, NIP29_KINDS.REMOVE_USER, NIP29_KINDS.DELETE_GROUP],
12349
- "#h": groupIds,
12350
- ...sinceTimestamp ? { since: sinceTimestamp } : {}
12348
+ "#h": groupIds
12351
12349
  }),
12352
12350
  { onEvent: (event) => this.handleModerationEvent(event) }
12353
12351
  );
12354
12352
  }
12355
12353
  subscribeToGroup(groupId) {
12356
12354
  if (!this.client) return;
12357
- const sinceTimestamp = this.getLatestKnownTimestamp([groupId]);
12355
+ const latestTimestamp = this.getLatestMessageTimestamp([groupId]);
12358
12356
  this.trackSubscription(
12359
12357
  createNip29Filter({
12360
12358
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12361
12359
  "#h": [groupId],
12362
- ...sinceTimestamp ? { since: sinceTimestamp } : {}
12360
+ ...latestTimestamp ? { since: latestTimestamp } : {}
12363
12361
  }),
12364
12362
  { onEvent: (event) => this.handleGroupEvent(event) }
12365
12363
  );
12366
12364
  this.trackSubscription(
12367
12365
  createNip29Filter({
12368
12366
  kinds: [NIP29_KINDS.DELETE_EVENT, NIP29_KINDS.REMOVE_USER, NIP29_KINDS.DELETE_GROUP],
12369
- "#h": [groupId],
12370
- ...sinceTimestamp ? { since: sinceTimestamp } : {}
12367
+ "#h": [groupId]
12371
12368
  }),
12372
12369
  { onEvent: (event) => this.handleModerationEvent(event) }
12373
12370
  );
@@ -12434,7 +12431,7 @@ var GroupChatModule = class {
12434
12431
  }
12435
12432
  group.updatedAt = event.created_at * 1e3;
12436
12433
  this.groups.set(groupId, group);
12437
- this.schedulePersist();
12434
+ this.persistGroups();
12438
12435
  } else if (event.kind === NIP29_KINDS.GROUP_MEMBERS) {
12439
12436
  this.updateMembersFromEvent(groupId, event);
12440
12437
  } else if (event.kind === NIP29_KINDS.GROUP_ADMINS) {
@@ -12455,7 +12452,7 @@ var GroupChatModule = class {
12455
12452
  }
12456
12453
  }
12457
12454
  this.deps.emitEvent("groupchat:updated", {});
12458
- this.schedulePersist();
12455
+ this.persistMessages();
12459
12456
  } else if (event.kind === NIP29_KINDS.REMOVE_USER) {
12460
12457
  if (this.processedEventIds.has(event.id)) return;
12461
12458
  const eventTimestampMs = event.created_at * 1e3;
@@ -12516,7 +12513,7 @@ var GroupChatModule = class {
12516
12513
  };
12517
12514
  this.saveMemberToMemory(member);
12518
12515
  }
12519
- this.schedulePersist();
12516
+ this.persistMembers();
12520
12517
  }
12521
12518
  updateAdminsFromEvent(groupId, event) {
12522
12519
  const pTags = event.tags.filter((t) => t[0] === "p");
@@ -12536,7 +12533,7 @@ var GroupChatModule = class {
12536
12533
  });
12537
12534
  }
12538
12535
  }
12539
- this.schedulePersist();
12536
+ this.persistMembers();
12540
12537
  }
12541
12538
  // ===========================================================================
12542
12539
  // Group Membership Restoration
@@ -12547,11 +12544,13 @@ var GroupChatModule = class {
12547
12544
  if (!myPubkey) return [];
12548
12545
  const groupIdsWithMembership = /* @__PURE__ */ new Set();
12549
12546
  await this.oneshotSubscription(
12550
- createNip29Filter({ kinds: [NIP29_KINDS.GROUP_MEMBERS], "#p": [myPubkey] }),
12547
+ new import_nostr_js_sdk4.Filter({ kinds: [NIP29_KINDS.GROUP_MEMBERS] }),
12551
12548
  {
12552
12549
  onEvent: (event) => {
12553
12550
  const groupId = this.getGroupIdFromMetadataEvent(event);
12554
- if (groupId) {
12551
+ if (!groupId) return;
12552
+ const pTags = event.tags.filter((t) => t[0] === "p");
12553
+ if (pTags.some((tag) => tag[1] === myPubkey)) {
12555
12554
  groupIdsWithMembership.add(groupId);
12556
12555
  }
12557
12556
  },
@@ -12563,24 +12562,22 @@ var GroupChatModule = class {
12563
12562
  );
12564
12563
  if (groupIdsWithMembership.size === 0) return [];
12565
12564
  const restoredGroups = [];
12566
- await Promise.all(
12567
- Array.from(groupIdsWithMembership).map(async (groupId) => {
12568
- if (this.groups.has(groupId)) return;
12569
- try {
12570
- const group = await this.fetchGroupMetadataInternal(groupId);
12571
- if (group) {
12572
- this.groups.set(groupId, group);
12573
- restoredGroups.push(group);
12574
- await Promise.all([
12575
- this.fetchAndSaveMembers(groupId),
12576
- this.fetchMessages(groupId)
12577
- ]);
12578
- }
12579
- } catch (error) {
12580
- logger.warn("GroupChat", "Failed to restore group", groupId, error);
12565
+ for (const groupId of groupIdsWithMembership) {
12566
+ if (this.groups.has(groupId)) continue;
12567
+ try {
12568
+ const group = await this.fetchGroupMetadataInternal(groupId);
12569
+ if (group) {
12570
+ this.groups.set(groupId, group);
12571
+ restoredGroups.push(group);
12572
+ await Promise.all([
12573
+ this.fetchAndSaveMembers(groupId),
12574
+ this.fetchMessages(groupId)
12575
+ ]);
12581
12576
  }
12582
- })
12583
- );
12577
+ } catch (error) {
12578
+ logger.warn("GroupChat", "Failed to restore group", groupId, error);
12579
+ }
12580
+ }
12584
12581
  if (restoredGroups.length > 0) {
12585
12582
  await this.subscribeToJoinedGroups();
12586
12583
  this.deps.emitEvent("groupchat:updated", {});
@@ -12966,7 +12963,7 @@ var GroupChatModule = class {
12966
12963
  if (group && (group.unreadCount || 0) > 0) {
12967
12964
  group.unreadCount = 0;
12968
12965
  this.groups.set(groupId, group);
12969
- this.schedulePersist();
12966
+ this.persistGroups();
12970
12967
  }
12971
12968
  }
12972
12969
  // ===========================================================================
@@ -12988,7 +12985,7 @@ var GroupChatModule = class {
12988
12985
  if (eventId) {
12989
12986
  this.removeMemberFromMemory(groupId, userPubkey);
12990
12987
  this.deps.emitEvent("groupchat:updated", {});
12991
- this.schedulePersist();
12988
+ this.persistMembers();
12992
12989
  return true;
12993
12990
  }
12994
12991
  return false;
@@ -13011,7 +13008,7 @@ var GroupChatModule = class {
13011
13008
  if (eventId) {
13012
13009
  this.deleteMessageFromMemory(groupId, messageId);
13013
13010
  this.deps.emitEvent("groupchat:updated", {});
13014
- this.schedulePersist();
13011
+ this.persistMessages();
13015
13012
  return true;
13016
13013
  }
13017
13014
  return false;
@@ -13091,19 +13088,13 @@ var GroupChatModule = class {
13091
13088
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
13092
13089
  * only sends events we don't already have.
13093
13090
  */
13094
- getLatestKnownTimestamp(groupIds) {
13091
+ getLatestMessageTimestamp(groupIds) {
13095
13092
  let latest = 0;
13096
13093
  for (const gid of groupIds) {
13097
13094
  const msgs = this.messages.get(gid);
13098
- if (msgs) {
13099
- for (const m of msgs) {
13100
- const ts = Math.floor(m.timestamp / 1e3);
13101
- if (ts > latest) latest = ts;
13102
- }
13103
- }
13104
- const group = this.groups.get(gid);
13105
- if (group) {
13106
- const ts = Math.floor((group.updatedAt || group.createdAt) / 1e3);
13095
+ if (!msgs) continue;
13096
+ for (const m of msgs) {
13097
+ const ts = Math.floor(m.timestamp / 1e3);
13107
13098
  if (ts > latest) latest = ts;
13108
13099
  }
13109
13100
  }
@@ -13178,7 +13169,7 @@ var GroupChatModule = class {
13178
13169
  });
13179
13170
  }
13180
13171
  }
13181
- this.schedulePersist();
13172
+ this.persistMembers();
13182
13173
  }
13183
13174
  async fetchGroupMembersInternal(groupId) {
13184
13175
  if (!this.client) return [];
@@ -13305,11 +13296,8 @@ var GroupChatModule = class {
13305
13296
  addProcessedEventId(eventId) {
13306
13297
  this.processedEventIds.add(eventId);
13307
13298
  if (this.processedEventIds.size > 1e4) {
13308
- let toDelete = 5e3;
13309
- for (const id of this.processedEventIds) {
13310
- if (toDelete-- <= 0) break;
13311
- this.processedEventIds.delete(id);
13312
- }
13299
+ const arr = Array.from(this.processedEventIds);
13300
+ this.processedEventIds = new Set(arr.slice(arr.length - 1e4));
13313
13301
  }
13314
13302
  }
13315
13303
  // ===========================================================================