Skip to content

Commit

Permalink
Enable group permissions update in Node SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Dec 20, 2024
1 parent dd18dd8 commit 5d59a95
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
20 changes: 18 additions & 2 deletions sdks/node-sdk/src/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type {
ConsentState,
Conversation as Group,
ListMessagesOptions,
MetadataField,
PermissionPolicy,
PermissionUpdateType,
} from "@xmtp/node-bindings";
import { AsyncStream, type StreamCallback } from "@/AsyncStream";
import type { Client } from "@/Client";
Expand Down Expand Up @@ -92,12 +95,25 @@ export class Conversation {
}

get permissions() {
const permissions = this.#group.groupPermissions();
return {
policyType: this.#group.groupPermissions().policyType(),
policySet: this.#group.groupPermissions().policySet(),
policyType: permissions.policyType(),
policySet: permissions.policySet(),
};
}

async updatePermission(
permissionType: PermissionUpdateType,
policy: PermissionPolicy,
metadataField?: MetadataField,
) {
return this.#group.updatePermissionPolicy(
permissionType,
policy,
metadataField,
);
}

isAdmin(inboxId: string) {
return this.#group.isAdmin(inboxId);
}
Expand Down
1 change: 1 addition & 0 deletions sdks/node-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export {
GroupPermissions,
GroupPermissionsOptions,
LogLevel,
MetadataField,
PermissionLevel,
PermissionPolicy,
PermissionUpdateType,
Expand Down
83 changes: 82 additions & 1 deletion sdks/node-sdk/test/Conversation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ConsentState } from "@xmtp/node-bindings";
import {
ConsentState,
MetadataField,
PermissionPolicy,
PermissionUpdateType,
} from "@xmtp/node-bindings";
import { describe, expect, it } from "vitest";
import {
ContentTypeTest,
Expand Down Expand Up @@ -427,4 +432,80 @@ describe("Conversation", () => {
await dmGroup2!.send("gm!");
expect(dmGroup2!.consentState).toBe(ConsentState.Allowed);
});

it("should update group permissions", async () => {
const user1 = createUser();
const user2 = createUser();
const client1 = await createRegisteredClient(user1);
await createRegisteredClient(user2);
const conversation = await client1.conversations.newGroup([
user2.account.address,
]);

expect(conversation.permissions.policySet).toEqual({
addMemberPolicy: 0,
removeMemberPolicy: 2,
addAdminPolicy: 3,
removeAdminPolicy: 3,
updateGroupNamePolicy: 0,
updateGroupDescriptionPolicy: 0,
updateGroupImageUrlSquarePolicy: 0,
updateGroupPinnedFrameUrlPolicy: 0,
});

await conversation.updatePermission(
PermissionUpdateType.AddMember,
PermissionPolicy.Admin,
);

await conversation.updatePermission(
PermissionUpdateType.RemoveMember,
PermissionPolicy.SuperAdmin,
);

await conversation.updatePermission(
PermissionUpdateType.AddAdmin,
PermissionPolicy.Admin,
);

await conversation.updatePermission(
PermissionUpdateType.RemoveAdmin,
PermissionPolicy.Admin,
);

await conversation.updatePermission(
PermissionUpdateType.UpdateMetadata,
PermissionPolicy.Admin,
MetadataField.GroupName,
);

await conversation.updatePermission(
PermissionUpdateType.UpdateMetadata,
PermissionPolicy.Admin,
MetadataField.Description,
);

await conversation.updatePermission(
PermissionUpdateType.UpdateMetadata,
PermissionPolicy.Admin,
MetadataField.ImageUrlSquare,
);

await conversation.updatePermission(
PermissionUpdateType.UpdateMetadata,
PermissionPolicy.Admin,
MetadataField.PinnedFrameUrl,
);

expect(conversation.permissions.policySet).toEqual({
addMemberPolicy: 2,
removeMemberPolicy: 3,
addAdminPolicy: 2,
removeAdminPolicy: 2,
updateGroupNamePolicy: 2,
updateGroupDescriptionPolicy: 2,
updateGroupImageUrlSquarePolicy: 2,
updateGroupPinnedFrameUrlPolicy: 2,
});
});
});

0 comments on commit 5d59a95

Please sign in to comment.