Skip to content

Commit

Permalink
Added ios side and new test for super admin remove
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed May 30, 2024
1 parent d41603d commit 388c7c4
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 3 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 95d6ace79946933ecf80684613842ee553dd76a2

COCOAPODS: 1.14.2
COCOAPODS: 1.15.2
87 changes: 87 additions & 0 deletions example/src/tests/groupPermissionsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,90 @@ test('in admin only group, members can not update group name after admin status
// throw new Error('Expected exception when non-admin attempts to update group name.')
return true
})

test('can not remove a super admin from a group', async () => {
// Create clients
const [alix, bo] = await createClients(3)

// Alix Create a group
const alixGroup = await alix.conversations.newGroup(
[bo.address],
'all_members'
)

let alixIsSuperAdmin = await alixGroup.isSuperAdmin(alix.inboxId)
let boIsSuperAdmin = await alixGroup.isSuperAdmin(bo.inboxId)
let numMembers = (await alixGroup.memberInboxIds()).length
assert(alixIsSuperAdmin, `alix should be a super admin`)
assert(!boIsSuperAdmin, `bo should not be a super admin`)
assert(
numMembers === 2,
`number of members should be 2 but was ${numMembers}`
)

await bo.conversations.syncGroups()
const boGroup = (await bo.conversations.listGroups())[0]
await boGroup.sync()

// Bo should not be able to remove alix from the group
try {
await boGroup.removeMembersByInboxId([alix.inboxId])
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
// expected
}

await boGroup.sync()
numMembers = (await alixGroup.memberInboxIds()).length
assert(alixIsSuperAdmin, `alix should be a super admin`)
assert(!boIsSuperAdmin, `bo should not be a super admin`)
assert(
numMembers === 2,
`number of members should be 2 but was ${numMembers}`
)

// Alix adds bo as a super admin
await alixGroup.addSuperAdmin(bo.inboxId)
await alixGroup.sync()
boIsSuperAdmin = await alixGroup.isSuperAdmin(bo.inboxId)
assert(boIsSuperAdmin, `bo should be a super admin`)
await boGroup.sync()
boIsSuperAdmin = await boGroup.isSuperAdmin(bo.inboxId)
assert(boIsSuperAdmin, `bo should be a super admin`)

// Uncommenting below causes an error
// intent 3 has reached max publish attempts
// error publishing intents CreateGroupContextExtProposalError(MlsGroupStateError(PendingCommit))
// try {
// await boGroup.removeMembersByInboxId([alix.inboxId])
// } catch (error) {
// // expected
// }
await boGroup.sync()
await alixGroup.sync()
numMembers = (await alixGroup.memberInboxIds()).length
assert(
numMembers === 2,
`number of members should be 2 but was ${numMembers}`
)

// Bo can remove alix as a super admin
await boGroup.sync()
await boGroup.removeSuperAdmin(alix.inboxId)
await boGroup.sync()
await alixGroup.sync()
alixIsSuperAdmin = await alixGroup.isSuperAdmin(alix.inboxId)
assert(!alixIsSuperAdmin, `alix should not be a super admin`)

// Now bo can remove Alix from the group
await boGroup.removeMembers([alix.address])
console.log('alix inbox id:' + String(alix.inboxId))
await boGroup.sync()
numMembers = (await boGroup.memberInboxIds()).length
assert(
numMembers === 1,
`number of members should be 1 but was ${numMembers}`
)

return true
})
84 changes: 82 additions & 2 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -805,15 +805,95 @@ public class XMTPModule: Module {
return try group.addedByInboxId()
}

AsyncFunction("isGroupAdmin") { (clientAddress: String, id: String) -> Bool in
AsyncFunction("creatorInboxId") { (clientAddress: String, id: String) -> String in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
guard let group = try await findGroup(clientAddress: clientAddress, id: id) else {
throw Error.conversationNotFound("no group found for \(id)")
}

return try group.isAdmin(inboxId: client.inboxID)
return try group.creatorInboxId()
}

AsyncFunction("isAdmin") { (clientAddress: String, id: String, inboxId: String) -> Bool in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
guard let group = try await findGroup(clientAddress: clientAddress, id: id) else {
throw Error.conversationNotFound("no group found for \(id)")
}
return try group.isAdmin(inboxId: inboxId)
}

AsyncFunction("isSuperAdmin") { (clientAddress: String, id: String, inboxId: String) -> Bool in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
guard let group = try await findGroup(clientAddress: clientAddress, id: id) else {
throw Error.conversationNotFound("no group found for \(id)")
}
return try group.isSuperAdmin(inboxId: inboxId)
}

AsyncFunction("listAdmins") { (clientAddress: String, id: String) -> [String] in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
guard let group = try await findGroup(clientAddress: clientAddress, id: id) else {
throw Error.conversationNotFound("no group found for \(id)")
}
return try group.listAdmins()
}

AsyncFunction("listSuperAdmins") { (clientAddress: String, id: String) -> [String] in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
guard let group = try await findGroup(clientAddress: clientAddress, id: id) else {
throw Error.conversationNotFound("no group found for \(id)")
}
return try group.listSuperAdmins()
}

AsyncFunction("addAdmin") { (clientAddress: String, id: String, inboxId: String) in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
guard let group = try await findGroup(clientAddress: clientAddress, id: id) else {
throw Error.conversationNotFound("no group found for \(id)")
}
try await group.addAdmin(inboxId: inboxId)
}

AsyncFunction("addSuperAdmin") { (clientAddress: String, id: String, inboxId: String) in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
guard let group = try await findGroup(clientAddress: clientAddress, id: id) else {
throw Error.conversationNotFound("no group found for \(id)")
}
try await group.addSuperAdmin(inboxId: inboxId)
}

AsyncFunction("removeAdmin") { (clientAddress: String, id: String, inboxId: String) in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
guard let group = try await findGroup(clientAddress: clientAddress, id: id) else {
throw Error.conversationNotFound("no group found for \(id)")
}
try await group.removeAdmin(inboxId: inboxId)
}

AsyncFunction("removeSuperAdmin") { (clientAddress: String, id: String, inboxId: String) in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
guard let group = try await findGroup(clientAddress: clientAddress, id: id) else {
throw Error.conversationNotFound("no group found for \(id)")
}
try await group.removeSuperAdmin(inboxId: inboxId)
}

AsyncFunction("processGroupMessage") { (clientAddress: String, id: String, encryptedMessage: String) -> String in
Expand Down

0 comments on commit 388c7c4

Please sign in to comment.