@unicitylabs/sphere-sdk 0.6.0 → 0.6.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.
package/dist/index.cjs CHANGED
@@ -8767,6 +8767,12 @@ var CommunicationsModule = class {
8767
8767
  this.unsubscribeComposing = deps.transport.onComposing?.((indicator) => {
8768
8768
  this.handleComposingIndicator(indicator);
8769
8769
  }) ?? null;
8770
+ if (deps.transport.onChatReady) {
8771
+ deps.transport.onChatReady(() => {
8772
+ const conversations = this.getConversations();
8773
+ deps.emitEvent("communications:ready", { conversationCount: conversations.size });
8774
+ });
8775
+ }
8770
8776
  }
8771
8777
  /**
8772
8778
  * Load messages from storage.
@@ -9409,6 +9415,7 @@ var GroupChatModule = class {
9409
9415
  await this.subscribeToJoinedGroups();
9410
9416
  }
9411
9417
  this.deps.emitEvent("groupchat:connection", { connected: true });
9418
+ this.deps.emitEvent("groupchat:ready", { groupCount: this.groups.size });
9412
9419
  } catch (error) {
9413
9420
  logger.error("GroupChat", "Failed to connect to relays", error);
9414
9421
  this.deps.emitEvent("groupchat:connection", { connected: false });
@@ -9527,17 +9534,23 @@ var GroupChatModule = class {
9527
9534
  const group = this.groups.get(groupId);
9528
9535
  if (!group) return;
9529
9536
  if (event.kind === NIP29_KINDS.GROUP_METADATA) {
9530
- if (!event.content || event.content.trim() === "") return;
9531
- try {
9532
- const metadata = JSON.parse(event.content);
9533
- group.name = metadata.name || group.name;
9534
- group.description = metadata.about || group.description;
9535
- group.picture = metadata.picture || group.picture;
9536
- group.updatedAt = event.created_at * 1e3;
9537
- this.groups.set(groupId, group);
9538
- this.persistGroups();
9539
- } catch {
9537
+ if (event.content && event.content.trim()) {
9538
+ try {
9539
+ const metadata = JSON.parse(event.content);
9540
+ group.name = metadata.name || group.name;
9541
+ group.description = metadata.about || group.description;
9542
+ group.picture = metadata.picture || group.picture;
9543
+ if (metadata["write-restricted"] === true) group.writeRestricted = true;
9544
+ else group.writeRestricted = void 0;
9545
+ } catch {
9546
+ }
9540
9547
  }
9548
+ for (const tag of event.tags) {
9549
+ if (tag[0] === "write-restricted") group.writeRestricted = true;
9550
+ }
9551
+ group.updatedAt = event.created_at * 1e3;
9552
+ this.groups.set(groupId, group);
9553
+ this.persistGroups();
9541
9554
  } else if (event.kind === NIP29_KINDS.GROUP_MEMBERS) {
9542
9555
  this.updateMembersFromEvent(groupId, event);
9543
9556
  } else if (event.kind === NIP29_KINDS.GROUP_ADMINS) {
@@ -9841,7 +9854,8 @@ var GroupChatModule = class {
9841
9854
  picture: options.picture,
9842
9855
  closed: true,
9843
9856
  private: isPrivate,
9844
- hidden: isPrivate
9857
+ hidden: isPrivate,
9858
+ ...options.writeRestricted ? { "write-restricted": true } : {}
9845
9859
  })
9846
9860
  });
9847
9861
  if (!eventId) return null;
@@ -10036,6 +10050,19 @@ var GroupChatModule = class {
10036
10050
  getMessages(groupId) {
10037
10051
  return (this.messages.get(groupId) || []).sort((a, b) => a.timestamp - b.timestamp);
10038
10052
  }
10053
+ getMessagesPage(groupId, options) {
10054
+ const limit = options?.limit ?? 20;
10055
+ const before = options?.before ?? Infinity;
10056
+ const groupMessages = this.messages.get(groupId) ?? [];
10057
+ const filtered = groupMessages.filter((m) => m.timestamp < before).sort((a, b) => b.timestamp - a.timestamp);
10058
+ const page = filtered.slice(0, limit);
10059
+ return {
10060
+ messages: page.reverse(),
10061
+ // chronological order
10062
+ hasMore: filtered.length > limit,
10063
+ oldestTimestamp: page.length > 0 ? page[0].timestamp : null
10064
+ };
10065
+ }
10039
10066
  getMembers(groupId) {
10040
10067
  return (this.members.get(groupId) || []).sort((a, b) => a.joinedAt - b.joinedAt);
10041
10068
  }
@@ -10142,6 +10169,17 @@ var GroupChatModule = class {
10142
10169
  const admins = await this.fetchRelayAdmins();
10143
10170
  return admins.has(myPubkey);
10144
10171
  }
10172
+ /**
10173
+ * Check if current user can write messages to a group.
10174
+ * For write-restricted groups, only admins/moderators can post.
10175
+ * For normal groups, any member can post.
10176
+ */
10177
+ canWriteToGroup(groupId) {
10178
+ const group = this.groups.get(groupId);
10179
+ if (!group) return false;
10180
+ if (!group.writeRestricted) return true;
10181
+ return this.isCurrentUserModerator(groupId);
10182
+ }
10145
10183
  getCurrentUserRole(groupId) {
10146
10184
  const myPubkey = this.getMyPublicKey();
10147
10185
  if (!myPubkey) return null;
@@ -10516,6 +10554,7 @@ var GroupChatModule = class {
10516
10554
  let description;
10517
10555
  let picture;
10518
10556
  let isPrivate = false;
10557
+ let writeRestricted = false;
10519
10558
  if (event.content && event.content.trim()) {
10520
10559
  try {
10521
10560
  const metadata = JSON.parse(event.content);
@@ -10523,6 +10562,7 @@ var GroupChatModule = class {
10523
10562
  description = metadata.about || metadata.description;
10524
10563
  picture = metadata.picture;
10525
10564
  isPrivate = metadata.private === true;
10565
+ if (metadata["write-restricted"] === true) writeRestricted = true;
10526
10566
  } catch {
10527
10567
  }
10528
10568
  }
@@ -10532,6 +10572,7 @@ var GroupChatModule = class {
10532
10572
  if (tag[0] === "picture" && tag[1]) picture = tag[1];
10533
10573
  if (tag[0] === "private") isPrivate = true;
10534
10574
  if (tag[0] === "public" && tag[1] === "false") isPrivate = true;
10575
+ if (tag[0] === "write-restricted") writeRestricted = true;
10535
10576
  }
10536
10577
  return {
10537
10578
  id: groupId,
@@ -10540,6 +10581,7 @@ var GroupChatModule = class {
10540
10581
  description,
10541
10582
  picture,
10542
10583
  visibility: isPrivate ? GroupVisibility.PRIVATE : GroupVisibility.PUBLIC,
10584
+ writeRestricted: writeRestricted || void 0,
10543
10585
  createdAt: event.created_at * 1e3
10544
10586
  };
10545
10587
  } catch {