Skip to content

Commit

Permalink
feat: listAll method for groups and conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Feb 14, 2024
1 parent 79b2526 commit e99a484
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,15 @@ class XMTPModule : Module() {
}
}

AsyncFunction("listAll") { clientAddress: String ->
val client = clients[clientAddress] ?: throw XMTPException("No client")
val conversationContainerList = client.conversations.list(includeGroups = true)
conversationContainerList.map { conversation ->
conversations[conversation.cacheKey(clientAddress)] = conversation
ConversationContainerWrapper.encode(client, conversation)
}
}

AsyncFunction("loadMessages") { clientAddress: String, topic: String, limit: Int?, before: Long?, after: Long?, direction: String? ->
logV("loadMessages")
val conversation =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package expo.modules.xmtpreactnativesdk.wrappers

import android.util.Base64
import com.google.gson.GsonBuilder
import org.xmtp.android.library.Client
import org.xmtp.android.library.Conversation

Expand All @@ -20,5 +21,11 @@ class ConversationContainerWrapper {
}
}
}

fun encode(client: Client, conversation: Conversation): String {
val gson = GsonBuilder().create()
val obj = ConversationContainerWrapper.encodeToObj(client, conversation)
return gson.toJson(obj)
}
}
}
34 changes: 34 additions & 0 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,40 @@ test('can stream groups', async () => {
return true
})

test('can list all groups and conversations', async () => {
// Create three MLS enabled Clients

Check warning on line 584 in example/src/tests.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `·`
const aliceClient = await Client.createRandom({

Check warning on line 585 in example/src/tests.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `·`
env: 'local',
enableAlphaMls: true,
})
const bobClient = await Client.createRandom({
env: 'local',
enableAlphaMls: true,
})
const camClient = await Client.createRandom({
env: 'local',
enableAlphaMls: true,
})

// Add one group and one conversation
const bobGroup = await bobClient.conversations.newGroup([aliceClient.address])
const aliceConversation = await aliceClient.conversations.newConversation(
camClient.address
)

const listedContainers = await aliceClient.conversations.listAll()

// Verify informatioin in listed containers is correct
if (listedContainers[0].topic !== bobGroup.topic ||

Check warning on line 607 in example/src/tests.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎····`
listedContainers[0].version !== ConversationVersion.GROUP ||
listedContainers[1].version !== ConversationVersion.DIRECT ||
listedContainers[1].createdAt !== aliceConversation.createdAt) {

Check warning on line 610 in example/src/tests.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎··`
throw Error('Listed containers should match streamed containers')
}

return true
})

test('can stream all groups and conversations', async () => {
// Create three MLS enabled Clients
const aliceClient = await Client.createRandom({
Expand Down
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { Query } from './lib/Query'
import { ConversationSendPayload } from './lib/types'
import { DefaultContentTypes } from './lib/types/DefaultContentType'
import { getAddress } from './utils/address'
import { ConversationContainer, ConversationVersion } from './lib/ConversationContainer'

Check warning on line 21 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

`./lib/ConversationContainer` import should occur before import of `./lib/DecodedMessage`

Check warning on line 21 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·ConversationContainer,·ConversationVersion·` with `⏎··ConversationContainer,⏎··ConversationVersion,⏎`

export * from './context'
export * from './hooks'
Expand Down Expand Up @@ -255,6 +256,22 @@ export async function listConversations<
)
}

export async function listAll<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
>(client: Client<ContentTypes>): Promise<ConversationContainer<ContentTypes>[]> {

Check warning on line 261 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `client:·Client<ContentTypes>` with `⏎··client:·Client<ContentTypes>⏎`
const list = await XMTPModule.listAll(client.address)
return list.map(

Check warning on line 263 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎····`
(json: string) => {
const jsonObj = JSON.parse(json)

Check warning on line 265 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
if (jsonObj.version === ConversationVersion.GROUP) {

Check warning on line 266 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
return new Group(client, jsonObj)
} else {
return new Conversation(client, jsonObj)
}
}
)
}

export async function listMessages<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
>(
Expand Down
20 changes: 16 additions & 4 deletions src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ export default class Conversations<
return result
}

/**
* This method returns a list of all conversations and groups that the client is a member of.
*
* @returns {Promise<ConversationContainer[]>} A Promise that resolves to an array of ConversationContainer objects.
*/
async listAll(): Promise<ConversationContainer<ContentTypes>[]> {
console.log("attempting list all from Conversations")
const result = await XMTPModule.listAll(this.client)

for (const conversationContainer of result) {
this.known[conversationContainer.topic] = true
}

return result
}

/**
* This method streams groups that the client is a member of.
*
Expand Down Expand Up @@ -193,10 +209,6 @@ export default class Conversations<
}

this.known[conversationContainer.topic] = true
console.log(
'Version on emitter call: ' +
JSON.stringify({ clientAddress, conversationContainer })
)
if (conversationContainer.version === ConversationVersion.GROUP) {
return await callback(
new Group(this.client, conversationContainer as Group<ContentTypes>)
Expand Down

0 comments on commit e99a484

Please sign in to comment.