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

Group consent state #371

Merged
merged 6 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -1027,6 +1027,14 @@ class XMTPModule : Module() {
}
}

AsyncFunction("groupConsentState") Coroutine { clientAddress: String, groupId: String ->
withContext(Dispatchers.IO) {
val group = findGroup(clientAddress, groupId)
?: throw XMTPException("no group found for $groupId")
consentStateToString(Conversation.Group(group).consentState())
}
}

AsyncFunction("consentList") { clientAddress: String ->
val client = clients[clientAddress] ?: throw XMTPException("No client")
client.contacts.consentList.entries.map { ConsentWrapper.encode(it.value) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class GroupWrapper {
"createdAt" to group.createdAt.time,
"peerAddresses" to Conversation.Group(group).peerAddresses,
"version" to "GROUP",
"topic" to group.id.toHex(),
"topic" to group.topic,
"permissionLevel" to permissionString,
"adminAddress" to group.adminAddress()
)
Expand Down
31 changes: 26 additions & 5 deletions example/src/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,28 @@ function GroupListItem({
const [messages, setMessages] = useState<
DecodedMessage<SupportedContentTypes>[]
>([])
const lastMessage = messages?.[0]
const [getConsentState, setConsentState] = useState<string | undefined>()
nplasterer marked this conversation as resolved.
Show resolved Hide resolved

const denyGroup = async () => {
await client?.contacts.denyGroups([group.id])
group
.consentState()
.then(setConsentState)
nplasterer marked this conversation as resolved.
Show resolved Hide resolved
.catch((e) => {
console.error('Error denying contact: ', e)
})
}

useEffect(() => {
group
?.sync()
.then(() => group.messages())
.then(setMessages)
.then(() => group.consentState())
.then((result) => {
setConsentState(result)
})
.catch((e) => {
console.error('Error fetching group messages: ', e)
})
Expand All @@ -128,16 +144,21 @@ function GroupListItem({
<Text style={{ fontWeight: 'bold' }}>
({messages?.length} messages)
</Text>
<Button
title="Deny"
onPress={denyGroup}
disabled={getConsentState === 'denied'}
/>
</View>
<View style={{ padding: 4 }}>
<Text style={{ fontWeight: 'bold', color: 'red' }}>
{getConsentState}
</Text>
<Text numberOfLines={1} ellipsizeMode="tail">
Fallback text
{lastMessage?.fallback}
</Text>
{/* <Text>{lastMessage?.senderAddress}:</Text>
<Text>{lastMessage?.senderAddress}:</Text>
<Text>{moment(lastMessage?.sent).fromNow()}</Text>
<Text style={{ fontWeight: 'bold', color: 'red' }}>
{getConsentState}
</Text> */}
</View>
</View>
</Pressable>
Expand Down
2 changes: 1 addition & 1 deletion ios/Wrappers/GroupWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct GroupWrapper {
"createdAt": UInt64(group.createdAt.timeIntervalSince1970 * 1000),
"peerAddresses": XMTP.Conversation.group(group).peerAddresses,
"version": "GROUP",
"topic": group.id.toHex,
"topic": group.topic,
"permissionLevel": permissionString,
"adminAddress": try group.adminAddress()
]
Expand Down
7 changes: 7 additions & 0 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,13 @@ public class XMTPModule: Module {
}
return ConsentWrapper.consentStateToString(state: await conversation.consentState())
}

AsyncFunction("groupConsentState") { (clientAddress: String, groupId: String) -> String in
guard let group = try await findGroup(clientAddress: clientAddress, id: groupId) else {
throw Error.conversationNotFound("no group found for \(groupId)")
}
return ConsentWrapper.consentStateToString(state: await XMTP.Conversation.group(group).consentState())
}

AsyncFunction("consentList") { (clientAddress: String) -> [String] in
guard let client = await clientsManager.getClient(key: clientAddress) else {
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,13 @@ export async function conversationConsentState(
)
}

export async function groupConsentState(
clientAddress: string,
groupId: string
): Promise<ConsentState> {
return await XMTPModule.groupConsentState(clientAddress, groupId)
}

export async function isAllowed(
clientAddress: string,
address: string
Expand Down
7 changes: 6 additions & 1 deletion src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ export class Group<
peerAddresses: string[]
adminAddress: string
permissionLevel: 'everyone_admin' | 'creator_admin'
topic: string
}
) {
this.client = client
this.id = params.id
this.createdAt = params.createdAt
this.peerAddresses = params.peerAddresses
this.topic = params.id
this.topic = params.topic
this.adminAddress = params.adminAddress
this.permissionLevel = params.permissionLevel
}
Expand Down Expand Up @@ -225,4 +226,8 @@ export class Group<
throw e
}
}

async consentState(): Promise<'allowed' | 'denied' | 'unknown'> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey I am wondering whether this is the final state for the group consentState?

I want to display which members of a group have accepted the group invite or not. AFAIK this can only be done on the client for now?

If this function returned the state of each individual member then it would be easier to do directly from the network.

Is this something that might be implemented in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm not sure I understand the question? Are you looking for a way that every person in the group can see if someone else has approved or denied the group? Inherently the personal private preferences are just that private and personal. So no one else in the group can decrypt if your blocked that person or group only you can.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep exactly what I was wondering & that clarifies it.

Pending / joined members will have to just be a client-side feature in that case.

Thanks!

return await XMTP.groupConsentState(this.clientAddress, this.id)
}
}
Loading