@unicitylabs/sphere-sdk 0.6.0 → 0.6.1

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/README.md CHANGED
@@ -409,6 +409,12 @@ const privateGroup = await gc.createGroup({
409
409
  visibility: GroupVisibility.PRIVATE,
410
410
  });
411
411
 
412
+ // Create a write-restricted group (only admins/writers can post)
413
+ const announcements = await gc.createGroup({
414
+ name: 'Announcements',
415
+ writeRestricted: true,
416
+ });
417
+
412
418
  // Discover and join
413
419
  const available = await gc.fetchAvailableGroups(); // public groups on relay
414
420
  await gc.joinGroup(group.id);
@@ -455,6 +461,7 @@ const members = gc.getMembers(group.id);
455
461
  gc.isCurrentUserAdmin(group.id); // boolean
456
462
  gc.isCurrentUserModerator(group.id); // boolean
457
463
  await gc.canModerateGroup(group.id); // includes relay admin check
464
+ gc.canWriteToGroup(group.id); // false if write-restricted and not admin/moderator
458
465
 
459
466
  // Moderate (requires admin/moderator role)
460
467
  await gc.kickUser(group.id, userPubkey, 'reason');
@@ -487,6 +494,7 @@ interface GroupData {
487
494
  name: string;
488
495
  description?: string;
489
496
  visibility: GroupVisibility; // 'PUBLIC' | 'PRIVATE'
497
+ writeRestricted?: boolean; // Only admins and moderators can post
490
498
  memberCount?: number;
491
499
  unreadCount?: number;
492
500
  lastMessageTime?: number;
@@ -9161,17 +9161,23 @@ var GroupChatModule = class {
9161
9161
  const group = this.groups.get(groupId);
9162
9162
  if (!group) return;
9163
9163
  if (event.kind === NIP29_KINDS.GROUP_METADATA) {
9164
- if (!event.content || event.content.trim() === "") return;
9165
- try {
9166
- const metadata = JSON.parse(event.content);
9167
- group.name = metadata.name || group.name;
9168
- group.description = metadata.about || group.description;
9169
- group.picture = metadata.picture || group.picture;
9170
- group.updatedAt = event.created_at * 1e3;
9171
- this.groups.set(groupId, group);
9172
- this.persistGroups();
9173
- } catch {
9164
+ if (event.content && event.content.trim()) {
9165
+ try {
9166
+ const metadata = JSON.parse(event.content);
9167
+ group.name = metadata.name || group.name;
9168
+ group.description = metadata.about || group.description;
9169
+ group.picture = metadata.picture || group.picture;
9170
+ if (metadata["write-restricted"] === true) group.writeRestricted = true;
9171
+ else group.writeRestricted = void 0;
9172
+ } catch {
9173
+ }
9174
9174
  }
9175
+ for (const tag of event.tags) {
9176
+ if (tag[0] === "write-restricted") group.writeRestricted = true;
9177
+ }
9178
+ group.updatedAt = event.created_at * 1e3;
9179
+ this.groups.set(groupId, group);
9180
+ this.persistGroups();
9175
9181
  } else if (event.kind === NIP29_KINDS.GROUP_MEMBERS) {
9176
9182
  this.updateMembersFromEvent(groupId, event);
9177
9183
  } else if (event.kind === NIP29_KINDS.GROUP_ADMINS) {
@@ -9475,7 +9481,8 @@ var GroupChatModule = class {
9475
9481
  picture: options.picture,
9476
9482
  closed: true,
9477
9483
  private: isPrivate,
9478
- hidden: isPrivate
9484
+ hidden: isPrivate,
9485
+ ...options.writeRestricted ? { "write-restricted": true } : {}
9479
9486
  })
9480
9487
  });
9481
9488
  if (!eventId) return null;
@@ -9776,6 +9783,17 @@ var GroupChatModule = class {
9776
9783
  const admins = await this.fetchRelayAdmins();
9777
9784
  return admins.has(myPubkey);
9778
9785
  }
9786
+ /**
9787
+ * Check if current user can write messages to a group.
9788
+ * For write-restricted groups, only admins/moderators can post.
9789
+ * For normal groups, any member can post.
9790
+ */
9791
+ canWriteToGroup(groupId) {
9792
+ const group = this.groups.get(groupId);
9793
+ if (!group) return false;
9794
+ if (!group.writeRestricted) return true;
9795
+ return this.isCurrentUserModerator(groupId);
9796
+ }
9779
9797
  getCurrentUserRole(groupId) {
9780
9798
  const myPubkey = this.getMyPublicKey();
9781
9799
  if (!myPubkey) return null;
@@ -10150,6 +10168,7 @@ var GroupChatModule = class {
10150
10168
  let description;
10151
10169
  let picture;
10152
10170
  let isPrivate = false;
10171
+ let writeRestricted = false;
10153
10172
  if (event.content && event.content.trim()) {
10154
10173
  try {
10155
10174
  const metadata = JSON.parse(event.content);
@@ -10157,6 +10176,7 @@ var GroupChatModule = class {
10157
10176
  description = metadata.about || metadata.description;
10158
10177
  picture = metadata.picture;
10159
10178
  isPrivate = metadata.private === true;
10179
+ if (metadata["write-restricted"] === true) writeRestricted = true;
10160
10180
  } catch {
10161
10181
  }
10162
10182
  }
@@ -10166,6 +10186,7 @@ var GroupChatModule = class {
10166
10186
  if (tag[0] === "picture" && tag[1]) picture = tag[1];
10167
10187
  if (tag[0] === "private") isPrivate = true;
10168
10188
  if (tag[0] === "public" && tag[1] === "false") isPrivate = true;
10189
+ if (tag[0] === "write-restricted") writeRestricted = true;
10169
10190
  }
10170
10191
  return {
10171
10192
  id: groupId,
@@ -10174,6 +10195,7 @@ var GroupChatModule = class {
10174
10195
  description,
10175
10196
  picture,
10176
10197
  visibility: isPrivate ? GroupVisibility.PRIVATE : GroupVisibility.PUBLIC,
10198
+ writeRestricted: writeRestricted || void 0,
10177
10199
  createdAt: event.created_at * 1e3
10178
10200
  };
10179
10201
  } catch {