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

Inbox Id Private Preferences #396

Merged
merged 11 commits into from
May 31, 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
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:0.12.0"
implementation "org.xmtp:android:0.12.1"
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.facebook.react:react-native:0.71.3'
implementation "com.daveanthonythomas.moshipack:moshipack:1.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,34 @@ class XMTPModule : Module() {
}
}

AsyncFunction("isInboxAllowed") { clientAddress: String, inboxId: String ->
logV("isInboxIdAllowed")
val client = clients[clientAddress] ?: throw XMTPException("No client")
client.contacts.isInboxAllowed(inboxId)
}

AsyncFunction("isInboxDenied") { clientAddress: String, inboxId: String ->
logV("isInboxIdDenied")
val client = clients[clientAddress] ?: throw XMTPException("No client")
client.contacts.isInboxDenied(inboxId)
}

AsyncFunction("denyInboxes") Coroutine { clientAddress: String, inboxIds: List<String> ->
withContext(Dispatchers.IO) {
logV("denyInboxIds")
val client = clients[clientAddress] ?: throw XMTPException("No client")
client.contacts.denyInboxes(inboxIds)
}
}

AsyncFunction("allowInboxes") Coroutine { clientAddress: String, inboxIds: List<String> ->
withContext(Dispatchers.IO) {
logV("allowInboxIds")
val client = clients[clientAddress] ?: throw XMTPException("No client")
client.contacts.allowInboxes(inboxIds)
}
}

