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