From b013d97a4400bdbbcc182077fc6f8bb62d471fbf Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 13 Jun 2024 18:09:32 -0700 Subject: [PATCH 1/4] fix: inbox id versus client inbox id --- .../modules/xmtpreactnativesdk/XMTPModule.kt | 37 +++++++++---------- example/src/tests/groupPermissionsTests.ts | 15 ++++---- src/index.ts | 33 ++++++++++++----- src/lib/Group.ts | 12 +++--- 4 files changed, 55 insertions(+), 42 deletions(-) diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index c1642afe6..aa76efc15 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -947,21 +947,21 @@ class XMTPModule : Module() { } } - AsyncFunction("isAdmin") Coroutine { id: String, inboxId: String -> + AsyncFunction("isAdmin") Coroutine { clientInboxId: String, id: String, inboxId: String -> withContext(Dispatchers.IO) { logV("isGroupAdmin") - val client = clients[inboxId] ?: throw XMTPException("No client") - val group = findGroup(inboxId, id) + val client = clients[clientInboxId] ?: throw XMTPException("No client") + val group = findGroup(clientInboxId, id) group?.isAdmin(inboxId) } } - AsyncFunction("isSuperAdmin") Coroutine { id: String, inboxId: String -> + AsyncFunction("isSuperAdmin") Coroutine { clientInboxId: String, id: String, inboxId: String -> withContext(Dispatchers.IO) { logV("isSuperAdmin") - val client = clients[inboxId] ?: throw XMTPException("No client") - val group = findGroup(inboxId, id) + val client = clients[clientInboxId] ?: throw XMTPException("No client") + val group = findGroup(clientInboxId, id) group?.isSuperAdmin(inboxId) } @@ -987,41 +987,40 @@ class XMTPModule : Module() { } } - AsyncFunction("addAdmin") Coroutine { id: String, inboxId: String -> + AsyncFunction("addAdmin") Coroutine { clientInboxId: String, id: String, inboxId: String -> withContext(Dispatchers.IO) { logV("addAdmin") - val client = clients[inboxId] ?: throw XMTPException("No client") - val group = findGroup(inboxId, id) - + val client = clients[clientInboxId] ?: throw XMTPException("No client") + val group = findGroup(clientInboxId, id) group?.addAdmin(inboxId) } } - AsyncFunction("addSuperAdmin") Coroutine { id: String, inboxId: String -> + AsyncFunction("addSuperAdmin") Coroutine { clientInboxId: String, id: String, inboxId: String -> withContext(Dispatchers.IO) { logV("addSuperAdmin") - val client = clients[inboxId] ?: throw XMTPException("No client") - val group = findGroup(inboxId, id) + val client = clients[clientInboxId] ?: throw XMTPException("No client") + val group = findGroup(clientInboxId, id) group?.addSuperAdmin(inboxId) } } - AsyncFunction("removeAdmin") Coroutine { id: String, inboxId: String -> + AsyncFunction("removeAdmin") Coroutine { clientInboxId: String, id: String, inboxId: String -> withContext(Dispatchers.IO) { logV("removeAdmin") - val client = clients[inboxId] ?: throw XMTPException("No client") - val group = findGroup(inboxId, id) + val client = clients[clientInboxId] ?: throw XMTPException("No client") + val group = findGroup(clientInboxId, id) group?.removeAdmin(inboxId) } } - AsyncFunction("removeSuperAdmin") Coroutine { id: String, inboxId: String -> + AsyncFunction("removeSuperAdmin") Coroutine { clientInboxId: String, id: String, inboxId: String -> withContext(Dispatchers.IO) { logV("removeSuperAdmin") - val client = clients[inboxId] ?: throw XMTPException("No client") - val group = findGroup(inboxId, id) + val client = clients[clientInboxId] ?: throw XMTPException("No client") + val group = findGroup(clientInboxId, id) group?.removeSuperAdmin(inboxId) } diff --git a/example/src/tests/groupPermissionsTests.ts b/example/src/tests/groupPermissionsTests.ts index 129cf206b..1c865b6b0 100644 --- a/example/src/tests/groupPermissionsTests.ts +++ b/example/src/tests/groupPermissionsTests.ts @@ -85,7 +85,7 @@ test('in admin only group, members can not update group name unless they are an // Alix Create a group const alixGroup = await alix.conversations.newGroup( [bo.address, caro.address], - 'admin_only' + { permissionLevel: 'admin_only' } ) if (alixGroup.permissionLevel !== 'admin_only') { @@ -120,7 +120,7 @@ test('in admin only group, members can update group name once they are an admin' // Alix Create a group const alixGroup = await alix.conversations.newGroup( [bo.address, caro.address], - 'admin_only' + { permissionLevel: 'admin_only' } ) if (alixGroup.permissionLevel !== 'admin_only') { @@ -171,7 +171,7 @@ test('in admin only group, members can not update group name after admin status // Alix Create a group const alixGroup = await alix.conversations.newGroup( [bo.address, caro.address], - 'admin_only' + { permissionLevel: 'admin_only' } ) if (alixGroup.permissionLevel !== 'admin_only') { @@ -235,10 +235,9 @@ test('can not remove a super admin from a group', async () => { const [alix, bo] = await createClients(3) // Alix Create a group - const alixGroup = await alix.conversations.newGroup( - [bo.address], - 'all_members' - ) + const alixGroup = await alix.conversations.newGroup([bo.address], { + permissionLevel: 'all_members', + }) let alixIsSuperAdmin = await alixGroup.isSuperAdmin(alix.inboxId) let boIsSuperAdmin = await alixGroup.isSuperAdmin(bo.inboxId) @@ -323,7 +322,7 @@ test('can commit after invalid permissions commit', async () => { // Bo creates a group with Alix and Caro const boGroup = await bo.conversations.newGroup( [alix.address, caro.address], - 'all_members' + { permissionLevel: 'admin_only' } ) await alix.conversations.syncGroups() const alixGroup = (await alix.conversations.listGroups())[0] diff --git a/src/index.ts b/src/index.ts index 058363456..7ab57618e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -742,15 +742,20 @@ export async function creatorInboxId( return XMTPModule.creatorInboxId(inboxId, id) as InboxId } -export async function isAdmin(id: string, inboxId: string): Promise { - return XMTPModule.isAdmin(id, inboxId) +export async function isAdmin( + clientInboxId: string, + id: string, + inboxId: string +): Promise { + return XMTPModule.isAdmin(clientInboxId, id, inboxId) } export async function isSuperAdmin( + clientInboxId: string, id: string, inboxId: string ): Promise { - return XMTPModule.isSuperAdmin(id, inboxId) + return XMTPModule.isSuperAdmin(clientInboxId, id, inboxId) } export async function listAdmins( @@ -767,26 +772,36 @@ export async function listSuperAdmins( return XMTPModule.listSuperAdmins(inboxId, id) } -export async function addAdmin(id: string, inboxId: string): Promise { - return XMTPModule.addAdmin(id, inboxId) +export async function addAdmin( + clientInboxId: string, + id: string, + inboxId: string +): Promise { + return XMTPModule.addAdmin(clientInboxId, id, inboxId) } export async function addSuperAdmin( + clientInboxId: string, id: string, inboxId: string ): Promise { - return XMTPModule.addSuperAdmin(id, inboxId) + return XMTPModule.addSuperAdmin(clientInboxId, id, inboxId) } -export async function removeAdmin(id: string, inboxId: string): Promise { - return XMTPModule.removeAdmin(id, inboxId) +export async function removeAdmin( + clientInboxId: string, + id: string, + inboxId: string +): Promise { + return XMTPModule.removeAdmin(clientInboxId, id, inboxId) } export async function removeSuperAdmin( + clientInboxId: string, id: string, inboxId: string ): Promise { - return XMTPModule.removeSuperAdmin(id, inboxId) + return XMTPModule.removeSuperAdmin(clientInboxId, id, inboxId) } export async function allowGroups( diff --git a/src/lib/Group.ts b/src/lib/Group.ts index 62824beab..9522699d6 100644 --- a/src/lib/Group.ts +++ b/src/lib/Group.ts @@ -288,7 +288,7 @@ export class Group< * To get the latest admin status from the network, call sync() first. */ async isAdmin(inboxId: InboxId): Promise { - return XMTP.isAdmin(this.id, inboxId) + return XMTP.isAdmin(this.client.inboxId, this.id, inboxId) } /** @@ -298,7 +298,7 @@ export class Group< * To get the latest super admin status from the network, call sync() first. */ async isSuperAdmin(inboxId: InboxId): Promise { - return XMTP.isSuperAdmin(this.id, inboxId) + return XMTP.isSuperAdmin(this.client.inboxId, this.id, inboxId) } /** @@ -326,7 +326,7 @@ export class Group< * Will throw if the user does not have the required permissions. */ async addAdmin(inboxId: InboxId): Promise { - return XMTP.addAdmin(this.id, inboxId) + return XMTP.addAdmin(this.client.inboxId, this.id, inboxId) } /** @@ -336,7 +336,7 @@ export class Group< * Will throw if the user does not have the required permissions. */ async addSuperAdmin(inboxId: InboxId): Promise { - return XMTP.addSuperAdmin(this.id, inboxId) + return XMTP.addSuperAdmin(this.client.inboxId, this.id, inboxId) } /** @@ -346,7 +346,7 @@ export class Group< * Will throw if the user does not have the required permissions. */ async removeAdmin(inboxId: InboxId): Promise { - return XMTP.removeAdmin(this.id, inboxId) + return XMTP.removeAdmin(this.client.inboxId, this.id, inboxId) } /** @@ -356,7 +356,7 @@ export class Group< * Will throw if the user does not have the required permissions. */ async removeSuperAdmin(inboxId: InboxId): Promise { - return XMTP.removeSuperAdmin(this.id, inboxId) + return XMTP.removeSuperAdmin(this.client.inboxId, this.id, inboxId) } async processMessage( From 8db478386e36bdbb3d1de5917e8e144f330a19dd Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 13 Jun 2024 18:37:13 -0700 Subject: [PATCH 2/4] fix it on iOS --- ios/XMTPModule.swift | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index 9032eefa4..005db6d3f 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -847,21 +847,21 @@ public class XMTPModule: Module { return try group.creatorInboxId() } - AsyncFunction("isAdmin") { (id: String, inboxId: String) -> Bool in - guard let client = await clientsManager.getClient(key: inboxId) else { + AsyncFunction("isAdmin") { (clientInboxId: String, id: String, inboxId: String) -> Bool in + guard let client = await clientsManager.getClient(key: clientInboxId) else { throw Error.noClient } - guard let group = try await findGroup(inboxId: inboxId, id: id) else { + guard let group = try await findGroup(inboxId: clientInboxId, id: id) else { throw Error.conversationNotFound("no group found for \(id)") } return try group.isAdmin(inboxId: inboxId) } - AsyncFunction("isSuperAdmin") { (id: String, inboxId: String) -> Bool in - guard let client = await clientsManager.getClient(key: inboxId) else { + AsyncFunction("isSuperAdmin") { (clientInboxId: String, id: String, inboxId: String) -> Bool in + guard let client = await clientsManager.getClient(key: clientInboxId) else { throw Error.noClient } - guard let group = try await findGroup(inboxId: inboxId, id: id) else { + guard let group = try await findGroup(inboxId: clientInboxId, id: id) else { throw Error.conversationNotFound("no group found for \(id)") } return try group.isSuperAdmin(inboxId: inboxId) @@ -887,41 +887,41 @@ public class XMTPModule: Module { return try group.listSuperAdmins() } - AsyncFunction("addAdmin") { (id: String, inboxId: String) in - guard let client = await clientsManager.getClient(key: inboxId) else { + AsyncFunction("addAdmin") { (clientInboxId: String, id: String, inboxId: String) in + guard let client = await clientsManager.getClient(key: clientInboxId) else { throw Error.noClient } - guard let group = try await findGroup(inboxId: inboxId, id: id) else { + guard let group = try await findGroup(inboxId: clientInboxId, id: id) else { throw Error.conversationNotFound("no group found for \(id)") } try await group.addAdmin(inboxId: inboxId) } - AsyncFunction("addSuperAdmin") { (id: String, inboxId: String) in - guard let client = await clientsManager.getClient(key: inboxId) else { + AsyncFunction("addSuperAdmin") { (clientInboxId: String, id: String, inboxId: String) in + guard let client = await clientsManager.getClient(key: clientInboxId) else { throw Error.noClient } - guard let group = try await findGroup(inboxId: inboxId, id: id) else { + guard let group = try await findGroup(inboxId: clientInboxId, id: id) else { throw Error.conversationNotFound("no group found for \(id)") } try await group.addSuperAdmin(inboxId: inboxId) } - AsyncFunction("removeAdmin") { (id: String, inboxId: String) in - guard let client = await clientsManager.getClient(key: inboxId) else { + AsyncFunction("removeAdmin") { (clientInboxId: String, id: String, inboxId: String) in + guard let client = await clientsManager.getClient(key: clientInboxId) else { throw Error.noClient } - guard let group = try await findGroup(inboxId: inboxId, id: id) else { + guard let group = try await findGroup(inboxId: clientInboxId, id: id) else { throw Error.conversationNotFound("no group found for \(id)") } try await group.removeAdmin(inboxId: inboxId) } - AsyncFunction("removeSuperAdmin") { (id: String, inboxId: String) in - guard let client = await clientsManager.getClient(key: inboxId) else { + AsyncFunction("removeSuperAdmin") { (clientInboxId: String, id: String, inboxId: String) in + guard let client = await clientsManager.getClient(key: clientInboxId) else { throw Error.noClient } - guard let group = try await findGroup(inboxId: inboxId, id: id) else { + guard let group = try await findGroup(inboxId: clientInboxId, id: id) else { throw Error.conversationNotFound("no group found for \(id)") } try await group.removeSuperAdmin(inboxId: inboxId) From a6069730a21c4b087462b0970190ba1ce3c45796 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 13 Jun 2024 18:45:16 -0700 Subject: [PATCH 3/4] fix up the test --- example/src/tests/groupPermissionsTests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/src/tests/groupPermissionsTests.ts b/example/src/tests/groupPermissionsTests.ts index 1c865b6b0..bab1bb89d 100644 --- a/example/src/tests/groupPermissionsTests.ts +++ b/example/src/tests/groupPermissionsTests.ts @@ -345,7 +345,7 @@ test('can commit after invalid permissions commit', async () => { // Verify that Alix can update the group name await boGroup.sync() await alixGroup.sync() - await alixGroup.updateGroupName('Alix group name') + await boGroup.updateGroupName('Alix group name') await alixGroup.sync() await boGroup.sync() assert( From 7e1ddef6225e5d4f91b760ff8d46558522a68ae2 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 13 Jun 2024 18:46:09 -0700 Subject: [PATCH 4/4] another pass on the test --- example/src/tests/groupPermissionsTests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/src/tests/groupPermissionsTests.ts b/example/src/tests/groupPermissionsTests.ts index bab1bb89d..81121f9b7 100644 --- a/example/src/tests/groupPermissionsTests.ts +++ b/example/src/tests/groupPermissionsTests.ts @@ -322,7 +322,7 @@ test('can commit after invalid permissions commit', async () => { // Bo creates a group with Alix and Caro const boGroup = await bo.conversations.newGroup( [alix.address, caro.address], - { permissionLevel: 'admin_only' } + { permissionLevel: 'all_members' } ) await alix.conversations.syncGroups() const alixGroup = (await alix.conversations.listGroups())[0] @@ -345,7 +345,7 @@ test('can commit after invalid permissions commit', async () => { // Verify that Alix can update the group name await boGroup.sync() await alixGroup.sync() - await boGroup.updateGroupName('Alix group name') + await alixGroup.updateGroupName('Alix group name') await alixGroup.sync() await boGroup.sync() assert(