AsyncFunction("refreshConsentList") Coroutine { clientAddress: String ->
withContext(Dispatchers.IO) {
val client = clients[clientAddress] ?: throw XMTPException("No client")
Expand Down Expand Up @@ -1222,14 +1250,14 @@ class XMTPModule : Module() {
logV("allowGroups")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val groupDataIds = groupIds.mapNotNull { Hex.hexStringToByteArray(it) }
client.contacts.allowGroup(groupDataIds)
client.contacts.allowGroups(groupDataIds)
}

AsyncFunction("denyGroups") Coroutine { clientAddress: String, groupIds: List<String> ->
logV("denyGroups")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val groupDataIds = groupIds.mapNotNull { Hex.hexStringToByteArray(it) }
client.contacts.denyGroup(groupDataIds)
client.contacts.denyGroups(groupDataIds)
}

AsyncFunction("isGroupAllowed") { clientAddress: String, groupId: String ->
Expand Down
16 changes: 8 additions & 8 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ PODS:
- hermes-engine/Pre-built (= 0.71.14)
- hermes-engine/Pre-built (0.71.14)
- libevent (2.1.12)
- LibXMTP (0.5.0-beta1)
- LibXMTP (0.5.0-beta2)
- Logging (1.0.0)
- MessagePacker (0.4.7)
- MMKV (1.3.5):
Expand Down Expand Up @@ -449,16 +449,16 @@ PODS:
- GenericJSON (~> 2.0)
- Logging (~> 1.0.0)
- secp256k1.swift (~> 0.1)
- XMTP (0.11.0):
- XMTP (0.11.1):
- Connect-Swift (= 0.12.0)
- GzipSwift
- LibXMTP (= 0.5.0-beta1)
- LibXMTP (= 0.5.0-beta2)
- web3.swift
- XMTPReactNative (0.1.0):
- ExpoModulesCore
- MessagePacker
- secp256k1.swift
- XMTP (= 0.11.0)
- XMTP (= 0.11.1)
- Yoga (1.14.0)

DEPENDENCIES:
Expand Down Expand Up @@ -711,7 +711,7 @@ SPEC CHECKSUMS:
GzipSwift: 893f3e48e597a1a4f62fafcb6514220fcf8287fa
hermes-engine: d7cc127932c89c53374452d6f93473f1970d8e88
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
LibXMTP: 38800678de153b444c5a9b41a991080ee33e11ed
LibXMTP: 0a2e98cc3b101e53538c54061633e6587dc861bf
Logging: 9ef4ecb546ad3169398d5a723bc9bea1c46bef26
MessagePacker: ab2fe250e86ea7aedd1a9ee47a37083edd41fd02
MMKV: 506311d0494023c2f7e0b62cc1f31b7370fa3cfb
Expand Down Expand Up @@ -763,10 +763,10 @@ SPEC CHECKSUMS:
secp256k1.swift: a7e7a214f6db6ce5db32cc6b2b45e5c4dd633634
SwiftProtobuf: 407a385e97fd206c4fbe880cc84123989167e0d1
web3.swift: 2263d1e12e121b2c42ffb63a5a7beb1acaf33959
XMTP: 2fb42dccbf89949175e99309f337ec11d828b989
XMTPReactNative: 53915a0488e11be25777aa0b96d7091fa3c3c05b
XMTP: b2145642aae614c82b8115d479442f58513139b6
XMTPReactNative: 009e2c8fa77df1210aa595b06b0ec46feeff3be6
Yoga: e71803b4c1fff832ccf9b92541e00f9b873119b9

PODFILE CHECKSUM: 95d6ace79946933ecf80684613842ee553dd76a2

COCOAPODS: 1.15.2
COCOAPODS: 1.14.2
51 changes: 46 additions & 5 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,9 @@ test('can remove members from a group', async () => {
await alixGroup.sync()
const alixGroupMembers = await alixGroup.memberInboxIds()
if (alixGroupMembers.length !== 2) {
throw new Error('num group members should be 2')
throw new Error(
'num group members should be 2 but was' + alixGroupMembers.length
)
}

await caroGroups[0].sync()
Expand All @@ -528,8 +530,11 @@ test('can remove members from a group', async () => {
}

const caroGroupMembers = await caroGroups[0].memberInboxIds()
if (caroGroupMembers.length !== 2) {
throw new Error('num group members should be 2')
// should be 3 since they wont get new updates to the group after being removed
if (caroGroupMembers.length !== 3) {
throw new Error(
'num group members should be 3 but was' + caroGroupMembers.length
)
}

return true
Expand Down Expand Up @@ -1224,8 +1229,9 @@ test('creating a group should allow group', async () => {

const group = await alix.conversations.newGroup([bo.address])
const consent = await alix.contacts.isGroupAllowed(group.id)
const groupConsent = await group.isAllowed()

if (!consent) {
if (!consent || !groupConsent) {
throw Error('Group should be allowed')
}

Expand All @@ -1235,6 +1241,12 @@ test('creating a group should allow group', async () => {
`the message should have a consent state of allowed but was ${state}`
)

const consentList = await alix.contacts.consentList()
assert(
consentList[0].permissionType === 'allowed',
`the message should have a consent state of allowed but was ${consentList[0].permissionType}`
)

return true
})

Expand Down Expand Up @@ -1262,8 +1274,11 @@ test('can deny a group', async () => {
throw Error('Group should be unknown')
}
await bo.contacts.denyGroups([alixGroup.id])
await bo.conversations.syncGroups()
const boGroups = await bo.conversations.listGroups()
const isDenied = await bo.contacts.isGroupDenied(alixGroup.id)
if (!isDenied) {
const isGroupDenied = await boGroups[0].isDenied()
if (!isDenied || !isGroupDenied) {
throw Error('Group should be denied')
}
await bo.contacts.allowGroups([alixGroup.id])
Expand All @@ -1275,6 +1290,32 @@ test('can deny a group', async () => {
return true
})

test('can allow and deny a inbox id', async () => {
const [alix, bo] = await createClients(2)
const startConsent = await bo.contacts.isInboxAllowed(alix.inboxId)
if (startConsent) {
throw Error('inbox id should be unknown')
}
await bo.contacts.denyInboxes([alix.inboxId])
const isDenied = await bo.contacts.isInboxDenied(alix.inboxId)
if (!isDenied) {
throw Error('inbox id should be denied')
}
await bo.contacts.allowInboxes([alix.inboxId])
const isAllowed = await bo.contacts.isInboxAllowed(alix.inboxId)
if (!isAllowed) {
throw Error('inbox id should be allowed')
}

const consentList = await bo.contacts.consentList()
assert(
consentList[0].entryType === 'inbox_id',
`the message should have a type of inbox_id but was ${consentList[0].entryType}`
)

return true
})

test('can check if group is allowed', async () => {
const [alix, bo] = await createClients(2)
const alixGroup = await alix.conversations.newGroup([bo.address])
Expand Down
32 changes: 30 additions & 2 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,34 @@ public class XMTPModule: Module {
}
try await client.contacts.allow(addresses: addresses)
}

AsyncFunction("isInboxAllowed") { (clientAddress: String, inboxId: String) -> Bool in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
return await client.contacts.isInboxAllowed(inboxId: inboxId)
}

AsyncFunction("isInboxDenied") { (clientAddress: String, inboxId: String) -> Bool in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
return await client.contacts.isInboxDenied(inboxId: inboxId)
}

AsyncFunction("denyInboxes") { (clientAddress: String, inboxIds: [String]) in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
try await client.contacts.denyInboxes(inboxIds: inboxIds)
}

AsyncFunction("allowInboxes") { (clientAddress: String, inboxIds: [String]) in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
try await client.contacts.allowInboxes(inboxIds: inboxIds)
}

AsyncFunction("refreshConsentList") { (clientAddress: String) -> [String] in
guard let client = await clientsManager.getClient(key: clientAddress) else {
Expand Down Expand Up @@ -1120,15 +1148,15 @@ public class XMTPModule: Module {
throw Error.noClient
}
let groupDataIds = groupIds.compactMap { Data(hex: $0) }
try await client.contacts.allowGroup(groupIds: groupDataIds)
try await client.contacts.allowGroups(groupIds: groupDataIds)
}

AsyncFunction("denyGroups") { (clientAddress: String, groupIds: [String]) in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
let groupDataIds = groupIds.compactMap { Data(hex: $0) }
try await client.contacts.denyGroup(groupIds: groupDataIds)
try await client.contacts.denyGroups(groupIds: groupDataIds)
}

AsyncFunction("isGroupAllowed") { (clientAddress: String, groupId: String) -> Bool in
Expand Down
2 changes: 1 addition & 1 deletion ios/XMTPReactNative.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ Pod::Spec.new do |s|
s.source_files = "**/*.{h,m,swift}"
s.dependency 'secp256k1.swift'
s.dependency "MessagePacker"
s.dependency "XMTP", "= 0.11.0"
s.dependency "XMTP", "= 0.11.1"
end
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,35 @@
return XMTPModule.isGroupDenied(clientAddress, groupId)
}

export async function allowInboxes(
clientAddress: string,
inboxIds: string[]
): Promise<void> {
return XMTPModule.allowInboxes(clientAddress, inboxIds)
}

export async function denyInboxes(
clientAddress: string,
inboxIds: string[]
): Promise<void> {
return XMTPModule.denyInboxes(clientAddress, inboxIds)
}

export async function isInboxAllowed(
clientAddress: string,
inboxId: string
): Promise<boolean> {
return XMTPModule.isInboxAllowed(clientAddress, inboxId)
}

export async function isInboxDenied(
clientAddress: string,
inboxId: string
): Promise<boolean> {
return XMTPModule.isInboxDenied(clientAddress, inboxId)
}


Check warning on line 853 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
export async function processGroupMessage<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
>(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ConsentListEntry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type ConsentState = 'allowed' | 'denied' | 'unknown'

export type ConsentListEntryType = 'address'
export type ConsentListEntryType = 'address' | 'group_id' | 'inbox_id'

export class ConsentListEntry {
value: string
Expand Down
16 changes: 16 additions & 0 deletions src/lib/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,20 @@ export default class Contacts {
async isGroupDenied(groupId: string): Promise<boolean> {
return await XMTPModule.isGroupDenied(this.client.address, groupId)
}

async allowInboxes(inboxIds: string[]): Promise<void> {
return await XMTPModule.allowInboxes(this.client.address, inboxIds)
}

async denyInboxes(inboxIds: string[]): Promise<void> {
return await XMTPModule.denyInboxes(this.client.address, inboxIds)
}

async isInboxAllowed(inboxId: string): Promise<boolean> {
return await XMTPModule.isInboxAllowed(this.client.address, inboxId)
}

async isInboxDenied(inboxId: string): Promise<boolean> {
return await XMTPModule.isInboxDenied(this.client.address, inboxId)
}
}
8 changes: 8 additions & 0 deletions src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,12 @@ export class Group<
async consentState(): Promise<'allowed' | 'denied' | 'unknown'> {
return await XMTP.groupConsentState(this.clientAddress, this.id)
}

async isAllowed(): Promise<boolean> {
return await XMTP.isGroupAllowed(this.client.address, this.id)
}

async isDenied(): Promise<boolean> {
return await XMTP.isGroupDenied(this.client.address, this.id)
}
}
Loading