@unicitylabs/sphere-sdk 0.6.10-dev.3 → 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({
@@ -12188,20 +12188,8 @@ var GroupChatModule = class {
12188
12188
  }
12189
12189
  }
12190
12190
  }
12191
- async destroy() {
12191
+ destroy() {
12192
12192
  this.destroyConnection();
12193
- if (this.persistTimer) {
12194
- clearTimeout(this.persistTimer);
12195
- this.persistTimer = null;
12196
- }
12197
- if (this.deps) {
12198
- try {
12199
- if (this.persistPromise) await this.persistPromise;
12200
- await this.doPersistAll();
12201
- } catch (err) {
12202
- logger.debug("GroupChat", "Persist on destroy failed", err);
12203
- }
12204
- }
12205
12193
  this.groups.clear();
12206
12194
  this.messages.clear();
12207
12195
  this.members.clear();
@@ -12210,7 +12198,10 @@ var GroupChatModule = class {
12210
12198
  this.messageHandlers.clear();
12211
12199
  this.relayAdminPubkeys = null;
12212
12200
  this.relayAdminFetchPromise = null;
12213
- this.persistPromise = null;
12201
+ if (this.persistTimer) {
12202
+ clearTimeout(this.persistTimer);
12203
+ this.persistTimer = null;
12204
+ }
12214
12205
  this.deps = null;
12215
12206
  }
12216
12207
  destroyConnection() {
@@ -12335,12 +12326,12 @@ var GroupChatModule = class {
12335
12326
  if (!this.client) return;
12336
12327
  const groupIds = Array.from(this.groups.keys());
12337
12328
  if (groupIds.length === 0) return;
12338
- const sinceTimestamp = this.getLatestKnownTimestamp(groupIds);
12329
+ const latestTimestamp = this.getLatestMessageTimestamp(groupIds);
12339
12330
  this.trackSubscription(
12340
12331
  createNip29Filter({
12341
12332
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12342
12333
  "#h": groupIds,
12343
- ...sinceTimestamp ? { since: sinceTimestamp } : {}
12334
+ ...latestTimestamp ? { since: latestTimestamp } : {}
12344
12335
  }),
12345
12336
  { onEvent: (event) => this.handleGroupEvent(event) }
12346
12337
  );
@@ -12361,12 +12352,12 @@ var GroupChatModule = class {
12361
12352
  }
12362
12353
  subscribeToGroup(groupId) {
12363
12354
  if (!this.client) return;
12364
- const sinceTimestamp = this.getLatestKnownTimestamp([groupId]);
12355
+ const latestTimestamp = this.getLatestMessageTimestamp([groupId]);
12365
12356
  this.trackSubscription(
12366
12357
  createNip29Filter({
12367
12358
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12368
12359
  "#h": [groupId],
12369
- ...sinceTimestamp ? { since: sinceTimestamp } : {}
12360
+ ...latestTimestamp ? { since: latestTimestamp } : {}
12370
12361
  }),
12371
12362
  { onEvent: (event) => this.handleGroupEvent(event) }
12372
12363
  );
@@ -12440,7 +12431,7 @@ var GroupChatModule = class {
12440
12431
  }
12441
12432
  group.updatedAt = event.created_at * 1e3;
12442
12433
  this.groups.set(groupId, group);
12443
- this.schedulePersist();
12434
+ this.persistGroups();
12444
12435
  } else if (event.kind === NIP29_KINDS.GROUP_MEMBERS) {
12445
12436
  this.updateMembersFromEvent(groupId, event);
12446
12437
  } else if (event.kind === NIP29_KINDS.GROUP_ADMINS) {
@@ -12461,7 +12452,7 @@ var GroupChatModule = class {
12461
12452
  }
12462
12453
  }
12463
12454
  this.deps.emitEvent("groupchat:updated", {});
12464
- this.schedulePersist();
12455
+ this.persistMessages();
12465
12456
  } else if (event.kind === NIP29_KINDS.REMOVE_USER) {
12466
12457
  if (this.processedEventIds.has(event.id)) return;
12467
12458
  const eventTimestampMs = event.created_at * 1e3;
@@ -12522,7 +12513,7 @@ var GroupChatModule = class {
12522
12513
  };
12523
12514
  this.saveMemberToMemory(member);
12524
12515
  }
12525
- this.schedulePersist();
12516
+ this.persistMembers();
12526
12517
  }
12527
12518
  updateAdminsFromEvent(groupId, event) {
12528
12519
  const pTags = event.tags.filter((t) => t[0] === "p");
@@ -12542,7 +12533,7 @@ var GroupChatModule = class {
12542
12533
  });
12543
12534
  }
12544
12535
  }
12545
- this.schedulePersist();
12536
+ this.persistMembers();
12546
12537
  }
12547
12538
  // ===========================================================================
12548
12539
  // Group Membership Restoration
