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