Skip to content

Commit

Permalink
Merge pull request #419 from xmtp/np/fix-permissions
Browse files Browse the repository at this point in the history
Fix permissions incorrectly updating
  • Loading branch information
nplasterer authored Jun 14, 2024
2 parents bda89e3 + 7e1ddef commit 30e6970
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 60 deletions.
37 changes: 18 additions & 19 deletions android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
15 changes: 7 additions & 8 deletions example/src/tests/groupPermissionsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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: 'all_members' }
)
await alix.conversations.syncGroups()
const alixGroup = (await alix.conversations.listGroups())[0]
Expand Down
36 changes: 18 additions & 18 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
33 changes: 24 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,15 +742,20 @@ export async function creatorInboxId(
return XMTPModule.creatorInboxId(inboxId, id) as InboxId
}

export async function isAdmin(id: string, inboxId: string): Promise<boolean> {
return XMTPModule.isAdmin(id, inboxId)
export async function isAdmin(
clientInboxId: string,
id: string,
inboxId: string
): Promise<boolean> {
return XMTPModule.isAdmin(clientInboxId, id, inboxId)
}

export async function isSuperAdmin(
clientInboxId: string,
id: string,
inboxId: string
): Promise<boolean> {
return XMTPModule.isSuperAdmin(id, inboxId)
return XMTPModule.isSuperAdmin(clientInboxId, id, inboxId)
}

export async function listAdmins(
Expand All @@ -767,26 +772,36 @@ export async function listSuperAdmins(
return XMTPModule.listSuperAdmins(inboxId, id)
}

export async function addAdmin(id: string, inboxId: string): Promise<void> {
return XMTPModule.addAdmin(id, inboxId)
export async function addAdmin(
clientInboxId: string,
id: string,
inboxId: string
): Promise<void> {
return XMTPModule.addAdmin(clientInboxId, id, inboxId)
}

export async function addSuperAdmin(
clientInboxId: string,
id: string,
inboxId: string
): Promise<void> {
return XMTPModule.addSuperAdmin(id, inboxId)
return XMTPModule.addSuperAdmin(clientInboxId, id, inboxId)
}

export async function removeAdmin(id: string, inboxId: string): Promise<void> {
return XMTPModule.removeAdmin(id, inboxId)
export async function removeAdmin(
clientInboxId: string,
id: string,
inboxId: string
): Promise<void> {
return XMTPModule.removeAdmin(clientInboxId, id, inboxId)
}

export async function removeSuperAdmin(
clientInboxId: string,
id: string,
inboxId: string
): Promise<void> {
return XMTPModule.removeSuperAdmin(id, inboxId)
return XMTPModule.removeSuperAdmin(clientInboxId, id, inboxId)
}

export async function allowGroups(
Expand Down
12 changes: 6 additions & 6 deletions src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export class Group<
* To get the latest admin status from the network, call sync() first.
*/
async isAdmin(inboxId: InboxId): Promise<boolean> {
return XMTP.isAdmin(this.id, inboxId)
return XMTP.isAdmin(this.client.inboxId, this.id, inboxId)
}

/**
Expand All @@ -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<boolean> {
return XMTP.isSuperAdmin(this.id, inboxId)
return XMTP.isSuperAdmin(this.client.inboxId, this.id, inboxId)
}

/**
Expand Down Expand Up @@ -326,7 +326,7 @@ export class Group<
* Will throw if the user does not have the required permissions.
*/
async addAdmin(inboxId: InboxId): Promise<void> {
return XMTP.addAdmin(this.id, inboxId)
return XMTP.addAdmin(this.client.inboxId, this.id, inboxId)
}

/**
Expand All @@ -336,7 +336,7 @@ export class Group<
* Will throw if the user does not have the required permissions.
*/
async addSuperAdmin(inboxId: InboxId): Promise<void> {
return XMTP.addSuperAdmin(this.id, inboxId)
return XMTP.addSuperAdmin(this.client.inboxId, this.id, inboxId)
}

/**
Expand All @@ -346,7 +346,7 @@ export class Group<
* Will throw if the user does not have the required permissions.
*/
async removeAdmin(inboxId: InboxId): Promise<void> {
return XMTP.removeAdmin(this.id, inboxId)
return XMTP.removeAdmin(this.client.inboxId, this.id, inboxId)
}

/**
Expand All @@ -356,7 +356,7 @@ export class Group<
* Will throw if the user does not have the required permissions.
*/
async removeSuperAdmin(inboxId: InboxId): Promise<void> {
return XMTP.removeSuperAdmin(this.id, inboxId)
return XMTP.removeSuperAdmin(this.client.inboxId, this.id, inboxId)
}

async processMessage(
Expand Down

0 comments on commit 30e6970

Please sign in to comment.