@@ -12553,11 +12544,13 @@ var GroupChatModule = class {
12553
12544
  if (!myPubkey) return [];
12554
12545
  const groupIdsWithMembership = /* @__PURE__ */ new Set();
12555
12546
  await this.oneshotSubscription(
12556
- createNip29Filter({ kinds: [NIP29_KINDS.GROUP_MEMBERS], "#p": [myPubkey] }),
12547
+ new import_nostr_js_sdk4.Filter({ kinds: [NIP29_KINDS.GROUP_MEMBERS] }),
12557
12548
  {
12558
12549
  onEvent: (event) => {
12559
12550
  const groupId = this.getGroupIdFromMetadataEvent(event);
12560
- if (groupId) {
12551
+ if (!groupId) return;
12552
+ const pTags = event.tags.filter((t) => t[0] === "p");
12553
+ if (pTags.some((tag) => tag[1] === myPubkey)) {
12561
12554
  groupIdsWithMembership.add(groupId);
12562
12555
  }
12563
12556
  },
@@ -12569,24 +12562,22 @@ var GroupChatModule = class {
12569
12562
  );
12570
12563
  if (groupIdsWithMembership.size === 0) return [];
12571
12564
  const restoredGroups = [];
12572
- await Promise.all(
12573
- Array.from(groupIdsWithMembership).map(async (groupId) => {
12574
- if (this.groups.has(groupId)) return;
12575
- try {
12576
- const group = await this.fetchGroupMetadataInternal(groupId);
12577
- if (group) {
12578
- this.groups.set(groupId, group);
12579
- restoredGroups.push(group);
12580
- await Promise.all([
12581
- this.fetchAndSaveMembers(groupId),
12582
- this.fetchMessages(groupId)
12583
- ]);
12584
- }
12585
- } catch (error) {
12586
- 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
+ ]);
12587
12576
  }
12588
- })
12589
- );
12577
+ } catch (error) {
12578
+ logger.warn("GroupChat", "Failed to restore group", groupId, error);
12579
+ }
12580
+ }
12590
12581
  if (restoredGroups.length > 0) {
12591
12582
  await this.subscribeToJoinedGroups();
12592
12583
  this.deps.emitEvent("groupchat:updated", {});
@@ -12972,7 +12963,7 @@ var GroupChatModule = class {
12972
12963
  if (group && (group.unreadCount || 0) > 0) {
12973
12964
  group.unreadCount = 0;
12974
12965
  this.groups.set(groupId, group);
12975
- this.schedulePersist();
12966
+ this.persistGroups();
12976
12967
  }
12977
12968
  }
12978
12969
  // ===========================================================================
@@ -12994,7 +12985,7 @@ var GroupChatModule = class {
12994
12985
  if (eventId) {
12995
12986
  this.removeMemberFromMemory(groupId, userPubkey);
12996
12987
  this.deps.emitEvent("groupchat:updated", {});
12997
- this.schedulePersist();
12988
+ this.persistMembers();
12998
12989
  return true;
12999
12990
  }
13000
12991
  return false;
@@ -13017,7 +13008,7 @@ var GroupChatModule = class {
13017
13008
  if (eventId) {
13018
13009
  this.deleteMessageFromMemory(groupId, messageId);
13019
13010
  this.deps.emitEvent("groupchat:updated", {});
13020
- this.schedulePersist();
13011
+ this.persistMessages();
13021
13012
  return true;
13022
13013
  }
13023
13014
  return false;
@@ -13097,19 +13088,13 @@ var GroupChatModule = class {
13097
13088
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
13098
13089
  * only sends events we don't already have.
13099
13090
  */
13100
- getLatestKnownTimestamp(groupIds) {
13091
+ getLatestMessageTimestamp(groupIds) {
13101
13092
  let latest = 0;
13102
13093
  for (const gid of groupIds) {
13103
13094
  const msgs = this.messages.get(gid);
13104
- if (msgs) {
13105
- for (const m of msgs) {
13106
- const ts = Math.floor(m.timestamp / 1e3);
13107
- if (ts > latest) latest = ts;
13108
- }
13109
- }
13110
- const group = this.groups.get(gid);
13111
- if (group) {
13112
- 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);
13113
13098
  if (ts > latest) latest = ts;
13114
13099
  }
13115
13100
  }
@@ -13184,7 +13169,7 @@ var GroupChatModule = class {
13184
13169
  });
13185
13170
  }
13186
13171
  }
13187
- this.schedulePersist();
13172
+ this.persistMembers();
13188
13173
  }
13189
13174
  async fetchGroupMembersInternal(groupId) {
13190
13175
  if (!this.client) return [];
@@ -13311,11 +13296,8 @@ var GroupChatModule = class {
13311
13296
  addProcessedEventId(eventId) {
13312
13297
  this.processedEventIds.add(eventId);
13313
13298
  if (this.processedEventIds.size > 1e4) {
13314
- let toDelete = 5e3;
13315
- for (const id of this.processedEventIds) {
13316
- if (toDelete-- <= 0) break;
13317
- this.processedEventIds.delete(id);
13318
- }
13299
+ const arr = Array.from(this.processedEventIds);
13300
+ this.processedEventIds = new Set(arr.slice(arr.length - 1e4));
13319
13301
  }
13320
13302
  }
13321
13303
  // ===========================================================================