@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.
@@ -3330,7 +3330,7 @@ declare class GroupChatModule {
3330
3330
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
3331
3331
  * only sends events we don't already have.
3332
3332
  */
3333
- private getLatestMessageTimestamp;
3333
+ private getLatestKnownTimestamp;
3334
3334
  private fetchRelayAdmins;
3335
3335
  private doFetchRelayAdmins;
3336
3336
  private fetchGroupMetadataInternal;
@@ -3330,7 +3330,7 @@ declare class GroupChatModule {
3330
3330
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
3331
3331
  * only sends events we don't already have.
3332
3332
  */
3333
- private getLatestMessageTimestamp;
3333
+ private getLatestKnownTimestamp;
3334
3334
  private fetchRelayAdmins;
3335
3335
  private doFetchRelayAdmins;
3336
3336
  private fetchGroupMetadataInternal;
@@ -12250,45 +12250,48 @@ var GroupChatModule = class {
12250
12250
  if (!this.client) return;
12251
12251
  const groupIds = Array.from(this.groups.keys());
12252
12252
  if (groupIds.length === 0) return;
12253
- const latestTimestamp = this.getLatestMessageTimestamp(groupIds);
12253
+ const sinceTimestamp = this.getLatestKnownTimestamp(groupIds);
12254
12254
  this.trackSubscription(
12255
12255
  createNip29Filter({
12256
12256
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12257
12257
  "#h": groupIds,
12258
- ...latestTimestamp ? { since: latestTimestamp } : {}
12258
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12259
12259
  }),
12260
12260
  { onEvent: (event) => this.handleGroupEvent(event) }
12261
12261
  );
12262
12262
  this.trackSubscription(
12263
12263
  createNip29Filter({
12264
12264
  kinds: [NIP29_KINDS.GROUP_METADATA, NIP29_KINDS.GROUP_MEMBERS, NIP29_KINDS.GROUP_ADMINS],
12265
- "#d": groupIds
12265
+ "#d": groupIds,
12266
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12266
12267
  }),
12267
12268
  { onEvent: (event) => this.handleMetadataEvent(event) }
12268
12269
  );
12269
12270
  this.trackSubscription(
12270
12271
  createNip29Filter({
12271
12272
  kinds: [NIP29_KINDS.DELETE_EVENT, NIP29_KINDS.REMOVE_USER, NIP29_KINDS.DELETE_GROUP],
12272
- "#h": groupIds
12273
+ "#h": groupIds,
12274
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12273
12275
  }),
12274
12276
  { onEvent: (event) => this.handleModerationEvent(event) }
12275
12277
  );
12276
12278
  }
12277
12279
  subscribeToGroup(groupId) {
12278
12280
  if (!this.client) return;
12279
- const latestTimestamp = this.getLatestMessageTimestamp([groupId]);
12281
+ const sinceTimestamp = this.getLatestKnownTimestamp([groupId]);
12280
12282
  this.trackSubscription(
12281
12283
  createNip29Filter({
12282
12284
  kinds: [NIP29_KINDS.CHAT_MESSAGE, NIP29_KINDS.THREAD_ROOT, NIP29_KINDS.THREAD_REPLY],
12283
12285
  "#h": [groupId],
12284
- ...latestTimestamp ? { since: latestTimestamp } : {}
12286
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12285
12287
  }),
12286
12288
  { onEvent: (event) => this.handleGroupEvent(event) }
12287
12289
  );
12288
12290
  this.trackSubscription(
12289
12291
  createNip29Filter({
12290
12292
  kinds: [NIP29_KINDS.DELETE_EVENT, NIP29_KINDS.REMOVE_USER, NIP29_KINDS.DELETE_GROUP],
12291
- "#h": [groupId]
12293
+ "#h": [groupId],
12294
+ ...sinceTimestamp ? { since: sinceTimestamp } : {}
12292
12295
  }),
12293
12296
  { onEvent: (event) => this.handleModerationEvent(event) }
12294
12297
  );
@@ -12355,7 +12358,7 @@ var GroupChatModule = class {
12355
12358
  }
12356
12359
  group.updatedAt = event.created_at * 1e3;
12357
12360
  this.groups.set(groupId, group);
12358
- this.persistGroups();
12361
+ this.schedulePersist();
12359
12362
  } else if (event.kind === NIP29_KINDS.GROUP_MEMBERS) {
12360
12363
  this.updateMembersFromEvent(groupId, event);
12361
12364
  } else if (event.kind === NIP29_KINDS.GROUP_ADMINS) {
@@ -12376,7 +12379,7 @@ var GroupChatModule = class {
12376
12379
  }
12377
12380
  }
12378
12381
  this.deps.emitEvent("groupchat:updated", {});
12379
- this.persistMessages();
12382
+ this.schedulePersist();
12380
12383
  } else if (event.kind === NIP29_KINDS.REMOVE_USER) {
12381
12384
  if (this.processedEventIds.has(event.id)) return;
12382
12385
  const eventTimestampMs = event.created_at * 1e3;
@@ -12437,7 +12440,7 @@ var GroupChatModule = class {
12437
12440
  };
12438
12441
  this.saveMemberToMemory(member);
12439
12442
  }
12440
- this.persistMembers();
12443
+ this.schedulePersist();
12441
12444
  }
