@calimero-network/mero-react 9.6.1 → 9.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
@@ -562,6 +562,10 @@ var NODE_ONLY = /* @__PURE__ */ new Set([
562
562
  "uninstallApplication",
563
563
  "generateContextIdentity",
564
564
  "deleteContext",
565
+ // A node's leaveContext is a local opt-out (a tombstone, its own identity rows)
566
+ // and publishes nothing; an account has nothing local to opt out of, and
567
+ // leaving the context's group instead is leaveGroup.
568
+ "leaveContext",
565
569
  "createContextAlias",
566
570
  "createApplicationAlias",
567
571
  "createDeviceAlias",
@@ -580,6 +584,11 @@ var NODE_ONLY = /* @__PURE__ */ new Set([
580
584
  "getMigrationStatus",
581
585
  "getCascadeStatus"
582
586
  ]);
587
+ function isAlreadyDirectMember(e) {
588
+ if (!(e instanceof Error)) return false;
589
+ const status = e.status;
590
+ return (status === void 0 || status === 409) && /signer [0-9a-f]+ is a direct member/i.test(e.message);
591
+ }
583
592
  function createAccountAdmin(input, deps = {}) {
584
593
  const { session: s, read, app } = input;
585
594
  const governGroupOp = deps.govern?.group ?? governGroup;
@@ -610,8 +619,17 @@ function createAccountAdmin(input, deps = {}) {
610
619
  await knowNamespaceOf(groupId);
611
620
  return governRootOp(session, groupId, op);
612
621
  };
622
+ async function joinOpen(groupId) {
623
+ try {
624
+ await root(s, groupId, meroJs.memberJoinedOpenOp({ member: s.account, groupId, credential: s.credential }));
625
+ return true;
626
+ } catch (e) {
627
+ if (isAlreadyDirectMember(e)) return false;
628
+ throw e;
629
+ }
630
+ }
613
631
  async function isMemberOfContextGroup(contextId, knownGroupId) {
614
- const groupId = knownGroupId ?? String(await relay().getContextGroup(contextId));
632
+ const groupId = String(await relay().getContextGroup(contextId));
615
633
  const { members } = await relay().listGroupMembers(groupId);
616
634
  return members.some((m) => m.identity.toLowerCase() === s.account.toLowerCase());
617
635
  }
@@ -679,11 +697,6 @@ function createAccountAdmin(input, deps = {}) {
679
697
  async leaveNamespace(namespaceId) {
680
698
  await group(s, namespaceId, meroJs.memberLeftOp(s.account));
681
699
  },
682
- // An account holds no context identity: leaving a context is leaving its group.
683
- async leaveContext(contextId) {
684
- const groupId = await relay().getContextGroup(contextId);
685
- await group(s, String(groupId), meroJs.memberLeftOp(s.account));
686
- },
687
700
  // An account runs as itself in every context it can reach (the relay
688
701
  // executes as the account), so its identity there is the account. Owned
689
702
  // when its account is a member of the context's group, as on a node a
@@ -692,11 +705,7 @@ function createAccountAdmin(input, deps = {}) {
692
705
  return { identities: await isMemberOfContextGroup(contextId) ? [s.account] : [] };
693
706
  },
694
707
  async joinContext(contextId) {
695
- const groupId = String(await relay().getContextGroup(contextId));
696
- if (await isMemberOfContextGroup(contextId, groupId)) {
697
- return { contextId, memberPublicKey: s.account };
698
- }
699
- await root(s, groupId, meroJs.memberJoinedOpenOp({ member: s.account, groupId, credential: s.credential }));
708
+ await joinOpen(String(await relay().getContextGroup(contextId)));
700
709
  return { contextId, memberPublicKey: s.account };
701
710
  },
702
711
  async createContext(req) {
@@ -738,12 +747,7 @@ function createAccountAdmin(input, deps = {}) {
738
747
  // An open subgroup is joined the way `joinContext` joins one: MemberJoinedOpen
739
748
  // on the subgroup, and nothing to do for a member already in it.
740
749
  async joinSubgroupInheritance(groupId) {
741
- const { members } = await relay().listGroupMembers(groupId);
742
- if (members.some((m) => m.identity.toLowerCase() === s.account.toLowerCase())) {
743
- return { groupId, memberPublicKey: s.account, wasInherited: false };
744
- }
745
- await root(s, groupId, meroJs.memberJoinedOpenOp({ member: s.account, groupId, credential: s.credential }));
746
- return { groupId, memberPublicKey: s.account, wasInherited: true };
750
+ return { groupId, memberPublicKey: s.account, wasInherited: await joinOpen(groupId) };
747
751
  },
748
752
  async detachContextFromGroup(groupId, contextId) {
749
753
  await group(s, groupId, meroJs.contextDetachedOp(contextId));