Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds list all method for groups and conv in Android #260

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
36 changes: 36 additions & 0 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
appVersion: 'Testing/0.0.0',
enableAlphaMls: true,
})
} catch (error: any) {

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

View workflow job for this annotation

GitHub Actions / lint

'error' is defined but never used
return error.message.endsWith(
'Environment must be "local" or "dev" to enable alpha MLS'
)
Expand Down Expand Up @@ -580,6 +580,42 @@
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 @@ -255,6 +259,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
Loading