12442
12445
  updateAdminsFromEvent(groupId, event) {
12443
12446
  const pTags = event.tags.filter((t) => t[0] === "p");
@@ -12457,7 +12460,7 @@ var GroupChatModule = class {
12457
12460
  });
12458
12461
  }
12459
12462
  }
12460
- this.persistMembers();
12463
+ this.schedulePersist();
12461
12464
  }
12462
12465
  // ===========================================================================
12463
12466
  // Group Membership Restoration
@@ -12468,13 +12471,11 @@ var GroupChatModule = class {
12468
12471
  if (!myPubkey) return [];
12469
12472
  const groupIdsWithMembership = /* @__PURE__ */ new Set();
12470
12473
  await this.oneshotSubscription(
12471
- new Filter3({ kinds: [NIP29_KINDS.GROUP_MEMBERS] }),
12474
+ createNip29Filter({ kinds: [NIP29_KINDS.GROUP_MEMBERS], "#p": [myPubkey] }),
12472
12475
  {
12473
12476
  onEvent: (event) => {
12474
12477
  const groupId = this.getGroupIdFromMetadataEvent(event);
12475
- if (!groupId) return;
12476
- const pTags = event.tags.filter((t) => t[0] === "p");
12477
- if (pTags.some((tag) => tag[1] === myPubkey)) {
12478
+ if (groupId) {
12478
12479
  groupIdsWithMembership.add(groupId);
12479
12480
  }
12480
12481
  },
@@ -12486,22 +12487,24 @@ var GroupChatModule = class {
12486
12487
  );
12487
12488
  if (groupIdsWithMembership.size === 0) return [];
12488
12489
  const restoredGroups = [];
12489
- for (const groupId of groupIdsWithMembership) {
12490
- if (this.groups.has(groupId)) continue;
12491
- try {
12492
- const group = await this.fetchGroupMetadataInternal(groupId);
12493
- if (group) {
12494
- this.groups.set(groupId, group);
12495
- restoredGroups.push(group);
12496
- await Promise.all([
12497
- this.fetchAndSaveMembers(groupId),
12498
- this.fetchMessages(groupId)
12499
- ]);
12490
+ await Promise.all(
12491
+ Array.from(groupIdsWithMembership).map(async (groupId) => {
12492
+ if (this.groups.has(groupId)) return;
12493
+ try {
12494
+ const group = await this.fetchGroupMetadataInternal(groupId);
12495
+ if (group) {
12496
+ this.groups.set(groupId, group);
12497
+ restoredGroups.push(group);
12498
+ await Promise.all([
12499
+ this.fetchAndSaveMembers(groupId),
12500
+ this.fetchMessages(groupId)
12501
+ ]);
12502
+ }
12503
+ } catch (error) {
12504
+ logger.warn("GroupChat", "Failed to restore group", groupId, error);
12500
12505
  }
12501
- } catch (error) {
12502
- logger.warn("GroupChat", "Failed to restore group", groupId, error);
12503
- }
12504
- }
12506
+ })
12507
+ );
12505
12508
  if (restoredGroups.length > 0) {
12506
12509
  await this.subscribeToJoinedGroups();
12507
12510
  this.deps.emitEvent("groupchat:updated", {});
@@ -12887,7 +12890,7 @@ var GroupChatModule = class {
12887
12890
  if (group && (group.unreadCount || 0) > 0) {
12888
12891
  group.unreadCount = 0;
12889
12892
  this.groups.set(groupId, group);
12890
- this.persistGroups();
12893
+ this.schedulePersist();
12891
12894
  }
12892
12895
  }
12893
12896
  // ===========================================================================
@@ -12909,7 +12912,7 @@ var GroupChatModule = class {
12909
12912
  if (eventId) {
12910
12913
  this.removeMemberFromMemory(groupId, userPubkey);
12911
12914
  this.deps.emitEvent("groupchat:updated", {});
12912
- this.persistMembers();
12915
+ this.schedulePersist();
12913
12916
  return true;
12914
12917
  }
12915
12918
  return false;
@@ -12932,7 +12935,7 @@ var GroupChatModule = class {
12932
12935
  if (eventId) {
12933
12936
  this.deleteMessageFromMemory(groupId, messageId);
12934
12937
  this.deps.emitEvent("groupchat:updated", {});
12935
- this.persistMessages();
12938
+ this.schedulePersist();
12936
12939
  return true;
12937
12940
  }
12938
12941
  return false;
@@ -13012,13 +13015,19 @@ var GroupChatModule = class {
13012
13015
  * or 0 if no messages exist. Used to set `since` on subscriptions so the relay
13013
13016
  * only sends events we don't already have.
13014
13017
  */
13015
- getLatestMessageTimestamp(groupIds) {
13018
+ getLatestKnownTimestamp(groupIds) {
13016
13019
  let latest = 0;
13017
13020
  for (const gid of groupIds) {
13018
13021
  const msgs = this.messages.get(gid);
13019
- if (!msgs) continue;
13020
- for (const m of msgs) {
13021
- const ts = Math.floor(m.timestamp / 1e3);
13022
+ if (msgs) {
13023
+ for (const m of msgs) {
13024
+ const ts = Math.floor(m.timestamp / 1e3);
13025
+ if (ts > latest) latest = ts;
13026
+ }
13027
+ }
13028
+ const group = this.groups.get(gid);
13029
+ if (group) {
13030
+ const ts = Math.floor((group.updatedAt || group.createdAt) / 1e3);
13022
13031
  if (ts > latest) latest = ts;
13023
13032
  }
13024
13033
  }
@@ -13093,7 +13102,7 @@ var GroupChatModule = class {
13093
13102
  });
13094
13103
  }
13095
13104
  }
13096
- this.persistMembers();
13105
+ this.schedulePersist();
13097
13106
  }
13098
13107
  async fetchGroupMembersInternal(groupId) {
13099
13108
  if (!this.client) return [];
@@ -13220,8 +13229,11 @@ var GroupChatModule = class {
13220
13229
  addProcessedEventId(eventId) {
13221
13230
  this.processedEventIds.add(eventId);
13222
13231
  if (this.processedEventIds.size > 1e4) {
13223
- const arr = Array.from(this.processedEventIds);
13224
- this.processedEventIds = new Set(arr.slice(arr.length - 1e4));
13232
+ let toDelete = 5e3;
13233
+ for (const id of this.processedEventIds) {
13234
+ if (toDelete-- <= 0) break;
13235
+ this.processedEventIds.delete(id);
13236
+ }
13225
13237
  }
13226
13238
  }
13227
13239
  // ===========================================================================