Skip to content

Commit

Permalink
Merge pull request #260 from xmtp/cv/list-all
Browse files Browse the repository at this point in the history
Adds list all method for groups and conv in Android
  • Loading branch information
cameronvoell authored Feb 15, 2024
2 parents a50bae3 + c245d26 commit 5a2ef36
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,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 @@ -18,5 +19,11 @@ class ConversationContainerWrapper {
}
}
}

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

test('can list all groups and conversations', async () => {
// Create three MLS enabled Clients
const aliceClient = await Client.createRandom({
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 ||
listedContainers[0].version !== ConversationVersion.GROUP ||
listedContainers[1].version !== ConversationVersion.DIRECT ||
listedContainers[1].createdAt !== aliceConversation.createdAt
) {
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
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
PreparedLocalMessage,
} from './lib/ContentCodec'
import { Conversation } from './lib/Conversation'
import {
ConversationContainer,
ConversationVersion,
} from './lib/ConversationContainer'
import { DecodedMessage } from './lib/DecodedMessage'
import { Group } from './lib/Group'
import type { Query } from './lib/Query'
Expand Down Expand Up @@ -263,6 +267,22 @@ export async function listConversations<
)
}

export async function listAll<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
>(
client: Client<ContentTypes>
): Promise<ConversationContainer<ContentTypes>[]> {
const list = await XMTPModule.listAll(client.address)
return list.map((json: string) => {
const jsonObj = JSON.parse(json)
if (jsonObj.version === ConversationVersion.GROUP) {
return new Group(client, jsonObj)
} else {
return new Conversation(client, jsonObj)
}
})
}

export async function listMessages<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
>(
Expand Down
19 changes: 15 additions & 4 deletions src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ 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>[]> {
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 +208,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 5a2ef36

Please sign in to comment.