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

Start on groups example #239

Merged
merged 10 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ android/keystores/debug.keystore

# Typedocs
docs/
**/.yarn/*
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ class XMTPModule : Module() {
client.canMessage(peerAddress)
}

AsyncFunction("canGroupMessage") { clientAddress: String, peerAddresses: List<String> ->
logV("canGroupMessage")
val client = clients[clientAddress] ?: throw XMTPException("No client")
if (client.libXMTPClient == null) {
throw XMTPException("Create client with enableAlphaMLS true in order to call canGroupMessage")
}
client.canMessage(peerAddresses)
}

AsyncFunction("staticCanMessage") { peerAddress: String, environment: String, appVersion: String? ->
try {
logV("staticCanMessage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import org.xmtp.android.library.codecs.description
import org.xmtp.android.library.codecs.getReactionAction
import org.xmtp.android.library.codecs.getReactionSchema
import org.xmtp.android.library.codecs.id
import uniffi.xmtpv3.org.xmtp.android.library.codecs.ContentTypeGroupMembershipChange
import uniffi.xmtpv3.org.xmtp.android.library.codecs.GroupMembershipChangeCodec
import uniffi.xmtpv3.org.xmtp.android.library.codecs.GroupMembershipChanges
import java.net.URL

class ContentJson(
Expand All @@ -51,6 +54,7 @@ class ContentJson(
Client.register(RemoteAttachmentCodec())
Client.register(ReplyCodec())
Client.register(ReadReceiptCodec())
Client.register(GroupMembershipChangeCodec())
}

fun fromJsonObject(obj: JsonObject): ContentJson {
Expand Down Expand Up @@ -171,6 +175,21 @@ class ContentJson(
"readReceipt" to ""
)

ContentTypeGroupMembershipChange.id -> mapOf(
"groupChange" to mapOf(
"membersAdded" to (content as GroupMembershipChanges).membersAddedList.map {
mapOf(
"address" to it.accountAddress,
"initiatedByAddress" to it.initiatedByAccountAddress
)},
"membersRemoved" to content.membersRemovedList.map {
mapOf(
"address" to it.accountAddress,
"initiatedByAddress" to it.initiatedByAccountAddress
)},
)
)

else -> {
val json = JsonObject()
encodedContent?.let {
Expand Down
6 changes: 6 additions & 0 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { XmtpProvider } from 'xmtp-react-native-sdk'

import ConversationCreateScreen from './src/ConversationCreateScreen'
import ConversationScreen from './src/ConversationScreen'
import GroupScreen from './src/GroupScreen'
import HomeScreen from './src/HomeScreen'
import LaunchScreen from './src/LaunchScreen'
import { Navigator } from './src/Navigation'
Expand Down Expand Up @@ -86,6 +87,11 @@ export default function App() {
options={{ title: 'Conversation' }}
initialParams={{ topic: '' }}
/>
<Navigator.Screen
name="group"
component={GroupScreen}
options={{ title: 'Group' }}
/>
<Navigator.Screen
name="conversationCreate"
component={ConversationCreateScreen}
Expand Down
36 changes: 29 additions & 7 deletions example/src/ConversationCreateScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NativeStackScreenProps } from '@react-navigation/native-stack'
import React, { useState } from 'react'
import { Button, ScrollView, Text, TextInput } from 'react-native'
import { Button, ScrollView, Switch, Text, TextInput, View } from 'react-native'
import { useXmtp } from 'xmtp-react-native-sdk'

import { NavigationParamList } from './Navigation'
Expand All @@ -13,19 +13,32 @@ export default function ConversationCreateScreen({
const [alert, setAlert] = useState<string>('')
const [isCreating, setCreating] = useState<boolean>(false)
const { client } = useXmtp()
const [groupsEnabled, setGroupsEnabled] = useState(false)

const startNewConversation = async (toAddress: string) => {
if (!client) {
setAlert('Client not initialized')
return
}
const canMessage = await client.canMessage(toAddress)
if (!canMessage) {
setAlert(`${toAddress} is not on the XMTP network yet`)
return
if (groupsEnabled) {
const canMessage = (await client.canGroupMessage([toAddress]))[0]
if (!canMessage) {
setAlert(`${toAddress} cannot be added to a group conversation yet`)
return
}
const group = await client.conversations.newGroup([toAddress])
navigation.navigate('group', { id: group.id })
} else {
const canMessage = await client.canMessage(toAddress)
if (!canMessage) {
setAlert(`${toAddress} is not on the XMTP network yet`)
return
}
const convo = await client.conversations.newConversation(toAddress)
navigation.navigate('conversation', { topic: convo.topic })
}
const convo = await client.conversations.newConversation(toAddress)
navigation.navigate('conversation', { topic: convo.topic })
}

return (
<>
<ScrollView>
Expand All @@ -48,6 +61,15 @@ export default function ConversationCreateScreen({
opacity: isCreating ? 0.5 : 1,
}}
/>
<View>
<Switch
value={groupsEnabled}
onValueChange={() =>
setGroupsEnabled((previousState) => !previousState)
}
/>
<Text>Create Group: {groupsEnabled ? 'ON' : 'OFF'}</Text>
</View>
<Button
title="Start conversation"
onPress={() => {
Expand Down
Loading
Loading